@xaendar/signals 0.4.6 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -193,6 +193,7 @@ declare global {
193
193
  export { };
194
194
 
195
195
  import { NoArgsVoidFunction } from '@xaendar/types';
196
+ import { NoArgsVoidFunction as NoArgsVoidFunction_2 } from '@xaendar/types';
196
197
 
197
198
  /**
198
199
  * A read-only Signal whose value is derived lazily from other Signals.
@@ -297,7 +298,26 @@ declare type ComputedState = 'dirty' | 'checked' | 'computing' | 'clean';
297
298
  * is tracked as a dependency.
298
299
  * @returns A disposer function that, when called, permanently stops the effect.
299
300
  */
300
- export declare function effect(fn: NoArgsVoidFunction): NoArgsVoidFunction;
301
+ export declare function effect(fn: NoArgsVoidFunction, options?: EffectOptions): NoArgsVoidFunction;
302
+
303
+ /**
304
+ * Options for configuring an effect.
305
+ */
306
+ export declare type EffectOptions = {
307
+ /**
308
+ * Registers a cleanup function that is called before the effect re-runs or when it is disposed.
309
+ * @param cleanupFn - The function to invoke during cleanup.
310
+ */
311
+ onCleanup?: NoArgsVoidFunction_2;
312
+ /**
313
+ * Called before the effect re-runs.
314
+ */
315
+ onBeforeRun?: () => void;
316
+ /**
317
+ * Called after the effect re-runs.
318
+ */
319
+ onAfterRun?: () => void;
320
+ };
301
321
 
302
322
  /**
303
323
  * Utility type to extract the inner type of a Signal.State.
@@ -967,7 +967,7 @@ function loadSignals(options) {
967
967
  * is tracked as a dependency.
968
968
  * @returns A disposer function that, when called, permanently stops the effect.
969
969
  */
970
- function effect(fn) {
970
+ function effect(fn, options) {
971
971
  /**
972
972
  * Wrap the user callback in a Computed so that automatic dependency
973
973
  * tracking (via pushComputed / popComputed) works for free.
@@ -987,13 +987,17 @@ function effect(fn) {
987
987
  needsEnqueue = false;
988
988
  queueMicrotask(() => {
989
989
  needsEnqueue = true;
990
+ options?.onBeforeRun?.();
990
991
  watcher.getPending().forEach((computed) => computed.get());
992
+ options?.onAfterRun?.();
991
993
  watcher.watch();
992
994
  });
993
995
  }
994
996
  });
997
+ options?.onBeforeRun?.();
995
998
  watcher.watch(computed);
996
999
  computed.get();
1000
+ options?.onAfterRun?.();
997
1001
  /**
998
1002
  * Disposer — call this to permanently stop the effect.
999
1003
  *
@@ -1001,7 +1005,10 @@ function effect(fn) {
1001
1005
  * (Watcher → Computed → all sources), preventing any further
1002
1006
  * notifications and allowing GC.
1003
1007
  */
1004
- return () => watcher.unwatch(computed);
1008
+ return () => {
1009
+ options?.onCleanup?.();
1010
+ watcher.unwatch(computed);
1011
+ };
1005
1012
  }
1006
1013
  //#endregion
1007
1014
  export { effect, loadSignals };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xaendar/signals",
3
- "version": "0.4.6",
3
+ "version": "0.4.9",
4
4
  "description": "A library implementing a reactive system for Xaendar framework. This signal implementation is based on the TC39 Stage1 proposal for reactive primitives.",
5
5
  "sideEffects": false,
6
6
  "type": "module",
@@ -16,7 +16,7 @@
16
16
  }
17
17
  },
18
18
  "dependencies": {
19
- "@xaendar/common": "0.4.6",
20
- "@xaendar/types": "0.4.6"
19
+ "@xaendar/common": "0.4.9",
20
+ "@xaendar/types": "0.4.9"
21
21
  }
22
22
  }