Skip to content

Class: State<T> ​

Defined in: state.ts:25

A reactive state variable that notifies registered watchers whenever its value changes.

Example ​

ts
const counter = new State(0)
counter.watch((newVal, oldVal) => {
  facile.write('#count', `Count: ${newVal}`)
})
counter.value = 1 // triggers the watcher
counter.value     // 1

Type Parameters ​

T ​

T

The type of the value handled by this state.

Constructors ​

Constructor ​

new State<T>(initialValue): State<T>

Defined in: state.ts:29

Parameters ​

initialValue ​

T

Returns ​

State<T>

Accessors ​

value ​

Get Signature ​

get value(): T

Defined in: state.ts:37

The current value. Setting a new value notifies all watchers if it changed. Does nothing if the new value is the same as the current one.

Returns ​

T

Set Signature ​

set value(newValue): void

Defined in: state.ts:41

Parameters ​
newValue ​

T

Returns ​

void

Methods ​

unwatch() ​

unwatch(callback): void

Defined in: state.ts:69

Removes a previously registered watcher.

Parameters ​

callback ​

WatchCallback<T>

The callback to remove.

Returns ​

void


watch() ​

watch(callback, init?): void

Defined in: state.ts:58

Registers a callback to be invoked whenever the value changes.

Parameters ​

callback ​

WatchCallback<T>

The function to call when the value changes.

init? ​

boolean = false

If true, the callback is invoked immediately with the current value. Defaults to false.

Returns ​

void