From c3c483bb36292f51fc041237efa388dc760373f5 Mon Sep 17 00:00:00 2001 From: Linus Miller Date: Sun, 1 Dec 2019 18:31:27 +0100 Subject: [PATCH] Implement extra arguments functionality --- README.md | 1 + index.d.ts | 2 +- src/index.js | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f307157..bf967c8 100644 --- a/README.md +++ b/README.md @@ -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** diff --git a/index.d.ts b/index.d.ts index 3dee759..b7175e1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -20,7 +20,7 @@ export interface Store { getState(): K; } -export default function createStore(state?: K): Store; +export default function createStore(state?: K, extraArgs?: any[] | any): Store; export interface ActionMap { [actionName: string]: ActionCreator; diff --git a/src/index.js b/src/index.js index 44b9290..a917f61 100644 --- a/src/index.js +++ b/src/index.js @@ -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 || {}; @@ -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);