Skip to content

Latest commit

 

History

History

docs

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

universal-stores

universal-stores

Table of contents

Classes

Type Aliases

Variables

Functions

Type Aliases

DerivedStoreConfig

Ƭ DerivedStoreConfig<T>: Object

Configurations for derived stores.

Type parameters

Name
T

Type declaration

Name Type Description
comparator? EqualityComparator<T> (optional, defaults to (a, b) => a === b) a function that's used to determine if the current value of the store value is different from the one being set and thus if the store needs to be updated and the subscribers notified.

Defined in

src/lib/composition.ts:18


EqualityComparator

Ƭ EqualityComparator<T>: (a: T, b: T) => boolean

Type parameters

Name
T

Type declaration

▸ (a, b): boolean

A comparison function used to optimize subscribers notifications. Used in Store

Parameters
Name Type
a T
b T
Returns

boolean

Defined in

src/lib/store.ts:27


Getter

Ƭ Getter<T>: () => T

Type parameters

Name
T

Type declaration

▸ (): T

A generic getter function. Used in Store

Returns

T

Defined in

src/lib/store.ts:21


ReactiveRoot

Ƭ ReactiveRoot: Object

A reactive root provides a scope for all the effect it contains. This scope can then be destroyed (and all the effect cleaned up) by calling the dispose method.

Type declaration

Name Type
dispose () => void
makeEffect (fn: () => void | () => void) => void

Defined in

src/lib/effect.ts:82


ReadonlyStore

Ƭ ReadonlyStore<T>: Object

A store that can have subscribers and emit values to them. It also provides the current value upon subscription. It's readonly in the sense that it doesn't provide direct set/update methods, unlike Store, therefore its value can only be changed by a StartHandler (see also makeReadonlyStore).

Type parameters

Name
T

Type declaration

Name Type
content () => T
nOfSubscriptions () => number
subscribe (subscriber: Subscriber<T>) => Unsubscribe
watch () => T

Defined in

src/lib/store.ts:43


Setter

Ƭ Setter<T>: (newValue: T) => void

Type parameters

Name
T

Type declaration

▸ (newValue): void

A generic setter function. Used in Store

Parameters
Name Type
newValue T
Returns

void

Defined in

src/lib/store.ts:19


StartHandler

Ƭ StartHandler<T>: (set: Setter<T>) => StopHandler | void

Type parameters

Name
T

Type declaration

▸ (set): StopHandler | void

A function that gets called once a store gets at least one subscriber. Used in Store

Parameters
Name Type
set Setter<T>
Returns

StopHandler | void

Defined in

src/lib/store.ts:31


StopHandler

Ƭ StopHandler: () => void

Type declaration

▸ (): void

A function that gets called once a store reaches 0 subscribers. Used in Store

Returns

void

Defined in

src/lib/store.ts:29


Store

Ƭ Store<T>: ReadonlyStore<T> & { set: (v: T) => void ; update: (updater: Updater<T>) => void }

A store that can have subscribers and emit values to them. It also provides the current value upon subscription.

Type parameters

Name
T

Defined in

src/lib/store.ts:87


StoreConfig

Ƭ StoreConfig<T>: Object

Configurations for Store and ReadonlyStore.

Type parameters

Name
T

Type declaration

Name Type Description
comparator? EqualityComparator<T> (optional, defaults to (a, b) => a === b) a function that's used to determine if the current value of the store value is different from the one being set and thus if the store needs to be updated and the subscribers notified.
start? StartHandler<T> (optional) a StartHandler that will get called once there is at least one subscriber to this store.

Defined in

src/lib/store.ts:104


Subscriber

Ƭ Subscriber<T>: (current: T) => void

Type parameters

Name
T

Type declaration

▸ (current): void

A generic subscriber that takes a value emitted by a signal as its only parameter.

Parameters
Name Type
current T
Returns

void

Defined in

node_modules/@cdellacqua/signals/dist/index.d.ts:2


Unsubscribe

Ƭ Unsubscribe: () => void

Type declaration

▸ (): void

A function that's used to unsubscribe a subscriber from a signal.

Returns

void

Defined in

node_modules/@cdellacqua/signals/dist/index.d.ts:4


Update

Ƭ Update<T>: (updater: (current: T) => T) => void

Type parameters

Name
T

Type declaration

▸ (updater): void

A generic update function. Used in Store

Parameters
Name Type
updater (current: T) => T
Returns

void

Defined in

src/lib/store.ts:25


Updater

Ƭ Updater<T>: (current: T) => T

Type parameters

Name
T

Type declaration

▸ (current): T

A generic updater function. Used in Store

Parameters
Name Type
current T
Returns

T

Defined in

src/lib/store.ts:23

Variables

storeRuntime

Const storeRuntime: Object

Type declaration

Name Type
storeCount number

Defined in

src/lib/store.ts:33

Functions

batchEffects

batchEffects(action): void

Run the passed function, enqueueing and deduplicating the effects it may trigger, in order to run them just at the end to avoid "glitches".

NOTE: batchEffects can be nested, all updates will automatically be accumulated in the outmost "batch" before the effects are executed.

Parameters

Name Type Description
action () => void A function that directly or indirectly updates one or more stores.

Returns

void

Defined in

src/lib/effect.ts:162


makeDerivedStore

makeDerivedStore<TIn, TOut>(readonlyStore, map, config?): ReadonlyStore<TOut>

Create a derived store.

Example usage:

const source$ = makeStore(10);
const derived$ = makeDerivedStore(source$, (v) => v * 2);
source$.subscribe((v) => console.log(v)); // prints 10
derived$.subscribe((v) => console.log(v)); // prints 20
source$.set(16); // triggers both console.logs, printing 16 and 32

Type parameters

Name
TIn
TOut

Parameters

Name Type Description
readonlyStore ReadonlyStore<TIn> a store or readonly store.
map (value: TIn) => TOut a function that takes the current value of the source store and maps it to another value.
config? DerivedStoreConfig<TOut> a DerivedStoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers.

Returns

ReadonlyStore<TOut>

Defined in

src/lib/composition.ts:41

makeDerivedStore<TIn, TOut>(readonlyStores, map, config?): ReadonlyStore<TOut>

Create a derived store from multiple sources.

Example usage:

const source1$ = makeStore(10);
const source2$ = makeStore(-10);
const derived$ = makeDerivedStore([source1$, source2$], ([v1, v2]) => v1 + v2);
source1$.subscribe((v) => console.log(v)); // prints 10
source2$.subscribe((v) => console.log(v)); // prints -10
derived$.subscribe((v) => console.log(v)); // prints 0
source1$.set(11); // prints 11 (first console.log) and 1 (third console.log)
source2$.set(9); // prints 9 (second console.log) and 20 (third console.log)

Type parameters

Name Type
TIn extends unknown[] | [unknown, ...unknown[]]
TOut TOut

Parameters

Name Type Description
readonlyStores { [K in string | number | symbol]: ReadonlyStore<TIn[K]> } an array of stores or readonly stores.
map (value: { [K in string | number | symbol]: TIn[K] }) => TOut a function that takes the current value of all the source stores and maps it to another value.
config? DerivedStoreConfig<TOut> a DerivedStoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers.

Returns

ReadonlyStore<TOut>

Defined in

src/lib/composition.ts:65

makeDerivedStore<TIn, TOut>(readonlyStores, map, config?): ReadonlyStore<TOut>

Create a derived store from multiple sources.

Example usage:

const source1$ = makeStore(10);
const source2$ = makeStore(-10);
const derived$ = makeDerivedStore({v1: source1$, v2: source2$}, ({v1, v2}) => v1 + v2);
source1$.subscribe((v) => console.log(v)); // prints 10
source2$.subscribe((v) => console.log(v)); // prints -10
derived$.subscribe((v) => console.log(v)); // prints 0
source1$.set(11); // prints 11 (first console.log) and 1 (third console.log)
source2$.set(9); // prints 9 (second console.log) and 20 (third console.log)

Type parameters

Name
TIn
TOut

Parameters

Name Type Description
readonlyStores { [K in string | number | symbol]: ReadonlyStore<TIn[K]> } an array of stores or readonly stores.
map (value: { [K in string | number | symbol]: TIn[K] }) => TOut a function that takes the current value of all the source stores and maps it to another value.
config? DerivedStoreConfig<TOut> a DerivedStoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers.

Returns

ReadonlyStore<TOut>

Defined in

src/lib/composition.ts:89


makeReactiveRoot

makeReactiveRoot(): ReactiveRoot

Create a ReactiveRoot, providing a makeEffect and a dispose function.

Returns

ReactiveRoot

Defined in

src/lib/effect.ts:101


makeReadonlyStore

makeReadonlyStore<T>(initialValue, start?): ReadonlyStore<T>

Make a store of type T.

Example usage:

let value = 0;
const store$ = makeReadonlyStore(value, (set) => {
	value++;
	set(value);
});
console.log(store$.content()); // 1
store$.subscribe((v) => console.log(v)); // immediately prints 2
console.log(store$.content()); // 2

Type parameters

Name
T

Parameters

Name Type Description
initialValue undefined | T the initial value of the store.
start? StartHandler<T> a StartHandler that will get called once there is at least one subscriber to this store.

Returns

ReadonlyStore<T>

a ReadonlyStore

Defined in

src/lib/store.ts:258

makeReadonlyStore<T>(initialValue, config?): ReadonlyStore<T>

Make a store of type T.

Example usage:

const store$ = makeReadonlyStore({prop: 'some value'}, {
	comparator: (a, b) => a.prop === b.prop,
	start: (set) => {
		// ...
	},
});

Type parameters

Name
T

Parameters

Name Type Description
initialValue undefined | T the initial value of the store.
config? StoreConfig<T> a StoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a StartHandler.

Returns

ReadonlyStore<T>

a ReadonlyStore

Defined in

src/lib/store.ts:279

makeReadonlyStore<T>(initialValue, startOrConfig?): ReadonlyStore<T>

Make a store of type T.

Example usage:

let value = 0;
const store$ = makeReadonlyStore(value, (set) => {
	value++;
	set(value);
});
console.log(store$.content()); // 1
store$.subscribe((v) => console.log(v)); // immediately prints 2
console.log(store$.content()); // 2

Type parameters

Name
T

Parameters

Name Type Description
initialValue undefined | T the initial value of the store.
startOrConfig? StartHandler<T> | StoreConfig<T> a StartHandler or a StoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a StartHandler.

Returns

ReadonlyStore<T>

a ReadonlyStore

Defined in

src/lib/store.ts:302


makeStore

makeStore<T>(initialValue, start?): Store<T>

Make a store of type T.

Example usage:

const store$ = makeStore(0);
console.log(store$.content()); // 0
store$.subscribe((v) => console.log(v));
store$.set(10); // will trigger the above console log, printing 10

Type parameters

Name
T

Parameters

Name Type Description
initialValue undefined | T the initial value of the store.
start? StartHandler<T> a StartHandler that will get called once there is at least one subscriber to this store.

Returns

Store<T>

a Store

Defined in

src/lib/store.ts:128

makeStore<T>(initialValue, config?): Store<T>

Make a store of type T.

Example usage:

const store$ = makeStore(0);
console.log(store$.content()); // 0
store$.subscribe((v) => console.log(v));
store$.set(10); // will trigger the above console log, printing 10

Type parameters

Name
T

Parameters

Name Type Description
initialValue undefined | T the initial value of the store.
config? StoreConfig<T> a StoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a StartHandler.

Returns

Store<T>

a Store

Defined in

src/lib/store.ts:144

makeStore<T>(initialValue, startOrConfig?): Store<T>

Make a store of type T.

Example usage:

const store$ = makeStore(0);
console.log(store$.content()); // 0
store$.subscribe((v) => console.log(v));
store$.set(10); // will trigger the above console log, printing 10

Type parameters

Name
T

Parameters

Name Type Description
initialValue undefined | T the initial value of the store.
startOrConfig? StartHandler<T> | StoreConfig<T> a StartHandler or a StoreConfig which contains configuration information such as a value comparator to avoid needless notifications to subscribers and a StartHandler.

Returns

Store<T>

a Store

Defined in

src/lib/store.ts:160