Skip to content

Commit 79f8a1f

Browse files
authored
simplify module store proxy
1 parent e4d18a9 commit 79f8a1f

File tree

3 files changed

+7
-128
lines changed

3 files changed

+7
-128
lines changed

package.json

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

platform/src/kernel/registry/__tests__/store.test.js

+1-71
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import { SET_SHARED } from "../../../constants";
44
import { setStore, getStore } from "../store";
55

66
describe("store registry", () => {
7-
const ORIGINAL_NODE_ENV = process.env.NODE_ENV;
8-
97
const debugSpy = jest.spyOn(console, "debug").mockImplementation(() => {});
108
const errorSpy = jest.spyOn(console, "error").mockImplementation(() => {});
119

1210
beforeEach(() => {
1311
setStore(null);
14-
process.env.NODE_ENV = ORIGINAL_NODE_ENV;
1512
debugSpy.mockClear();
1613
errorSpy.mockClear();
1714
});
1815

1916
afterAll(() => {
20-
process.env.NODE_ENV = ORIGINAL_NODE_ENV;
2117
debugSpy.mockRestore();
2218
errorSpy.mockRestore();
2319
});
@@ -68,71 +64,7 @@ describe("store registry", () => {
6864
});
6965

7066
describe("namespace", () => {
71-
it(".getState in NODE_ENV=development", () => {
72-
setStore(
73-
configureStore([])({
74-
modules: {
75-
"my-feature": {
76-
foo: "bat",
77-
},
78-
"my-other-feature": {
79-
foo: "baz",
80-
},
81-
},
82-
env: {},
83-
shared: {
84-
beach: "bar",
85-
},
86-
}),
87-
);
88-
89-
process.env.NODE_ENV = "development";
90-
91-
const store = getStore().namespace("my-feature");
92-
93-
expect(typeof store.getState).toEqual("function");
94-
95-
const state = store.getState();
96-
97-
expect(state).toEqual(store.getState());
98-
99-
expect(state).toEqual({
100-
foo: "bat",
101-
env: {},
102-
shared: {
103-
beach: "bar",
104-
},
105-
});
106-
expect(state.valueOf()).toEqual({
107-
foo: "bat",
108-
env: {},
109-
shared: {
110-
beach: "bar",
111-
},
112-
});
113-
expect(state.toString()).toEqual("[object Object]");
114-
expect(state.shared).toEqual({ beach: "bar" });
115-
errorSpy.mockClear();
116-
117-
expect(state.other).toBeUndefined();
118-
expect(errorSpy).toHaveBeenCalledWith('module "my-feature" tried to access reducer "other" that it does not own.');
119-
120-
expect("shared" in state).toEqual(true);
121-
expect("foo" in state).toEqual(true);
122-
expect("other" in state).toEqual(false);
123-
124-
expect(Reflect.ownKeys(state)).toEqual(["foo", "env", "shared"]);
125-
126-
state.foo = "mutation";
127-
128-
expect(() => {
129-
state.shared.beach = "injection";
130-
}).toThrow(TypeError);
131-
132-
expect(state.shared.beach).toEqual("bar");
133-
});
134-
135-
it(".getState in NODE_ENV=production", () => {
67+
it(".getState", () => {
13668
setStore(
13769
configureStore([])({
13870
modules: {
@@ -150,8 +82,6 @@ describe("store registry", () => {
15082
}),
15183
);
15284

153-
process.env.NODE_ENV = "production";
154-
15585
let store = getStore().namespace("my-feature");
15686

15787
expect(typeof store.getState).toEqual("function");

platform/src/kernel/registry/store.js

+5-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { SET_SHARED } from "../../constants";
2-
import { warning } from "../../utils";
32

43
const RUNE = "$";
54
const BROADCAST_ACTION_PREFIX = "@@";
@@ -72,61 +71,11 @@ const handler = {
7271
const state = store.getState();
7372
if (prevState !== state) {
7473
prevState = state;
75-
if (process.env.NODE_ENV === "development") {
76-
stateProxy = new Proxy(
77-
{
78-
...state.modules[name],
79-
env: state.env,
80-
shared: Object.freeze(state.shared),
81-
},
82-
{
83-
get(ref, reducer) {
84-
switch (reducer) {
85-
case Symbol.iterator: {
86-
return Reflect.get(ref, reducer);
87-
}
88-
case "toString":
89-
case Symbol.toStringTag: {
90-
return () => "[object Object]";
91-
}
92-
case "valueOf": {
93-
return () => ref;
94-
}
95-
default: {
96-
if (reducer in ref) {
97-
return ref[reducer];
98-
}
99-
warning(`module "${name}" tried to access reducer "${reducer}" that it does not own.`);
100-
return undefined;
101-
}
102-
}
103-
},
104-
has(ref, reducer) {
105-
return reducer in ref;
106-
},
107-
ownKeys(ref) {
108-
return Reflect.ownKeys(ref);
109-
},
110-
getOwnPropertyDescriptor(ref, key) {
111-
return {
112-
value: this.get(ref, key),
113-
enumerable: true,
114-
configurable: true,
115-
};
116-
},
117-
set(ref, reducer, value) {
118-
ref[reducer] = value;
119-
return true;
120-
},
121-
},
122-
);
123-
} else {
124-
stateProxy = {
125-
...state.modules[name],
126-
env: state.env,
127-
shared: Object.freeze(state.shared),
128-
};
129-
}
74+
stateProxy = {
75+
...state.modules[name],
76+
env: state.env,
77+
shared: Object.freeze(state.shared),
78+
};
13079
}
13180
return stateProxy;
13281
},

0 commit comments

Comments
 (0)