const context: Function[] = []
const getCurrentObserver = () => context[context.length - 1]
 
export function createSignal<T = any>(value: T) {
  const subscribers = new Set<Function>()
 
  const read = () => {
    const current = getCurrentObserver()
    if (current) subscribers.add(current)
    return value
  }
 
  const write = (nextValue: T) => {
    value = nextValue
    for (const sub of subscribers) sub()
  }
 
  return [read, write] as const
}
 
export function createEffect(fn: Function) {
  const execute = () => {
    context.push(execute)
    try {
      fn()
    } finally {
      context.pop()
    }
  }
  execute()
}