Skip to content

Commit

Permalink
refactor: remove stack and use a single effect variable
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleShit committed May 14, 2024
1 parent b1e472e commit 9d62ada
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/signals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type Signal<T = unknown> = {

type EffectCallback = () => void;

const stack = new Set<EffectCallback>();
let currentEffect: EffectCallback | null = null;

export function createSignal<T>(
initialValue: T,
Expand All @@ -18,7 +18,9 @@ export function createSignal<T>(
listeners: new Set(),

get: () => {
stack.forEach((cb) => signal.listeners.add(cb));
if (currentEffect) {
signal.listeners.add(currentEffect);
}

return signal.value;
},
Expand Down Expand Up @@ -47,15 +49,15 @@ export function createEffect(
) {
const effectFn = options.batch ? debounce(cb) : cb;

stack.add(effectFn);
currentEffect = effectFn;

try {
cb();
// eslint-disable-next-line no-useless-catch -- Intentionally rethrowing the error
} catch (e) {
throw e;
} finally {
stack.delete(effectFn);
currentEffect = null;
}
}

Expand Down

0 comments on commit 9d62ada

Please # to comment.