Class Store

A data store that automatically subscribes the current scope to updates whenever data is read from it.

Supported data types are: string, number, boolean, undefined, null, Array, object and Map. The latter three will always have Store objects as values, creating a tree of Store-objects.

Hierarchy

  • Store

Constructors

  • Create a new store with the given value as its value. Defaults to undefined if no value is given. When the value is a plain JavaScript object, an Array or a Map, it will be stored as a tree of Stores. (Calling get on the store will recreate the original data strucure, though.)

    Example

    let emptyStore = new Store()
    let numStore = new Store(42)
    let objStore = new Store({x: {alice: 1, bob: 2}, y: [9,7,5,3,1]})

    Returns Store

  • Parameters

    • value: any

    Returns Store

Methods

  • Returns the number of items in the specified collection, and subscribes the current scope to changes in this count.

    Returns

    The number of items contained in the collection, or 0 if the value is undefined.

    Throws

    When the value is not a collection and not undefined, an Error will be thrown.

    Parameters

    • Rest ...path: any[]

      Any path terms to resolve before retrieving the value.

    Returns number

  • Sets the value for the store to undefined, which causes it to be omitted from the map (or array, if it's at the end)

    Example

    let store = new Store({a: 1, b: 2})
    store.delete('a')
    assert(store.get() == {b: 2})

    store = new Store(['a','b','c'])
    store.delete(1)
    assert(store.get() == ['a', undefined, 'c'])
    store.delete(2)
    assert(store.get() == ['a'])

    Parameters

    • Rest ...path: any

    Returns void

  • Returns

    Resolves path and then retrieves the value that is there, subscribing to all read Store values. If path does not exist, undefined is returned.

    Example

    let store = new Store({a: {b: {c: {d: 42}}}})
    assert('a' in store.get())
    assert(store.get('a', 'b') === {c: {d: 42}})

    Parameters

    • Rest ...path: any[]

      Any path terms to resolve before retrieving the value.

    Returns any

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type array. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns any[]

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type boolean. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns boolean

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type function. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns Function

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type map. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns Map<any, any>

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type number. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns number

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type object. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns object

  • Like get, but the first parameter is the default value (returned when the Store contains undefined). This default value is also used to determine the expected type, and to throw otherwise.

    Example

    let store = {x: 42}
    assert(getOr(99, 'x') == 42)
    assert(getOr(99, 'y') == 99)
    getOr('hello', x') # throws TypeError (because 42 is not a string)

    Type Parameters

    • T

    Parameters

    • defaultValue: T
    • Rest ...path: any[]

    Returns T

  • Returns

    Like get, but throws a TypeError if the resulting value is not of type string. Using this instead of just get is especially useful from within TypeScript.

    Parameters

    • Rest ...path: any[]

    Returns string

  • Returns a strings describing the type of the store value, subscribing to changes of this type. Note: this currently also subscribes to changes of primitive values, so changing a value from 3 to 4 would cause the scope to be rerun. This is not great, and may change in the future. This caveat does not apply to changes made inside an object, Array or Map.

    Returns

    Possible options: "undefined", "null", "boolean", "number", "string", "function", "array", "map" or "object".

    Parameters

    • Rest ...path: any[]

      Any path terms to resolve before retrieving the value.

    Returns string

  • Returns

    The index for this Store within its parent collection. This will be a number when the parent collection is an array, a string when it's an object, or any data type when it's a Map.

    Example

    let store = new Store({x: 123})
    let subStore = store.ref('x')
    assert(subStore.get() === 123)
    assert(subStore.index() === 'x') // <----

    Returns any

  • Returns

    Returns true when the Store was created by refing a path that does not exist.

    Returns boolean

  • Checks if the specified collection is empty, and subscribes the current scope to changes of the emptiness of this collection.

    Returns

    When the specified collection is not empty true is returned. If it is empty or if the value is undefined, false is returned.

    Throws

    When the value is not a collection and not undefined, an Error will be thrown.

    Parameters

    • Rest ...path: any[]

      Any path terms to resolve before retrieving the value.

    Returns boolean

  • Similar to ref(), but instead of returning undefined, new objects are created when a path does not exist yet. An error is still thrown when the path tries to index an invalid type. Unlike ref, makeRef does not subscribe to the path levels, as it is intended to be a write-only operation.

    Example

    let store = new Store() // Value is `undefined`

    let ref = store.makeRef('a', 'b', 'c')
    assert(store.get() == {a: {b: {}}}

    ref.set(42)
    assert(store.get() == {a: {b: {c: 42}}}

    ref.makeRef('d') // Throw Error (42 is not a collection)

    Parameters

    • Rest ...path: any[]

    Returns Store

  • Applies a filter/map function on each item within the Store's collection, and reactively manages the returned Map Store to hold any results.

    Returns

    • A map Store with the values returned by func and the corresponding keys from the original map, array or object Store.

    When items disappear from the Store or are changed in a way that func depends upon, the resulting items are removed from the output Store as well. When multiple input items produce the same output keys, this may lead to unexpected results.

    Parameters

    • func: ((store: Store) => any)

      Function that transform the given store into an output value or undefined in case this value should be skipped:

        • (store: Store): any
        • Parameters

          Returns any

    Returns Store

  • Sets the Store to the given mergeValue, but without deleting any pre-existing items when a collection overwrites a similarly typed collection. This results in a deep merge.

    Example

    let store = new Store({a: {x: 1}})
    store.merge({a: {y: 2}, b: 3})
    assert(store.get() == {a: {x: 1, y: 2}, b: 3})

    Parameters

    • Rest ...pathAndValue: any

    Returns void

  • peek the current value, pass it through func, and set the resulting value.

    Parameters

    • func: ((value: any) => any)

      The function transforming the value.

        • (value: any): any
        • Parameters

          • value: any

          Returns any

    Returns void

  • Applies a filter/map function on each item within the Store's collection, each of which can deliver any number of key/value pairs, and reactively manages the returned map Store to hold any results.

    Returns

    • A map Store with the key/value pairs returned by all func invocations.

    When items disappear from the Store or are changed in a way that func depends upon, the resulting items are removed from the output Store as well. When multiple input items produce the same output keys, this may lead to unexpected results.

    Parameters

    • func: ((store: Store) => any)

      Function that transform the given store into output values that can take one of the following forms:

      • an Object or a Map: Each key/value pair will be added to the output Store.
      • anything else: No key/value pairs are added to the output Store.
        • (store: Store): any
        • Parameters

          Returns any

    Returns Store

  • Iterate the specified collection (Array, Map or object), running the given code block for each item. When items are added to the collection at some later point, the code block will be ran for them as well. When an item is removed, the Store.clean handlers left by its code block are executed.

    Parameters

    • Rest ...pathAndFuncs: any

    Returns void

  • Like get, but doesn't subscribe to changes.

    Parameters

    • Rest ...path: any[]

    Returns any

  • Pushes a value to the end of the Array that is at the specified path in the store. If that store path is undefined, an Array is created first. The last argument is the value to be added, any earlier arguments indicate the path.

    Example

    let store = new Store()
    store.push(3) // Creates the array
    store.push(6)
    assert(store.get() == [3,6])

    store = new Store({myArray: [1,2]})
    store.push('myArray', 3)
    assert(store.get() == {myArray: [1,2,3]})

    Parameters

    • Rest ...pathAndValue: any[]

    Returns number

  • Retrieve a value, subscribing to all read Store values. This is a more flexible form of the get and peek methods.

    Returns

    The resulting value, or undefined if the path does not exist.

    Parameters

    • opts: {
          defaultValue?: any;
          depth?: number;
          path?: any[];
          peek?: boolean;
          type?: string;
      }
      • Optional defaultValue?: any

        Return this value when the path does not exist. Defaults to undefined.

      • Optional depth?: number

        Limit the depth of the retrieved data structure to this positive integer. When depth is 1, only a single level of the value at path is unpacked. This makes no difference for primitive values (like strings), but for objects, maps and arrays, it means that each value in the resulting data structure will be a reference to the Store for that value.

      • Optional path?: any[]

        The value for this path should be retrieved. Defaults to [], meaning the entire Store.

      • Optional peek?: boolean

        When peek is undefined or false, the current scope will automatically be subscribed to changes of any parts of the store being read. When true, no subscribers will be performed.

      • Optional type?: string

        A string specifying what type the query is expected to return. Options are: "undefined", "null", "boolean", "number", "string", "function", "array", "map" and "object". If the store holds a different type of value, a TypeError exception is thrown. By default (when type is undefined) no type checking is done.

    Returns any

  • Return a Store deeper within the tree by resolving the given path, subscribing to every level. In case undefined is encountered while resolving the path, a newly created Store containing undefined is returned. In that case, the Store's isDetached method will return true. In case something other than a collection is encountered, an error is thrown.

    Parameters

    • Rest ...path: any[]

    Returns Store

  • Sets the value to the last given argument. Any earlier argument are a Store-path that is first resolved/created using makeRef.

    When a Store is passed in as the value, its value will be copied (subscribing to changes). In case the value is an object, an Array or a Map, a reference to that data structure will be created, so that changes made through one Store will be reflected through the other. Be carefull not to create loops in your Store tree that way, as that would cause any future call to get to throw a RangeError (Maximum call stack size exceeded.)

    If you intent to make a copy instead of a reference, call get on the origin Store.

    Example

    let store = new Store() // Value is `undefined`

    store.set('x', 6) // Causes the store to become an object
    assert(store.get() == {x: 6})

    store.set('a', 'b', 'c', 'd') // Create parent path as objects
    assert(store.get() == {x: 6, a: {b: {c: 'd'}}})

    store.set(42) // Overwrites all of the above
    assert(store.get() == 42)

    store.set('x', 6) // Throw Error (42 is not a collection)

    Parameters

    • Rest ...pathAndValue: any[]

    Returns void

Generated using TypeDoc