Skip to content

Commit 9d0f3a3

Browse files
author
anakin_karrot
committed
simplify
1 parent 91277f1 commit 9d0f3a3

File tree

1 file changed

+83
-132
lines changed

1 file changed

+83
-132
lines changed

extensions/plugin-history-sync/src/historySyncPlugin.tsx

Lines changed: 83 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { NavigationProcess } from "./NavigationProcess/NavigationProcess";
2828
import { SerialNavigationProcess } from "./NavigationProcess/SerialNavigationProcess";
2929
import { normalizeActivityRouteMap } from "./normalizeActivityRouteMap";
3030
import { Publisher } from "./Publisher";
31-
import type { RouteLike } from "./RouteLike";
31+
import type { HistoryEntry, RouteLike } from "./RouteLike";
3232
import { RoutesProvider } from "./RoutesContext";
3333
import { sortActivityRoutes } from "./sortActivityRoutes";
3434

@@ -258,156 +258,107 @@ export function historySyncPlugin<
258258
};
259259
const defaultHistory =
260260
targetActivityRoute.defaultHistory?.(params) ?? [];
261+
const historyEntryToEvents = ({
262+
activityName,
263+
activityParams,
264+
additionalSteps = [],
265+
}: HistoryEntry): (
266+
| Omit<PushedEvent, "eventDate">
267+
| Omit<StepPushedEvent, "eventDate">
268+
)[] => [
269+
{
270+
name: "Pushed",
271+
id: id(),
272+
activityId: id(),
273+
activityName,
274+
activityParams: {
275+
...activityParams,
276+
},
277+
activityContext: {
278+
path: currentPath,
279+
lazyActivityComponentRenderContext: {
280+
shouldRenderImmediately: true,
281+
},
282+
},
283+
},
284+
...additionalSteps.map(
285+
({
286+
stepParams,
287+
hasZIndex,
288+
}): Omit<StepPushedEvent, "eventDate"> => ({
289+
name: "StepPushed",
290+
id: id(),
291+
stepId: id(),
292+
stepParams,
293+
hasZIndex,
294+
}),
295+
),
296+
];
297+
const createTargetActivityPushEvent = (): Omit<
298+
PushedEvent,
299+
"eventDate"
300+
> => ({
301+
name: "Pushed",
302+
id: id(),
303+
activityId: id(),
304+
activityName: targetActivityRoute.activityName,
305+
activityParams:
306+
makeTemplate(targetActivityRoute, options.urlPatternOptions).parse(
307+
currentPath,
308+
) ?? urlSearchParamsToMap(pathToUrl(currentPath).searchParams),
309+
activityContext: {
310+
path: currentPath,
311+
lazyActivityComponentRenderContext: {
312+
shouldRenderImmediately: true,
313+
},
314+
},
315+
});
261316

262317
if (options.skipDefaultHistorySetupTransition) {
263318
initialSetupProcess = new SerialNavigationProcess([
264-
(): (
265-
| Omit<PushedEvent, "eventDate">
266-
| Omit<StepPushedEvent, "eventDate">
267-
)[] => [
268-
...defaultHistory.flatMap(
269-
({
270-
activityName,
271-
activityParams,
272-
additionalSteps = [],
273-
}): (
274-
| Omit<PushedEvent, "eventDate">
275-
| Omit<StepPushedEvent, "eventDate">
276-
)[] => {
277-
const activityId = id();
319+
() => [
320+
...defaultHistory.flatMap((historyEntry) =>
321+
historyEntryToEvents(historyEntry).map((event) => {
322+
if (event.name !== "Pushed") return event;
278323

279324
activityActivationMonitors.push(
280325
new DefaultHistoryActivityActivationMonitor(
281-
activityId,
326+
event.activityId,
282327
initialSetupProcess!,
283328
),
284329
);
285330

286-
return [
287-
{
288-
name: "Pushed",
289-
id: id(),
290-
activityId,
291-
activityName,
292-
activityParams: {
293-
...activityParams,
294-
},
295-
activityContext: {
296-
path: currentPath,
297-
lazyActivityComponentRenderContext: {
298-
shouldRenderImmediately: true,
299-
},
300-
},
301-
skipEnterActiveState: true,
302-
},
303-
...additionalSteps.map(
304-
({
305-
stepParams,
306-
hasZIndex,
307-
}): Omit<StepPushedEvent, "eventDate"> => ({
308-
name: "StepPushed",
309-
id: id(),
310-
stepId: id(),
311-
stepParams,
312-
hasZIndex,
313-
}),
314-
),
315-
];
316-
},
331+
return {
332+
...event,
333+
skipEnterActiveState: true,
334+
};
335+
}),
317336
),
318337
{
319-
name: "Pushed",
320-
id: id(),
321-
activityId: id(),
322-
activityName: targetActivityRoute.activityName,
323-
activityParams:
324-
makeTemplate(
325-
targetActivityRoute,
326-
options.urlPatternOptions,
327-
).parse(currentPath) ??
328-
urlSearchParamsToMap(pathToUrl(currentPath).searchParams),
329-
activityContext: {
330-
path: currentPath,
331-
lazyActivityComponentRenderContext: {
332-
shouldRenderImmediately: true,
333-
},
334-
},
338+
...createTargetActivityPushEvent(),
335339
skipEnterActiveState: true,
336340
},
337341
],
338342
]);
339343
} else {
340344
initialSetupProcess = new SerialNavigationProcess([
341-
...defaultHistory.map(
342-
({ activityName, activityParams, additionalSteps = [] }) =>
343-
() => {
344-
const events: (
345-
| Omit<PushedEvent, "eventDate">
346-
| Omit<StepPushedEvent, "eventDate">
347-
)[] = [
348-
{
349-
name: "Pushed",
350-
id: id(),
351-
activityId: id(),
352-
activityName,
353-
activityParams: {
354-
...activityParams,
355-
},
356-
activityContext: {
357-
path: currentPath,
358-
lazyActivityComponentRenderContext: {
359-
shouldRenderImmediately: true,
360-
},
361-
},
362-
},
363-
...additionalSteps.map(
364-
({
365-
stepParams,
366-
hasZIndex,
367-
}): Omit<StepPushedEvent, "eventDate"> => ({
368-
name: "StepPushed",
369-
id: id(),
370-
stepId: id(),
371-
stepParams,
372-
hasZIndex,
373-
}),
345+
...defaultHistory.map((historyEntry) => () => {
346+
const events = historyEntryToEvents(historyEntry);
347+
348+
for (const event of events) {
349+
if (event.name === "Pushed") {
350+
activityActivationMonitors.push(
351+
new DefaultHistoryActivityActivationMonitor(
352+
event.activityId,
353+
initialSetupProcess!,
374354
),
375-
];
376-
377-
for (const event of events) {
378-
if (event.name === "Pushed") {
379-
activityActivationMonitors.push(
380-
new DefaultHistoryActivityActivationMonitor(
381-
event.activityId,
382-
initialSetupProcess!,
383-
),
384-
);
385-
}
386-
}
387-
388-
return events;
389-
},
390-
),
391-
() => [
392-
{
393-
name: "Pushed",
394-
id: id(),
395-
activityId: id(),
396-
activityName: targetActivityRoute.activityName,
397-
activityParams:
398-
makeTemplate(
399-
targetActivityRoute,
400-
options.urlPatternOptions,
401-
).parse(currentPath) ??
402-
urlSearchParamsToMap(pathToUrl(currentPath).searchParams),
403-
activityContext: {
404-
path: currentPath,
405-
lazyActivityComponentRenderContext: {
406-
shouldRenderImmediately: true,
407-
},
408-
},
409-
},
410-
],
355+
);
356+
}
357+
}
358+
359+
return events;
360+
}),
361+
() => [createTargetActivityPushEvent()],
411362
]);
412363
}
413364

0 commit comments

Comments
 (0)