From 10e5920a79fa8876924a06b9e62375947854b94d Mon Sep 17 00:00:00 2001 From: Alexander Khoroshikh <32790736+AlexandrHoroshih@users.noreply.github.com> Date: Sun, 7 Sep 2025 11:41:17 +0700 Subject: [PATCH 1/4] Implement test for custom serializers in getScope --- src/get-scope.browser.test.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/get-scope.browser.test.ts b/src/get-scope.browser.test.ts index 81c5097..7acaf3b 100644 --- a/src/get-scope.browser.test.ts +++ b/src/get-scope.browser.test.ts @@ -159,6 +159,30 @@ describe("getClientScope", () => { expect(clientScopeTwo.getState($count)).toEqual(4); }); + + test("should support custom serializers", async () => { + const $homeDate = createStore(null, { + serialize: { + read: (dateStringOrNull) => + typeof dateStringOrNull === "string" ? new Date(dateStringOrNull) : null, + write: (dateOrNull) => (dateOrNull ? dateOrNull.toISOString() : null), + }, + }); + + const serverScope = fork(); + + await allSettled($homeDate, {scope: serverScope, params: new Date(2024, 10, 3) }); + + const values = serialize(serverScope); + + const scope = getScope(values); + + const clientValue = scope.getState($homeDate); + + console.log(clientValue); + + expect(clientValue instanceof Date).toBe(true); + }); }); describe("getScope implementation details", () => { From 4bee076653ce3fe3b12930f63ae40b7bad490919 Mon Sep 17 00:00:00 2001 From: Alexander Khoroshikh <32790736+AlexandrHoroshih@users.noreply.github.com> Date: Sun, 7 Sep 2025 11:44:34 +0700 Subject: [PATCH 2/4] Add 'sid' property to server scope test --- src/get-scope.browser.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/get-scope.browser.test.ts b/src/get-scope.browser.test.ts index 7acaf3b..45bde73 100644 --- a/src/get-scope.browser.test.ts +++ b/src/get-scope.browser.test.ts @@ -167,6 +167,7 @@ describe("getClientScope", () => { typeof dateStringOrNull === "string" ? new Date(dateStringOrNull) : null, write: (dateOrNull) => (dateOrNull ? dateOrNull.toISOString() : null), }, + sid: "test_sid", }); const serverScope = fork(); From a25c735902f311d6f740473f47787e9f2be2673d Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Sun, 7 Sep 2025 14:33:31 +0700 Subject: [PATCH 3/4] update test --- src/get-scope.browser.test.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/get-scope.browser.test.ts b/src/get-scope.browser.test.ts index 45bde73..54fa014 100644 --- a/src/get-scope.browser.test.ts +++ b/src/get-scope.browser.test.ts @@ -96,7 +96,7 @@ describe("getClientScope", () => { }); /** * Current fix for this test is only implemented inside `effector-react@22.5.4` - * + * * TODO: After fix is ported into original createWatch of `effector` package in the 23.0.0 release, remove skip */ test.skip("watchers should re-run, if value is changed after server values injection", async () => { @@ -161,18 +161,23 @@ describe("getClientScope", () => { }); test("should support custom serializers", async () => { - const $homeDate = createStore(null, { - serialize: { - read: (dateStringOrNull) => - typeof dateStringOrNull === "string" ? new Date(dateStringOrNull) : null, - write: (dateOrNull) => (dateOrNull ? dateOrNull.toISOString() : null), - }, - sid: "test_sid", + const $homeDate = createStore(null, { + serialize: { + read: (dateStringOrNull) => + typeof dateStringOrNull === "string" + ? new Date(dateStringOrNull) + : null, + write: (dateOrNull) => (dateOrNull ? dateOrNull.toISOString() : null), + }, + sid: "test_sid", }); const serverScope = fork(); - await allSettled($homeDate, {scope: serverScope, params: new Date(2024, 10, 3) }); + await allSettled($homeDate, { + scope: serverScope, + params: new Date(2024, 10, 3), + }); const values = serialize(serverScope); @@ -180,9 +185,8 @@ describe("getClientScope", () => { const clientValue = scope.getState($homeDate); - console.log(clientValue); - expect(clientValue instanceof Date).toBe(true); + expect(clientValue!.getTime()).toEqual(new Date(2024, 10, 3).getTime()); }); }); From 52d5e86361f53f53d1d86945fa6f22bbc654b22b Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Sun, 7 Sep 2025 14:50:26 +0700 Subject: [PATCH 4/4] Fix support for custom serializers --- src/get-scope.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/get-scope.ts b/src/get-scope.ts index a562222..4480a22 100644 --- a/src/get-scope.ts +++ b/src/get-scope.ts @@ -75,6 +75,11 @@ function INTERNAL_getClientScope(values?: Values) { function HACK_injectValues(scope: Scope, values: Values) { // @ts-expect-error this is a really hacky way to "hydrate" scope Object.assign(scope.values.sidMap, values); + /** + * We should explicitly set this flag to true, because otherwise the scope will be treated as it was not created from serialized values + * => effector will not apply custom serializers to the scope + */ + (scope as any).fromSerialize = true; } function HACK_updateScopeRefs(tscope: Scope, values: Values) {