async-reactivity 2.0.28 → 2.0.29

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/lib/ref.js CHANGED
@@ -3,14 +3,14 @@ export default class Ref {
3
3
  isEqual;
4
4
  dependents = new Set();
5
5
  _value;
6
- constructor(_value, isEqual = (defaultIsEqual)) {
7
- this._value = _value;
6
+ constructor(value, isEqual = (defaultIsEqual)) {
7
+ this._value = value;
8
8
  this.isEqual = isEqual;
9
9
  }
10
- set value(_value) {
10
+ set value(value) {
11
11
  const oldValue = this._value;
12
- this._value = _value;
13
- if (!this.isEqual(_value, oldValue)) {
12
+ this._value = value;
13
+ if (this.dependents.size > 0 && !this.isEqual(value, oldValue)) {
14
14
  this.invalidate();
15
15
  }
16
16
  }
package/lib/ref.test.js CHANGED
@@ -11,11 +11,15 @@ describe('ref', function () {
11
11
  a.value = 4;
12
12
  assert.strictEqual(a.value, 4);
13
13
  });
14
- it('setter isEqual', function () {
14
+ it('should not invalidate when isEqual returns true', function () {
15
15
  const a = new Ref(5, () => true);
16
16
  new Watcher(a, () => {
17
17
  assert.fail();
18
18
  }, false);
19
19
  a.value = 4;
20
20
  });
21
+ it('should not call isEqual when there are no dependents', function () {
22
+ const a = new Ref(5, () => assert.fail());
23
+ a.value = 4;
24
+ });
21
25
  });
package/lib/tracker.js ADDED
@@ -0,0 +1,18 @@
1
+ export default class Tracker {
2
+ dependents = new Set();
3
+ _value;
4
+ addDependent(dependent) {
5
+ this.dependents.add(dependent);
6
+ }
7
+ removeDependent(dependent) {
8
+ this.dependents.delete(dependent);
9
+ }
10
+ invalidate() {
11
+ for (const dependent of [...this.dependents.keys()]) {
12
+ dependent.invalidate(this);
13
+ }
14
+ }
15
+ get value() {
16
+ return this._value;
17
+ }
18
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-reactivity",
3
- "version": "2.0.28",
3
+ "version": "2.0.29",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",
package/src/ref.test.ts CHANGED
@@ -14,11 +14,16 @@ describe('ref', function () {
14
14
  assert.strictEqual(a.value, 4);
15
15
  });
16
16
 
17
- it('setter isEqual', function () {
17
+ it('should not invalidate when isEqual returns true', function () {
18
18
  const a = new Ref(5, () => true);
19
19
  new Watcher(a, () => {
20
20
  assert.fail();
21
21
  }, false);
22
22
  a.value = 4;
23
23
  });
24
+
25
+ it('should not call isEqual when there are no dependents', function () {
26
+ const a = new Ref(5, () => assert.fail());
27
+ a.value = 4;
28
+ });
24
29
  });
package/src/ref.ts CHANGED
@@ -7,15 +7,15 @@ export default class Ref<T> implements Dependency<T> {
7
7
  protected dependents = new Set<Dependent>();
8
8
  protected _value?: T;
9
9
 
10
- constructor(_value: T, isEqual = defaultIsEqual<T>) {
11
- this._value = _value;
10
+ constructor(value: T, isEqual = defaultIsEqual<T>) {
11
+ this._value = value;
12
12
  this.isEqual = isEqual;
13
13
  }
14
14
 
15
- public set value(_value: T) {
15
+ public set value(value: T) {
16
16
  const oldValue = this._value!;
17
- this._value = _value;
18
- if (!this.isEqual(_value, oldValue)) {
17
+ this._value = value;
18
+ if (this.dependents.size > 0 && !this.isEqual(value, oldValue)) {
19
19
  this.invalidate();
20
20
  }
21
21
  }
package/types/ref.d.ts CHANGED
@@ -4,8 +4,8 @@ export default class Ref<T> implements Dependency<T> {
4
4
  private isEqual;
5
5
  protected dependents: Set<Dependent>;
6
6
  protected _value?: T;
7
- constructor(_value: T, isEqual?: (v1: T, v2: T) => boolean);
8
- set value(_value: T);
7
+ constructor(value: T, isEqual?: (v1: T, v2: T) => boolean);
8
+ set value(value: T);
9
9
  get value(): T;
10
10
  addDependent(dependent: Dependent): void;
11
11
  removeDependent(dependent: Dependent): void;
@@ -1 +1 @@
1
- {"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,OAAO,CAA2B;IAC1C,SAAS,CAAC,UAAU,iBAAwB;IAC5C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAET,MAAM,EAAE,CAAC,EAAE,OAAO,4BAAoB;IAKlD,IAAW,KAAK,CAAC,MAAM,EAAE,CAAC,EAMzB;IAED,IAAW,KAAK,IAAI,CAAC,CAEpB;IAEM,YAAY,CAAC,SAAS,EAAE,SAAS;IAIjC,eAAe,CAAC,SAAS,EAAE,SAAS;IAIpC,UAAU,IAAI,IAAI;CAK5B"}
1
+ {"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;IAChD,OAAO,CAAC,OAAO,CAA2B;IAC1C,SAAS,CAAC,UAAU,iBAAwB;IAC5C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;gBAET,KAAK,EAAE,CAAC,EAAE,OAAO,4BAAoB;IAKjD,IAAW,KAAK,CAAC,KAAK,EAAE,CAAC,EAMxB;IAED,IAAW,KAAK,IAAI,CAAC,CAEpB;IAEM,YAAY,CAAC,SAAS,EAAE,SAAS;IAIjC,eAAe,CAAC,SAAS,EAAE,SAAS;IAIpC,UAAU,IAAI,IAAI;CAK5B"}
@@ -0,0 +1,10 @@
1
+ import Dependent from "./dependent.js";
2
+ export default class Tracker<T> {
3
+ protected dependents: Set<Dependent>;
4
+ protected _value?: T;
5
+ addDependent(dependent: Dependent): void;
6
+ removeDependent(dependent: Dependent): void;
7
+ invalidate(): void;
8
+ get value(): T | undefined;
9
+ }
10
+ //# sourceMappingURL=tracker.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../src/tracker.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,MAAM,CAAC,OAAO,OAAO,OAAO,CAAC,CAAC;IAC1B,SAAS,CAAC,UAAU,iBAAwB;IAC5C,SAAS,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAEd,YAAY,CAAC,SAAS,EAAE,SAAS;IAIjC,eAAe,CAAC,SAAS,EAAE,SAAS;IAIpC,UAAU,IAAI,IAAI;IAMzB,IAAW,KAAK,kBAEf;CACJ"}