Skip to content

Commit

Permalink
fix: add fix to broken test
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Dec 26, 2024
1 parent cad759a commit 01a1422
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions packages/store/src/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Derived } from './derived'
import type { Store } from './store'
import type { Derived } from './derived'

/**
* This is here to solve the pyramid dependency problem where:
Expand Down Expand Up @@ -34,20 +34,29 @@ let __batchDepth = 0
const __pendingUpdates = new Set<Store<unknown>>()

function __flush_internals(relatedVals: Set<Derived<unknown>>) {
for (const derived of relatedVals) {
// First sort deriveds by dependency order
const sorted = Array.from(relatedVals).sort((a, b) => {
// If a depends on b, b should go first
if (a instanceof Derived && a.options.deps.includes(b)) return 1;
// If b depends on a, a should go first
if (b instanceof Derived && b.options.deps.includes(a)) return -1;
return 0;
});

for (const derived of sorted) {
if (__depsThatHaveWrittenThisTick.current.includes(derived)) {
continue
continue;
}

__depsThatHaveWrittenThisTick.current.push(derived)
derived.recompute()
__depsThatHaveWrittenThisTick.current.push(derived);
derived.recompute();

const stores = __derivedToStore.get(derived)
const stores = __derivedToStore.get(derived);
if (stores) {
for (const store of stores) {
const relatedLinkedDerivedVals = __storeToDerived.get(store)
if (!relatedLinkedDerivedVals) continue
__flush_internals(relatedLinkedDerivedVals)
const relatedLinkedDerivedVals = __storeToDerived.get(store);
if (!relatedLinkedDerivedVals) continue;
__flush_internals(relatedLinkedDerivedVals);
}
}
}
Expand Down

0 comments on commit 01a1422

Please sign in to comment.