Skip to content

Commit e409ed0

Browse files
authored
simplify shared state action creator and reducer, separate imnternal platform shared reducer into its own reducer
1 parent abae898 commit e409ed0

File tree

17 files changed

+494
-496
lines changed

17 files changed

+494
-496
lines changed

bootstrap/src/store/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { all, fork } from "redux-saga/effects";
66
import {
77
setStore,
88
sharedReducer,
9+
envReducer,
910
modulesReducer,
1011
createLoaderMiddleware,
1112
createDynamicMiddleware,
@@ -44,6 +45,7 @@ export default (router, fetchContext, bootstrapMiddlewares) => {
4445
const reducer = combineReducers({
4546
runtime: runtimeReducer,
4647
shared: sharedReducer,
48+
env: envReducer,
4749
modules: modulesReducer,
4850
});
4951

jest/__mocks__/platform.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,10 @@ module.exports = {
2525
refresh: () => ({
2626
type: constants.REFRESH,
2727
}),
28-
setGlobalShared: (data) => ({
28+
setShared: (data) => ({
2929
type: constants.SET_SHARED,
3030
payload: {
3131
data,
32-
module: false,
33-
},
34-
}),
35-
setLocalShared: (data) => ({
36-
type: constants.SET_SHARED,
37-
payload: {
38-
data,
39-
module: true,
4032
},
4133
}),
4234
},
@@ -70,5 +62,6 @@ module.exports = {
7062
manualCleanup: () => {},
7163
getStore: () => store,
7264
sharedReducer: (state = {}, action) => state,
65+
envReducer: (state = {}, action) => state,
7366
modulesReducer: (state = {}, action) => state,
7467
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@lastui/rocker",
3-
"version": "0.19.21",
3+
"version": "0.19.22",
44
"license": "Apache-2.0",
55
"author": "[email protected]",
66
"homepage": "https://github.com/lastui/rocker#readme",

platform/src/__tests__/index.test.js

+8-15
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ describe("safe module exports", () => {
1515
it("exposes expected", () => {
1616
expect(Object.keys(all.actions).length).toEqual(4);
1717
expect(all.actions.setLanguage).toBeDefined();
18-
expect(all.actions.setGlobalShared).toBeDefined();
19-
expect(all.actions.setLocalShared).toBeDefined();
18+
expect(all.actions.setShared).toBeDefined();
2019
expect(all.actions.refresh).toBeDefined();
2120
});
2221

@@ -29,27 +28,21 @@ describe("safe module exports", () => {
2928
});
3029
});
3130

32-
it(".setGlobalShared", () => {
33-
expect(all.actions.setGlobalShared({ foo: "bar" })).toEqual({
34-
type: "@@shared/SET_SHARED",
31+
it(".setShared", () => {
32+
expect(all.actions.setShared({ foo: "bar" })).toEqual({
33+
type: "@@shared/SET",
3534
payload: {
3635
data: {
3736
foo: "bar",
3837
},
39-
module: false,
4038
},
4139
});
4240
});
4341

44-
it(".setLocalShared", () => {
45-
expect(all.actions.setLocalShared({ foo: "bar" })).toEqual({
46-
type: "@@shared/SET_SHARED",
47-
payload: {
48-
data: {
49-
foo: "bar",
50-
},
51-
module: true,
52-
},
42+
it(".clearShared", () => {
43+
expect(all.actions.clearShared()).toEqual({
44+
type: "@@shared/CLEAR",
45+
payload: {},
5346
});
5447
});
5548

platform/src/component/Module.jsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import moduleLoader from "../kernel/registry/loader";
66
import Fallback from "./Fallback";
77

88
const Module = forwardRef((props, ref) => {
9-
const isReady = useSelector((state) => Boolean(state.shared.readyModules[props.name]));
9+
const isReady = useSelector((state) => Boolean(state.env.readyModules[props.name]));
1010

11-
const lastUpdate = useSelector((state) => state.shared.lastUpdate);
11+
const lastUpdate = useSelector((state) => state.env.lastUpdate);
1212

1313
const [lastLocalUpdate, setLastLocalUpdate] = useState(0);
1414

platform/src/component/__tests__/Module.test.jsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe("<Module />", () => {
5656

5757
it("renders loaded module component", () => {
5858
const store = configureStore([])({
59-
shared: {
59+
env: {
6060
readyModules: {
6161
"my-feature": true,
6262
},
@@ -72,7 +72,7 @@ describe("<Module />", () => {
7272

7373
it("renders children of loaded module", () => {
7474
const store = configureStore([])({
75-
shared: {
75+
env: {
7676
readyModules: {
7777
"my-feature": true,
7878
},
@@ -96,7 +96,7 @@ describe("<Module />", () => {
9696

9797
it("renders nothing if module not ready", () => {
9898
const store = configureStore([])({
99-
shared: {
99+
env: {
100100
readyModules: {
101101
"my-feature": false,
102102
},
@@ -119,7 +119,7 @@ describe("<Module />", () => {
119119

120120
it("renders nothing if there is no view", async () => {
121121
const store = configureStore([])({
122-
shared: {
122+
env: {
123123
readyModules: {
124124
"my-feature-without-view": true,
125125
},
@@ -142,7 +142,7 @@ describe("<Module />", () => {
142142

143143
it("renders fallback while loading", () => {
144144
const store = configureStore([])({
145-
shared: {
145+
env: {
146146
readyModules: {
147147
"my-loading-feature": false,
148148
},
@@ -166,7 +166,7 @@ describe("<Module />", () => {
166166

167167
it("renders nothing if there is no module name provided", () => {
168168
const store = configureStore([])({
169-
shared: {
169+
env: {
170170
readyModules: {},
171171
},
172172
});
@@ -187,7 +187,7 @@ describe("<Module />", () => {
187187

188188
it("renders nothing if module is not available", () => {
189189
const store = configureStore([])({
190-
shared: {
190+
env: {
191191
readyModules: {},
192192
},
193193
});

platform/src/constants.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export const MODULE_UNLOADED = "@@modules/UNLOADED";
1414

1515
export const I18N_MESSAGES_BATCH = "@@shared/I18N_MESSAGES_BATCH";
1616

17-
export const SET_SHARED = "@@shared/SET_SHARED";
17+
export const SET_SHARED = "@@shared/SET";
18+
export const CLEAR_SHARED = "@@shared/CLEAR";

platform/src/index.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import "regenerator-runtime/runtime";
22

33
import Module from "./component/Module";
4-
import { SET_LANGUAGE, REFRESH, SET_SHARED } from "./constants";
4+
import { SET_LANGUAGE, REFRESH, SET_SHARED, CLEAR_SHARED } from "./constants";
55
import registerModule from "./register";
66

77
function setLanguage(language) {
@@ -19,30 +19,26 @@ function refresh() {
1919
};
2020
}
2121

22-
function setGlobalShared(data) {
22+
function setShared(data) {
2323
return {
2424
type: SET_SHARED,
2525
payload: {
2626
data,
27-
module: false,
2827
},
2928
};
3029
}
3130

32-
function setLocalShared(data) {
31+
function clearShared() {
3332
return {
34-
type: SET_SHARED,
35-
payload: {
36-
data,
37-
module: true,
38-
},
33+
type: CLEAR_SHARED,
34+
payload: {},
3935
};
4036
}
4137

4238
const actions = {
4339
setLanguage,
44-
setGlobalShared,
45-
setLocalShared,
40+
setShared,
41+
clearShared,
4642
refresh,
4743
};
4844

platform/src/kernel/__tests__/index.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as all from "..";
22

33
describe("kernel module exports", () => {
44
it("should expose fixed number of things", () => {
5-
expect(Object.keys(all).length).toEqual(12);
5+
expect(Object.keys(all).length).toEqual(13);
66
});
77

88
describe("setStore", () => {
@@ -31,7 +31,7 @@ describe("kernel module exports", () => {
3131

3232
describe("constants", () => {
3333
it("exposes expected", () => {
34-
expect(Object.keys(all.constants).length).toEqual(13);
34+
expect(Object.keys(all.constants).length).toEqual(14);
3535
});
3636
});
3737

platform/src/kernel/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import registerModule from "../register";
88
import createDynamicMiddleware from "./middleware/dynamic";
99
import createLoaderMiddleware from "./middleware/loader";
1010
import createSagaMiddleware from "./middleware/saga";
11+
import envReducer from "./reducer/env";
1112
import modulesReducer from "./reducer/modules";
1213
import sharedReducer from "./reducer/shared";
1314
import { manualCleanup } from "./registry/loader";
@@ -18,6 +19,7 @@ export {
1819
constants,
1920
createLoaderMiddleware,
2021
sharedReducer,
22+
envReducer,
2123
modulesReducer,
2224
createDynamicMiddleware,
2325
createSagaMiddleware,
@@ -32,6 +34,7 @@ export default {
3234
constants,
3335
createLoaderMiddleware,
3436
sharedReducer,
37+
envReducer,
3538
modulesReducer,
3639
createDynamicMiddleware,
3740
createSagaMiddleware,

0 commit comments

Comments
 (0)