watch-state 3.5.2 → 3.6.1

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.
package/State/State.d.ts CHANGED
@@ -17,8 +17,21 @@ import { Observable } from '../Observable';
17
17
  * // Update value
18
18
  * count.value++ // logs: 1
19
19
  */
20
- export declare class State<V = unknown> extends Observable<V> {
21
- constructor(initial?: V);
20
+ export declare class State<T = unknown> extends Observable<T> {
21
+ /** Current value. No auto-subscription on direct access (unlike `value`). */
22
+ raw: T;
23
+ /**
24
+ * Initial state value set during construction.
25
+ * Used by `reset()` to restore state to its original value.
26
+ * Allows checking if the state has been modified.
27
+ *
28
+ * @example
29
+ * const count = new State(0)
30
+ *
31
+ * const isChanged = count.initial === count.raw
32
+ */
33
+ readonly initial: T;
34
+ constructor(...args: T extends undefined ? [T?] : [T]);
22
35
  /**
23
36
  * Current state value. Updates watchers only on actual changes (strict `!==`).
24
37
  * Using `value` inside a `Watch` callback automatically subscribes to changes.
@@ -30,16 +43,29 @@ export declare class State<V = unknown> extends Observable<V> {
30
43
  * count.value = 1 // triggers watchers
31
44
  * count.value = 1 // no trigger
32
45
  */
33
- get value(): V;
34
- set value(value: V);
46
+ get value(): T;
47
+ set value(value: T);
35
48
  /**
36
- * @experimental
37
49
  * Sets the state value. Identical to the `value` setter but returns `void`.
38
50
  * Useful as a shorthand in arrow functions: `() => state.set(value)` instead of `() => { state.value = value }`
39
51
  *
40
52
  * `state.set` cannot be used as a standalone function: `const set = state.set`
41
53
  */
42
- set(value: V): void;
54
+ set(value: T): void;
55
+ /**
56
+ * Resets state to its initial value.
57
+ * Triggers watchers only if the current value differs from the initial value.
58
+ *
59
+ * @example
60
+ * const count = new State(0)
61
+ *
62
+ * new Watch(() => console.log(count.value)) // logs: 0
63
+ *
64
+ * count.value = 5 // logs: 5
65
+ *
66
+ * count.reset() // logs: 0
67
+ */
68
+ reset(): void;
43
69
  /**
44
70
  * Force triggers all watchers even if value didn't change.
45
71
  *
@@ -24,7 +24,7 @@ import { queueWatchers } from '../Compute/Compute.es6.js';
24
24
  class State extends Observable {
25
25
  constructor(initial) {
26
26
  super();
27
- this.rawValue = initial;
27
+ this.raw = this.initial = initial;
28
28
  }
29
29
  /**
30
30
  * Current state value. Updates watchers only on actual changes (strict `!==`).
@@ -41,13 +41,12 @@ class State extends Observable {
41
41
  return super.value;
42
42
  }
43
43
  set value(value) {
44
- if (this.rawValue !== value) {
45
- this.rawValue = value;
44
+ if (this.raw !== value) {
45
+ this.raw = value;
46
46
  this.update();
47
47
  }
48
48
  }
49
49
  /**
50
- * @experimental
51
50
  * Sets the state value. Identical to the `value` setter but returns `void`.
52
51
  * Useful as a shorthand in arrow functions: `() => state.set(value)` instead of `() => { state.value = value }`
53
52
  *
@@ -56,6 +55,22 @@ class State extends Observable {
56
55
  set(value) {
57
56
  this.value = value;
58
57
  }
58
+ /**
59
+ * Resets state to its initial value.
60
+ * Triggers watchers only if the current value differs from the initial value.
61
+ *
62
+ * @example
63
+ * const count = new State(0)
64
+ *
65
+ * new Watch(() => console.log(count.value)) // logs: 0
66
+ *
67
+ * count.value = 5 // logs: 5
68
+ *
69
+ * count.reset() // logs: 0
70
+ */
71
+ reset() {
72
+ this.value = this.initial;
73
+ }
59
74
  /**
60
75
  * Force triggers all watchers even if value didn't change.
61
76
  *
package/State/State.js CHANGED
@@ -28,7 +28,7 @@ var Compute = require('../Compute/Compute.js');
28
28
  class State extends Observable.Observable {
29
29
  constructor(initial) {
30
30
  super();
31
- this.rawValue = initial;
31
+ this.raw = this.initial = initial;
32
32
  }
33
33
  /**
34
34
  * Current state value. Updates watchers only on actual changes (strict `!==`).
@@ -45,13 +45,12 @@ class State extends Observable.Observable {
45
45
  return super.value;
46
46
  }
47
47
  set value(value) {
48
- if (this.rawValue !== value) {
49
- this.rawValue = value;
48
+ if (this.raw !== value) {
49
+ this.raw = value;
50
50
  this.update();
51
51
  }
52
52
  }
53
53
  /**
54
- * @experimental
55
54
  * Sets the state value. Identical to the `value` setter but returns `void`.
56
55
  * Useful as a shorthand in arrow functions: `() => state.set(value)` instead of `() => { state.value = value }`
57
56
  *
@@ -60,6 +59,22 @@ class State extends Observable.Observable {
60
59
  set(value) {
61
60
  this.value = value;
62
61
  }
62
+ /**
63
+ * Resets state to its initial value.
64
+ * Triggers watchers only if the current value differs from the initial value.
65
+ *
66
+ * @example
67
+ * const count = new State(0)
68
+ *
69
+ * new Watch(() => console.log(count.value)) // logs: 0
70
+ *
71
+ * count.value = 5 // logs: 5
72
+ *
73
+ * count.reset() // logs: 0
74
+ */
75
+ reset() {
76
+ this.value = this.initial;
77
+ }
63
78
  /**
64
79
  * Force triggers all watchers even if value didn't change.
65
80
  *
package/Watch/Watch.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Destructor, Observer, Watcher } from '../types';
1
+ import type { Destructor, Observer, Reaction, Watcher } from '../types';
2
2
  /**
3
3
  * Watcher class for reactive state tracking.
4
4
  * Executes callback function when observed states change.
@@ -15,16 +15,24 @@ import type { Destructor, Observer, Watcher } from '../types';
15
15
  * count.value = 1 // triggers watcher callback
16
16
  */
17
17
  export declare class Watch implements Observer {
18
- readonly watcher: Watcher<void>;
18
+ readonly reaction: Watcher<void> | Reaction<void>;
19
19
  /** Whether the watcher has been destroyed */
20
20
  destroyed: boolean;
21
+ /** Tracks if the computation has run at least once. */
22
+ updated: boolean;
21
23
  /** Cleanup functions to run when watcher is destroyed */
22
24
  readonly destructors: Set<Destructor>;
23
25
  /** Child observers created within this watcher's scope */
24
- readonly childrenObservers: Set<Observer>;
26
+ readonly children: Set<Observer>;
27
+ /** @deprecated Use `children` */
28
+ get childrenObservers(): Set<Observer>;
25
29
  /** @deprecated Use `childrenObservers` */
26
30
  get childWatchers(): Set<Observer>;
27
- constructor(watcher: Watcher<void>, freeParent?: boolean, freeUpdate?: boolean);
31
+ /** @deprecated Use `reaction` */
32
+ get watcher(): Watcher<void> | Reaction<void>;
33
+ constructor(reaction: Reaction<void>, freeParent?: boolean, freeUpdate?: boolean);
34
+ /** @deprecated `update` argument is deprecated, use `Reaction` */
35
+ constructor(reaction: Watcher<void>, freeParent?: boolean, freeUpdate?: boolean);
28
36
  /** Destroy watcher and cleanup all dependencies */
29
37
  destroy(): void;
30
38
  /** Force watcher update regardless of state changes */
@@ -1,7 +1,7 @@
1
1
  import '../helpers/index.es6.js';
2
2
  import { bindObserver } from '../helpers/bindObserver/bindObserver.es6.js';
3
- import { watchWithScope } from '../helpers/watchWithScope/watchWithScope.es6.js';
4
3
  import { destroyWatchers } from '../helpers/destroyWatchers/destroyWatchers.es6.js';
4
+ import { watchWithScope } from '../helpers/watchWithScope/watchWithScope.es6.js';
5
5
 
6
6
  /**
7
7
  * Watcher class for reactive state tracking.
@@ -19,26 +19,36 @@ import { destroyWatchers } from '../helpers/destroyWatchers/destroyWatchers.es6.
19
19
  * count.value = 1 // triggers watcher callback
20
20
  */
21
21
  class Watch {
22
+ // TODO: remove in major release
23
+ /** @deprecated Use `children` */
24
+ get childrenObservers() {
25
+ return this.children;
26
+ }
22
27
  // TODO: remove in major release
23
28
  /** @deprecated Use `childrenObservers` */
24
29
  get childWatchers() {
25
- return this.childrenObservers;
30
+ return this.children;
26
31
  }
27
- constructor(watcher, freeParent, freeUpdate) {
28
- this.watcher = watcher;
32
+ // TODO: remove in major release
33
+ /** @deprecated Use `reaction` */
34
+ get watcher() {
35
+ return this.reaction;
36
+ }
37
+ constructor(reaction, freeParent, freeUpdate) {
38
+ this.reaction = reaction;
29
39
  /** Whether the watcher has been destroyed */
30
40
  this.destroyed = false;
41
+ /** Tracks if the computation has run at least once. */
42
+ this.updated = false;
31
43
  /** Cleanup functions to run when watcher is destroyed */
32
44
  this.destructors = new Set();
33
45
  /** Child observers created within this watcher's scope */
34
- this.childrenObservers = new Set();
46
+ this.children = new Set();
35
47
  if (!freeParent) {
36
48
  bindObserver(this);
37
49
  }
38
50
  if (!freeUpdate) {
39
- watchWithScope(this, () => {
40
- watcher(false);
41
- });
51
+ this.update();
42
52
  }
43
53
  }
44
54
  /** Destroy watcher and cleanup all dependencies */
@@ -49,7 +59,8 @@ class Watch {
49
59
  update() {
50
60
  if (!this.destroyed) {
51
61
  watchWithScope(this, () => {
52
- this.watcher(true);
62
+ this.reaction(this.updated); // TODO: remove `this.updated` in major release
63
+ this.updated = true;
53
64
  });
54
65
  }
55
66
  }
package/Watch/Watch.js CHANGED
@@ -4,8 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  require('../helpers/index.js');
6
6
  var bindObserver = require('../helpers/bindObserver/bindObserver.js');
7
- var watchWithScope = require('../helpers/watchWithScope/watchWithScope.js');
8
7
  var destroyWatchers = require('../helpers/destroyWatchers/destroyWatchers.js');
8
+ var watchWithScope = require('../helpers/watchWithScope/watchWithScope.js');
9
9
 
10
10
  /**
11
11
  * Watcher class for reactive state tracking.
@@ -23,26 +23,36 @@ var destroyWatchers = require('../helpers/destroyWatchers/destroyWatchers.js');
23
23
  * count.value = 1 // triggers watcher callback
24
24
  */
25
25
  class Watch {
26
+ // TODO: remove in major release
27
+ /** @deprecated Use `children` */
28
+ get childrenObservers() {
29
+ return this.children;
30
+ }
26
31
  // TODO: remove in major release
27
32
  /** @deprecated Use `childrenObservers` */
28
33
  get childWatchers() {
29
- return this.childrenObservers;
34
+ return this.children;
30
35
  }
31
- constructor(watcher, freeParent, freeUpdate) {
32
- this.watcher = watcher;
36
+ // TODO: remove in major release
37
+ /** @deprecated Use `reaction` */
38
+ get watcher() {
39
+ return this.reaction;
40
+ }
41
+ constructor(reaction, freeParent, freeUpdate) {
42
+ this.reaction = reaction;
33
43
  /** Whether the watcher has been destroyed */
34
44
  this.destroyed = false;
45
+ /** Tracks if the computation has run at least once. */
46
+ this.updated = false;
35
47
  /** Cleanup functions to run when watcher is destroyed */
36
48
  this.destructors = new Set();
37
49
  /** Child observers created within this watcher's scope */
38
- this.childrenObservers = new Set();
50
+ this.children = new Set();
39
51
  if (!freeParent) {
40
52
  bindObserver.bindObserver(this);
41
53
  }
42
54
  if (!freeUpdate) {
43
- watchWithScope.watchWithScope(this, () => {
44
- watcher(false);
45
- });
55
+ this.update();
46
56
  }
47
57
  }
48
58
  /** Destroy watcher and cleanup all dependencies */
@@ -53,7 +63,8 @@ class Watch {
53
63
  update() {
54
64
  if (!this.destroyed) {
55
65
  watchWithScope.watchWithScope(this, () => {
56
- this.watcher(true);
66
+ this.reaction(this.updated); // TODO: remove `this.updated` in major release
67
+ this.updated = true;
57
68
  });
58
69
  }
59
70
  }
@@ -3,9 +3,9 @@ import { scope } from '../../constants.es6.js';
3
3
  function bindObserver(observer) {
4
4
  const { activeWatcher } = scope;
5
5
  if (activeWatcher) {
6
- activeWatcher.childrenObservers.add(observer);
6
+ activeWatcher.children.add(observer);
7
7
  activeWatcher.destructors.add(() => {
8
- activeWatcher.childrenObservers.delete(observer);
8
+ activeWatcher.children.delete(observer);
9
9
  });
10
10
  }
11
11
  }
@@ -7,9 +7,9 @@ var constants = require('../../constants.js');
7
7
  function bindObserver(observer) {
8
8
  const { activeWatcher } = constants.scope;
9
9
  if (activeWatcher) {
10
- activeWatcher.childrenObservers.add(observer);
10
+ activeWatcher.children.add(observer);
11
11
  activeWatcher.destructors.add(() => {
12
- activeWatcher.childrenObservers.delete(observer);
12
+ activeWatcher.children.delete(observer);
13
13
  });
14
14
  }
15
15
  }
@@ -9,7 +9,7 @@ function clearWatcher(watcher) {
9
9
  if (skipLoop)
10
10
  return;
11
11
  while ((currentWatcher = clearStack.shift())) {
12
- currentWatcher.childrenObservers.forEach(destroyWatchers);
12
+ currentWatcher.children.forEach(destroyWatchers);
13
13
  for (const destructor of currentWatcher.destructors) {
14
14
  currentWatcher.destructors.delete(destructor);
15
15
  destructor();
@@ -13,7 +13,7 @@ function clearWatcher(watcher) {
13
13
  if (skipLoop)
14
14
  return;
15
15
  while ((currentWatcher = clearStack.shift())) {
16
- currentWatcher.childrenObservers.forEach(destroyWatchers.destroyWatchers);
16
+ currentWatcher.children.forEach(destroyWatchers.destroyWatchers);
17
17
  for (const destructor of currentWatcher.destructors) {
18
18
  currentWatcher.destructors.delete(destructor);
19
19
  destructor();
@@ -6,7 +6,7 @@ function destroyWatchers(observer) {
6
6
  if (skipLoop)
7
7
  return;
8
8
  while ((currentWatcher = destroyStack.shift())) {
9
- currentWatcher.childrenObservers.forEach(observer => {
9
+ currentWatcher.children.forEach(observer => {
10
10
  destroyStack.push(observer);
11
11
  });
12
12
  for (const destructor of currentWatcher.destructors) {
@@ -10,7 +10,7 @@ function destroyWatchers(observer) {
10
10
  if (skipLoop)
11
11
  return;
12
12
  while ((currentWatcher = destroyStack.shift())) {
13
- currentWatcher.childrenObservers.forEach(observer => {
13
+ currentWatcher.children.forEach(observer => {
14
14
  destroyStack.push(observer);
15
15
  });
16
16
  for (const destructor of currentWatcher.destructors) {
package/index.min.js CHANGED
@@ -1 +1 @@
1
- var WatchState=function(e){"use strict";const t={activeWatcher:void 0,eventDeep:0};function s(e){const{activeWatcher:s}=t;s&&(s.childrenObservers.add(e),s.destructors.add((()=>{s.childrenObservers.delete(e)})))}const r=[];let c;function n(e){const t=Boolean(r.length);if(r.push(e),!t)for(;c=r.shift();){c.childrenObservers.forEach((e=>{r.push(e)}));for(const e of c.destructors)c.destructors.delete(e),e();c.destroyed=!0}}const i=[];let a,o,h,u;function d(e){const t=Boolean(i.length);if(i.push(e),!t)for(;a=i.shift();){a.childrenObservers.forEach(n);for(const e of a.destructors)a.destructors.delete(e),e()}}function l(e,s){const r=t.activeWatcher;t.activeWatcher=e,s(),t.activeWatcher=r}class v{constructor(){this.observers=new Set}get value(){const{activeWatcher:e}=t;return e&&(this.observers.add(e),e.destructors.add((()=>{this.observers.delete(e)}))),this.rawValue}}function f(e){if(!e.size)return;const t=e.values().next().value;return e.delete(t),t}const p=new Set,b=new Set;function w(){if(!u){for(u=!0;(o=f(p))||(h=f(b));)o?o.invalid=!0:(d(h),h.update());u=!1}}function W(e){const s=!t.eventDeep&&!b.size&&!p.size,r=[...b];b.clear(),e.forEach((e=>{b.add(e),e instanceof D&&p.add(e)})),r.forEach((e=>b.add(e))),s&&w()}const y=[];let O;function S(e){const t=y.length;if(y.push(e),!t)for(;O=y.shift();)O instanceof D&&(y.push(...O.observers),O.invalid=!0)}class D extends v{get childWatchers(){return this.childrenObservers}constructor(e,t,r){super(),this.invalid=!0,this.updated=!1,this.destroyed=!1,this.isCache=!0,this.destructors=new Set,this.childrenObservers=new Set,this.watcher=e,t||s(this),r&&this.forceUpdate()}update(){S(this);const e=[...this.observers];let t;for(;t=e.pop();){if(!(t instanceof D))return this.forceUpdate();e.push(...t.observers)}}forceUpdate(){this.destroyed||(this.invalid=!1,l(this,(()=>{const e=this.watcher(!!this.updated&&(this.updated=!0));e!==this.rawValue&&(this.rawValue=e,W(this.observers))})))}get value(){return this.invalid&&this.forceUpdate(),this.destroyed?this.rawValue:super.value}destroy(){n(this)}}function g(e){const{activeWatcher:s}=t;t.activeWatcher=void 0;const r=e();return t.activeWatcher=s,r}return e.Cache=class extends D{},e.Compute=D,e.Observable=v,e.State=class extends v{constructor(e){super(),this.rawValue=e}get value(){return super.value}set value(e){this.rawValue!==e&&(this.rawValue=e,this.update())}set(e){this.value=e}update(){W(this.observers)}},e.Watch=class{get childWatchers(){return this.childrenObservers}constructor(e,t,r){this.watcher=e,this.destroyed=!1,this.destructors=new Set,this.childrenObservers=new Set,t||s(this),r||l(this,(()=>{e(!1)}))}destroy(){n(this)}update(){this.destroyed||l(this,(()=>{this.watcher(!0)}))}},e.bindObserver=s,e.callEvent=function(e){const s=g((()=>{t.eventDeep++;const s=e();return t.eventDeep--,s}));return t.eventDeep||w(),s},e.clearWatcher=d,e.createEvent=function(e){return function(...s){const r=g((()=>{t.eventDeep++;const r=e.apply(this,s);return t.eventDeep--,r}));return t.eventDeep||w(),r}},e.destroyWatchers=n,e.forceQueueWatchers=w,e.invalidateCache=function(e){S(e)},e.invalidateCompute=S,e.onDestroy=function(e){t.activeWatcher&&t.activeWatcher.destructors.add(e)},e.queueWatchers=W,e.scope=t,e.shiftSet=f,e.unwatch=g,e.watchWithScope=l,Object.defineProperty(e,"__esModule",{value:!0}),e}({});
1
+ var WatchState=function(e){"use strict";const t={activeWatcher:void 0,eventDeep:0};function s(e){const{activeWatcher:s}=t;s&&(s.children.add(e),s.destructors.add((()=>{s.children.delete(e)})))}const r=[];let i;function n(e){const t=Boolean(r.length);if(r.push(e),!t)for(;i=r.shift();){i.children.forEach((e=>{r.push(e)}));for(const e of i.destructors)i.destructors.delete(e),e();i.destroyed=!0}}const c=[];let a,h,o,d;function u(e){const t=Boolean(c.length);if(c.push(e),!t)for(;a=c.shift();){a.children.forEach(n);for(const e of a.destructors)a.destructors.delete(e),e()}}function l(e,s){const r=t.activeWatcher;t.activeWatcher=e,s(),t.activeWatcher=r}class v{constructor(){this.observers=new Set}get rawValue(){return this.raw}set rawValue(e){this.raw=e}get value(){const{activeWatcher:e}=t;return e&&(this.observers.add(e),e.destructors.add((()=>{this.observers.delete(e)}))),this.raw}}function f(e){if(!e.size)return;const t=e.values().next().value;return e.delete(t),t}const p=new Set,w=new Set;function W(){if(!d){for(d=!0;(h=f(p))||(o=f(w));)h?h.invalid=!0:(u(o),o.update());d=!1}}function b(e){const s=!t.eventDeep&&!w.size&&!p.size,r=[...w];w.clear(),e.forEach((e=>{w.add(e),e instanceof D&&p.add(e)})),r.forEach((e=>w.add(e))),s&&W()}const g=[];let y;function S(e){const t=g.length;if(g.push(e),!t)for(;y=g.shift();)y instanceof D&&(g.push(...y.observers),y.invalid=!0)}class D extends v{get childrenObservers(){return this.children}get childWatchers(){return this.children}get watcher(){return this.reaction}constructor(e,t,r){super(),this.reaction=e,this.invalid=!0,this.updated=!1,this.destroyed=!1,this.isCache=!0,this.destructors=new Set,this.children=new Set,t||s(this),r&&this.forceUpdate()}update(){S(this);const e=[...this.observers];let t;for(;t=e.pop();){if(!(t instanceof D))return this.forceUpdate();e.push(...t.observers)}}forceUpdate(){this.destroyed||(this.invalid=!1,l(this,(()=>{const e=this.reaction(this.updated);this.updated=!0,e!==this.raw&&(this.raw=e,b(this.observers))})))}get value(){return this.invalid&&this.forceUpdate(),this.destroyed?this.raw:super.value}destroy(){n(this)}}function E(e){const{activeWatcher:s}=t;t.activeWatcher=void 0;const r=e();return t.activeWatcher=s,r}return e.Cache=class extends D{},e.Compute=D,e.Observable=v,e.State=class extends v{constructor(e){super(),this.raw=this.initial=e}get value(){return super.value}set value(e){this.raw!==e&&(this.raw=e,this.update())}set(e){this.value=e}reset(){this.value=this.initial}update(){b(this.observers)}},e.Watch=class{get childrenObservers(){return this.children}get childWatchers(){return this.children}get watcher(){return this.reaction}constructor(e,t,r){this.reaction=e,this.destroyed=!1,this.updated=!1,this.destructors=new Set,this.children=new Set,t||s(this),r||this.update()}destroy(){n(this)}update(){this.destroyed||l(this,(()=>{this.reaction(this.updated),this.updated=!0}))}},e.bindObserver=s,e.callEvent=function(e){const s=E((()=>{t.eventDeep++;const s=e();return t.eventDeep--,s}));return t.eventDeep||W(),s},e.clearWatcher=u,e.createEvent=function(e){return function(...s){const r=E((()=>{t.eventDeep++;const r=e.apply(this,s);return t.eventDeep--,r}));return t.eventDeep||W(),r}},e.destroyWatchers=n,e.forceQueueWatchers=W,e.invalidateCache=function(e){S(e)},e.invalidateCompute=S,e.onDestroy=function(e){t.activeWatcher&&t.activeWatcher.destructors.add(e)},e.queueWatchers=b,e.scope=t,e.shiftSet=f,e.unwatch=E,e.watchWithScope=l,Object.defineProperty(e,"__esModule",{value:!0}),e}({});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "watch-state",
3
- "version": "3.5.2",
3
+ "version": "3.6.1",
4
4
  "description": "CANT inc. state management system.",
5
5
  "author": "Mikhail Lysikov <d8corp@mail.ru>",
6
6
  "license": "MIT",
@@ -36,7 +36,7 @@
36
36
  "test:jest": "jest",
37
37
  "test:coverage": "jest --coverage",
38
38
  "test:size": "size-limit",
39
- "speed": "innetjs run speed.test.ts",
39
+ "speed": "innetjs run speed.test.ts -c tsconfig.speed.json",
40
40
  "release": "innetjs release -p -m",
41
41
  "patch": "innetjs patch",
42
42
  "minor": "innetjs minor",
@@ -51,7 +51,7 @@
51
51
  "import": {
52
52
  "./release/index.es6.js": "*"
53
53
  },
54
- "limit": "1.07 kB"
54
+ "limit": "1095 B"
55
55
  },
56
56
  {
57
57
  "name": "Core",
@@ -59,7 +59,7 @@
59
59
  "import": {
60
60
  "./release/index.es6.js": "{ Watch, State, Compute, createEvent }"
61
61
  },
62
- "limit": "850 B"
62
+ "limit": "878 B"
63
63
  }
64
64
  ]
65
65
  }
package/types.d.ts CHANGED
@@ -6,18 +6,22 @@ export interface Observer {
6
6
  * Child observers created within this observer's scope.
7
7
  * Used for hierarchical cleanup.
8
8
  */
9
- childrenObservers: Set<Observer>;
9
+ children: Set<Observer>;
10
10
  /** Cleanup functions to run on destroy (e.g., unsubscribes). */
11
11
  destructors: Set<Destructor>;
12
12
  /** Stop observation and remove all dependencies. */
13
13
  destroy: () => void;
14
14
  /** Force re-run of the observer's logic. */
15
15
  update: () => void;
16
+ /** Tracks if the computation has run at least once. */
17
+ updated: boolean;
16
18
  /**
17
19
  * Indicates if observer has been destroyed.
18
20
  * Prevents accidental use after cleanup.
19
21
  */
20
22
  destroyed: boolean;
23
+ /** @deprecated Use `children` */
24
+ childrenObservers: Set<Observer>;
21
25
  /** @deprecated Use `childrenObservers` */
22
26
  childWatchers: Set<Observer>;
23
27
  /** @deprecated Use `observer instanceof Compute` */
@@ -30,5 +34,11 @@ export interface Scope {
30
34
  /** Current nesting depth of events */
31
35
  eventDeep: number;
32
36
  }
33
- /** Watcher callback signature. First call: `update=false`, updates: `update=true`. */
37
+ /** @deprecated `update` argument is deprecated, use `Reaction` */
34
38
  export type Watcher<T> = (update: boolean) => T;
39
+ /**
40
+ * A reactive function that tracks dependencies and can to derives a value.
41
+ * Used in `Watch` for side effects and in `Compute` for memoized values.
42
+ * @template T The type of the derived value
43
+ */
44
+ export type Reaction<T> = () => T;
@@ -67,6 +67,7 @@ function createEvent(callback) {
67
67
  return function (...args) {
68
68
  const result = unwatch(() => {
69
69
  scope.eventDeep++;
70
+ // @ts-expect-error this
70
71
  const result = callback.apply(this, args);
71
72
  scope.eventDeep--;
72
73
  return result;
@@ -71,6 +71,7 @@ function createEvent(callback) {
71
71
  return function (...args) {
72
72
  const result = unwatch.unwatch(() => {
73
73
  constants.scope.eventDeep++;
74
+ // @ts-expect-error this
74
75
  const result = callback.apply(this, args);
75
76
  constants.scope.eventDeep--;
76
77
  return result;