Skip to content

Class: ReactiveState

Provides a syncronized state storage which is accessible in both browser and web worker contexts

Constructors

Constructor

ts
new ReactiveState(namespace: string, secret: string): ReactiveState;

Creates a new instance of the ReactiveState class

Parameters

ParameterTypeDescription
namespacestringThe namespace used to identify the reactive state storage
secretstringThe secret used to encrypt the values stored in the reactive state storage

Returns

ReactiveState

Accessors

count

Get Signature

ts
get count(): number;

Returns an integer representing the number of records in the reactive state storage

Returns

number


onError

Set Signature

ts
set onError(onError: (error: Error) => void): void;

Sets the error handler for the reactive state storage

Parameters
ParameterTypeDescription
onError(error: Error) => voidThe error handler function
Returns

void


size

Get Signature

ts
get size(): number;

Returns an integer representing the number of records in the reactive state storage

Returns

number

Methods

[iterator]()

ts
iterator: Generator<any[], void, unknown>;

The [Symbol.iterator]() method of ReactiveState implements the iterable protocol and allows ReactiveState objects to be consumed by most syntaxes expecting iterables, such as the spread syntax and for...of loops. It returns an iterator object that yields the key-value pairs of the ReactiveState object.

Returns

Generator<any[], void, unknown>

An iterator object that contains the key-value pairs of the ReactiveState object


clear()

ts
clear(): Promise<void>;

Clears all values stored in the reactive state storage

Returns

Promise<void>


entries()

ts
entries(): IterableIterator<[string, any]>;

Returns a new iterator object that contains the [key, value] pairs for each element in the reactive state storage

Returns

IterableIterator<[string, any]>

An iterator object that contains the [key, value] pairs for each element in the reactive state storage


fetch()

ts
fetch(keys: string[]): Record<string, any>;

Retrieves multiple values from the reactive state storage

Parameters

ParameterTypeDescription
keysstring[]The keys used to identify the values

Returns

Record<string, any>

An object with the keys representing the keys requested and their corresponding values in the reactive state storage


forEach()

ts
forEach(callbackfn: (value: any, key: string, map: Map<string, any>) => void, thisArg?: any): void;

Executes a provided function once per each key/value pair in this reactive state storage

Parameters

ParameterTypeDescription
callbackfn(value: any, key: string, map: Map<string, any>) => voidA function that accepts up to three arguments. The forEach method calls the callbackfn function for each key/value pair in the reactive state storage.
thisArg?anyValue to use as this when executing callbackfn

Returns

void

Remarks

The map object provided to the callback function is a clone of the original map object. This is done to prevent any modifications to the original map object during iteration.


get()

ts
get(key: string): any;

Retrieve the value stored in the reactive state storage for the given key

Parameters

ParameterTypeDescription
keystringThe key used to identify the value

Returns

any

The value stored in the reactive state storage


has()

ts
has(key: string): boolean;

Returns a boolean indicating whether the reactive state storage contains a value for the given key

Parameters

ParameterTypeDescription
keystringThe key used to identify the value

Returns

boolean


keys()

ts
keys(): IterableIterator<string>;

Returns an iterator object which contains the keys for each element in the reactive state storage

Returns

IterableIterator<string>

An iterator object that contains the keys for each element in the reactive state storage


off()

ts
off(): void;

Removes all event listeners from the reactive state storage

Returns

void


offChange()

ts
offChange(callback: (key: string, is: any, was?: any) => void): this;

Unsubscribe from changes in the reactive state storage

Parameters

ParameterTypeDescription
callback(key: string, is: any, was?: any) => voidA function which is called when the value of the reactive state storage changes

Returns

this

The current instance of the ReactiveState class


offChangeToKey()

ts
offChangeToKey(key: string, callback: (is: any, was?: any) => void): this;

Unsubscribe from changes in the reactive state storage for a specific key

Parameters

ParameterTypeDescription
keystring-
callback(is: any, was?: any) => voidA function which is called when the value of the reactive state storage changes

Returns

this

The current instance of the ReactiveState class


onceChange()

ts
onceChange(callback: (key: string, is: any, was?: any) => void): this;

Subscribe to changes in the reactive state storage once

Parameters

ParameterTypeDescription
callback(key: string, is: any, was?: any) => voidA function which is called when the value of the reactive state storage changes

Returns

this

The current instance of the ReactiveState class

Remarks

This can be called multiple times for multiple callbacks


onceChangeToKey()

ts
onceChangeToKey(key: string, callback: (is: any, was?: any) => void): this;

Subscribe to changes in the reactive state storage once for a specific key

Parameters

ParameterTypeDescription
keystring-
callback(is: any, was?: any) => voidA function which is called when the value of the reactive state storage changes

Returns

this

The current instance of the ReactiveState class

Remarks

This can be called multiple times for multiple callbacks


onChange()

ts
onChange(callback: (key: string, is: any, was?: any) => void): this;

Subscribe to changes in the reactive state storage

Parameters

ParameterTypeDescription
callback(key: string, is: any, was?: any) => voidA function which is called when the value of the reactive state storage changes

Returns

this

The current instance of the ReactiveState class

Remarks

This can be called multiple times for multiple callbacks


onChangeToKey()

ts
onChangeToKey(key: string, callback: (is: any, was?: any) => void): this;

Subscribe to changes in the reactive state storage for a specific key

Parameters

ParameterTypeDescription
keystring-
callback(is: any, was?: any) => voidA function which is called when the value of the reactive state storage changes

Returns

this

The current instance of the ReactiveState class

Remarks

This can be called multiple times for multiple callbacks


put()

ts
put(values: {
  key: string;
  value: any;
}[]): Promise<void>;

Stores multiple values in the reactive state storage

Parameters

ParameterTypeDescription
values{ key: string; value: any; }[]An array of objects containing the key and value to be stored in the reactive state storage

Returns

Promise<void>

Remarks

Using the put method is faster and more efficient than using the set method for multiple values


remove()

ts
remove(key: string): Promise<void>;

Removes the value stored in the reactive state storage for the given key

Parameters

ParameterTypeDescription
keystringThe key used to identify the value

Returns

Promise<void>


set()

ts
set(key: string, value: any): Promise<void>;

Stores the value in the reactive state storage for the given key

Parameters

ParameterTypeDescription
keystringThe key used to identify the value
valueanyThe value to be stored in the reactive state storage

Returns

Promise<void>

Remarks

If you have a large number of values to store, consider using the put method instead


subscribe()

ts
subscribe(observer: Partial<Observer<Record<string, any>[]>>): Subscription;

Subscribe to updates from the reactive state storage

Parameters

ParameterType
observerPartial<Observer<Record<string, any>[]>>

Returns

Subscription

See

rxjs documentation for more information


values()

ts
values(): IterableIterator<any>;

Returns an iterator object which contains the values for each element in the reactive state storage

Returns

IterableIterator<any>

An iterator object that contains the values for each element in the reactive state storage