Skip to content

Commit 619f1b7

Browse files
committed
Implement extra argument
1 parent 4dc3fde commit 619f1b7

File tree

5 files changed

+31
-30
lines changed

5 files changed

+31
-30
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Creates a new store, which is a tiny evented state container.
191191
**Parameters**
192192

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

195196
**Examples**
196197

index.d.ts

+22-22
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,46 @@
33
// K - Store state
44
// I - Injected props to wrapped component
55

6-
export type Listener<K> = (state: K, action?: Action<K>, update?: Partial<K>) => void;
6+
export type Listener<K, E> = (state: K, action?: Action<K, E>, update?: Partial<K>) => void;
77
export type Unsubscribe = () => void;
88

9-
export type AsyncActionFn<K> = (getState: () => K, action: (action: Action<K>) => Promise<void> | void) => Promise<Partial<K> | void>;
10-
export type SyncActionFn<K> = (getState: () => K, action: (action: Action<K>) => Promise<void> | void) => Partial<K> | void;
11-
export type ActionFn<K> = AsyncActionFn<K> | SyncActionFn<K>;
9+
export type AsyncActionFn<K, E> = (getState: () => K, action: (action: Action<K, E>) => Promise<void> | void, extraArg: E) => Promise<Partial<K> | void>;
10+
export type SyncActionFn<K, E> = (getState: () => K, action: (action: Action<K, E>, E) => Promise<void> | void, extraArg: E) => Partial<K> | void;
11+
export type ActionFn<K, E> = AsyncActionFn<K, E> | SyncActionFn<K, E>;
1212

13-
export type AsyncActionObject<K> = {
13+
export type AsyncActionObject<K, E> = {
1414
type: string;
15-
action: AsyncActionFn<K>;
15+
action: AsyncActionFn<K, E>;
1616
}
17-
export type SyncActionObject<K> = {
17+
export type SyncActionObject<K, E> = {
1818
type: string;
19-
action: SyncActionFn<K>;
19+
action: SyncActionFn<K, E>;
2020
}
21-
export type ActionObject<K> = AsyncActionObject<K> | SyncActionObject<K>;
21+
export type ActionObject<K, E> = AsyncActionObject<K, E> | SyncActionObject<K, E>;
2222

23-
export type Action<K> = ActionObject<K> | ActionFn<K>;
23+
export type Action<K, E> = ActionObject<K, E> | ActionFn<K, E>;
2424

25-
export type AsyncActionCreator<K> = (...args: any[]) => AsyncActionFn<K> | AsyncActionObject<K>;
26-
export type SyncActionCreator<K> = (...args: any[]) => SyncActionFn<K> | SyncActionObject<K>;
27-
export type ActionCreator<K> = AsyncActionCreator<K> | SyncActionCreator<K>;
25+
export type AsyncActionCreator<K, E> = (...args: any[]) => AsyncActionFn<K, E> | AsyncActionObject<K, E>;
26+
export type SyncActionCreator<K, E> = (...args: any[]) => SyncActionFn<K, E> | SyncActionObject<K, E>;
27+
export type ActionCreator<K, E> = AsyncActionCreator<K, E> | SyncActionCreator<K, E>;
2828

29-
export type ActionCreatorsObject<K> = {
30-
[actionCreator: string]: ActionCreator<K>
29+
export type ActionCreatorsObject<K, E> = {
30+
[actionCreator: string]: ActionCreator<K, E>
3131
}
3232

3333
export type MappedActionCreators<A> = {
34-
[P in keyof A]: A[P] extends AsyncActionCreator<any> ? (...args: any[]) => Promise<void> : (...args: any[]) => void
34+
[P in keyof A]: A[P] extends AsyncActionCreator<any, any> ? (...args: any[]) => Promise<void> : (...args: any[]) => void
3535
}
3636

3737

38-
export interface Store<K> {
39-
action(action: Action<K>): Promise<void> | void;
40-
setState<U extends keyof K>(update: Pick<K, U>, overwrite?: boolean, action?: Action<K>): void;
41-
subscribe(f: Listener<K>): Unsubscribe;
42-
unsubscribe(f: Listener<K>): void;
38+
export interface Store<K, E> {
39+
action(action: Action<K, E>): Promise<void> | void;
40+
setState<U extends keyof K>(update: Pick<K, U>, overwrite?: boolean, action?: Action<K, E>): void;
41+
subscribe(f: Listener<K, E>): Unsubscribe;
42+
unsubscribe(f: Listener<K, E>): void;
4343
getState(): K;
4444
}
4545

46-
export default function createStore<K>(state?: K): Store<K>;
46+
export default function createStore<K, E>(state?: K, extraArg?: E): Store<K, E>;
4747

4848
export type StateMapper<T, K, I> = (state: K, props: T) => I;

preact.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ declare module 'unistore/preact' {
77
import * as Preact from 'preact';
88
import { StateMapper, Store, ActionCreatorsObject, MappedActionCreators } from 'unistore';
99

10-
export function connect<T, S, K, I, A extends ActionCreatorsObject<K>>(
10+
export function connect<T, S, K, I, A extends ActionCreatorsObject<K, E>, E = any>(
1111
mapStateToProps: string | Array<string> | StateMapper<T, K, I> | null,
1212
actions?: A,
1313
): (
1414
Child: Preact.ComponentConstructor<T & I & MappedActionCreators<A>, S> | Preact.AnyComponent<T & I & MappedActionCreators<A>, S>
1515
) => Preact.ComponentConstructor<T | T & I, S>;
1616

17-
export interface ProviderProps<K> {
18-
store: Store<K>;
17+
export interface ProviderProps<K, E = any> {
18+
store: Store<K, E>;
1919
}
2020

2121
export class Provider<K> extends Preact.Component<ProviderProps<K>> {

react.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ declare module 'unistore/react' {
88
import * as React from 'react';
99
import { StateMapper, Store, ActionCreatorsObject, MappedActionCreators } from 'unistore';
1010

11-
export function connect<T, S, K, I, A extends ActionCreatorsObject<K>>(
11+
export function connect<T, S, K, I, A extends ActionCreatorsObject<K, E>, E = any>(
1212
mapStateToProps: string | Array<string> | StateMapper<T, K, I> | null,
1313
actions?: A,
1414
): (
1515
Child: ((props: T & I & MappedActionCreators<A>) => React.ReactNode) | React.ComponentClass<T & I & MappedActionCreators<A>, S> | React.FC<T & I & MappedActionCreators<A>>
1616
) => React.ComponentClass<T | T & I, S> | React.FC<T | T & I>;
1717

18-
export interface ProviderProps<K> {
19-
store: Store<K>;
18+
export interface ProviderProps<K, E = any> {
19+
store: Store<K, E>;
2020
}
2121

2222
export class Provider<K> extends React.Component<ProviderProps<K>, {}> {

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { assign } from './util';
1111
* store.setState({ a: 'b' }); // logs { a: 'b' }
1212
* store.setState({ c: 'd' }); // logs { a: 'b', c: 'd' }
1313
*/
14-
export default function createStore(state) {
14+
export default function createStore(state, extraArg) {
1515
let listeners = [];
1616
state = state || {};
1717

@@ -52,7 +52,7 @@ export default function createStore(state) {
5252
function apply(result) {
5353
setState(result, false, action);
5454
}
55-
let ret = (action.action || action)(this.getState, this.action);
55+
let ret = (action.action || action)(this.getState, this.action, extraArg);
5656
if (ret != null) {
5757
if (ret.then) return ret.then(apply);
5858
return apply(ret);

0 commit comments

Comments
 (0)