Skip to main content
Version: next

Class: Nexo

Defined in: src/Nexo.ts:31

Represents a proxy factory for creating and managing proxy objects.

Remarks

This class provides utilities for creating proxies, retrieving existing ones by unique IDs, and interacting with them through event-driven mechanisms.

It emits the following events:

  • proxy: Fired whenever a new proxy is created.
  • error: Fired when an error occurs.
  • proxy.handler: Fired when any proxy handler function is invoked.
  • proxy.error: Fired when a proxy operation fails.

Example

// Example of listening to 'proxy' event every time a new proxy is created.
const nexo = new Nexo();
const listener = (event: NexoEvent) => {};

nexo.on('proxy', listener);

// The listener will be called when a new proxy is created.
const proxy = nexo.create();

Extends

Implements

  • Nexo

Constructors

new Nexo()

new Nexo(): Nexo

Defined in: src/Nexo.ts:115

Initializes the Nexo proxy system and event hooks.

Returns

Nexo

Remarks

Inherits all event management behavior from NexoEmitter. Use on() and emit() to subscribe and react to internal proxy lifecycle events.

Overrides

NexoEmitter.constructor

Properties

entries

readonly entries: NexoMap<Proxy>

Defined in: src/Nexo.ts:41

A map that stores unique proxy IDs associated with their respective WeakRef references to the proxy objects.

Remarks

This map allows quick access to proxies by their unique ID, ensuring that proxies are properly managed and referenced.

Implementation of

nx.Nexo.entries

Methods

create()

create(target?): Proxy

Defined in: src/Nexo.ts:174

Creates a new proxy for a given target.

Parameters

target?

Traceable

An optional object to associate with the proxy. Defaults to a blank function.

Returns

Proxy

A new proxy wrapping the given target.

Remarks

This method always returns a new proxy, even when called multiple times with the same target. Use this method when you need isolated proxies or don't require a persistent reference by ID.

To retrieve or reuse proxies by ID, prefer using nexo.use(id, target) instead.

Example

const nexo = new Nexo();
const proxy = nexo.create();

// Create proxies for the same target
const proxy1 = nexo.create(console.log);
const proxy2 = nexo.create(console.log);
console.log(proxy1 === proxy2); // false

Implementation of

nx.Nexo.create


use()

use(id, target?): Proxy

Defined in: src/Nexo.ts:149

Creates or retrieves a proxy by its unique ID.

Parameters

id

string

A stable identifier to associate with the proxy.

target?

Traceable

An optional object to wrap in a proxy.

Returns

Proxy

A proxy associated with the ID and optional target.

Remarks

  • If an ID already exists and no target is provided, the existing proxy is returned.
  • If a target is provided, a new proxy is created and associated with the given ID, regardless of whether the ID was used before.

This allows reusing the same ID with updated targets and also creating multiple proxies for the same target under different IDs.

Example

const nexo = new Nexo();

// First use creates a proxy
const proxy1 = nexo.use('foo', {});

// Retrieves the existing proxy by ID
const proxy2 = nexo.use('foo');
console.log(proxy1 === proxy2); // true

// Replaces the proxy under the same ID with a new target
const proxy3 = nexo.use('foo', { updated: true });
console.log(proxy3 !== proxy1); // true

Implementation of

nx.Nexo.use


isProxy()

static isProxy(value): value is Proxy

Defined in: src/Nexo.ts:54

Determines whether the given value is a registered Proxy instance.

This function checks if the value exists in the internal proxy map, meaning it was previously registered as a proxy via the system.

Acts as a type guard for narrowing unknown to Proxy.

Parameters

value

unknown

The value to check.

Returns

value is Proxy

true if the value is a known Proxy, otherwise false.


isTraceable()

static isTraceable(value): value is Traceable

Defined in: src/Nexo.ts:70

Determines whether the given value is a Traceable entity.

A value is considered traceable if it is a non-null object or function. This check is used to determine whether the value is eligible to be linked to a proxy in the system's internal tracking.

Acts as a type guard to narrow the type to Traceable.

Parameters

value

unknown

The value to evaluate.

Returns

value is Traceable

true if the value is Traceable, otherwise false.


wrap()

static wrap(proxy): ProxyWrapper

Defined in: src/Nexo.ts:99

Provides a wrapper for an existing proxy.

Parameters

proxy

Proxy

An existing proxy object

Returns

ProxyWrapper

A wrapper for the proxy that allows interaction with proxy events

Remarks

This method wraps a proxy object and allows interaction with the proxy's events and properties. Proxy-related events follow the format proxy.handler, where the handler corresponds to one of the standard proxy handler functions such as apply, construct, get, etc.

Example

// Wrapping an existing proxy and listening to 'proxy.get' event
const nexo = new Nexo();
const proxy = nexo.create();
const wrapper = Nexo.wrap(proxy);

wrapper.on('proxy.get', (event: ProxyEvent) => {});

Throws

Error if the wrapper cannot be found.