Skip to content
Draft
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
284 changes: 201 additions & 83 deletions extensions/plugin-history-sync/src/historySyncPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type HistorySyncPluginOptions<T, K extends Extract<keyof T, string>> = (
useHash?: boolean;
history?: History;
urlPatternOptions?: UrlPatternOptions;
disableDefaultHistorySetupTransition?: boolean;
};

export function historySyncPlugin<
Expand Down Expand Up @@ -258,109 +259,226 @@ export function historySyncPlugin<
const defaultHistory =
targetActivityRoute.defaultHistory?.(params) ?? [];

initialSetupProcess = new SerialNavigationProcess([
...defaultHistory.map(
({ activityName, activityParams, additionalSteps = [] }) =>
() => {
const events: (
| Omit<PushedEvent, "eventDate">
| Omit<StepPushedEvent, "eventDate">
)[] = [
{
name: "Pushed",
id: id(),
activityId: id(),
activityName,
activityParams: {
...activityParams,
},
activityContext: {
path: currentPath,
lazyActivityComponentRenderContext: {
shouldRenderImmediately: true,
},
},
},
...additionalSteps.map(
({
stepParams,
hasZIndex,
}): Omit<StepPushedEvent, "eventDate"> => ({
name: "StepPushed",
id: id(),
stepId: id(),
stepParams,
hasZIndex,
}),
),
];

for (const event of events) {
if (event.name === "Pushed") {
if (options.disableDefaultHistorySetupTransition) {
initialSetupProcess = new SerialNavigationProcess([
() => {
const events: (
| Omit<PushedEvent, "eventDate">
| Omit<StepPushedEvent, "eventDate">
)[] = [
...defaultHistory.flatMap(
({ activityName, activityParams, additionalSteps = [] }) => {
const activityId = id();

activityActivationMonitors.push(
new DefaultHistoryActivityActivationMonitor(
event.activityId,
activityId,
initialSetupProcess!,
),
);

const events: (
| Omit<PushedEvent, "eventDate">
| Omit<StepPushedEvent, "eventDate">
)[] = [
{
name: "Pushed",
id: id(),
activityId,
activityName,
activityParams: {
...activityParams,
},
activityContext: {
path: currentPath,
lazyActivityComponentRenderContext: {
shouldRenderImmediately: true,
},
},
skipEnterActiveState: true,
},
...additionalSteps.map(
({
stepParams,
hasZIndex,
}): Omit<StepPushedEvent, "eventDate"> => ({
name: "StepPushed",
id: id(),
stepId: id(),
stepParams,
hasZIndex,
}),
),
];

return events;
},
),
{
name: "Pushed",
id: id(),
activityId: id(),
activityName: targetActivityRoute.activityName,
activityParams:
makeTemplate(
targetActivityRoute,
options.urlPatternOptions,
).parse(currentPath) ??
urlSearchParamsToMap(pathToUrl(currentPath).searchParams),
activityContext: {
path: currentPath,
lazyActivityComponentRenderContext: {
shouldRenderImmediately: true,
},
},
skipEnterActiveState: true,
},
];

return events;
},
]);
} else {
initialSetupProcess = new SerialNavigationProcess([
...defaultHistory.map(
({ activityName, activityParams, additionalSteps = [] }) =>
() => {
const events: (
| Omit<PushedEvent, "eventDate">
| Omit<StepPushedEvent, "eventDate">
)[] = [
{
name: "Pushed",
id: id(),
activityId: id(),
activityName,
activityParams: {
...activityParams,
},
activityContext: {
path: currentPath,
lazyActivityComponentRenderContext: {
shouldRenderImmediately: true,
},
},
},
...additionalSteps.map(
({
stepParams,
hasZIndex,
}): Omit<StepPushedEvent, "eventDate"> => ({
name: "StepPushed",
id: id(),
stepId: id(),
stepParams,
hasZIndex,
}),
),
];

for (const event of events) {
if (event.name === "Pushed") {
activityActivationMonitors.push(
new DefaultHistoryActivityActivationMonitor(
event.activityId,
initialSetupProcess!,
),
);
}
}
}

return events;
},
),
() => [
{
name: "Pushed",
id: id(),
activityId: id(),
activityName: targetActivityRoute.activityName,
activityParams:
makeTemplate(
targetActivityRoute,
options.urlPatternOptions,
).parse(currentPath) ??
urlSearchParamsToMap(pathToUrl(currentPath).searchParams),
activityContext: {
path: currentPath,
lazyActivityComponentRenderContext: {
shouldRenderImmediately: true,
return events;
},
),
() => [
{
name: "Pushed",
id: id(),
activityId: id(),
activityName: targetActivityRoute.activityName,
activityParams:
makeTemplate(
targetActivityRoute,
options.urlPatternOptions,
).parse(currentPath) ??
urlSearchParamsToMap(pathToUrl(currentPath).searchParams),
activityContext: {
path: currentPath,
lazyActivityComponentRenderContext: {
shouldRenderImmediately: true,
},
},
},
},
],
]);
],
]);
}

return initialSetupProcess
.captureNavigationOpportunity(null)
.map((event) => ({
.map((event, index) => ({
...event,
eventDate: Date.now() - MINUTE,
eventDate: Date.now() - MINUTE + index,
}));
},
onInit({ actions: { getStack, dispatchEvent, push, stepPush } }) {
const stack = getStack();
const rootActivity = stack.activities[0];

const match = activityRoutes.find(
(r) => r.activityName === rootActivity.name,
)!;
const template = makeTemplate(match, options.urlPatternOptions);
for (const activity of stack.activities) {
if (
activity.transitionState === "enter-done" ||
activity.transitionState === "enter-active"
) {
const match = activityRoutes.find(
(r) => r.activityName === activity.name,
)!;
const template = makeTemplate(match, options.urlPatternOptions);

const lastStep = last(rootActivity.steps);
if (activity.isRoot) {
requestHistoryTick(() => {
silentFlag = true;
replaceState({
history,
pathname: template.fill(activity.params),
state: {
activity: activity,
step: activity.steps[0],
},
useHash: options.useHash,
});
});
} else {
requestHistoryTick(() => {
silentFlag = true;
pushState({
history,
pathname: template.fill(activity.params),
state: {
activity: activity,
step: activity.steps[0],
},
useHash: options.useHash,
});
});
}

requestHistoryTick(() => {
silentFlag = true;
replaceState({
history,
pathname: template.fill(rootActivity.params),
state: {
activity: rootActivity,
step: lastStep,
},
useHash: options.useHash,
});
});
for (const step of activity.steps) {
if (!step.exitedBy && step.enteredBy.name !== "Pushed") {
requestHistoryTick(() => {
silentFlag = true;
pushState({
history,
pathname: template.fill(activity.params),
state: {
activity: activity,
step: step,
},
useHash: options.useHash,
});
});
}
}
}
}

const onPopState: Listener = (e) => {
if (silentFlag) {
Expand Down
Loading