Permalink
Browse files

Alternative implementation for #10.

  • Loading branch information...
developit committed Dec 7, 2017
1 parent 32a3c94 commit 0e77f65fca8545b655c9b8814ec42d9419402e52
Showing with 17 additions and 10 deletions.
  1. +6 −4 README.md
  2. +11 −6 unistore.js
View
@@ -148,15 +148,17 @@ Register a listener function to be called whenever state is changed.
**Parameters**
- `listener` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
- `listener` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** A function to call when state changes. Gets passed the new state.
Returns **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** unsubscribe()
##### unsubscribe
Remove a previously-registered listener function.
**Parameters**
- `listener` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**
- `listener` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** The callback previously passed to `subscribe()` that should be removed.
##### getState
@@ -192,12 +194,12 @@ Returns **Component** ConnectedComponent
Provider exposes a store (passed as `props.store`) into context.
Generally, an entire application is wrapped in a single `<Provider>` at the root.
Generally, an entire application is wrapped in a single `<Provider>` at the root.
**Parameters**
- `props` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
- `props.store` **Store** A {Store} instance to expose via context.
- `props.store` **Store** A {Store} instance to expose via context.
### Reporting Issues
View
@@ -15,6 +15,11 @@ export function createStore(state) {
let listeners = [];
state = state || {};
function unsubscribe(listener) {
let i = listeners.indexOf(listener);
listeners.splice(i, !!~i);
}
/** An observable state container, returned from {@link createStore}
* @name store
*/
@@ -31,19 +36,19 @@ export function createStore(state) {
},
/** Register a listener function to be called whenever state is changed.
* @param {Function} listener
* @param {Function} listener A function to call when state changes. Gets passed the new state.
* @returns {Function} unsubscribe()
*/
subscribe(listener) {
listeners.push(listener);
return () => { unsubscribe(listener); };
},
/** Remove a previously-registered listener function.
* @param {Function} listener
* @param {Function} listener The callback previously passed to `subscribe()` that should be removed.
* @function
*/
unsubscribe(listener) {
let i = listeners.indexOf(listener);
listeners.splice(i, !!~i);
},
unsubscribe,
/** Retrieve the current state object.
* @returns {Object} state

0 comments on commit 0e77f65

Please sign in to comment.