From 157fac7cc293705839232bbd2ea6b87bf02b18b8 Mon Sep 17 00:00:00 2001 From: Linus Miller Date: Mon, 6 Apr 2020 17:51:29 +0200 Subject: [PATCH] Implement extra argument --- README.md | 1 + devtools.d.ts | 2 +- index.d.ts | 44 ++++++++++++++++++++++---------------------- preact.d.ts | 6 +++--- react.d.ts | 6 +++--- src/index.js | 4 ++-- 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 35a8aed..aa100a8 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 `{}`) +- `extraArg` **Examples** diff --git a/devtools.d.ts b/devtools.d.ts index 41678cf..a7ccdc0 100644 --- a/devtools.d.ts +++ b/devtools.d.ts @@ -1,3 +1,3 @@ import { Store } from "unistore"; -export default function unistoreDevTools(store: Store): Store; +export default function unistoreDevTools(store: Store): Store; diff --git a/index.d.ts b/index.d.ts index a81ede6..81e2c73 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,46 +3,46 @@ // K - Store state // I - Injected props to wrapped component -export type Listener = (state: K, action?: Action, update?: Partial) => void; +export type Listener = (state: K, action?: Action, update?: Partial) => void; export type Unsubscribe = () => void; -export type AsyncActionFn = (getState: () => K, action: (action: Action) => Promise | void) => Promise | void>; -export type SyncActionFn = (getState: () => K, action: (action: Action) => Promise | void) => Partial | void; -export type ActionFn = AsyncActionFn | SyncActionFn; +export type AsyncActionFn = (getState: () => K, action: (action: Action) => Promise | void, extraArg: E) => Promise | void>; +export type SyncActionFn = (getState: () => K, action: (action: Action, E) => Promise | void, extraArg: E) => Partial | void; +export type ActionFn = AsyncActionFn | SyncActionFn; -export type AsyncActionObject = { +export type AsyncActionObject = { type: string; - action: AsyncActionFn; + action: AsyncActionFn; } -export type SyncActionObject = { +export type SyncActionObject = { type: string; - action: SyncActionFn; + action: SyncActionFn; } -export type ActionObject = AsyncActionObject | SyncActionObject; +export type ActionObject = AsyncActionObject | SyncActionObject; -export type Action = ActionObject | ActionFn; +export type Action = ActionObject | ActionFn; -export type AsyncActionCreator = (...args: any[]) => AsyncActionFn | AsyncActionObject; -export type SyncActionCreator = (...args: any[]) => SyncActionFn | SyncActionObject; -export type ActionCreator = AsyncActionCreator | SyncActionCreator; +export type AsyncActionCreator = (...args: any[]) => AsyncActionFn | AsyncActionObject; +export type SyncActionCreator = (...args: any[]) => SyncActionFn | SyncActionObject; +export type ActionCreator = AsyncActionCreator | SyncActionCreator; -export type ActionCreatorsObject = { - [actionCreator: string]: ActionCreator +export type ActionCreatorsObject = { + [actionCreator: string]: ActionCreator } export type MappedActionCreators = { - [P in keyof A]: A[P] extends AsyncActionCreator ? (...args: any[]) => Promise : (...args: any[]) => void + [P in keyof A]: A[P] extends AsyncActionCreator ? (...args: any[]) => Promise : (...args: any[]) => void } -export interface Store { - dispatch(action: Action): Promise | void; - setState(update: Pick, overwrite?: boolean, action?: Action): void; - subscribe(f: Listener): Unsubscribe; - unsubscribe(f: Listener): void; +export interface Store { + dispatch(action: Action): Promise | void; + setState(update: Pick, overwrite?: boolean, action?: Action): void; + subscribe(f: Listener): Unsubscribe; + unsubscribe(f: Listener): void; getState(): K; } -export default function createStore(state?: K): Store; +export default function createStore(state?: K, extraArg?: E): Store; export type StateMapper = (state: K, props: T) => I; diff --git a/preact.d.ts b/preact.d.ts index cf3bd5b..71896b7 100644 --- a/preact.d.ts +++ b/preact.d.ts @@ -7,15 +7,15 @@ declare module 'unistore/preact' { import * as Preact from 'preact'; import { StateMapper, Store, ActionCreatorsObject, MappedActionCreators } from 'unistore'; - export function connect>( + export function connect, E = any>( mapStateToProps: string | Array | StateMapper | null, actions?: A, ): ( Child: Preact.ComponentConstructor, S> | Preact.AnyComponent, S> ) => Preact.ComponentConstructor; - export interface ProviderProps { - store: Store; + export interface ProviderProps { + store: Store; } export class Provider extends Preact.Component> { diff --git a/react.d.ts b/react.d.ts index 41137f2..b4cbfb3 100644 --- a/react.d.ts +++ b/react.d.ts @@ -8,15 +8,15 @@ declare module 'unistore/react' { import * as React from 'react'; import { StateMapper, Store, ActionCreatorsObject, MappedActionCreators } from 'unistore'; - export function connect>( + export function connect, E = any>( mapStateToProps: string | Array | StateMapper | null, actions?: A, ): ( Child: ((props: T & I & MappedActionCreators) => React.ReactNode) | React.ComponentClass, S> | React.FC> ) => React.ComponentClass | React.FC; - export interface ProviderProps { - store: Store; + export interface ProviderProps { + store: Store; } export class Provider extends React.Component, {}> { diff --git a/src/index.js b/src/index.js index f6976b4..509056d 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, extraArg) { let listeners = []; state = state || {}; @@ -32,7 +32,7 @@ export default function createStore(state) { function apply(result) { setState(result, false, action); } - let ret = (action.action || action)(getState, dispatch); + let ret = (action.action || action)(getState, dispatch, extraArg); if (ret != null) { if (ret.then) return ret.then(apply); return apply(ret);