proposal-signals icon indicating copy to clipboard operation
proposal-signals copied to clipboard

False positive in cycle detection for asymmetric cycles

Open prophile opened this issue 1 year ago • 0 comments

Consider the following code:

import { Signal } from './dist/index.js'

let switchFlag = false

const state = new Signal.State({})
let b
const a = new Signal.Computed(() => {
  if (switchFlag) {
    return b.get()
  } else {
    return state.get()
  }
})
b = new Signal.Computed(() => {
  if (switchFlag) {
    return state.get()
  } else {
    return a.get()
  }
})
const c = new Signal.Computed(() => [a.get(), b.get()])

c.get()
switchFlag = true
state.set({})
c.get()

For either value of switchFlag the dependency graph is acyclic, but this code currently raises "Detected cycle in computations" according to the polyfill at f7c550b.

The "Algorithm: recalculate dirty computed Signal" specification heavily implies that sources aren't retained between evaluations of a Computed's callback.

prophile avatar Apr 29 '24 17:04 prophile