Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
45 changes: 34 additions & 11 deletions core/src/activity-utils/makeStackReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ import { makeActivitiesReducer } from "./makeActivitiesReducer";
import { makeActivityReducer } from "./makeActivityReducer";
import { makeReducer } from "./makeReducer";

function calculateGlobalTransitionState(
activities: Activity[],
currentState: Stack["globalTransitionState"],
): Stack["globalTransitionState"] {
if (currentState === "paused") {
return "paused";
}

const hasActiveTransition = activities.some(
(activity) =>
activity.transitionState === "enter-active" ||
activity.transitionState === "exit-active",
);

return hasActiveTransition ? "loading" : "idle";
}

function withPauseReducer<T extends DomainEvent>(
reducer: (stack: Stack, event: T) => Stack,
) {
Expand Down Expand Up @@ -63,20 +80,25 @@ function withActivitiesReducer<T extends DomainEvent>(
);
}

const isLoading = activities.find(
(activity) =>
activity.transitionState === "enter-active" ||
activity.transitionState === "exit-active",
const globalTransitionState = calculateGlobalTransitionState(
activities,
stack.globalTransitionState,
);

const globalTransitionState =
stack.globalTransitionState === "paused"
? "paused"
: isLoading
? "loading"
: "idle";
const result = reducer(
{ ...stack, activities, globalTransitionState },
event,
);

const updatedGlobalTransitionState = calculateGlobalTransitionState(
result.activities,
result.globalTransitionState,
);

return reducer({ ...stack, activities, globalTransitionState }, event);
return {
...result,
globalTransitionState: updatedGlobalTransitionState,
};
Comment on lines +83 to +101
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

global transition state should be evaluated latest (another with hof?)

};
}

Expand Down Expand Up @@ -123,6 +145,7 @@ export function makeStackReducer(context: {
return {
...stack,
globalTransitionState: "paused",
pausedEvents: stack.pausedEvents ?? [],
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug fix

};
}, context),
),
Expand Down
30 changes: 30 additions & 0 deletions integrations/react/src/__internal__/suspensePlugin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Suspense, useEffect } from "react";
import type { StackflowReactPlugin } from "./StackflowReactPlugin";
import { useCoreActions } from "./core";

export function suspensePlugin(): StackflowReactPlugin {
return () => ({
key: "plugin-suspense",
wrapActivity: ({ activity }) => {
return (
<Suspense fallback={<SuspenseFallback />}>{activity.render()}</Suspense>
);
},
});
}

export function SuspenseFallback() {
const { pause, resume } = useCoreActions();

useEffect(() => {
console.log("lets pause");
pause();

return () => {
console.log("lets resume");
resume();
};
}, []);

return null;
}
41 changes: 5 additions & 36 deletions integrations/react/src/future/loader/loaderPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ function createBeforeRouteHandler<
[activityName in RegisteredActivityName]: ActivityComponentType<any>;
},
>(input: StackflowInput<T, R>): OnBeforeRoute {
return ({
actionParams,
actions: { overrideActionParams, pause, resume },
}) => {
return ({ actionParams, actions: { overrideActionParams } }) => {
const { activityName, activityParams, activityContext } = actionParams;

const matchActivity = input.config.activities.find(
Expand All @@ -106,28 +103,15 @@ function createBeforeRouteHandler<

const loaderDataPromise =
loaderData instanceof Promise ? loaderData : undefined;
const lazyComponentPromise =
"_load" in matchActivityComponent
? matchActivityComponent._load?.()
: undefined;

if (loaderDataPromise || lazyComponentPromise) {
pause();
}
Promise.allSettled([loaderDataPromise, lazyComponentPromise])
.then(([loaderDataPromiseResult, lazyComponentPromiseResult]) => {
Promise.allSettled([loaderDataPromise]).then(
([loaderDataPromiseResult]) => {
printLoaderDataPromiseError({
promiseResult: loaderDataPromiseResult,
activityName: matchActivity.name,
});
printLazyComponentPromiseError({
promiseResult: lazyComponentPromiseResult,
activityName: matchActivity.name,
});
})
.finally(() => {
resume();
});
},
);

overrideActionParams({
...actionParams,
Expand All @@ -153,18 +137,3 @@ function printLoaderDataPromiseError({
);
}
}

function printLazyComponentPromiseError({
promiseResult,
activityName,
}: {
promiseResult: PromiseSettledResult<any>;
activityName: string;
}) {
if (promiseResult.status === "rejected") {
console.error(promiseResult.reason);
console.error(
`The above error occurred while loading a lazy react component of the "${activityName}" activity`,
);
}
}
6 changes: 2 additions & 4 deletions integrations/react/src/future/stackflow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MainRenderer from "../__internal__/MainRenderer";
import { makeActivityId } from "../__internal__/activity";
import { CoreProvider } from "../__internal__/core";
import { PluginsProvider } from "../__internal__/plugins";
import { suspensePlugin } from "../__internal__/suspensePlugin";
import { isBrowser, makeRef } from "../__internal__/utils";
import type { StackflowReactPlugin } from "../stable";
import type { Actions } from "./Actions";
Expand Down Expand Up @@ -57,11 +58,8 @@ export function stackflow<
...(input.plugins ?? [])
.flat(Number.POSITIVE_INFINITY as 0)
.map((p) => p as StackflowReactPlugin),

/**
* `loaderPlugin()` must be placed after `historySyncPlugin()`
*/
loaderPlugin(input),
suspensePlugin(),
];

const enoughPastTime = () =>
Expand Down