Introspection and Changing methods
get(key)
: any
Gets the value at the given key. throws on non-iterables. note - if get doesn't find a result,
for maps, if the Map.has() method fails, it uses a reducer to allow custom comparators to return a match.
hasKey(key)
: boolean
whether the collection has an item under the given key. Throws on non-iterables.
hasItem(item)
: boolean
"has" methods uses referential identity for detection. You can tune the collection with custom comparators to use other criteria for equality.
keyOf(item)
: key | undefined
Similar to indexOf for arrays; however indexOf returns -1 for un-found items, and this method returns undefined. Note -- an item may appear in several places in the same collection; this method returns the first key in which the item occurs. (you could call it "firstKeyOf")
when trying to "find" an item in the collection it uses === comparison by default. If you want to find the first item like a given one, see the section on comparators.
.onChange: (newStore: StoreType, method: string, input: any[]) => any
this is a function that will get a message any time the store is updated -- just before the store is actually updated. This means you can compare the current collection.value value with the argument of the onChange method.
Setting onChange disables any previous hook present in onChange.
.onChange
is undefined by default.
If you put a function in onChange that throws then the new value will not be injected into the collection; the error
will be expressed in console.warn
but not thrown to the caller. So, if you want to look out for errors in the change cycle,
you must put try/catch into the onChange method you inject.
onChange is a good place to put methods into the collection that reject bad value updates, to enforce a schema.