@bumble/store v0.3.1
Table of Contents
- Bumble Store State Management
- BumbleStore
- onStateChange
- StateSelector
- StateAction
- initStore
- getBackgroundPage
Bumble Store State Management
A flexible state management library for Chrome Extensions. Inspired by the React Component state API.
BumbleStore
getState
Returns copy of state object or value returned from mapping fn
Parameters
keyOrFn
(string | StateSelector) key name or fn :: {state} -> any
Examples
getState('apples').then((apples) => {
console.log(apples)
})
Returns Promise Resolves with the state object or the value of the state property.
setState
Sets state asynchronously using a state object or a function that returns an object.
Parameters
newStateOrFn
(string | StateAction) key name or fn :: {state} -> {state}
Examples
setState({ apples: 2 })
.then((state) => {
console.log('Number of apples:', state.apples)
})
Returns Promise<Object> Resolves to a copy of the new state object.
onStateChange
addListener
Adds a listener function to onStateChange.
Parameters
listener
Function A state property name or fn :: {state} -> any
Examples
store.onStateChange.addListener(fn)
Returns undefined Returns undefined.
removeListener
Removes a listener from onStateChange.
Parameters
listener
Function The listener function to remove.
Examples
store.onStateChange.removeListener(fn)
Returns undefined Returns undefined.
haslistener
Returns true if onStateChange has the listener.
Parameters
listener
Function Function to match.
Examples
store.onStateChange.hasListener(fn)
Returns boolean Returns true if onStateChange has the listener.
haslisteners
Returns true if function has any listeners.
Examples
store.onStateChange.hasListeners()
Returns boolean Returns true onStateChange has any listeners.
fireListeners
Calls all the onStateChange listeners.
Examples
store.onStateChange.fireListeners()
Returns undefined Returns undefined.
StateSelector
Derive a value from the current state.
Type: Function
Parameters
state
Object The current state.
Returns any Any derived value.
StateAction
Map the state object at the time setState() fires.
Type: Function
Parameters
state
Object A copy of current state object.
Returns Object The new state object.
initStore
Sets up state and immediately calls the callback. Sets window.store as a Promise that resolves with the store after the callback completes.
Parameters
initialState
Object The initial state values.
Examples
const {} = store.initStore({ apples: 2 })
const defaultState = { apples: 2 }
storageLocal.get('state')
.then(({state = defaultState}) => state)
.then(store.initStore)
.then(({ setState, getState, onStateChange }) => {
console.log('Store has been initialized.')
})
Returns BumbleStore The initialized store.
getBackgroundPage
Retrieves the Window object for the background page running inside the current extension. If the background page is unloaded, this will load the background page before resolving.
See Chrome API Docs and MDN.
Examples
getBackgroundPage().then((bgWindow) => {
// The background page window.
})
Returns Promise<Window> A Promise that will be fulfilled with the Window object for the background page, if there is one.