Skip to content

copy

copy<T>(dst, src): boolean

Defined in: aberdeen.ts:1832

Recursively copies properties or array items from src to dst. It’s designed to work efficiently with reactive proxies created by proxy.

  • Minimizes Updates: When copying between objects/arrays (proxied or not), if a nested object exists in dst with the same constructor as the corresponding object in src, copy will recursively copy properties into the existing dst object instead of replacing it. This minimizes change notifications for reactive (proxied) destinations.
  • Fast with Proxies: When copying to/from proxied objects, copy uses Aberdeen internals to speed things up (compared to a non-Aberdeen-aware deep copy).

T extends object

The type of the objects being copied.

T

The destination object/array/Map (proxied or unproxied).

T

The source object/array/Map (proxied or unproxied). It won’t be modified.

boolean

true if any changes were made to dst, or false if not.

Error if attempting to copy an array into a non-array or vice versa.

Basic Copy

const $source = A.proxy({ a: 1, b: { c: 2 } });
const $dest = A.proxy({ b: { d: 3 } });
A.copy($dest, $source);
console.log($dest); // proxy({ a: 1, b: { c: 2 } })
A.copy($dest, 'b', { e: 4 });
console.log($dest); // proxy({ a: 1, b: { e: 4 } })

copy<T>(dst, dstKey, src): boolean

Defined in: aberdeen.ts:1839

Like above, but copies src into dst[dstKey]. This is useful if you’re unsure if dst[dstKey] already exists (as the right type of object) or if you don’t want to subscribe to dst[dstKey].

T extends object

T

keyof T

Optional key in dst to copy into.

T[keyof T]

boolean