Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/fix-duplicate-interval.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stackflow/core": patch
---

fix(core): prevent duplicate setInterval in dispatchEvent
10 changes: 10 additions & 0 deletions core/src/makeCoreStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export function makeCoreStore(options: MakeCoreStoreOptions): CoreStore {
value: aggregate(events.value, new Date().getTime()),
};

let currentInterval: ReturnType<typeof setInterval> | null = null;

const actions: StackflowActions = {
getStack() {
return stack.value;
Expand All @@ -98,6 +100,10 @@ export function makeCoreStore(options: MakeCoreStoreOptions): CoreStore {
events.value.push(newEvent);
setStackValue(nextStackValue);

if (currentInterval !== null) {
clearInterval(currentInterval);
}

const interval = setInterval(() => {
const nextStackValue = aggregate(events.value, new Date().getTime());

Expand All @@ -107,8 +113,12 @@ export function makeCoreStore(options: MakeCoreStoreOptions): CoreStore {

if (nextStackValue.globalTransitionState === "idle") {
clearInterval(interval);
if (currentInterval === interval) {
currentInterval = null;
}
}
}, INTERVAL_MS);
currentInterval = interval;
},
push: () => {},
replace: () => {},
Expand Down
Loading