Skip to content

Commit 51f0e49

Browse files
Merge pull request #36 from effector/custom-serialize-issue
Fix custom serialize support for newer effector versions
2 parents af85259 + 52d5e86 commit 51f0e49

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/get-scope.browser.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe("getClientScope", () => {
9696
});
9797
/**
9898
* Current fix for this test is only implemented inside `[email protected]`
99-
*
99+
*
100100
* TODO: After fix is ported into original createWatch of `effector` package in the 23.0.0 release, remove skip
101101
*/
102102
test.skip("watchers should re-run, if value is changed after server values injection", async () => {
@@ -159,6 +159,35 @@ describe("getClientScope", () => {
159159

160160
expect(clientScopeTwo.getState($count)).toEqual(4);
161161
});
162+
163+
test("should support custom serializers", async () => {
164+
const $homeDate = createStore<Date | null>(null, {
165+
serialize: {
166+
read: (dateStringOrNull) =>
167+
typeof dateStringOrNull === "string"
168+
? new Date(dateStringOrNull)
169+
: null,
170+
write: (dateOrNull) => (dateOrNull ? dateOrNull.toISOString() : null),
171+
},
172+
sid: "test_sid",
173+
});
174+
175+
const serverScope = fork();
176+
177+
await allSettled($homeDate, {
178+
scope: serverScope,
179+
params: new Date(2024, 10, 3),
180+
});
181+
182+
const values = serialize(serverScope);
183+
184+
const scope = getScope(values);
185+
186+
const clientValue = scope.getState($homeDate);
187+
188+
expect(clientValue instanceof Date).toBe(true);
189+
expect(clientValue!.getTime()).toEqual(new Date(2024, 10, 3).getTime());
190+
});
162191
});
163192

164193
describe("getScope implementation details", () => {

src/get-scope.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ function INTERNAL_getClientScope(values?: Values) {
7575
function HACK_injectValues(scope: Scope, values: Values) {
7676
// @ts-expect-error this is a really hacky way to "hydrate" scope
7777
Object.assign(scope.values.sidMap, values);
78+
/**
79+
* We should explicitly set this flag to true, because otherwise the scope will be treated as it was not created from serialized values
80+
* => effector will not apply custom serializers to the scope
81+
*/
82+
(scope as any).fromSerialize = true;
7883
}
7984

8085
function HACK_updateScopeRefs(tscope: Scope, values: Values) {

0 commit comments

Comments
 (0)