-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved Fast Refresh support (#4436)
* Initial groundwork for Fast Refresh changes * Remove `persistedRef` * Expand on the custom rehydration logic in effect to cover for reconnecting effects * Revert test changes from "Initial groundwork for Fast Refresh changes" This partially reverts commit af40d5b. * Allow rehydrating inline actors * use `??` * Changeset --------- Co-authored-by: David Khourshid <[email protected]>
- Loading branch information
1 parent
51c20bc
commit 340aee6
Showing
14 changed files
with
531 additions
and
254 deletions.
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,5 @@ | ||
--- | ||
'@xstate/react': minor | ||
--- | ||
|
||
Fast refresh now works as expected for most use-cases. |
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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { AnyActorRef, Snapshot } from 'xstate'; | ||
|
||
const forEachActor = ( | ||
actorRef: AnyActorRef, | ||
callback: (ref: AnyActorRef) => void | ||
) => { | ||
callback(actorRef); | ||
const children = actorRef.getSnapshot().children; | ||
if (children) { | ||
Object.values(children).forEach((child) => { | ||
forEachActor(child as AnyActorRef, callback); | ||
}); | ||
} | ||
}; | ||
|
||
export function stopRootWithRehydration(actorRef: AnyActorRef) { | ||
// persist state here in a custom way allows us to persist inline actors and to preserve actor references | ||
// we do it to avoid setState in useEffect when the effect gets "reconnected" | ||
// this currently only happens in Strict Effects but it simulates the Offscreen aka Activity API | ||
// it also just allows us to end up with a somewhat more predictable behavior for the users | ||
const persistedSnapshots: Array<[AnyActorRef, Snapshot<unknown>]> = []; | ||
forEachActor(actorRef, (ref) => { | ||
persistedSnapshots.push([ref, ref.getSnapshot()]); | ||
// muting observers allow us to avoid `useSelector` from being notified about the stopped state | ||
// React reconnects its subscribers (from the useSyncExternalStore) on its own | ||
// and userland subscibers should basically always do the same anyway | ||
// as each subscription should have its own cleanup logic and that should be called each such reconnect | ||
(ref as any).observers = new Set(); | ||
}); | ||
actorRef.stop(); | ||
persistedSnapshots.forEach(([ref, snapshot]) => { | ||
(ref as any)._processingStatus = 0; | ||
(ref as any)._state = snapshot; | ||
}); | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.