Contents

Observer API: All Pages

The API reference of @playcanvas/observer 1.7.1: observable data (Observer and ObserverList), events and undo history, which PCUI components bind to.

The same pages as Markdown, for AI agents: llms-full.txt.

Contents

EventHandle

Class

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/event-handle.ts#L11

EventHandle manages the binding and unbinding of event listeners. It provides a convenient way to add, remove, and invoke event handlers associated with specific event names. Each EventHandle is linked to an 'owner' object, typically an instance of the Events class, allowing for elegant event management and chaining.

Constructors

constructor

new EventHandle(owner: Events, name: string, fn: HandleEvent)

Creates an instance of EventHandle.

Parameters

Methods

call

call(events: Events, ...args: any[]): void

Invokes the callback function associated with the event handle. This method directly triggers the event's callback without the event being emitted by the event system.

Parameters

on

on(name: string, fn: HandleEvent): EventHandle

Registers a new event listener on the same owner as the EventHandle. This method allows chaining additional event listeners to the owner of this event handle.

Parameters

Returns EventHandle: EventHandle

unbind

unbind(): void

Unbinds the event handle from the owner, effectively removing the event listener. After calling this method, the event handle will no longer trigger the callback function when the event is emitted.

Events

Class

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/events.ts#L38

Base class for event handling, providing mechanisms to register, emit, and unbind events. This class supports adding event listeners, emitting events with up to 8 arguments, and managing multiple emitters.

Example

// Create an instance of the Events class
const events = new Events();

// Register an event listener
events.on('testEvent', (arg1, arg2) => {
    console.log('Event triggered with arguments:', arg1, arg2);
});

// Emit the event
events.emit('testEvent', 'value1', 'value2');

// Unbind the event listener
events.unbind('testEvent');

Constructors

constructor

new Events()

Creates a new Events instance.

Accessors

suspendEvents

get suspendEvents(): boolean
set suspendEvents(value: boolean)

Gets whether events are suspended.

Methods

addEmitter

addEmitter(emitter: Events): void

Adds another emitter. Any events fired by this instance will also be fired on the additional emitter.

Parameters

emit

emit(name: string, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any): Events

Emits the specified event, executing all registered listeners for that event with the provided arguments. If events are suspended, the emit operation will be ignored.

Parameters

Returns Events: The current instance for chaining.

Example

// Register an event listener
events.on('testEvent', (arg1, arg2) => {
    console.log('Event triggered with arguments:', arg1, arg2);
});

// Emit the event
events.emit('testEvent', 'value1', 'value2');

// Emit the event with more arguments
events.emit('testEvent', 'value1', 'value2', 'value3', 'value4');

on

on(name: string, fn: HandleEvent): EventHandle

Registers an event listener for the specified event name. If the event is emitted, the callback function is executed with up to 8 arguments.

Parameters

Returns EventHandle: An EventHandle object that can be used to unbind the event listener.

Example

// Register an event listener
events.on('testEvent', (arg1, arg2) => {
    console.log('Event triggered with arguments:', arg1, arg2);
});

// Emit the event
events.emit('testEvent', 'value1', 'value2');

once

once(name: string, fn: HandleEvent): EventHandle

Registers a one-time event listener for the specified event name. The callback function is executed the next time the event is emitted, and then automatically unbound.

Parameters

Returns EventHandle: An EventHandle object that can be used to unbind the event listener before it is triggered.

Example

// Register a one-time event listener
events.once('testEvent', (arg1, arg2) => {
    console.log('Event triggered once with arguments:', arg1, arg2);
});

// Emit the event
events.emit('testEvent', 'value1', 'value2'); // The callback will be called and then unbound.

// Emit the event again
events.emit('testEvent', 'value1', 'value2'); // The callback will not be called this time.

removeEmitter

removeEmitter(emitter: Events): void

Removes emitter.

Parameters

unbind

unbind(name?: string, fn?: HandleEvent): Events

Unbinds an event listener for the specified event name. If a callback function is provided, only that specific listener is removed. If no callback is provided, all listeners for the event are removed. If no event name is provided, all listeners for all events are removed.

Parameters

Returns Events: The current instance for chaining.

Example

// Register an event listener
const callback = (arg1, arg2) => {
    console.log('Event triggered with arguments:', arg1, arg2);
};
events.on('testEvent', callback);

// Unbind the specific event listener
events.unbind('testEvent', callback);

// Unbind all listeners for a specific event
events.unbind('testEvent');

// Unbind all listeners for all events
events.unbind();

History

Class · extends Events

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/history.ts#L54

Manages history actions for undo/redo operations. This class keeps track of actions that can be undone and redone, allowing for complex state management in applications such as editors, games, or any interactive applications where state changes need to be reversible.

Example

const history = new History();

// Define an action
const action = {
  name: 'draw',
  undo: () => { console.log('Undo draw'); },
  redo: () => { console.log('Redo draw'); }
};

// Add the action to history
history.add(action);

// Perform undo
history.undo();

// Perform redo
history.redo();

Accessors

canRedo

get canRedo(): boolean
set canRedo(value: boolean)

Gets whether we can redo at this time.

canUndo

get canUndo(): boolean
set canUndo(value: boolean)

Gets whether we can undo at this time.

currentAction

get currentAction(): HistoryAction

The current history action.

executing

get executing(): number
set executing(value: number)

Gets the number of async actions currently executing.

lastAction

get lastAction(): HistoryAction

The last action committed to the history.

Methods

add

add(action: HistoryAction): boolean

Adds a new history action to the stack. If the action has a combine flag and matches the current action's name, the redo function of the current action is updated. If actions have been undone before adding this new action, it removes all actions that come after the current action to maintain a consistent history.

Parameters

Returns boolean: Returns true if the action is successfully added, false otherwise.

addAndExecute

addAndExecute(action: HistoryAction): Promise<void>

Adds a new history action and immediately executes its redo function.

Parameters

Returns Promise<void>: A promise that resolves once the redo function has been executed.

clear

clear(): void

Clears all history actions.

redo

redo(): Promise<void>

Redoes the next history action. This retrieves the next action from the history stack and executes the action's redo function.

Returns Promise<void>: A promise that resolves once the redo function has been executed.

undo

undo(): Promise<void>

Undoes the last history action. This method retrieves the current action from the history stack and executes the action's undo function.

Returns Promise<void>: A promise that resolves once the undo function has been executed.

Inherited from Events

Observer

Class · extends Events

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/observer.ts#L61

The Observer class is used to observe and manage changes to an object. It allows for tracking modifications to nested properties, emitting events on changes, and maintaining state consistency. This is particularly useful in applications where state management and change tracking are critical, such as in data-driven interfaces or collaborative applications.

Example

const data = {
  name: 'John',
  age: 30,
  address: {
    city: 'New York',
    zip: '10001'
  }
};

const observer = new Observer(data);

observer.on('name:set', (newValue, oldValue) => {
  console.log(`Name changed from ${oldValue} to ${newValue}`);
});

observer.set('name', 'Jane'); // Logs: Name changed from John to Jane

Constructors

constructor

new Observer<T extends object>(data?: Partial<T>, options?: ObserverOptions<T>)

Creates a new Observer instance.

Parameters

Methods

destroy

destroy(): void

Destroys the observer instance.

get

get<P extends string>(path: P, raw: true): any

Parameters

Returns any: The value at the specified path.

get<P extends string>(path: P, raw?: false): P extends Key<T> ? T[P] : any

Parameters

Returns P extends Key<T> ? T[P] : any: The value at the specified path.

has

has(path: string): boolean

Query whether the object has the specified property.

Parameters

Returns boolean: Returns true if the value is present and false otherwise.

insert

insert(path: string, value: any, ind?: number, silent?: boolean, remote?: boolean): boolean

Parameters

Returns boolean: Returns true if the value was successfully inserted and false otherwise.

json

json(): T

Returns T: The current state of the object tracked by the observer.

json(target: any): any

Parameters

Returns any: The current state of the object tracked by the observer.

latest

latest(): Observer<T>

Returns the latest observer instance. This is important when dealing with undo / redo where the observer might have been deleted and/or possibly re-created.

Returns Observer<T>: The latest instance of the observer.

move

move(path: string, indOld: number, indNew: number, silent?: boolean, remote?: boolean): boolean

Parameters

Returns boolean: Returns true if the value was successfully moved and false otherwise.

remove

remove(path: string, ind: number, silent?: boolean, remote?: boolean): boolean

Parameters

Returns boolean: Returns true if the value was successfully removed and false otherwise.

removeValue

removeValue(path: string, value: any, silent?: boolean, remote?: boolean): boolean

Parameters

Returns boolean: Returns true if the value was successfully removed and false otherwise.

set

set<P extends string>(path: P, value: P extends Key<T> ? T[P] : any, silent?: boolean, remote?: boolean, force?: boolean): boolean

Parameters

Returns boolean: Returns true if the value was successfully set and false otherwise.

unset

unset(path: string, silent?: boolean, remote?: boolean): boolean

Parameters

Returns boolean: Returns true if the value was successfully unset and false otherwise.

Inherited from Events

ObserverHistory

Class · extends Events

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/observer-history.ts#L15

The ObserverHistory module provides a mechanism for tracking changes to an Observer object and storing them in a history stack.

Constructors

constructor

new ObserverHistory<T extends object>(args?: object)

Parameters

Inherited from Events

ObserverList

Class · extends Events

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/observer-list.ts#L8

The ObserverList class is a list of Observer objects.

Constructors

constructor

new ObserverList<T extends unknown>(options?: object)

Parameters

Inherited from Events

HandleEvent

Type alias

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/events.ts#L16

Callback used by Events and EventHandle functions. Note the callback is limited to 8 arguments.

type HandleEvent = (arg1?: Value, arg2?: Value, arg3?: Value, arg4?: Value, arg5?: Value, arg6?: Value, arg7?: Value, arg8?: Value) => void

HistoryAction

Type alias

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/history.ts#L6

Represents an action in the history.

type HistoryAction = undefined

ObserverSync

Type alias

Source: https://github.com/playcanvas/playcanvas-observer/blob/7b88674dee207b87954eabfb61685546418ad536/src/observer.ts#L12

The ObserverSync class is used to construct an interface for synchronizing changes from Observer to other services.

type ObserverSync = Events & { enabled: boolean; write: (args: Value[]) => void }