Options
All
  • Public
  • Public/Protected
  • All
Menu

Class BufferedProxy<T, K>

A BufferedProxy is a wrapper around a target object. Before values are set on the BufferedProxy, 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, the BufferedProxy can be flushed, and the cached changes will be set onto the target object with an overridable executionHandler.

Type parameters

  • T

  • K: keyof T

Hierarchy

  • BufferedProxy

Indexable

[key: string]: unknown

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).

Index

Constructors

constructor

  • new BufferedProxy(target: T, __namedParameters?: object): BufferedProxy
  • Creates a new instance of BufferedProxy.

    const user = { name: 'Lauren' };
    new BufferedProxy(user, bufferOptions);

    Parameters

    • target: T
    • Default value __namedParameters: object = {}
      • errorHandler: undefined | function
      • executionHandler: undefined | function

    Returns BufferedProxy

Properties

Private __cache__

__cache__: BufferCache<T> = Object.create(null)

errorHandler

errorHandler: BufferErrorHandler

Overridable error handler. Invoked when a ValidationResult is invalid.

executionHandler

executionHandler: BufferExecutionHandler<T>

Overridable execution handler. Invoked when the BufferedProxy is flushed.

Private target

target: T

Accessors

Private cache

  • get cache(): object

changed

  • get changed(): Partial<T>
  • Returns cached changes as an object.

    bufferedProxy.changed; // { name: 'Lauren' };

    Returns Partial<T>

changes

  • Returns cached changes as an array.

    bufferedProxy.changes; // [{ key: 'name', value: 'Lauren' }]

    Returns Array<BufferChange<T[K]>>

errored

  • get errored(): object
  • Returns cached errors as an object.

    bufferedProxy.errored;
    {
      name: {
        value: 'Lauren Elizabeth',
        messages: ['Name is too long']
      }
    };

    Returns object

    • [key: string]: object
      • messages: string[]
      • value: T[K]

errors

  • Returns cached errors as an array.

    bufferedProxy.errors;
    [
      { key: 'name', messages: ['must be letters'], value: 123 }
    ]

    Returns Array<BufferError<T[K]>>

Private invalidResults

Private validResults

Methods

flush

  • flush(): void
  • 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'

    Returns void

get

  • get(key: K): T[K] | unknown
  • 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'

    Parameters

    • key: K

    Returns T[K] | unknown

reset

  • reset(): void
  • Resets the cache.

    bufferedProxy.get('name'); // 'Lauren Elizabeth'
    bufferedProxy.reset();
    bufferedProxy.get('name'); // 'Lauren'

    Returns void

set

  • 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'

    Parameters

    Returns ValidationResult<T[K]>

Private updateCache

Generated using TypeDoc