-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix useHookstate store switchover #409
Open
speigg
wants to merge
2
commits into
avkonst:master
Choose a base branch
from
speigg:fix-InitStateStoreSwitchover
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"version": "1.0.0", | ||
"configurations": [ | ||
{ | ||
"name": "Test Core", | ||
"request": "launch", | ||
"runtimeArgs": [ | ||
"nx", | ||
"test", | ||
"core" | ||
], | ||
"runtimeExecutable": "pnpm", | ||
"skipFiles": [ | ||
"<node_internals>/**" | ||
], | ||
"type": "node" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -609,9 +609,10 @@ export function useHookstate<S, E extends {} = {}>( | |
// warning: this is called twice in react strict mode | ||
let store = parentMethods.store | ||
let onSetUsedCallback = () => setValue({ | ||
store: store, // immutable | ||
state: state, // immutable | ||
source: value.source // mutable, get the latest from value | ||
store, // immutable | ||
state, // immutable | ||
source: value.source, // mutable, get the latest from value, | ||
parentMethods | ||
}) | ||
let state = new StateMethodsImpl<S, E>( | ||
store, | ||
|
@@ -621,22 +622,26 @@ export function useHookstate<S, E extends {} = {}>( | |
onSetUsedCallback | ||
); | ||
return { | ||
store: store, | ||
state: state, | ||
source: source | ||
store, | ||
state, | ||
source, | ||
parentMethods | ||
} | ||
}; | ||
const [value, setValue] = React.useState(initializer); | ||
let [value, setValue] = React.useState(initializer); | ||
|
||
if (value.store !== parentMethods.store || !('source' in value)) { | ||
throw new StateInvalidUsageError(parentMethods.path, ErrorId.InitStateStoreSwitchover) | ||
value.state.onUnmount() | ||
value.parentMethods.unsubscribe(value.state); | ||
value = initializer() | ||
} | ||
|
||
// TODO move to a class hide props on prototype level | ||
// hide props from development tools | ||
Object.defineProperty(value, 'store', { enumerable: false }); | ||
Object.defineProperty(value, 'state', { enumerable: false }); | ||
Object.defineProperty(value, 'source', { enumerable: false }); | ||
Object.defineProperty(value, 'parentMethods', { enumerable: false }); | ||
|
||
value.state.reconstruct( | ||
parentMethods.path, | ||
|
@@ -674,7 +679,7 @@ export function useHookstate<S, E extends {} = {}>( | |
let initializer = () => { | ||
// warning: this is called twice in react strict mode | ||
let store = parentMethods.store | ||
let onSetUsedCallback = () => setValue({ | ||
let onSetUsedCallback = () => value.state.isMounted && setValue({ | ||
store: store, // immutable | ||
state: state, // immutable | ||
source: value.source // mutable, get the latest from value | ||
|
@@ -692,10 +697,12 @@ export function useHookstate<S, E extends {} = {}>( | |
source: source | ||
} | ||
} | ||
const [value, setValue] = React.useState(initializer); | ||
let [value, setValue] = React.useState(initializer); | ||
|
||
if (value.store !== parentMethods.store || !('source' in value)) { | ||
throw new StateInvalidUsageError(parentMethods.path, ErrorId.InitStateStoreSwitchover) | ||
value.state.onUnmount() | ||
value.store.unsubscribe(value.state); | ||
value = initializer() | ||
} | ||
|
||
// hide props from development tools | ||
|
@@ -743,7 +750,7 @@ export function useHookstate<S, E extends {} = {}>( | |
let initializer = () => { | ||
// warning: this is called twice in react strict mode | ||
let store = createStore(source) | ||
let onSetUsedCallback = () => setValue({ | ||
let onSetUsedCallback = () => value.state.isMounted && setValue({ | ||
store: store, | ||
state: state, | ||
}) | ||
|
@@ -759,10 +766,13 @@ export function useHookstate<S, E extends {} = {}>( | |
state: state | ||
} | ||
} | ||
const [value, setValue] = React.useState(initializer); | ||
let [value, setValue] = React.useState(initializer); | ||
|
||
if ('source' in value) { | ||
throw new StateInvalidUsageError(RootPath, ErrorId.InitStateStoreSwitchover) | ||
value.state.onUnmount() | ||
value.store.unsubscribe(value.state); | ||
value.store.deactivate() | ||
value = initializer() | ||
} | ||
|
||
// hide props from development tools | ||
|
@@ -1056,7 +1066,8 @@ class Store implements Subscribable { | |
set(path: Path, value: StateValueAtPath): SetActionDescriptor { | ||
if (this.edition < 0) { | ||
// TODO convert to console log | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe remove TODO comment? and update web docs for this error code? |
||
throw new StateInvalidUsageError(path, ErrorId.SetStateWhenDestroyed) | ||
// throw new StateInvalidUsageError(path, ErrorId.SetStateWhenDestroyed) | ||
console.warn(`Warning: HOOKSTATE-106: Attempt to set state when it is destroyed. [path: /${path.join('/')}]`) | ||
} | ||
|
||
if (path.length === 0) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you please extend the tests to make sure we can do state set action after the store is reinitialized? same for other tests you put.