Comparing version 1.1.0 to 1.2.0
@@ -1,15 +0,12 @@ | ||
import { Store as IStore, Listener, Unsubscribe, Middleware, Reducer, Dispatch, GetState } from './types'; | ||
import thunk, { ThunkMiddleware } from './middlewares/thunk'; | ||
import { Store as IStore, Listener, Unsubscribe, Middleware, Dispatch, GetState } from './types'; | ||
export * from './types'; | ||
export * from './middlewares/thunk'; | ||
export { thunk }; | ||
export default class Store<State, R = Reducer<State>> implements IStore<State, R> { | ||
static thunk: ThunkMiddleware<any, any>; | ||
export default class Store<S> implements IStore<S> { | ||
private state; | ||
private listeners; | ||
constructor(initialState: State); | ||
getState: GetState<State>; | ||
dispatch: Dispatch<R>; | ||
constructor(initialState: S); | ||
getState: GetState<S>; | ||
dispatch: Dispatch<S>; | ||
subscribe: (listener: Listener) => Unsubscribe; | ||
addMiddleware: <R2>(...middlewares: Middleware<State, R, R2>[]) => Store<State, R | R2>; | ||
addMiddleware: (...middlewares: Middleware[]) => this; | ||
} |
@@ -6,4 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var thunk_1 = require("./middlewares/thunk"); | ||
exports.thunk = thunk_1.default; | ||
__export(require("./middlewares/thunk")); | ||
@@ -20,3 +18,3 @@ var Store = (function () { | ||
_this.listeners.forEach(function (listener) { return listener(); }); | ||
return reducer; | ||
return _this.state; | ||
}; | ||
@@ -36,7 +34,3 @@ this.subscribe = function (listener) { | ||
throw new Error('Middleware is not a function: addMiddleware takes only middlewares as functions.'); | ||
middlewares.forEach(function (middleware) { | ||
var prevDispatch = _this.dispatch; | ||
var dispatch = function (reducer) { return middleware(_this)(prevDispatch)(reducer); }; | ||
_this.dispatch = dispatch; | ||
}); | ||
middlewares.forEach(function (middleware) { return (_this.dispatch = middleware(_this)(_this.dispatch)); }); | ||
return _this; | ||
@@ -48,4 +42,3 @@ }; | ||
}()); | ||
Store.thunk = thunk_1.default; | ||
exports.default = Store; | ||
//# sourceMappingURL=index.js.map |
@@ -1,15 +0,16 @@ | ||
import { Middleware, GetState, Dispatch, Reducer } from '../types'; | ||
export interface ThunkMiddleware<State, ExtraArgument> extends Middleware<State, Reducer<State>, Thunk<State, ExtraArgument, any>> { | ||
withExtraArgument: <EA>(extraArgument: EA) => ThunkMiddleware<State, EA>; | ||
import { Middleware, Dispatch, GetState } from '../types'; | ||
export interface Delegate<S, E, R> { | ||
(dispatch: Dispatch<S>, getState: GetState<S>, extraArgument: E): R; | ||
} | ||
export interface Delegate<State, ExtraArgument, Return> { | ||
(dispatch: ThunkDispatch<State, ExtraArgument>, getState: GetState<State>, extraArgument: ExtraArgument): Return; | ||
export interface Thunk<S, E, R> { | ||
(state: S): Delegate<S, E, R>; | ||
} | ||
export interface ThunkDispatch<State, ExtraArgument> extends Dispatch<Reducer<State>> { | ||
<Return>(reducer: Thunk<State, ExtraArgument, Return>): Return; | ||
declare module '../types' { | ||
interface Dispatch<S> { | ||
<R, E = any>(thunk: Thunk<S, E, R>): R; | ||
} | ||
} | ||
export interface Thunk<State, ExtraArgument, Return> { | ||
(state: State): Delegate<State, ExtraArgument, Return>; | ||
export interface ThunkMiddleware<E> extends Middleware { | ||
withExtraArgument: <E>(extraArgument: E) => ThunkMiddleware<E>; | ||
} | ||
declare const _default: ThunkMiddleware<any, any>; | ||
export default _default; | ||
export declare const thunk: ThunkMiddleware<{}>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var thunkFactory = function (extraArgument) { | ||
var thunk = function (store) { return function (next) { return function (reducer) { | ||
var thunk = (function (store) { return function (next) { return function (reducer) { | ||
if (typeof reducer !== 'function') | ||
throw new Error('Thunk requires reducers as functions'); | ||
var state = store.getState(); | ||
var result = reducer(state); | ||
throw new Error('Thunk reducer must return a function'); | ||
var result = reducer(store.getState()); | ||
if (typeof result === 'function') | ||
return result(store.dispatch, store.getState, extraArgument); | ||
else { | ||
next(function (_) { return result; }); | ||
return reducer; | ||
} | ||
}; }; }; | ||
thunk['withExtraArgument'] = thunkFactory; | ||
else | ||
return next(function (_) { return result; }); | ||
}; }; }); | ||
thunk.withExtraArgument = thunkFactory; | ||
return thunk; | ||
}; | ||
exports.default = thunkFactory(); | ||
exports.thunk = thunkFactory(); | ||
//# sourceMappingURL=thunk.js.map |
@@ -1,9 +0,9 @@ | ||
export interface GetState<State> { | ||
(): State; | ||
export interface GetState<S> { | ||
(): S; | ||
} | ||
export interface Reducer<State> { | ||
(state: State): State; | ||
export interface Reducer<S> { | ||
(state: S): S; | ||
} | ||
export interface Dispatch<Reducer> { | ||
(reducer: Reducer): Reducer; | ||
export interface Dispatch<S> { | ||
(reducer: Reducer<S>): S; | ||
} | ||
@@ -16,14 +16,10 @@ export interface Listener { | ||
} | ||
export interface Middleware<State, R1, R2> { | ||
(store: Store<State, R1>): { | ||
(next: Dispatch<R1>): { | ||
(reducer: R2): any; | ||
}; | ||
}; | ||
export interface Middleware { | ||
<S>(store: Store<S>): (next: Dispatch<S>) => Dispatch<S>; | ||
} | ||
export interface Store<State, R = Reducer<State>> { | ||
getState: GetState<State>; | ||
dispatch: Dispatch<R>; | ||
export interface Store<S> { | ||
getState: GetState<S>; | ||
dispatch: Dispatch<S>; | ||
subscribe(listener: Listener): Unsubscribe; | ||
addMiddleware<R2>(...middlewares: Middleware<State, R, R2>[]): Store<State, R | R2>; | ||
addMiddleware(...middlewares: Middleware[]): this; | ||
} |
{ | ||
"name": "repatch", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Dispatch reducers", | ||
@@ -5,0 +5,0 @@ "main": "lib/store/index", |
@@ -90,11 +90,5 @@ # <a href="https://www.npmjs.com/package/repatch"><img alt="Repatch" src="http://jaystack.com/wp-content/uploads/2017/08/repatch-logo.png" height="50px"></a> | ||
```javascript | ||
Middleware: Store -> (Reducer -> Reducer) -> Reducer -> any | ||
Middleware: Store -> Next -> Reducer -> any | ||
``` | ||
where | ||
```javascript | ||
Next: Reducer -> Reducer | ||
``` | ||
Use the `addMiddleware` method to chaining middlewares: | ||
@@ -101,0 +95,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13463
109
223