Function objectProxy

Creates a proxy of target object (or array), so that regular property setting will be intercepted and output on a Reactive object as well.

const { proxy, rx } = Rx.From.objectProxy({ colour: `red`, x: 10, y: 20 });

rx.onValue(v => {
// Get notified when proxy is changed
});

// Get and set properties as usual
console.log(proxy.x);
proxy.x = 20; // Triggers Reactive

Keep in mind that changing target directly won't affect the proxied object or Reactive. Thus, only update the proxied object after calling fromProxy.

The benefit of objectProxy instead of Rx.From.object is because the proxied object can be passed to other code that doesn't need to know anything about Reactive objects.

You can assign the return values to more meaningful names using JS syntax.

const { proxy:colour, rx:colourRx } = Rx.From.objectProxy({ colour: `red` });

If target is an array, it's not possible to change the shape of the array by adding or removing elements, only by updating existing ones. This follows the same behaviour of objects. Alternatively, use arrayProxy.

See also:

  • objectProxySymbol: Instead of {proxy,rx} return result, puts the rx under a symbol on the proxy.
  • arrayProxy: Proxy an array, allowing inserts and deletes.
  • Type Parameters

    • V extends object

    Parameters

    • target: V

    Returns {
        proxy: V;
        rx: Reactive<V> & {
            set(value: V): void;
        } & {
            onDiff(changes: ((changes: PathDataChange<any>[]) => void)): (() => void);
            onField(fieldName: string, handler: ((result: ObjectFieldHandler) => void)): (() => void);
            update(changedPart: RecursivePartial<V>): V;
            updateField(field: string, value: any): void;
        } & {
            last(): V;
        };
    }

    • proxy: V
    • rx: Reactive<V> & {
          set(value: V): void;
      } & {
          onDiff(changes: ((changes: PathDataChange<any>[]) => void)): (() => void);
          onField(fieldName: string, handler: ((result: ObjectFieldHandler) => void)): (() => void);
          update(changedPart: RecursivePartial<V>): V;
          updateField(field: string, value: any): void;
      } & {
          last(): V;
      }