Skip to content

Commit

Permalink
Merge pull request #7 from effector/fix-missing-state-refs-issues
Browse files Browse the repository at this point in the history
Update minimal effector version and fix bugs
  • Loading branch information
AlexandrHoroshih authored Apr 16, 2023
2 parents 1a693de + e9213bb commit 608334d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
"@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",
"vite": "^4.2.1",
"vitest": "^0.29.8"
},
"peerDependencies": {
"effector": "^22.7.0",
"effector": "^22.8.1",
"effector-react": "^22.5.1",
"react": "^18.2.0"
},
Expand Down
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 30 additions & 27 deletions src/get-scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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) {
/**
* [email protected] 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 = {};
}

0 comments on commit 608334d

Please sign in to comment.