-
Notifications
You must be signed in to change notification settings - Fork 112
feat(core): add prune API to clear event history #650
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
base: main
Are you sure you want to change the base?
Changes from all commits
1e4c2dc
3a2d940
aaef690
9362089
9583f12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,26 @@ | ||
| import { aggregate } from "../aggregate"; | ||
| import type { DomainEvent } from "../event-types"; | ||
| import { makeEvent } from "../event-utils"; | ||
| import type { StackflowActions, StackflowPlugin } from "../interfaces"; | ||
| import type { Stack } from "../Stack"; | ||
| import { triggerPreEffectHook } from "./triggerPreEffectHooks"; | ||
|
|
||
| type ActionCreatorOptions = { | ||
| dispatchEvent: StackflowActions["dispatchEvent"]; | ||
| pluginInstances: ReturnType<StackflowPlugin>[]; | ||
| actions: StackflowActions; | ||
| store: { | ||
| getStack: () => Stack; | ||
| events: { value: DomainEvent[] }; | ||
| setStackValue: (stack: Stack) => void; | ||
| }; | ||
| }; | ||
|
|
||
| export function makeActions({ | ||
| dispatchEvent, | ||
| pluginInstances, | ||
| actions, | ||
| store, | ||
| }: ActionCreatorOptions): Omit<StackflowActions, "dispatchEvent" | "getStack"> { | ||
| return { | ||
| push(params) { | ||
|
|
@@ -125,5 +135,114 @@ export function makeActions({ | |
|
|
||
| dispatchEvent("Resumed", nextActionParams); | ||
| }, | ||
| prune() { | ||
| const stack = store.getStack(); | ||
|
|
||
| if (stack.globalTransitionState === "paused") { | ||
| throw new Error("Cannot prune while the stack is paused"); | ||
| } | ||
|
|
||
| const activeActivities = stack.activities.filter( | ||
| (activity) => | ||
| activity.transitionState === "enter-active" || | ||
| activity.transitionState === "enter-done" || | ||
| activity.transitionState === "exit-active", | ||
| ); | ||
|
|
||
| const lastEvent = store.events.value[store.events.value.length - 1]; | ||
| const now = Math.max(new Date().getTime(), lastEvent?.eventDate ?? 0) + 1; | ||
|
|
||
| const originalInitialized = store.events.value.find( | ||
| (e) => e.name === "Initialized", | ||
| ); | ||
| const initializedEventDate = originalInitialized?.eventDate ?? now; | ||
|
|
||
| const activityRegisteredEvents = new Map< | ||
| string, | ||
| { | ||
| eventDate: number; | ||
| paramsSchema?: { | ||
| type: "object"; | ||
| properties: { | ||
| [key: string]: { | ||
| type: "string"; | ||
| enum?: string[]; | ||
| }; | ||
| }; | ||
| required: string[]; | ||
| }; | ||
| } | ||
| >(); | ||
| for (const event of store.events.value) { | ||
| if (event.name === "ActivityRegistered") { | ||
| activityRegisteredEvents.set(event.activityName, { | ||
| eventDate: event.eventDate, | ||
| ...(event.activityParamsSchema | ||
| ? { paramsSchema: event.activityParamsSchema } | ||
| : {}), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const newEvents: DomainEvent[] = [ | ||
| makeEvent("Initialized", { | ||
| transitionDuration: stack.transitionDuration, | ||
| eventDate: initializedEventDate, | ||
| }), | ||
| ...Array.from(activityRegisteredEvents.entries()).map( | ||
| ([activityName, registered]) => | ||
| makeEvent("ActivityRegistered", { | ||
| activityName, | ||
| eventDate: registered.eventDate, | ||
| ...(registered.paramsSchema | ||
| ? { activityParamsSchema: registered.paramsSchema } | ||
| : {}), | ||
| }), | ||
| ), | ||
| ]; | ||
|
|
||
| for (const activity of activeActivities) { | ||
| const isReplaced = activity.enteredBy.name === "Replaced"; | ||
|
|
||
| newEvents.push( | ||
| makeEvent(isReplaced ? "Replaced" : "Pushed", { | ||
| activityId: activity.id, | ||
| activityName: activity.name, | ||
| activityParams: activity.params, | ||
| eventDate: activity.enteredBy.eventDate, | ||
| skipEnterActiveState: true, | ||
| ...(activity.context ? { activityContext: activity.context } : {}), | ||
| }), | ||
| ); | ||
|
|
||
| for (const step of activity.steps.slice(1)) { | ||
| const isStepReplaced = step.enteredBy.name === "StepReplaced"; | ||
| newEvents.push( | ||
| makeEvent(isStepReplaced ? "StepReplaced" : "StepPushed", { | ||
| stepId: step.id, | ||
| stepParams: step.params, | ||
| eventDate: step.enteredBy.eventDate, | ||
| ...(step.hasZIndex ? { hasZIndex: step.hasZIndex } : {}), | ||
| }), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| const poppedEvents = activeActivities | ||
| .filter( | ||
| (activity) => | ||
| activity.transitionState === "exit-active" && | ||
| activity.exitedBy?.name === "Popped", | ||
| ) | ||
| .map((activity) => activity.exitedBy as DomainEvent) | ||
| .sort((a, b) => a.eventDate - b.eventDate); | ||
|
|
||
| newEvents.push(...poppedEvents); | ||
|
|
||
| store.events.value = newEvents; | ||
|
|
||
| const nextStackValue = aggregate(store.events.value, now); | ||
| store.setStackValue(nextStackValue); | ||
|
Comment on lines
+242
to
+245
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add error handling to prevent inconsistent state. If Wrap in try-catch to handle errors gracefully: -store.events.value = newEvents;
-
-const nextStackValue = aggregate(store.events.value, now);
-store.setStackValue(nextStackValue);
+try {
+ const nextStackValue = aggregate(newEvents, now);
+ store.events.value = newEvents;
+ store.setStackValue(nextStackValue);
+} catch (error) {
+ console.error("Failed to prune stack:", error);
+ // Store remains in original state
+}This ensures atomicity—either both updates succeed or neither does. 🤖 Prompt for AI Agents |
||
| }, | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.