redux-sacala 0.0.15 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.idea/modules.xml +8 -0
- package/.idea/redux-sacala.iml +12 -0
- package/.idea/vcs.xml +6 -0
- package/babel.config.js +6 -0
- package/build/redux-sacala.d.ts +33 -33
- package/build/redux-sacala.js +78 -77
- package/build/redux-sacala.js.map +1 -1
- package/build/redux-sacala.spec.d.ts +1 -0
- package/build/redux-sacala.spec.js +40 -0
- package/build/redux-sacala.spec.js.map +1 -0
- package/package.json +12 -5
- package/src/redux-sacala.spec.ts +62 -0
- package/src/redux-sacala.ts +8 -6
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<project version="4">
|
|
3
|
+
<component name="ProjectModuleManager">
|
|
4
|
+
<modules>
|
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/redux-sacala.iml" filepath="$PROJECT_DIR$/.idea/redux-sacala.iml" />
|
|
6
|
+
</modules>
|
|
7
|
+
</component>
|
|
8
|
+
</project>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
|
2
|
+
<module type="WEB_MODULE" version="4">
|
|
3
|
+
<component name="NewModuleRootManager">
|
|
4
|
+
<content url="file://$MODULE_DIR$">
|
|
5
|
+
<excludeFolder url="file://$MODULE_DIR$/temp" />
|
|
6
|
+
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
|
|
7
|
+
<excludeFolder url="file://$MODULE_DIR$/tmp" />
|
|
8
|
+
</content>
|
|
9
|
+
<orderEntry type="inheritedJdk" />
|
|
10
|
+
<orderEntry type="sourceFolder" forTests="false" />
|
|
11
|
+
</component>
|
|
12
|
+
</module>
|
package/.idea/vcs.xml
ADDED
package/babel.config.js
ADDED
package/build/redux-sacala.d.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
import { Dispatch, Action, Reducer, Middleware
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
[action: string]: ActionHandler<BlockState, any>;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
[effect: string]: (payload: any, extraArgument: ExtraArgument) => any;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
payload: SecondArgument<Handler>;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
[name in keyof Actions]: ActionCreator<Actions[name]>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
[key in keyof ReturnType<Map>]: (undefined extends FirstArgument<ReturnType<Map>[key]> ? () => Action : (payload: FirstArgument<ReturnType<Map>[key]>) => Action);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export declare function createReduxBlock<GlobalState, ExtraArgument = undefined>(): <Name extends keyof GlobalState, Actions extends ActionMap<GlobalState[Name]>, Effects extends EffectsMap<GlobalState, ExtraArgument>>({ name, initial, actions, effects }: {
|
|
23
|
-
name: Name;
|
|
24
|
-
initial: GlobalState[Name];
|
|
25
|
-
actions: Actions;
|
|
26
|
-
effects?: Effects | undefined;
|
|
27
|
-
}) => {
|
|
28
|
-
name: Name;
|
|
29
|
-
reducer: Reducer<GlobalState[Name]
|
|
30
|
-
createMiddleware: MiddlewareCreator<ExtraArgument>;
|
|
31
|
-
actions: ActionCreatorMap<Actions> & EffectsCreatorMap<GlobalState, ExtraArgument, Effects>;
|
|
32
|
-
};
|
|
33
|
-
export {};
|
|
1
|
+
import { Dispatch, Action, Reducer, Middleware } from "redux";
|
|
2
|
+
type UnknownToUndefined<T> = unknown extends T ? undefined : T;
|
|
3
|
+
type FirstArgument<F> = F extends (arg1: infer U, ...args: any[]) => any ? UnknownToUndefined<U> : undefined;
|
|
4
|
+
type SecondArgument<F> = F extends (arg1: any, arg2: infer U, ...args: any[]) => any ? UnknownToUndefined<U> : undefined;
|
|
5
|
+
type ActionHandler<BlockState, Payload> = (state: BlockState, payload: Payload) => BlockState;
|
|
6
|
+
type ActionMap<BlockState> = {
|
|
7
|
+
[action: string]: ActionHandler<BlockState, any>;
|
|
8
|
+
};
|
|
9
|
+
type EffectsMap<GlobalState, ExtraArgument> = (dispatch: Dispatch, getState: () => GlobalState) => {
|
|
10
|
+
[effect: string]: (payload: any, extraArgument: ExtraArgument) => any;
|
|
11
|
+
};
|
|
12
|
+
type ActionCreator<Handler extends ActionHandler<any, any>> = undefined extends SecondArgument<Handler> ? () => Action<string> : (payload: SecondArgument<Handler>) => (Action<string> & {
|
|
13
|
+
payload: SecondArgument<Handler>;
|
|
14
|
+
});
|
|
15
|
+
type ActionCreatorMap<Actions extends ActionMap<any>> = {
|
|
16
|
+
[name in keyof Actions]: ActionCreator<Actions[name]>;
|
|
17
|
+
};
|
|
18
|
+
type EffectsCreatorMap<GlobalState, ExtraArgument, Map extends EffectsMap<GlobalState, ExtraArgument>> = {
|
|
19
|
+
[key in keyof ReturnType<Map>]: (undefined extends FirstArgument<ReturnType<Map>[key]> ? () => Action : (payload: FirstArgument<ReturnType<Map>[key]>) => Action);
|
|
20
|
+
};
|
|
21
|
+
type MiddlewareCreator<T> = T extends undefined ? () => Middleware : (argument: T) => Middleware;
|
|
22
|
+
export declare function createReduxBlock<GlobalState, ExtraArgument = undefined>(): <Name extends keyof GlobalState & string, Actions extends ActionMap<GlobalState[Name]>, Effects extends EffectsMap<GlobalState, ExtraArgument>>({ name, initial, actions, effects }: {
|
|
23
|
+
name: Name;
|
|
24
|
+
initial: GlobalState[Name];
|
|
25
|
+
actions: Actions;
|
|
26
|
+
effects?: Effects | undefined;
|
|
27
|
+
}) => {
|
|
28
|
+
name: Name;
|
|
29
|
+
reducer: Reducer<GlobalState[Name]>;
|
|
30
|
+
createMiddleware: MiddlewareCreator<ExtraArgument>;
|
|
31
|
+
actions: ActionCreatorMap<Actions> & EffectsCreatorMap<GlobalState, ExtraArgument, Effects>;
|
|
32
|
+
};
|
|
33
|
+
export {};
|
package/build/redux-sacala.js
CHANGED
|
@@ -1,78 +1,79 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createReduxBlock = void 0;
|
|
4
|
+
function bindAll(map) {
|
|
5
|
+
const result = {};
|
|
6
|
+
for (const i in map) {
|
|
7
|
+
result[i] = map[i].bind(map);
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
function appendPrefix(prefix, map) {
|
|
12
|
+
const r = {};
|
|
13
|
+
for (const i in map) {
|
|
14
|
+
r[prefix + i] = map[i];
|
|
15
|
+
}
|
|
16
|
+
return r;
|
|
17
|
+
}
|
|
18
|
+
// Transformation
|
|
19
|
+
function createActionCreator(type) {
|
|
20
|
+
return (payload) => payload === undefined ? { type } : { type, payload };
|
|
21
|
+
}
|
|
22
|
+
function createEffectCreator(type) {
|
|
23
|
+
return (payload) => ({ type, payload });
|
|
24
|
+
}
|
|
25
|
+
function createReducer(prefix, initial, actionMap) {
|
|
26
|
+
const actions = appendPrefix(prefix + "/", bindAll(actionMap));
|
|
27
|
+
return (state = initial, action) => {
|
|
28
|
+
if (action && action.type) {
|
|
29
|
+
const handler = actions[action.type];
|
|
30
|
+
if (handler) {
|
|
31
|
+
return handler(state, action.payload);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
return state;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return state;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
function createMiddlewareCreator(prefix, effectsMap) {
|
|
43
|
+
return ((argument) => (store) => {
|
|
44
|
+
const effects = appendPrefix(prefix + "/", bindAll(effectsMap(store.dispatch, store.getState)));
|
|
45
|
+
return (next) => (action) => {
|
|
46
|
+
if (action && effects.hasOwnProperty(action.type)) {
|
|
47
|
+
effects[action.type](action.payload, argument);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
next(action);
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function fail() {
|
|
56
|
+
throw new Error("Can't have access to 'dispatch' and 'getState' during initialization");
|
|
57
|
+
}
|
|
58
|
+
function createReduxBlock() {
|
|
59
|
+
return function applyConfig({ name, initial, actions, effects }) {
|
|
60
|
+
const actionCreators = Object.keys(actions).reduce((r, key) => {
|
|
61
|
+
r[key] = createActionCreator(`${name}/${key}`);
|
|
62
|
+
return r;
|
|
63
|
+
}, {});
|
|
64
|
+
const effectCreators = Object.keys(effects ? effects(fail, fail) : {}).reduce((r, key) => {
|
|
65
|
+
r[key] = createEffectCreator(`${name}/${key}`);
|
|
66
|
+
return r;
|
|
67
|
+
}, {});
|
|
68
|
+
return {
|
|
69
|
+
name,
|
|
70
|
+
reducer: createReducer(name, initial, actions),
|
|
71
|
+
createMiddleware: effects
|
|
72
|
+
? createMiddlewareCreator(name, effects)
|
|
73
|
+
: (() => ((_) => (next) => next)),
|
|
74
|
+
actions: Object.assign(Object.assign({}, actionCreators), effectCreators)
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
exports.createReduxBlock = createReduxBlock;
|
|
78
79
|
//# sourceMappingURL=redux-sacala.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redux-sacala.js","sourceRoot":"","sources":["../src/redux-sacala.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"redux-sacala.js","sourceRoot":"","sources":["../src/redux-sacala.ts"],"names":[],"mappings":";;;AAOA,SAAS,OAAO,CAAwC,GAAM;IAC1D,MAAM,MAAM,GAAG,EAAc,CAAC;IAC9B,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE;QACjB,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KAChC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AAED,SAAS,YAAY,CAAI,MAAc,EAAE,GAAyB;IAC9D,MAAM,CAAC,GAA2B,EAAE,CAAC;IACrC,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE;QACjB,CAAC,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;KAC1B;IACD,OAAO,CAAC,CAAC;AACb,CAAC;AAuBD,iBAAiB;AACjB,SA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C,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAgB,EAAE,EAAE,CAAC,CAAC,IAAc,EAAE,EAAE,CAAC,IAAI,CAAC,CAAqC;YAClG,OAAO,kCACA,cAAc,GACd,cAAc,CACpB;SACJ,CAAC;IACN,CAAC,CAAA;AACL,CAAC;AArCD,4CAqCC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const redux_1 = require("redux");
|
|
4
|
+
const redux_sacala_1 = require("./redux-sacala");
|
|
5
|
+
const { actions: local, reducer: localReducer, createMiddleware: createLocalMiddleware, } = (0, redux_sacala_1.createReduxBlock)()({
|
|
6
|
+
name: "local",
|
|
7
|
+
initial: { count: 0 },
|
|
8
|
+
actions: {
|
|
9
|
+
inc(state) {
|
|
10
|
+
return { count: state.count + 1 };
|
|
11
|
+
},
|
|
12
|
+
set(_, count) {
|
|
13
|
+
return { count };
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
effects: (dispatch, getState) => {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
function createMyStore() {
|
|
21
|
+
return (0, redux_1.createStore)((0, redux_1.combineReducers)({
|
|
22
|
+
local: localReducer,
|
|
23
|
+
}), (0, redux_1.applyMiddleware)(createLocalMiddleware(100)));
|
|
24
|
+
}
|
|
25
|
+
let store = createMyStore();
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
store = createMyStore();
|
|
28
|
+
});
|
|
29
|
+
describe("Store with reducer and middleware", () => {
|
|
30
|
+
it("Should be updated on action without payload", () => {
|
|
31
|
+
store.dispatch(local.inc());
|
|
32
|
+
store.dispatch(local.inc());
|
|
33
|
+
expect(store.getState()).toEqual({ local: { count: 2 } });
|
|
34
|
+
});
|
|
35
|
+
it("Should be updated on action with payload", () => {
|
|
36
|
+
store.dispatch(local.set(12));
|
|
37
|
+
expect(store.getState()).toEqual({ local: { count: 12 } });
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
//# sourceMappingURL=redux-sacala.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redux-sacala.spec.js","sourceRoot":"","sources":["../src/redux-sacala.spec.ts"],"names":[],"mappings":";;AAAA,iCAKe;AACf,iDAAkD;AAUlD,MAAM,EACF,OAAO,EAAE,KAAK,EACd,OAAO,EAAE,YAAY,EACrB,gBAAgB,EAAE,qBAAqB,GAC1C,GAAG,IAAA,+BAAgB,GAAmB,CAAC;IACpC,IAAI,EAAE,OAAO;IACb,OAAO,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE;IACrB,OAAO,EAAE;QACL,GAAG,CAAC,KAAK;YACL,OAAO,EAAE,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,CAAC,EAAE,CAAC;QACtC,CAAC;QACD,GAAG,CAAC,CAAC,EAAE,KAAa;YAChB,OAAO,EAAE,KAAK,EAAE,CAAC;QACrB,CAAC;KACJ;IACD,OAAO,EAAE,CAAC,QAAQ,EAAE,QAAQ,EAAE,EAAE;QAC5B,OAAO,EAEN,CAAC;IACN,CAAC;CACJ,CAAC,CAAC;AAEH,SAAS,aAAa;IAClB,OAAO,IAAA,mBAAW,EAAC,IAAA,uBAAe,EAAC;QAC/B,KAAK,EAAE,YAAY;KACtB,CAAC,EAAE,IAAA,uBAAe,EAAC,qBAAqB,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;AACrD,CAAC;AAED,IAAI,KAAK,GAAG,aAAa,EAAE,CAAC;AAE5B,UAAU,CAAC,GAAG,EAAE;IACZ,KAAK,GAAG,aAAa,EAAE,CAAC;AAC5B,CAAC,CAAC,CAAC;AAEH,QAAQ,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC/C,EAAE,CAAC,6CAA6C,EAAE,GAAG,EAAE;QACnD,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;QAC5B,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC;QAC5B,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC;IAC9D,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,0CAA0C,EAAE,GAAG,EAAE;QAChD,KAAK,CAAC,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC;QAC9B,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,EAAE,EAAE,EAAE,CAAC,CAAC;IAC/D,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "redux-sacala",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"author": {
|
|
5
5
|
"name": "Andrey Monkin",
|
|
6
6
|
"email": "monkin.andrey@gmail.com"
|
|
7
7
|
},
|
|
8
8
|
"main": "build/redux-sacala.js",
|
|
9
9
|
"types": "build/redux-sacala.d.ts",
|
|
10
|
-
"
|
|
11
|
-
"redux": "^4.
|
|
10
|
+
"peerDependencies": {
|
|
11
|
+
"redux": "^4.2.1"
|
|
12
12
|
},
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"typescript": "
|
|
14
|
+
"typescript": "5.1.3",
|
|
15
|
+
"jest": "29.5.0",
|
|
16
|
+
"@types/jest": "29.5.2",
|
|
17
|
+
"babel-jest": "29.5.0",
|
|
18
|
+
"@babel/core": "7.22.5",
|
|
19
|
+
"@babel/preset-env": "7.22.5",
|
|
20
|
+
"@babel/preset-typescript": "7.22.5"
|
|
15
21
|
},
|
|
16
22
|
"scripts": {
|
|
17
|
-
"prepublish": "tsc"
|
|
23
|
+
"prepublish": "jest src && tsc",
|
|
24
|
+
"test": "jest src"
|
|
18
25
|
}
|
|
19
26
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Store,
|
|
3
|
+
createStore,
|
|
4
|
+
combineReducers,
|
|
5
|
+
applyMiddleware,
|
|
6
|
+
} from "redux";
|
|
7
|
+
import { createReduxBlock } from "./redux-sacala";
|
|
8
|
+
|
|
9
|
+
interface LocalState {
|
|
10
|
+
count: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
interface MyState {
|
|
14
|
+
local: LocalState;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const {
|
|
18
|
+
actions: local,
|
|
19
|
+
reducer: localReducer,
|
|
20
|
+
createMiddleware: createLocalMiddleware,
|
|
21
|
+
} = createReduxBlock<MyState, number>()({
|
|
22
|
+
name: "local",
|
|
23
|
+
initial: { count: 0 },
|
|
24
|
+
actions: {
|
|
25
|
+
inc(state) {
|
|
26
|
+
return { count: state.count + 1 };
|
|
27
|
+
},
|
|
28
|
+
set(_, count: number) {
|
|
29
|
+
return { count };
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
effects: (dispatch, getState) => {
|
|
33
|
+
return {
|
|
34
|
+
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
function createMyStore() {
|
|
40
|
+
return createStore(combineReducers({
|
|
41
|
+
local: localReducer,
|
|
42
|
+
}), applyMiddleware(createLocalMiddleware(100)));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let store = createMyStore();
|
|
46
|
+
|
|
47
|
+
beforeEach(() => {
|
|
48
|
+
store = createMyStore();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("Store with reducer and middleware", () => {
|
|
52
|
+
it("Should be updated on action without payload", () => {
|
|
53
|
+
store.dispatch(local.inc());
|
|
54
|
+
store.dispatch(local.inc());
|
|
55
|
+
expect(store.getState()).toEqual({ local: { count: 2 } });
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("Should be updated on action with payload", () => {
|
|
59
|
+
store.dispatch(local.set(12));
|
|
60
|
+
expect(store.getState()).toEqual({ local: { count: 12 } });
|
|
61
|
+
});
|
|
62
|
+
});
|
package/src/redux-sacala.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Dispatch, Action, Reducer, Middleware, AnyAction, Store, MiddlewareAPI } from "redux";
|
|
2
2
|
|
|
3
|
-
type
|
|
3
|
+
type UnknownToUndefined<T> = unknown extends T ? undefined : T;
|
|
4
4
|
|
|
5
|
-
type FirstArgument<F> =
|
|
6
|
-
type SecondArgument<F> =
|
|
5
|
+
type FirstArgument<F> = F extends (arg1: infer U, ...args: any[]) => any ? UnknownToUndefined<U> : undefined;
|
|
6
|
+
type SecondArgument<F> = F extends (arg1: any, arg2: infer U, ...args: any[]) => any ? UnknownToUndefined<U> : undefined;
|
|
7
7
|
|
|
8
8
|
function bindAll<T extends { [key: string]: Function }>(map: T): T {
|
|
9
9
|
const result = {} as any as T;
|
|
@@ -29,10 +29,12 @@ type EffectsMap<GlobalState, ExtraArgument> = (dispatch: Dispatch, getState: ()
|
|
|
29
29
|
// Output
|
|
30
30
|
type ActionCreator<Handler extends ActionHandler<any, any>> = undefined extends SecondArgument<Handler>
|
|
31
31
|
? () => Action<string>
|
|
32
|
-
: (payload: SecondArgument<Handler>) => Action<string> & { payload: SecondArgument<Handler> };
|
|
32
|
+
: (payload: SecondArgument<Handler>) => (Action<string> & { payload: SecondArgument<Handler> });
|
|
33
|
+
|
|
33
34
|
type ActionCreatorMap<Actions extends ActionMap<any>> = {
|
|
34
35
|
[name in keyof Actions]: ActionCreator<Actions[name]>;
|
|
35
36
|
};
|
|
37
|
+
|
|
36
38
|
type EffectsCreatorMap<GlobalState, ExtraArgument, Map extends EffectsMap<GlobalState, ExtraArgument>> = {
|
|
37
39
|
[key in keyof ReturnType<Map>]: (undefined extends FirstArgument<ReturnType<Map>[key]>
|
|
38
40
|
? () => Action
|
|
@@ -80,12 +82,12 @@ function createMiddlewareCreator<GlobalState, ExtraArgument>(prefix: string, eff
|
|
|
80
82
|
}
|
|
81
83
|
|
|
82
84
|
function fail(): never {
|
|
83
|
-
throw new Error("Can't have
|
|
85
|
+
throw new Error("Can't have access to 'dispatch' and 'getState' during initialization");
|
|
84
86
|
}
|
|
85
87
|
|
|
86
88
|
export function createReduxBlock<GlobalState, ExtraArgument = undefined>() {
|
|
87
89
|
return function applyConfig<
|
|
88
|
-
Name extends keyof GlobalState,
|
|
90
|
+
Name extends (keyof GlobalState) & string,
|
|
89
91
|
Actions extends ActionMap<GlobalState[Name]>,
|
|
90
92
|
Effects extends EffectsMap<GlobalState, ExtraArgument>
|
|
91
93
|
>({ name, initial, actions, effects }: {
|