Options
All
  • Public
  • Public/Protected
  • All
Menu

Module @crikey/stores-dynamic

@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.

API

Store creation functions:

Utility functions:

  • get_error - Uses get to retrieve the current store value and return its error property (or undefined)
  • get_value - Uses get to retrieve the current store value and return its value property (or throw its error property)
  • resolve - Resolves the given Dynamic item to its contained value, or throws its contained error
  • smart - Resolve store to a constant DynamicResolved value (on demand) if possible, or keep as a DynamicReadable

Type Guards:

Trigger functions:

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 comparing DynamicResolved values and determining if subscribers should be called
  • args - Optional array of arbitrary data to be passed as-is as the first argument to calculate
  • calculate([args,] resolve [,set]) - Callback used to derive the store value. Will be called each time any of the dependencies change
  • initial_value - Initial value of the store. Useful when calculate 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 a DynamicReadable store, then the store will be added as a dependency.
  • If resolve is called with a DynamicError or a DynamicReadable which resolves to a DynamicError, the contained error will be thrown. Each execution of calculate 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] }

Index

Type aliases

ComplexResolveDynamic: ResolveDynamic & { resolve: ResolveDynamic }
DeriveFn<A, R, SYNC, ASYNC>: [A] extends [never] ? [ASYNC] extends [never] ? (resolve: SYNC) => R : (resolve: SYNC, set: ASYNC) => R | Unsubscriber | void : [ASYNC] extends [never] ? (args: A, resolve: SYNC) => R : (args: A, resolve: SYNC, set: ASYNC) => R | Unsubscriber | void

Type parameters

  • A

  • R

  • SYNC

  • ASYNC

Dynamic<T>: DynamicResolved<T> | DynamicReadable<T>

Any kind of dynamic item

Type parameters

  • T

DynamicDependencies: { dependencies: DynamicDependents | undefined }

Type declaration

DynamicDependents: ReadonlySet<DynamicReadable<any>>
DynamicError: { error: any } & Partial<DynamicDependencies> & Partial<DynamicFlagConstant>

Hold an error thrown during the evaluation of a dynamic item

DynamicFlagConstant: { is_const: boolean }

Flag Dynamic value to determine if deriving calculation is static

Type declaration

  • is_const: boolean
DynamicReadable<T>: Readable

A store containing a dynamic result

Type parameters

  • T

DynamicResolved<T>: DynamicError | DynamicValue<T>

Hold the resolved value or error of a dynamic item

Type parameters

  • T

DynamicValue<T>: { value: T } & Partial<DynamicDependencies> & Partial<DynamicFlagConstant>

Hold the resolved value of a dynamic item

Type parameters

  • T

Inputs: [unknown, ...unknown[]] | unknown[]
ResolveDynamic: <V>(arg: Dynamic<V>) => V

Type declaration

    • Type parameters

      • V

      Parameters

      Returns V

Create Store Functions

  • Create a simple store which always returns the same value upon subscription

    Example:

    From ./packages/stores-base/examples/constant.test.ts#8~14

        const store = constant(10);

    console.log('store value:', get(store));

    // > store value: undefined

    Type parameters

    • T

    Parameters

    Returns DynamicReadable<T>

  • Create a simple store which always returns the same result - a DynamicError containing error.

    Example:

    From ./packages/stores-dynamic/examples/constant.test.ts#41~48

        const error = new Error('my error');
    const store = constant_error(error);

    console.log('store value:', get(store));

    // > store value: undefined

    Type parameters

    • T = unknown

    Parameters

    • error: any

    Returns DynamicReadable<T>

  • Create a simple store which always returns the same result - a DynamicValue containing value.

    Example:

    From ./packages/stores-dynamic/examples/constant.test.ts#25~31

        const store = constant_value(10);

    console.log('store value:', get(store));

    // > store value: undefined

    Type parameters

    • T

    Parameters

    • value: T

      the constant value of the store

    Returns DynamicReadable<T>

Other Functions

  • Transforms a regular store into a dynamic store by transforming its value into a @{link DynamicValue}

    See other signatures for alternate semantics

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#10~17


    const store = readable(1);
    const derived = dynamic(store);

    console.log('derived value:', get(derived));

    Type parameters

    • R

    Parameters

    • store: Readable

      the input store to transform

    • Optional dirty: boolean

      if true, creates a cheap wrapper around each subscription instead of a fully derived store

    Returns Readable

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For asynchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#59~82


    const a = writable({ value: { prop: 1 } });
    const derived = dynamic(
    (resolve) => {
    try {
    return { value: resolve(a)['prop'] }
    }
    catch (ex) {
    // natural place for error handling
    throw ex;
    }
    }
    );

    derived.subscribe((value) => console.log('derived value:', value))

    // @ts-ignore
    a.set({ value: null });

    // > derived value: { value: 1, dependencies: [a] }
    // > derived value: { error: TypeError, dependencies: [a] }

    Type parameters

    • R

    Parameters

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    • R

    Parameters

    Returns DynamicReadable<R | undefined>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    • R

    Parameters

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For asynchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#59~82


    const a = writable({ value: { prop: 1 } });
    const derived = dynamic(
    (resolve) => {
    try {
    return { value: resolve(a)['prop'] }
    }
    catch (ex) {
    // natural place for error handling
    throw ex;
    }
    }
    );

    derived.subscribe((value) => console.log('derived value:', value))

    // @ts-ignore
    a.set({ value: null });

    // > derived value: { value: 1, dependencies: [a] }
    // > derived value: { error: TypeError, dependencies: [a] }

    Type parameters

    Parameters

    • trigger: Trigger

      callback used to determine if subscribers should be called

    • args: A

      array of arguments to be passed to the callback unchanged

    • calculate: DeriveFn<A, Dynamic<R>, ComplexResolveDynamic, never>

      callback used to calculate the resulting store value

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    Parameters

    • trigger: Trigger

      callback used to determine if subscribers should be called

    • args: A

      array of arguments to be passed to the callback unchanged

    • calculate: DeriveFn<A, Dynamic<R>, ComplexResolveDynamic, ComplexSet>

      callback used to calculate the resulting store value

    Returns DynamicReadable<R | undefined>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    Parameters

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For asynchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#59~82


    const a = writable({ value: { prop: 1 } });
    const derived = dynamic(
    (resolve) => {
    try {
    return { value: resolve(a)['prop'] }
    }
    catch (ex) {
    // natural place for error handling
    throw ex;
    }
    }
    );

    derived.subscribe((value) => console.log('derived value:', value))

    // @ts-ignore
    a.set({ value: null });

    // > derived value: { value: 1, dependencies: [a] }
    // > derived value: { error: TypeError, dependencies: [a] }

    Type parameters

    • R

    Parameters

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    • R

    Parameters

    Returns DynamicReadable<R | undefined>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    • R

    Parameters

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For asynchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#59~82


    const a = writable({ value: { prop: 1 } });
    const derived = dynamic(
    (resolve) => {
    try {
    return { value: resolve(a)['prop'] }
    }
    catch (ex) {
    // natural place for error handling
    throw ex;
    }
    }
    );

    derived.subscribe((value) => console.log('derived value:', value))

    // @ts-ignore
    a.set({ value: null });

    // > derived value: { value: 1, dependencies: [a] }
    // > derived value: { error: TypeError, dependencies: [a] }

    Type parameters

    Parameters

    • args: A

      array of arguments to be passed to the callback unchanged

    • calculate: DeriveFn<A, Dynamic<R>, ComplexResolveDynamic, never>

      callback used to calculate the resulting store value

    Returns DynamicReadable<R>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    Parameters

    Returns DynamicReadable<R | undefined>

  • Derives a store via a calculation callback. Calculations can be dependent on any number of stores and will be recalculated as needed, Note: Whilst an array of arguments can be provided to be passed to the calculation, store dependencies need not be included in this list. The only requirement is that they be resolved via the resolve function passed to the calculate callback.

    Equivalent of derive, but store dependencies are determined dynamically as needed.

    For synchronous usage, see alternate signatures.

    Example:

    From ./packages/stores-dynamic/examples/dynamic.test.ts#27~47


    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] }
    _Note that if a dependency changes, the entire function is reevaluated_

    Type parameters

    Parameters

    Returns DynamicReadable<R>

  • get_error(dynamic: Dynamic<unknown>): any
  • get_value<T>(dynamic: Dynamic<T>): T | undefined
  • get_value<T, R>(dynamic: Dynamic<T>, default_: R): T | R
  • Returns item unchanged

    See alternate signatures for the real utility of this function

    Parameters

    Returns DynamicError

  • Returns item unchanged

    See alternate signatures for the real utility of this function

    Type parameters

    • R

    Parameters

    Returns DynamicValue<R>

  • Returns item unchanged

    See alternate signatures for the real utility of this function

    Type parameters

    • R

    Parameters

    Returns DynamicResolved<R>

  • Resolve store to a constant DynamicResolved value (on demand) if possible, or keep as a DynamicReadable.

    If item is not a store, returns the item unchanged.

    If item is a store: Upon initial introspection, the store is subscribed. If the resulting value is is static then it is cached, otherwise the store is cached. Future introspections operate solely on the cache.

    The net result is that values that can be calculated statically are only calculated once, but values which are calculated based off of dynamic dependencies will need to be calculated again upon subsequent subscriptions. Thus, for a single subscription the dynamic value will be calculated at least twice.

    Type parameters

    • R

    Parameters

    Returns Dynamic<R>

Generated using TypeDoc