|
| 1 | +import type { ReactiveFlags, ReactiveNode } from 'alien-signals/system'; |
| 2 | +import { createReactiveSystem } from 'alien-signals/system'; |
| 3 | +import type { UnComputedState } from '../unComputed'; |
| 4 | +import type { UnEffectState } from '../unEffect'; |
| 5 | +import type { UnEffectScopeState } from '../unEffectScope'; |
| 6 | +import type { UnSignalState } from '../unSignal'; |
| 7 | + |
| 8 | +export const Queued = 1 << 6; |
| 9 | + |
| 10 | +export const queuedEffects: Array< |
| 11 | + UnEffectState | UnEffectScopeState | undefined |
| 12 | +> = []; |
| 13 | +export const { |
| 14 | + link, |
| 15 | + unlink, |
| 16 | + propagate, |
| 17 | + checkDirty, |
| 18 | + endTracking, |
| 19 | + startTracking, |
| 20 | + shallowPropagate, |
| 21 | +} = createReactiveSystem({ |
| 22 | + update(signal: UnSignalState<unknown> | UnComputedState<unknown>): boolean { |
| 23 | + if ('getter' in signal) { |
| 24 | + return updateComputed(signal); |
| 25 | + } |
| 26 | + |
| 27 | + return updateSignal(signal, signal.value); |
| 28 | + }, |
| 29 | + notify, |
| 30 | + unwatched( |
| 31 | + node: |
| 32 | + | UnSignalState<unknown> |
| 33 | + | UnComputedState<unknown> |
| 34 | + | UnEffectState |
| 35 | + | UnEffectScopeState |
| 36 | + ) { |
| 37 | + if ('getter' in node) { |
| 38 | + let toRemove = node.deps; |
| 39 | + if (toRemove !== undefined) { |
| 40 | + node.flags = 17 as ReactiveFlags.Mutable | ReactiveFlags.Dirty; |
| 41 | + do { |
| 42 | + toRemove = unlink(toRemove, node); |
| 43 | + } while (toRemove !== undefined); |
| 44 | + } |
| 45 | + } else if (!('previousValue' in node)) { |
| 46 | + effectOper.call(node); |
| 47 | + } |
| 48 | + }, |
| 49 | +}); |
| 50 | + |
| 51 | +export interface UnReactivityState { |
| 52 | + batchDepth: number; |
| 53 | + notifyIndex: number; |
| 54 | + queuedEffectsLength: number; |
| 55 | + activeSub: ReactiveNode | undefined; |
| 56 | + activeScope: UnEffectScopeState | undefined; |
| 57 | +} |
| 58 | + |
| 59 | +export const REACTIVITY_STATE: UnReactivityState = { |
| 60 | + batchDepth: 0, |
| 61 | + notifyIndex: 0, |
| 62 | + queuedEffectsLength: 0, |
| 63 | + activeSub: undefined, |
| 64 | + activeScope: undefined, |
| 65 | +}; |
| 66 | + |
| 67 | +export function getCurrentSub(): ReactiveNode | undefined { |
| 68 | + return REACTIVITY_STATE.activeSub; |
| 69 | +} |
| 70 | + |
| 71 | +export function setCurrentSub( |
| 72 | + sub: ReactiveNode | undefined |
| 73 | +): ReactiveNode | undefined { |
| 74 | + const prevSub = REACTIVITY_STATE.activeSub; |
| 75 | + REACTIVITY_STATE.activeSub = sub; |
| 76 | + return prevSub; |
| 77 | +} |
| 78 | + |
| 79 | +export function getCurrentScope(): UnEffectScopeState | undefined { |
| 80 | + return REACTIVITY_STATE.activeScope; |
| 81 | +} |
| 82 | + |
| 83 | +export function setCurrentScope( |
| 84 | + scope: UnEffectScopeState | undefined |
| 85 | +): UnEffectScopeState | undefined { |
| 86 | + const prevScope = REACTIVITY_STATE.activeScope; |
| 87 | + REACTIVITY_STATE.activeScope = scope; |
| 88 | + return prevScope; |
| 89 | +} |
| 90 | + |
| 91 | +export function getBatchDepth(): number { |
| 92 | + return REACTIVITY_STATE.batchDepth; |
| 93 | +} |
| 94 | + |
| 95 | +export function startBatch(): void { |
| 96 | + ++REACTIVITY_STATE.batchDepth; |
| 97 | +} |
| 98 | + |
| 99 | +export function endBatch(): void { |
| 100 | + if (!--REACTIVITY_STATE.batchDepth) { |
| 101 | + flush(); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +export function updateSignal<T>(s: UnSignalState<T>, value: T): boolean { |
| 106 | + s.flags = 1 satisfies ReactiveFlags.Mutable; |
| 107 | + return s.previousValue !== (s.previousValue = value); |
| 108 | +} |
| 109 | + |
| 110 | +export function updateComputed<T>(c: UnComputedState<T>): boolean { |
| 111 | + const prevSub = setCurrentSub(c); |
| 112 | + startTracking(c); |
| 113 | + try { |
| 114 | + const oldValue = c.value; |
| 115 | + return oldValue !== (c.value = c.getter(oldValue)); |
| 116 | + } finally { |
| 117 | + setCurrentSub(prevSub); |
| 118 | + endTracking(c); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export function notify(e: UnEffectState | UnEffectScopeState): void { |
| 123 | + const flags = e.flags; |
| 124 | + if (!(flags & Queued)) { |
| 125 | + e.flags = flags | Queued; |
| 126 | + const subs = e.subs; |
| 127 | + if (subs === undefined) { |
| 128 | + queuedEffects[REACTIVITY_STATE.queuedEffectsLength++] = e; |
| 129 | + } else { |
| 130 | + notify(subs.sub); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export function run( |
| 136 | + e: UnEffectState | UnEffectScopeState, |
| 137 | + flags: ReactiveFlags |
| 138 | +): void { |
| 139 | + if ( |
| 140 | + flags & (16 satisfies ReactiveFlags.Dirty) || |
| 141 | + (flags & (32 satisfies ReactiveFlags.Pending) && checkDirty(e.deps!, e)) |
| 142 | + ) { |
| 143 | + const prev = setCurrentSub(e); |
| 144 | + startTracking(e); |
| 145 | + try { |
| 146 | + (e as UnEffectState).fn(); |
| 147 | + } finally { |
| 148 | + setCurrentSub(prev); |
| 149 | + endTracking(e); |
| 150 | + } |
| 151 | + |
| 152 | + return; |
| 153 | + } else if (flags & (32 satisfies ReactiveFlags.Pending)) { |
| 154 | + e.flags = flags & ~(32 satisfies ReactiveFlags.Pending); |
| 155 | + } |
| 156 | + |
| 157 | + let link = e.deps; |
| 158 | + while (link !== undefined) { |
| 159 | + const dep = link.dep; |
| 160 | + const depFlags = dep.flags; |
| 161 | + if (depFlags & Queued) { |
| 162 | + run(dep, (dep.flags = depFlags & ~Queued)); |
| 163 | + } |
| 164 | + |
| 165 | + link = link.nextDep; |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +export function flush(): void { |
| 170 | + while (REACTIVITY_STATE.notifyIndex < REACTIVITY_STATE.queuedEffectsLength) { |
| 171 | + const effect = queuedEffects[REACTIVITY_STATE.notifyIndex]!; |
| 172 | + queuedEffects[REACTIVITY_STATE.notifyIndex++] = undefined; |
| 173 | + run(effect, (effect.flags &= ~Queued)); |
| 174 | + } |
| 175 | + |
| 176 | + REACTIVITY_STATE.notifyIndex = 0; |
| 177 | + REACTIVITY_STATE.queuedEffectsLength = 0; |
| 178 | +} |
| 179 | + |
| 180 | +export function effectOper(this: UnEffectState | UnEffectScopeState): void { |
| 181 | + let dep = this.deps; |
| 182 | + while (dep !== undefined) { |
| 183 | + dep = unlink(dep, this); |
| 184 | + } |
| 185 | + |
| 186 | + const sub = this.subs; |
| 187 | + if (sub !== undefined) { |
| 188 | + unlink(sub); |
| 189 | + } |
| 190 | + |
| 191 | + this.flags = 0 satisfies ReactiveFlags.None; |
| 192 | +} |
0 commit comments