-
Notifications
You must be signed in to change notification settings - Fork 111
feat(react/future): Add prefetch API for lazy activity component and loader data. #616
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
Merged
Merged
Changes from 26 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
c447299
test
ENvironmentSet 479fa84
optimize
ENvironmentSet a1d42ba
fix
ENvironmentSet d97b03e
fix
ENvironmentSet 833d47d
format
ENvironmentSet 6ec8c73
format
ENvironmentSet e52974e
fix
ENvironmentSet 2658640
refactor
ENvironmentSet cb1ab37
refactor: reduce complexivity
6a8333b
fix: type error
9f952f7
feat: Detach prepare logic from useFlow
31c2f62
add exports
8530765
changeset
53e5454
chore: run format
orionmiz 3926564
Update changeset
e6a009a
Handle thenable
e2aed6f
optimize cache gc
a8dcadf
Optimize cache entry creation
792a17b
use activityComponentMapContext
5545142
do not cache rejected promise
978ba32
accept thenable
410b4b9
fix replace id bug
74ce220
fix import
dfd5291
fix import
ece2e9d
separate prepare from actions
2dee06d
update changeset
9ceb57f
spec compat
85868a3
use promiselike
9b2a4b5
revert
322f64d
run format
29b613b
don't cache rejected promise
19be7f7
throw error
b155672
Merge branch 'main' into route-preload
ENvironmentSet cbd7cfd
remove unused import
24c1600
add loader constructor
1ef17c7
fix
55f23c8
update
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,7 @@ | ||
| --- | ||
| "@stackflow/react": minor | ||
| --- | ||
|
|
||
| Add prefetch API for lazy activity component and loader data. | ||
| - A hook `usePrepare()` which returns `prepare(activityName[, activityParams])` is added for navigation warmup. | ||
| - A hook `useActivityPreparation(activityNames)` for preparing navigations inside a component is added. |
38 changes: 38 additions & 0 deletions
38
integrations/react/src/__internal__/ActivityComponentMapProvider.tsx
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,38 @@ | ||
| import type { RegisteredActivityName } from "@stackflow/config"; | ||
| import type { PropsWithChildren } from "react"; | ||
| import type { ActivityComponentType } from "./ActivityComponentType"; | ||
| import { createContext, useContext } from "react"; | ||
|
|
||
| const ActivityComponentMapContext = createContext< | ||
| | { | ||
| [activityName in RegisteredActivityName]: ActivityComponentType; | ||
| } | ||
| | null | ||
| >(null); | ||
|
|
||
| export function useActivityComponentMap() { | ||
| const context = useContext(ActivityComponentMapContext); | ||
| if (context === null) { | ||
| throw new Error( | ||
| "useActivityComponentMap must be used within ActivityComponentMapProvider", | ||
| ); | ||
| } | ||
| return context; | ||
| } | ||
|
|
||
| type ActivityComponentMapProviderProps = PropsWithChildren<{ | ||
| value: { | ||
| [activityName in RegisteredActivityName]: ActivityComponentType; | ||
| }; | ||
| }>; | ||
|
|
||
| export function ActivityComponentMapProvider({ | ||
| children, | ||
| value, | ||
| }: ActivityComponentMapProviderProps) { | ||
| return ( | ||
| <ActivityComponentMapContext.Provider value={value}> | ||
| {children} | ||
| </ActivityComponentMapContext.Provider> | ||
| ); | ||
| } |
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
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
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,15 @@ | ||
| export interface Thenable<T> { | ||
| then<R>( | ||
| onFulfilled: (value: T) => R, | ||
| onRejected?: (reason: unknown) => R, | ||
| ): Thenable<Awaited<R>>; | ||
| } | ||
|
|
||
| export function isThenable(value: unknown): value is Thenable<unknown> { | ||
| return ( | ||
| typeof value === "object" && | ||
| value !== null && | ||
| "then" in value && | ||
| typeof value.then === "function" | ||
| ); | ||
| } | ||
ENvironmentSet marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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 |
|---|---|---|
| @@ -1,28 +1,16 @@ | ||
| /** | ||
| * Main | ||
| */ | ||
|
|
||
| export * from "../__internal__/activity/useActivity"; | ||
|
|
||
| /** | ||
| * Types | ||
| */ | ||
| export * from "../__internal__/StackflowReactPlugin"; | ||
| /** | ||
| * Hooks | ||
| */ | ||
| export * from "../__internal__/stack/useStack"; | ||
| export * from "./Actions"; | ||
| export * from "./ActivityComponentType"; | ||
| /** | ||
| * Utils | ||
| */ | ||
| export * from "./lazy"; | ||
| export * from "./loader/useLoaderData"; | ||
| export * from "./StackComponentType"; | ||
| export * from "./StepActions"; | ||
| export * from "./stackflow"; | ||
| export * from "./useActivityParams"; | ||
| export * from "./useActivityPreparation"; | ||
| export * from "./usePrepare"; | ||
| export * from "./useConfig"; | ||
| export * from "./useFlow"; | ||
| export * from "./useStepFlow"; |
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
29 changes: 29 additions & 0 deletions
29
integrations/react/src/future/loader/DataLoaderContext.tsx
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,29 @@ | ||
| import { createContext, type ReactNode, useContext } from "react"; | ||
|
|
||
| export const DataLoaderContext = createContext< | ||
| ((activityName: string, activityParams: {}) => unknown) | null | ||
| >(null); | ||
ENvironmentSet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| export function DataLoaderProvider({ | ||
| loadData, | ||
| children, | ||
| }: { | ||
| loadData: (activityName: string, activityParams: {}) => unknown; | ||
| children: ReactNode; | ||
| }) { | ||
| return ( | ||
| <DataLoaderContext.Provider value={loadData}> | ||
| {children} | ||
| </DataLoaderContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useDataLoader() { | ||
| const loadData = useContext(DataLoaderContext); | ||
|
|
||
| if (!loadData) { | ||
| throw new Error("useDataLoader() must be used within a DataLoaderProvider"); | ||
| } | ||
|
|
||
| return loadData; | ||
| } | ||
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| export * from "./DataLoaderContext"; | ||
| export * from "./loaderPlugin"; | ||
| export * from "./useLoaderData"; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.