-
Notifications
You must be signed in to change notification settings - Fork 0
Store
A simple store that implements the Observer Pattern.
class Store<T> implements Subject<T> {
constructor(state: T);
public get state(): T;
public attach(observer: Observer<T>): void;
public detach(observer: Observer<T>): void;
public notify(): void;
public set(options: T | ((prevState: T) => T)): void;
static newPointer(): string;
}T
Any serializable type
Subject<T>
state: T |
readonly |
|---|
A deep clone of the internal state, to prevent referenced objects being directly mutated.
newPointer(): stringConvenience method to create a unique pointer.
attach(observer: Observer<T>): voidAttaches/subscribes a new Observer to the store.
-
observer: Observer<T>Observerto be attached to the store
detach(observer: Observer<T>): voidDetaches/unsubscribes an Observer from the store.
-
observer: Observer<T>The
Observerto detach
notify(): voidNotifies all the observers of a change. Will triffer automatically when the state is changed through Store.set(), but it can be forced by calling this method directly.
set(options: T | ((prevState: T) => T)): voidMethod to mutate the internal state. If a value is directly provided, assign the internal state to that value. A function can also be provided to access a dereferenced copy of the previous state.
-
options: T | ((prevState: T) => T)A value to set the new state, or a function that receives the previous state to manipulate it.