Any property that is not one of the getter/setter/methods on the
BufferedProxy instance. The value type is unknown
in order to avoid
having to predefine key/value pairs of the correct types in the target
object. Setting the index signature to [key: string]: T[K]
would allow us
to typecheck the value that is set on the proxy. However, no
getters/setters/methods can be added to the class. This is the tradeoff
we make for this particular design pattern (class based BufferedProxy).
Creates a new instance of BufferedProxy
.
const user = { name: 'Lauren' };
new BufferedProxy(user, bufferOptions);
Overridable error handler. Invoked when a ValidationResult
is invalid.
Overridable execution handler. Invoked when the BufferedProxy
is flushed.
Returns cached changes as an object.
bufferedProxy.changed; // { name: 'Lauren' };
Returns cached changes as an array.
bufferedProxy.changes; // [{ key: 'name', value: 'Lauren' }]
Returns cached errors as an object.
bufferedProxy.errored;
{
name: {
value: 'Lauren Elizabeth',
messages: ['Name is too long']
}
};
Returns cached errors as an array.
bufferedProxy.errors;
[
{ key: 'name', messages: ['must be letters'], value: 123 }
]
Applies all the changes to the target object with the executionHanlder
,
then resets the cache to an empty state. The default executionHandler
is Object.assign
, which mutates the target object directly.
const user = { name: 'Lauren' };
const bufferedProxy = new BufferedProxy(user);
bufferedProxy.set(\/* ... *\/);
bufferedProxy.flush();
user.name; // 'Lauren Elizabeth'
Retrieve value or error from cache by key. Returns property on the buffered proxy as a fallback, followed by the target object.
bufferedProxy.get('name'); // 'Lauren'
Resets the cache.
bufferedProxy.get('name'); // 'Lauren Elizabeth'
bufferedProxy.reset();
bufferedProxy.get('name'); // 'Lauren'
Sets a value or error into the cache, after the change has been
validated. Invokes the errorHandler
, if present.
const user = { name: 'Lauren' };
const bufferedProxy = new BufferedProxy(user);
bufferedProxy.set(
'name',
new ValidationResult('name', 'Lauren Elizabeth', [
{
message: ['name must be greater than 3 characters'],
validation: true
}
])
);
bufferedProxy.get('name'); // 'Lauren Elizabeth'
Generated using TypeDoc
A
BufferedProxy
is a wrapper around a target object. Before values are set on theBufferedProxy
, they are first validated. If the result is valid, we store the value in the cache. If it's not, we store it in our error cache. When ready, theBufferedProxy
can be flushed, and the cached changes will be set onto the target object with an overridableexecutionHandler
.