From e9213bbd54b31fcf130f94e8e207d529a5c468f8 Mon Sep 17 00:00:00 2001 From: AlexandrHoroshih Date: Mon, 17 Apr 2023 00:48:21 +0700 Subject: [PATCH] Update minimal effector version and fix bugs --- package.json | 4 ++-- pnpm-lock.yaml | 14 ++++++------ src/get-scope.ts | 57 +++++++++++++++++++++++++----------------------- 3 files changed, 39 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index cc95d66..5c3c2f7 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "@babel/core": "^7.21.4", "@types/react": "^18.0.31", "@vitejs/plugin-react": "^3.1.0", - "effector": "^22.7.0", + "effector": "^22.8.1", "effector-react": "^22.5.1", "react": "^18.2.0", "typescript": "^5.0.3", @@ -37,7 +37,7 @@ "vitest": "^0.29.8" }, "peerDependencies": { - "effector": "^22.7.0", + "effector": "^22.8.1", "effector-react": "^22.5.1", "react": "^18.2.0" }, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index c91976f..fa92279 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,7 +4,7 @@ specifiers: '@babel/core': ^7.21.4 '@types/react': ^18.0.31 '@vitejs/plugin-react': ^3.1.0 - effector: ^22.7.0 + effector: ^22.8.1 effector-react: ^22.5.1 react: ^18.2.0 typescript: ^5.0.3 @@ -15,8 +15,8 @@ devDependencies: '@babel/core': 7.21.4 '@types/react': 18.0.32 '@vitejs/plugin-react': 3.1.0_vite@4.2.1 - effector: 22.7.0 - effector-react: 22.5.1_xzy47btpk6b7akis5sqfq2jw2y + effector: 22.8.1 + effector-react: 22.5.1_7pz56azflmmsy3j6ndodafgmke react: 18.2.0 typescript: 5.0.3 vite: 4.2.1 @@ -703,20 +703,20 @@ packages: resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==} dev: true - /effector-react/22.5.1_xzy47btpk6b7akis5sqfq2jw2y: + /effector-react/22.5.1_7pz56azflmmsy3j6ndodafgmke: resolution: {integrity: sha512-Sy5b/sUEZoCuyoCr+SiHcgd0L+PQqnIGx+SuPCZ7MhECYxezkkeecsk0EVj9sykLL2qlEeg+rdiWvIATJgOTeA==} engines: {node: '>=11.0.0'} peerDependencies: effector: ^22.0.2 react: '>=16.8.0 <19.0.0' dependencies: - effector: 22.7.0 + effector: 22.8.1 react: 18.2.0 use-sync-external-store: 1.2.0_react@18.2.0 dev: true - /effector/22.7.0: - resolution: {integrity: sha512-C3jMCjRmU/j7Mu04q0zm45OYglQB9fYxSHNu51UrV77VDZJRs7nFmNfNiaXmgkKOnsUP4VuyE64aiSvAZWZo7g==} + /effector/22.8.1: + resolution: {integrity: sha512-368ysZBxxqr0ThVYElhmo8p8rT2wTZ0hF/BIg1FxNPSIdr/wvw4Hbnioy22CbgtU5d53CnadkJnSlKqfOICOwg==} engines: {node: '>=11.0.0'} dev: true diff --git a/src/get-scope.ts b/src/get-scope.ts index 1df0caa..6cc1c54 100644 --- a/src/get-scope.ts +++ b/src/get-scope.ts @@ -25,7 +25,7 @@ export function getClientScope(values?: Values) { if (!values) return _currentScope; HACK_injectValues(_currentScope, values); - HACK_resetScopeRefs(_currentScope); + HACK_updateScopeRefs(_currentScope, values); return _currentScope; } @@ -34,33 +34,36 @@ function HACK_injectValues(scope: Scope, values: Values) { const oldValues = serialize(scope); // @ts-expect-error this is a really hacky way to "hydrate" scope - if (scope.values) { - /** - * effector@22.8.0 and higher - */ + scope.values.sidMap = { + ...oldValues, + ...values, + }; +} + +function HACK_updateScopeRefs(scope: Scope, values: Values) { + const idSidMap = Object.fromEntries( + // @ts-expect-error + Object.entries(scope.sidIdMap).map(([sid, id]) => [id, sid]) + ); + + // @ts-expect-error + for (const id in scope.reg) { // @ts-expect-error - scope.values.sidMap = { - ...oldValues, - ...values, + const ref = scope.reg[id]; + if (!ref.meta || ref.meta?.derived) { + /** + * Force recalculation of derived values + */ + // @ts-expect-error + delete scope.reg[id]; + } else { + /** + * Update non-derived values + */ + const sid = idSidMap[id]; + if (sid && sid in values) { + ref.current = values[sid]; + } } - } else { - /** - * effector before 22.8.0 - */ - // @ts-expect-error this is a really hacky way to "hydrate" scope - scope.sidValuesMap = { - ...oldValues, - ...values, - }; } } - -function HACK_resetScopeRefs(scope: Scope) { - /** - * Kind of equal to proposed fork(scope) behaviour - */ - // @ts-expect-error hacky way to reset state refs owned by this scope - scope.reg = {}; - // @ts-expect-error hacky way to reset state refs owned by this scope - scope.sidIdMap = {}; -}