-
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 1 commit
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,42 @@ export function makeActions({ | |
|
|
||
| dispatchEvent("Resumed", nextActionParams); | ||
| }, | ||
| prune() { | ||
| const { activities } = store.getStack(); | ||
| const activeActivities = activities.filter( | ||
| (activity) => | ||
| activity.transitionState === "enter-active" || | ||
| activity.transitionState === "enter-done", | ||
| ); | ||
|
|
||
| const now = new Date().getTime(); | ||
|
|
||
| const newEvents: DomainEvent[] = [ | ||
| makeEvent("Initialized", { | ||
| transitionDuration: 0, | ||
| eventDate: now, | ||
| }), | ||
| ...activeActivities.map((activity) => | ||
| makeEvent("ActivityRegistered", { | ||
| activityName: activity.name, | ||
| eventDate: now, | ||
| }), | ||
| ), | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ...activeActivities.map((activity) => | ||
| makeEvent("Pushed", { | ||
| activityId: activity.id, | ||
| activityName: activity.name, | ||
| activityParams: activity.params, | ||
| eventDate: now, | ||
| skipEnterActiveState: true, | ||
| }), | ||
| ), | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]; | ||
|
|
||
| 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.