Skip to content

Commit

Permalink
Implement extra argument
Browse files Browse the repository at this point in the history
  • Loading branch information
lohfu committed Apr 7, 2020
1 parent 4361a83 commit 157fac7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 31 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 `{}`)
- `extraArg`

**Examples**

Expand Down
2 changes: 1 addition & 1 deletion devtools.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Store } from "unistore";

export default function unistoreDevTools<K>(store: Store<K>): Store<K>;
export default function unistoreDevTools<K, E>(store: Store<K, E>): Store<K, E>;
44 changes: 22 additions & 22 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,46 @@
// K - Store state
// I - Injected props to wrapped component

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

export type AsyncActionFn<K> = (getState: () => K, action: (action: Action<K>) => Promise<void> | void) => Promise<Partial<K> | void>;
export type SyncActionFn<K> = (getState: () => K, action: (action: Action<K>) => Promise<void> | void) => Partial<K> | void;
export type ActionFn<K> = AsyncActionFn<K> | SyncActionFn<K>;
export type AsyncActionFn<K, E> = (getState: () => K, action: (action: Action<K, E>) => Promise<void> | void, extraArg: E) => Promise<Partial<K> | void>;
export type SyncActionFn<K, E> = (getState: () => K, action: (action: Action<K, E>, E) => Promise<void> | void, extraArg: E) => Partial<K> | void;
export type ActionFn<K, E> = AsyncActionFn<K, E> | SyncActionFn<K, E>;

export type AsyncActionObject<K> = {
export type AsyncActionObject<K, E> = {
type: string;
action: AsyncActionFn<K>;
action: AsyncActionFn<K, E>;
}
export type SyncActionObject<K> = {
export type SyncActionObject<K, E> = {
type: string;
action: SyncActionFn<K>;
action: SyncActionFn<K, E>;
}
export type ActionObject<K> = AsyncActionObject<K> | SyncActionObject<K>;
export type ActionObject<K, E> = AsyncActionObject<K, E> | SyncActionObject<K, E>;

export type Action<K> = ActionObject<K> | ActionFn<K>;
export type Action<K, E> = ActionObject<K, E> | ActionFn<K, E>;

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

export type ActionCreatorsObject<K> = {
[actionCreator: string]: ActionCreator<K>
export type ActionCreatorsObject<K, E> = {
[actionCreator: string]: ActionCreator<K, E>
}

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


export interface Store<K> {
dispatch(action: Action<K>): Promise<void> | void;
setState<U extends keyof K>(update: Pick<K, U>, overwrite?: boolean, action?: Action<K>): void;
subscribe(f: Listener<K>): Unsubscribe;
unsubscribe(f: Listener<K>): void;
export interface Store<K, E> {
dispatch(action: Action<K, E>): Promise<void> | void;
setState<U extends keyof K>(update: Pick<K, U>, overwrite?: boolean, action?: Action<K, E>): void;
subscribe(f: Listener<K, E>): Unsubscribe;
unsubscribe(f: Listener<K, E>): void;
getState(): K;
}

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

export type StateMapper<T, K, I> = (state: K, props: T) => I;
6 changes: 3 additions & 3 deletions preact.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ declare module 'unistore/preact' {
import * as Preact from 'preact';
import { StateMapper, Store, ActionCreatorsObject, MappedActionCreators } from 'unistore';

export function connect<T, S, K, I, A extends ActionCreatorsObject<K>>(
export function connect<T, S, K, I, A extends ActionCreatorsObject<K, E>, E = any>(
mapStateToProps: string | Array<string> | StateMapper<T, K, I> | null,
actions?: A,
): (
Child: Preact.ComponentConstructor<T & I & MappedActionCreators<A>, S> | Preact.AnyComponent<T & I & MappedActionCreators<A>, S>
) => Preact.ComponentConstructor<T | T & I, S>;

export interface ProviderProps<K> {
store: Store<K>;
export interface ProviderProps<K, E = any> {
store: Store<K, E>;
}

export class Provider<K> extends Preact.Component<ProviderProps<K>> {
Expand Down
6 changes: 3 additions & 3 deletions react.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ declare module 'unistore/react' {
import * as React from 'react';
import { StateMapper, Store, ActionCreatorsObject, MappedActionCreators } from 'unistore';

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

export interface ProviderProps<K> {
store: Store<K>;
export interface ProviderProps<K, E = any> {
store: Store<K, E>;
}

export class Provider<K> extends React.Component<ProviderProps<K>, {}> {
Expand Down
4 changes: 2 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, extraArg) {
let listeners = [];
state = state || {};

Expand All @@ -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);
Expand Down

0 comments on commit 157fac7

Please sign in to comment.