@crikey/stores-dynamic
v0.0.21
Published
Types and functions for creating [Svelte](https://svelte.dev/) compatible stores.
Downloads
43
Readme
@crikey/stores-dynamic
Types and functions for creating Svelte compatible stores.
@crikey/stores-dynamic
stores further extend the svelte/store
contract to allow for dynamic dependencies and natural error handling.
See @crikey/stores-dynamic for full documentation.
API
Store creation functions:
constant
- Create aReadable
store with a fixedDynamicResolved
constant_value
- Create aReadable
store with a fixedDynamicValue
constant_error
- Create aReadable
store with a fixedDynamicError
dynamic
- Convert a standardReadable
to aDynamicReadable
dynamic
- Create aDynamicReadable
store derived from the resolved values of other stores
Utility functions:
get_error
- Usesget
to retrieve the current store value and return its error property (or undefined)get_value
- Usesget
to retrieve the current store value and return its value property (or throw its error property)resolve
- Resolves the givenDynamic
item to its contained value, or throws its contained errorsmart
- Resolve store to a constantDynamicResolved
value (on demand) if possible, or keep as aDynamicReadable
Type Guards:
is_dynamic_resolved
- Returns true if the given argument is aDynamicResolved
value (has either a value or an error property)is_dynamic_value
- Returns true if the given argument is aDynamicValue
is_dynamic_error
- Returns true if the given argument is aDynamicError
Trigger functions:
create_trigger_dynamic
- Creates a trigger function forDynamicResolved
values
Installation
# pnpm
$ pnpm add @crikey/stores-dynamic
# npm
$ npm add @crikey/stores-dynamic
# yarn
$ yarn add @crikey/stores-dynamic
Introduction
Dynamic stores store more than just a simple value, instead they store a DynamicResolved
which
can itself contain a value via DynamicValue
, or an error via DynamicError
. Storing values
and errors separately facilitates error handling.
Any Readable
containing an object with either a value
or error
property is considered a
DynamicReadable
and can safely be mixed with this library.
The kind of dynamic can be ascertained by querying its properties. An object with an error
property is a
DynamicError
, an object with a value
property is a DynamicValue
and an object with a subscribe
function is a Readable
.
Being able to distinguish types in this way allows for a cohesive syntax when dealing with otherwise arbitrary inputs.
Usage
The dynamic
function has many signatures:
Converting an existing Readable
dynamic(store)
Convert a regular Readable
store to a DynamicReadable
by wrapping its value via DynamicValue
Creating a new derived store
dynamic([trigger,] [args,] calculate [,initial_value])
trigger
- Optional function used for comparingDynamicResolved
values and determining if subscribers should be calledargs
- Optional array of arbitrary data to be passed as-is as the first argument tocalculate
calculate([args,] resolve [,set])
- Callback used to derive the store value. Will be called each time any of the dependencies changeinitial_value
- Initial value of the store. Useful whencalculate
is async.
If args
are provided to dynamic
, they are passed unchanged to calculate
as the first argument.
If calculate
accepts an argument after resolve
, it is deemed asynchronous.
resolve
can be used to obtain the inner value of any Dynamic
.
- If
resolve
is called with aDynamicReadable
store, then the store will be added as a dependency. - If
resolve
is called with aDynamicError
or aDynamicReadable
which resolves to aDynamicError
, the contained error will be thrown. Each execution ofcalculate
will subscribe to new stores as required and unsubscribe to stores no longer required.
Synchronous dynamic stores which are only dependent on constant inputs (see: DynamicFlagConstant
) will be cached.
This can be avoided by setting is_const
in the result of calculate
.
Examples
Example: Dynamic dependencies
import {writable} from "@crikey/stores-strict";
import {dynamic} from "@crikey/stores-dynamic";
const a = writable({ value: 0 });
const b = writable({ value: 'b value' });
const c = writable({ value: 'c value' });
const derived = dynamic(
(resolve) => {
return resolve(a) % 2 === 0
? { value: resolve(b) }
: { value: resolve(c) }
}
);
derived.subscribe((value) => console.log('derived value:', value))
a.set({ value: 1 });
// > derived value: { value: 'b value', dependencies: [a, b] }
// > derived value: { value: 'c value', dependencies: [a, c] }