Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-comics-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackflow/react": minor
---

feat(react): stabilize lazy load plugin
2 changes: 1 addition & 1 deletion integrations/react/src/future/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export * from "./useConfig";
/**
* Utils
*/
export * from "./lazy";
export * from "../__internal__/lazy";
67 changes: 67 additions & 0 deletions integrations/react/src/stable/LazyLoadPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import type { StackflowReactPlugin } from "../__internal__/StackflowReactPlugin";
import type { BaseActivities } from "./BaseActivities";
import type { StackflowOptions } from "./stackflow";

export function lazyLoadPlugin<T extends BaseActivities>(
options: StackflowOptions<T>,
): StackflowReactPlugin {
return () => ({
key: "plugin-lazy-load",
onBeforePush: createBeforeRouteHandler(options),
onBeforeReplace: createBeforeRouteHandler(options),
});
}

type OnBeforeRoute = NonNullable<
| ReturnType<StackflowReactPlugin>["onBeforePush"]
| ReturnType<StackflowReactPlugin>["onBeforeReplace"]
>;

function createBeforeRouteHandler<T extends BaseActivities>(
options: StackflowOptions<T>,
): OnBeforeRoute {
return ({ actionParams, actions: { pause, resume } }) => {
const { activityName } = actionParams;

const matchActivityComponent = options.activities[activityName];

if (!matchActivityComponent) {
return;
}

const lazyComponentPromise =
"_load" in matchActivityComponent
? matchActivityComponent._load?.()
: undefined;

if (!lazyComponentPromise) {
return;
}

pause();

lazyComponentPromise
.catch((reason) => {
printLazyComponentPromiseError({
reason,
activityName,
});
})
.finally(() => {
resume();
});
};
}

function printLazyComponentPromiseError({
reason,
activityName,
}: {
reason: PromiseRejectedResult["reason"];
activityName: string;
}) {
console.error(reason);
console.error(
`The above error occurred while loading a lazy react component of the "${activityName}" activity`,
);
}
1 change: 1 addition & 0 deletions integrations/react/src/stable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./useActiveEffect";
export * from "./useEnterDoneEffect";
export * from "./useStep";
export * from "./useStepActions";
export * from "../__internal__/lazy";
10 changes: 7 additions & 3 deletions integrations/react/src/stable/stackflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { CoreProvider } from "../__internal__/core";
import { PluginsProvider } from "../__internal__/plugins";
import { isBrowser, makeRef } from "../__internal__/utils";
import type { BaseActivities } from "./BaseActivities";
import { lazyLoadPlugin } from "./LazyLoadPlugin";
import type { UseActionsOutputType } from "./useActions";
import { useActions } from "./useActions";
import type { UseStepActionsOutputType } from "./useStepActions";
Expand Down Expand Up @@ -129,9 +130,12 @@ export type StackflowOutput<T extends BaseActivities> = {
export function stackflow<T extends BaseActivities>(
options: StackflowOptions<T>,
): StackflowOutput<T> {
const plugins = (options.plugins ?? [])
.flat(Number.POSITIVE_INFINITY as 0)
.map((p) => p as StackflowReactPlugin);
const plugins = [
...(options.plugins ?? [])
.flat(Number.POSITIVE_INFINITY as 0)
.map((p) => p as StackflowReactPlugin),
lazyLoadPlugin(options),
];

const activityComponentMap = Object.entries(options.activities).reduce(
(acc, [key, Activity]) => ({
Expand Down