# Observer

Class · extends [`Events`](https://api.playcanvas.com/observer/classes/Events.md)

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**

```ts
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

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

Creates a new Observer instance.

**Parameters**

- `data` (`Partial<`[`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`>`, optional): The initial data to observe.
- `options` (`ObserverOptions<`[`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`>`, optional): Additional options for the observer.

## Methods

### destroy

```ts
destroy(): void
```

Destroys the observer instance.

### get

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

**Parameters**

- `path` ([`P`](https://api.playcanvas.com/observer/classes/Observer.md#getp)): Path to the value.
- `raw` (`true`): Retrieve the observer object without converting it to JSON.

**Returns** `any`: The value at the specified path.

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

**Parameters**

- `path` ([`P`](https://api.playcanvas.com/observer/classes/Observer.md#getp-1)): Path to the value.
- `raw` (`false`, optional): Retrieve the observer object without converting it to JSON.

**Returns** [`P`](https://api.playcanvas.com/observer/classes/Observer.md#getp-1) `extends Key<`[`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`> ?` [`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`[`[`P`](https://api.playcanvas.com/observer/classes/Observer.md#getp-1)`] : any`: The value at the specified path.

### has

```ts
has(path: string): boolean
```

Query whether the object has the specified property.

**Parameters**

- `path` (`string`): Path to the value.

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

### insert

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

**Parameters**

- `path` (`string`): Path to the value.
- `value` (`any`): Value to insert.
- `ind` (`number`, optional): Index to insert the value at.
- `silent` (`boolean`, optional, default `false`): If true, the insert event will not be emitted.
- `remote` (`boolean`, optional, default `false`): State value passed to the set event used to disable remote event emission.

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

### json

```ts
json(): T
```

**Returns** [`T`](https://api.playcanvas.com/observer/classes/Observer.md#t): The current state of the object tracked by the observer.

```ts
json(target: any): any
```

**Parameters**

- `target` (`any`): The object to JSONify.

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

### latest

```ts
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`](https://api.playcanvas.com/observer/classes/Observer.md)`<`[`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`>`: The latest instance of the observer.

### move

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

**Parameters**

- `path` (`string`): Path to the value.
- `indOld` (`number`): Index of the value to move.
- `indNew` (`number`): Index to move the value to.
- `silent` (`boolean`, optional, default `false`): If true, the move event will not be emitted.
- `remote` (`boolean`, optional, default `false`): State value passed to the set event used to disable remote event emission.

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

### remove

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

**Parameters**

- `path` (`string`): Path to the value.
- `ind` (`number`): Index of the value.
- `silent` (`boolean`, optional, default `false`): If true, the remove event will not be emitted.
- `remote` (`boolean`, optional, default `false`): State value passed to the set event used to disable remote event emission.

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

### removeValue

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

**Parameters**

- `path` (`string`): Path to the value.
- `value` (`any`): Value to remove.
- `silent` (`boolean`, optional, default `false`): If true, the remove event will not be emitted.
- `remote` (`boolean`, optional, default `false`): State value passed to the set event used to disable remote event emission.

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

### set

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

**Parameters**

- `path` ([`P`](https://api.playcanvas.com/observer/classes/Observer.md#setp)): Path to the property in the object.
- `value` ([`P`](https://api.playcanvas.com/observer/classes/Observer.md#setp) `extends Key<`[`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`> ?` [`T`](https://api.playcanvas.com/observer/classes/Observer.md#t)`[`[`P`](https://api.playcanvas.com/observer/classes/Observer.md#setp)`] : any`): Value to set.
- `silent` (`boolean`, optional): If true, the change will not be recorded in history.
- `remote` (`boolean`, optional): State value passed to the set event used to disable remote event emission.
- `force` (`boolean`, optional): If true, the value will be set even if it is the same as the current value.

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

### unset

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

**Parameters**

- `path` (`string`): Path to the value.
- `silent` (`boolean`, optional, default `false`): If true, the change will not be recorded in history.
- `remote` (`boolean`, optional, default `false`): State value passed to the set event used to disable remote event emission.

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

## Inherited from [Events](https://api.playcanvas.com/observer/classes/Events.md)

- `get suspendEvents(): boolean` · `set suspendEvents(value: boolean)`
- `addEmitter(emitter: Events): void`
- `emit(name: string, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any): Observer<T>`
- `on(name: string, fn: HandleEvent): EventHandle`
- `once(name: string, fn: HandleEvent): EventHandle`
- `removeEmitter(emitter: Events): void`
- `unbind(name?: string, fn?: HandleEvent): Observer<T>`
