From 9d62adaacbbe5203574081b5c9d0466df0442774 Mon Sep 17 00:00:00 2001 From: StyleShit Date: Tue, 14 May 2024 20:57:03 +0300 Subject: [PATCH] refactor: remove stack and use a single effect variable --- src/signals.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/signals.ts b/src/signals.ts index 0c16949..54ff25c 100644 --- a/src/signals.ts +++ b/src/signals.ts @@ -7,7 +7,7 @@ type Signal = { type EffectCallback = () => void; -const stack = new Set(); +let currentEffect: EffectCallback | null = null; export function createSignal( initialValue: T, @@ -18,7 +18,9 @@ export function createSignal( listeners: new Set(), get: () => { - stack.forEach((cb) => signal.listeners.add(cb)); + if (currentEffect) { + signal.listeners.add(currentEffect); + } return signal.value; }, @@ -47,7 +49,7 @@ export function createEffect( ) { const effectFn = options.batch ? debounce(cb) : cb; - stack.add(effectFn); + currentEffect = effectFn; try { cb(); @@ -55,7 +57,7 @@ export function createEffect( } catch (e) { throw e; } finally { - stack.delete(effectFn); + currentEffect = null; } }