proxy
Creates a reactive proxy around the given data.
Reading properties from the returned proxy within a reactive scope (like one created by A or derive) establishes a subscription. Modifying properties through the proxy will notify subscribed scopes, causing them to re-execute.
- Plain objects, arrays, Maps, and Sets are wrapped in a standard JavaScript
Proxythat intercepts property access and mutations, but otherwise works like the underlying data. - Primitives (string, number, boolean, null, undefined) are wrapped in an object
{ value: T }which is then proxied. Access the primitive via the.valueproperty. - Promises are represented by proxied objects
{ busy: boolean, value?: T, error?: any }. Initially,busyistrue. When the promise resolves,valueis set andbusyis set tofalse. If the promise is rejected,erroris set andbusyis also set tofalse.
Use unproxy to get the original underlying data back.
By convention in the examples below, local variables that hold proxied values are prefixed with $.
target
The object, array, Map, Set, or primitive value to make reactive.
Template
Section titled “Template”T
The type of the data being proxied.
Examples
Section titled “Examples”Object
const $state = A.proxy({ count: 0, message: 'Hello' });A(() => console.log($state.message)); // Subscribes to messagesetTimeout(() => $state.message = 'World', 1000); // Triggers the observing functionsetTimeout(() => $state.count++, 2000); // Triggers nothingArray
const $items = A.proxy(['a', 'b']);A(() => console.log($items.length)); // Subscribes to lengthsetTimeout(() => $items.push('c'), 2000); // Triggers the observing functionPrimitive
const $name = A.proxy('Aberdeen');A(() => console.log($name.value)); // Subscribes to valuesetTimeout(() => $name.value = 'UI', 2000); // Triggers the observing functionSet
const $tags = A.proxy(new Set(['ui', 'tiny']));A(() => console.log($tags.has('ui'), $tags.size));setTimeout(() => $tags.add('fast'), 1000);Class instance
class Widget { constructor(public name: string, public width: number, public height: number) {} grow() { this.width *= 2; } toString() { return `${this.name}Widget (${this.width}x${this.height})`; }}let $graph: Widget = A.proxy(new Widget('Graph', 200, 100));A(() => console.log(''+$graph));setTimeout(() => $graph.grow(), 2000);setTimeout(() => $graph.grow(), 4000);Call Signature
Section titled “Call Signature”proxy<
T>(target):PromiseProxy<T>
Defined in: aberdeen.ts:1666
Type Parameters
Section titled “Type Parameters”T extends unknown
Parameters
Section titled “Parameters”target
Section titled “target”Promise<T>
Returns
Section titled “Returns”PromiseProxy<T>
Call Signature
Section titled “Call Signature”proxy<
T>(target):Textendsnumber?number:Textendsstring?string:Textendsboolean?boolean:T[]
Defined in: aberdeen.ts:1667
Type Parameters
Section titled “Type Parameters”T extends unknown
Parameters
Section titled “Parameters”target
Section titled “target”T[]
Returns
Section titled “Returns”T extends number ? number : T extends string ? string : T extends boolean ? boolean : T[]
Call Signature
Section titled “Call Signature”proxy<
T>(target):T
Defined in: aberdeen.ts:1668
Type Parameters
Section titled “Type Parameters”T extends object
Parameters
Section titled “Parameters”target
Section titled “target”T
Returns
Section titled “Returns”T
Call Signature
Section titled “Call Signature”proxy<
T>(target):ValueRef<Textendsnumber?number:Textendsstring?string:Textendsboolean?boolean:T>
Defined in: aberdeen.ts:1669
Type Parameters
Section titled “Type Parameters”T extends unknown
Parameters
Section titled “Parameters”target
Section titled “target”T
Returns
Section titled “Returns”ValueRef<T extends number ? number : T extends string ? string : T extends boolean ? boolean : T>