-
Notifications
You must be signed in to change notification settings - Fork 114
feat(react): support lazy activity with internal plugin #617
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
Open
orionmiz
wants to merge
14
commits into
main
Choose a base branch
from
lazy-activity
base: main
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 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f2592b0
feat(react): add lazy activity plugin for stable version
orionmiz bab05f7
refactor: use enum
orionmiz 341ed38
refactor: remove any type
orionmiz 58d2ddc
fix: work same for replace too
orionmiz 7770a81
fix: handle memo component
orionmiz 1c22f60
chore: add source comment
orionmiz 60127cc
chore: add changeset
orionmiz 6b8fa40
chore: run format
orionmiz a488fe9
fix: accept suggestion
orionmiz eebe0c6
feat: add version check
orionmiz 8823102
Merge branch 'main' into lazy-activity
orionmiz fe404be
trigger ci
orionmiz c0b23e9
Merge branch 'main' into lazy-activity
orionmiz 7726c76
Merge branch 'main' into lazy-activity
orionmiz 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 hidden or 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 @@ | ||
| --- | ||
| "@stackflow/react": minor | ||
| --- | ||
|
|
||
| feat(react): support lazy activity with internal plugin |
This file contains hidden or 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,146 @@ | ||
| import type { ActivityComponentType } from "../__internal__/ActivityComponentType"; | ||
| import type { StackflowReactPlugin } from "../__internal__/StackflowReactPlugin"; | ||
|
|
||
| // https://github.com/facebook/react/blob/v19.1.1/packages/shared/ReactSymbols.js#L32 | ||
| const REACT_LAZY_TYPE: symbol = Symbol.for("react.lazy"); | ||
| const REACT_MEMO_TYPE: symbol = Symbol.for("react.memo"); | ||
|
|
||
| // https://github.com/facebook/react/blob/v19.1.1/packages/react/src/ReactLazy.js | ||
| interface Wakeable { | ||
| then(onFulfill: () => unknown, onReject: () => unknown): undefined | Wakeable; | ||
| } | ||
|
|
||
| interface ThenableImpl<T> { | ||
| then( | ||
| onFulfill: (value: T) => unknown, | ||
| onReject: (error: unknown) => unknown, | ||
| ): undefined | Wakeable; | ||
| } | ||
| interface UntrackedThenable<T> extends ThenableImpl<T> { | ||
| status?: undefined; | ||
| } | ||
|
|
||
| interface PendingThenable<T> extends ThenableImpl<T> { | ||
| status: "pending"; | ||
| } | ||
|
|
||
| interface FulfilledThenable<T> extends ThenableImpl<T> { | ||
| status: "fulfilled"; | ||
| value: T; | ||
| } | ||
|
|
||
| interface RejectedThenable<T> extends ThenableImpl<T> { | ||
| status: "rejected"; | ||
| reason: unknown; | ||
| } | ||
|
|
||
| type Thenable<T> = | ||
| | UntrackedThenable<T> | ||
| | PendingThenable<T> | ||
| | FulfilledThenable<T> | ||
| | RejectedThenable<T>; | ||
|
|
||
| const Uninitialized = -1; | ||
| const Pending = 0; | ||
| const Resolved = 1; | ||
| const Rejected = 2; | ||
|
|
||
| type UninitializedPayload<T> = { | ||
| _status: -1; | ||
| _result: () => Thenable<{ default: T }>; | ||
| }; | ||
|
|
||
| type PendingPayload = { | ||
| _status: 0; | ||
| _result: Wakeable; | ||
| }; | ||
|
|
||
| type ResolvedPayload<T> = { | ||
| _status: 1; | ||
| _result: { default: T }; | ||
| }; | ||
|
|
||
| type RejectedPayload = { | ||
| _status: 2; | ||
| _result: unknown; | ||
| }; | ||
|
|
||
| type Payload<T> = | ||
| | UninitializedPayload<T> | ||
| | PendingPayload | ||
| | ResolvedPayload<T> | ||
| | RejectedPayload; | ||
|
|
||
| type LazyComponent = { | ||
| $$typeof: symbol | number; | ||
| _payload: Payload<unknown>; | ||
| }; | ||
|
|
||
| // https://github.com/facebook/react/blob/v19.1.1/packages/react/src/ReactMemo.js | ||
| type MemoComponent = { | ||
| $$typeof: symbol | number; | ||
| type: React.ElementType; | ||
| }; | ||
|
|
||
| function isLazyComponent(component: unknown): component is LazyComponent { | ||
| const isLazy = | ||
| typeof component === "object" && | ||
| component !== null && | ||
| "$$typeof" in component && | ||
| component.$$typeof === REACT_LAZY_TYPE && | ||
| "_payload" in component; | ||
| return isLazy; | ||
| } | ||
|
|
||
| function isMemoComponent(component: unknown): component is MemoComponent { | ||
| const isMemo = | ||
| typeof component === "object" && | ||
| component !== null && | ||
| "$$typeof" in component && | ||
| component.$$typeof === REACT_MEMO_TYPE; | ||
| return isMemo; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| export function lazyActivityPlugin(activityComponentMap: { | ||
| [key: string]: ActivityComponentType; | ||
| }): StackflowReactPlugin { | ||
| function handleLazyActivity({ | ||
| actions, | ||
| actionParams, | ||
| }: { | ||
| actions: { pause: () => void; resume: () => void }; | ||
| actionParams: { activityName: string }; | ||
| }) { | ||
| let Activity = activityComponentMap[actionParams.activityName]; | ||
|
|
||
| if (isMemoComponent(Activity)) { | ||
| Activity = Activity.type as ActivityComponentType; | ||
| } | ||
|
orionmiz marked this conversation as resolved.
Outdated
|
||
|
|
||
| if ( | ||
| isLazyComponent(Activity) && | ||
| Activity._payload._status === Uninitialized | ||
| ) { | ||
| actions.pause(); | ||
|
|
||
| Activity._payload._result().then( | ||
| () => { | ||
| actions.resume(); | ||
| }, | ||
| () => { | ||
| actions.resume(); | ||
| }, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return () => ({ | ||
| key: "plugin-lazy-activity", | ||
| onBeforePush({ actions, actionParams }) { | ||
| handleLazyActivity({ actions, actionParams }); | ||
| }, | ||
| onBeforeReplace({ actions, actionParams }) { | ||
| handleLazyActivity({ actions, actionParams }); | ||
| }, | ||
| }); | ||
| } | ||
This file contains hidden or 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
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.
Consider the risks of using React internals
Using React's internal symbols (
react.lazyandreact.memo) makes this implementation fragile. These symbols are not part of React's public API and could change in future versions without notice, potentially breaking this plugin.Consider adding:
🤖 Prompt for AI Agents