Permalink
Browse files

Hoist unsubscribe function.

  • Loading branch information...
developit committed Dec 7, 2017
2 parents a458460 + 0e77f65 commit 7fadde7c560822b6ffdb3f4da20d031a0b4d7420
Showing with 21 additions and 19 deletions.
  1. +7 −7 README.md
  2. +14 −12 unistore.js
View
@@ -124,7 +124,7 @@ Creates a new store, which is a tiny evented state container.
let store = createStore();
store.subscribe( state => console.log(state) );
store.setState({ a: 'b' }); // logs { a: 'b' }
store.setState({ c: 'd' }); // logs { c: 'd' }
store.setState({ c: 'd' }); // logs { a: 'b', c: 'd' }
```
Returns **[store](#store)**
@@ -148,21 +148,21 @@ Register a listener function to be called whenever state is changed, and returns
**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
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
Retreive the current state object.
Retrieve the current state object.
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** state
@@ -194,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
*/
@@ -30,22 +35,20 @@ export function createStore(state) {
for (let i=0; i<listeners.length; i++) listeners[i](state);
},
/** Register a listener function to be called whenever state is changed, and returns the unsubscribe for that listener.
* @param {Function} listener
* @return {Function} unsubscribe
/** Register a listener function to be called whenever state is changed.
* @param {Function} listener A function to call when state changes. Gets passed the new state.
* @returns {Function} unsubscribe()
*/
subscribe(listener) {
listeners.push(listener);
return () => this.unsubscribe(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
@@ -85,12 +88,11 @@ export function connect(mapStateToProps, actions) {
this.setState(null);
}
};
let unsub;
this.componentDidMount = () => {
unsub = store.subscribe(update);
store.subscribe(update);
};
this.componentWillUnmount = () => {
unsub();
store.unsubscribe(update);
};
this.render = props => h(Child, assign(assign(assign({}, boundActions), props), state));
}

0 comments on commit 7fadde7

Please sign in to comment.