Skip to content

Commit

Permalink
Implement extra arguments functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
lkmill committed Dec 1, 2019
1 parent 3b1e5b9 commit 415d795
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Creates a new store, which is a tiny evented state container.
**Parameters**

- `state` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** Optional initial state (optional, default `{}`)
- `extraArgs`

**Examples**

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface Store<K> {
getState(): K;
}

export default function createStore<K>(state?: K): Store<K>;
export default function createStore<K>(state?: K, extraArgs?: any[] | any): Store<K>;

export interface ActionMap<K> {
[actionName: string]: ActionCreator<K>;
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { assign } from './util';
* store.setState({ a: 'b' }); // logs { a: 'b' }
* store.setState({ c: 'd' }); // logs { a: 'b', c: 'd' }
*/
export default function createStore(state) {
export default function createStore(state, extraArgs) {
let listeners = [];
state = state || {};

Expand Down Expand Up @@ -52,7 +52,8 @@ export default function createStore(state) {
function apply(result) {
setState(result, false, action);
}
let ret = (action.action || action)(state, this);
let args = [state, this];
let ret = (action.action || action).apply(null, extraArgs ? args.concat(extraArgs) : args);
if (ret != null) {
if (ret.then) return ret.then(apply);
return apply(ret);
Expand Down

0 comments on commit 415d795

Please sign in to comment.