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
Store
s. (Calling get on the store will recreate the original data strucure, though.)
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 the number of items in the specified collection, and subscribes the current scope to changes in this count.
The number of items contained in the collection, or 0 if the value is undefined.
When the value is not a collection and not undefined, an Error will be thrown.
Rest
...path: any[]Any path terms to resolve before retrieving the value.
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)
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'])
Rest
...path: anyResolves path
and then retrieves the value that is there, subscribing
to all read Store
values. If path
does not exist, undefined
is returned.
let store = new Store({a: {b: {c: {d: 42}}}})
assert('a' in store.get())
assert(store.get('a', 'b') === {c: {d: 42}})
Rest
...path: any[]Any path terms to resolve before retrieving the value.
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.
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)
Rest
...path: any[]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
.
Possible options: "undefined", "null", "boolean", "number", "string", "function", "array", "map" or "object".
Rest
...path: any[]Any path terms to resolve before retrieving the value.
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
.
let store = new Store({x: 123})
let subStore = store.ref('x')
assert(subStore.get() === 123)
assert(subStore.index() === 'x') // <----
Returns true
when the Store
was created by refing a path that
does not exist.
Checks if the specified collection is empty, and subscribes the current scope to changes of the emptiness of this collection.
When the specified collection is not empty true
is returned. If it is empty or if the value is undefined, false
is returned.
When the value is not a collection and not undefined, an Error will be thrown.
Rest
...path: any[]Any path terms to resolve before retrieving the value.
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.
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)
Rest
...path: any[]Applies a filter/map function on each item within the Store
's collection,
and reactively manages the returned Map
Store
to hold any results.
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.
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.
let store = new Store({a: {x: 1}})
store.merge({a: {y: 2}, b: 3})
assert(store.get() == {a: {x: 1, y: 2}, b: 3})
Rest
...pathAndValue: anyApplies 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.
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.
Function that transform the given store into output values that can take one of the following forms:
Object
or a Map
: Each key/value pair will be added to the output Store
.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.
Rest
...pathAndFuncs: anyLike get, but doesn't subscribe to changes.
Rest
...path: 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.
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]})
Rest
...pathAndValue: any[]Retrieve a value, subscribing to all read Store
values. This is a more flexible
form of the get and peek methods.
The resulting value, or undefined
if the path
does not exist.
Optional
defaultReturn this value when the path
does not exist. Defaults to undefined
.
Optional
depth?: numberLimit 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?: booleanWhen 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?: stringA 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.
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.
Rest
...path: any[]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
.
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)
Rest
...pathAndValue: any[]Generated using TypeDoc
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
andMap
. The latter three will always haveStore
objects as values, creating a tree ofStore
-objects.