1.0.46 • Published 7 years ago
nexustate v1.0.46
Description
Nexustate is designed to manage state and send notifications to listeners when state changes.
install
npm install --save nexustate
API Reference
Nexustate({ saveCallback = null, loadCallback = null, storageKey = 'default', persist = false })
parameters:
persist: Save and load this state? Changes will be loaded using theload()function and saved after changes occur or with thesave()functionsaveCallback: This function will be called on a throttled interval when your state changes if persist is trueloadCallback: This function is called when theload()function is called,load()must be called manually unless usinggetNexustate()storageKey: When calling thesaveCallbackandloadCallback, this key is the root key
response:
New instance of Nexustate
example:
import { Nexustate } from 'nexustate';
const saveState = {};
const state = new Nexustate({ saveCallback: (key, value) => { saveState[key] = value; }, loadCallback: (key) => saveState[key] });
const logChange = (data) => console.log;
state.listen({ key: ['some', 'key'], callback: logChange });
state.set({ some: { key: 'hello' } });
// logChange is called with the value 'hello'
// saveState['default'] is changed to { some: { key: 'hello' } };Nexustate.set(object, options = { immediatePersist: false, noNotify: false })
parameters:
object: Apply this object into the current stateoptions.immediatePersist: Don't throttle the save function, call it immediately. Useful for testsoptions.noNotify: Don't send notifications to listeners
response:
Current full state
example:
state.set({ some: { key: 'hello' } }, { immediatePersist: true });Nexustate.setKey(key, object, options = { immediatePersist: false, noNotify: false })
parameters:
key: String or array representing the location we want to change, this value does not have to already exist for set to workobject: Apply this object into the current stateoptions.immediatePersist: Don't throttle the save function, call it immediately. Useful for testsoptions.noNotify: Don't send notifications to listeners
response:
Current full state
example:
state.setKey(['a', 'b', 'c'], 'hi');
state.get(null) // returns { a: { b: { c: 'hi' } } };Nexustate.listen({ key: null, callback: () => {}, alias: null, component: null, transform: null, noChildUpdates: false })
parameters:
key: String or array for the path we want to listen for changes incallback: When something changes in that path, run this callback with the new dataalias: Send this alias along with the change to callbackcomponent: Used as a reference to remove all callbacks for a given entity using unlistenComponenttransform: Function to transform the incoming data before executing callbacknoChildUpdates: Don't listen for any updates to nested child records
response:
Success boolean
example:
state.listen({ key: 'a.b.c', data => console.log , alias: 'boop', transform: value => `+++${value}` });1.0.46
7 years ago
1.0.45
7 years ago
1.0.44
7 years ago
1.0.43
7 years ago
1.0.42
7 years ago
1.0.41
7 years ago
1.0.40
7 years ago
1.0.39
7 years ago
1.0.38
7 years ago
1.0.37
7 years ago
1.0.36
7 years ago
1.0.35
7 years ago
1.0.34
7 years ago
1.0.33
7 years ago
1.0.32
7 years ago
1.0.31
7 years ago
1.0.30
7 years ago
1.0.29
7 years ago
1.0.28
7 years ago
1.0.27
7 years ago
1.0.25
7 years ago
1.0.24
7 years ago
1.0.23
7 years ago
1.0.22
7 years ago
1.0.21
7 years ago
1.0.20
7 years ago