async-reactivity 2.0.1 → 2.0.2

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.
@@ -0,0 +1,245 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import 'mocha';
11
+ import assert from 'assert';
12
+ import { Computed, Ref } from './index.js';
13
+ describe('computed', function () {
14
+ it('lazy compute', function () {
15
+ let gate = false;
16
+ const a = new Computed(() => {
17
+ gate = true;
18
+ return 5;
19
+ });
20
+ assert.strictEqual(gate, false);
21
+ assert.strictEqual(a.value, 5);
22
+ assert.strictEqual(gate, true);
23
+ });
24
+ it('cache', function () {
25
+ let gate = false;
26
+ const a = new Computed(() => {
27
+ gate = true;
28
+ return 5;
29
+ });
30
+ assert.strictEqual(a.value, 5);
31
+ gate = false;
32
+ assert.strictEqual(a.value, 5);
33
+ assert.strictEqual(gate, false);
34
+ });
35
+ it('invalidate dependents', function () {
36
+ const a = new Ref(5);
37
+ const b = new Computed((value) => {
38
+ return value(a) + 4;
39
+ });
40
+ assert.strictEqual(b.value, 9);
41
+ a.value = 6;
42
+ assert.strictEqual(b.value, 10);
43
+ });
44
+ it('dependents up-to-date', function () {
45
+ const a = new Ref(5);
46
+ const b = new Ref(10);
47
+ let gate;
48
+ const c = new Computed((value) => {
49
+ gate = true;
50
+ return value(a) < 10 ? value(b) : 0;
51
+ });
52
+ assert.strictEqual(c.value, 10);
53
+ a.value = 15;
54
+ assert.strictEqual(c.value, 0);
55
+ b.value = 15;
56
+ gate = false;
57
+ assert.strictEqual(c.value, 0);
58
+ assert.strictEqual(gate, false);
59
+ });
60
+ it('detect circular dependency', function () {
61
+ // @ts-expect-error
62
+ const a = new Computed((value) => {
63
+ return value(b);
64
+ });
65
+ // @ts-expect-error
66
+ const b = new Computed((value) => {
67
+ return value(a);
68
+ });
69
+ assert.throws(() => a.value);
70
+ });
71
+ xit('detect circular deeper dependency', function () {
72
+ // do not support for better performance
73
+ assert.fail('not implemented');
74
+ });
75
+ it('throw error', function () {
76
+ const a = new Computed(() => {
77
+ throw new Error();
78
+ });
79
+ assert.throws(() => a.value);
80
+ });
81
+ it('ignore same ref value', function () {
82
+ let gate = 0;
83
+ const a = new Ref(5);
84
+ const b = new Computed((value) => {
85
+ gate++;
86
+ return value(a);
87
+ });
88
+ assert.strictEqual(b.value, 5);
89
+ a.value = 5;
90
+ assert.strictEqual(b.value, 5);
91
+ assert.strictEqual(gate, 1);
92
+ });
93
+ it('ignore same computed value', function () {
94
+ let gate = 0;
95
+ const a = new Ref(5);
96
+ const b = new Computed((value) => {
97
+ return value(a) % 2;
98
+ });
99
+ const c = new Computed((value) => {
100
+ gate++;
101
+ return value(b) + 5;
102
+ });
103
+ assert.strictEqual(c.value, 6);
104
+ a.value = 7;
105
+ assert.strictEqual(c.value, 6);
106
+ assert.strictEqual(gate, 1);
107
+ });
108
+ });
109
+ describe('async computed', function () {
110
+ it('getter', function () {
111
+ return __awaiter(this, void 0, void 0, function* () {
112
+ const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
113
+ yield new Promise(resolve => setTimeout(resolve));
114
+ return 5;
115
+ }));
116
+ assert.strictEqual(yield a.value, 5);
117
+ });
118
+ });
119
+ it('tracks async dependencies', function () {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ const a = new Ref(5);
122
+ const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
123
+ yield new Promise(resolve => setTimeout(resolve));
124
+ return value(a) + 5;
125
+ }));
126
+ assert.strictEqual(yield b.value, 10);
127
+ a.value = 6;
128
+ assert.strictEqual(yield b.value, 11);
129
+ });
130
+ });
131
+ it('get value while computing', function () {
132
+ return __awaiter(this, void 0, void 0, function* () {
133
+ const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
134
+ yield new Promise(resolve => setTimeout(resolve));
135
+ return 5;
136
+ }));
137
+ a.value;
138
+ assert.strictEqual(yield a.value, 5);
139
+ });
140
+ });
141
+ it('detect circular dependency', function () {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ // @ts-expect-error
144
+ const a = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
145
+ yield new Promise(resolve => setTimeout(resolve));
146
+ return value(b);
147
+ }));
148
+ // @ts-expect-error
149
+ const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
150
+ yield new Promise(resolve => setTimeout(resolve));
151
+ return value(a);
152
+ }));
153
+ assert.rejects(() => __awaiter(this, void 0, void 0, function* () { return yield a.value; }));
154
+ });
155
+ });
156
+ it('dependency changed while computing', function () {
157
+ return __awaiter(this, void 0, void 0, function* () {
158
+ const a = new Ref(5);
159
+ const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () { return value(a) + 5; }));
160
+ b.value; // trigger compute
161
+ a.value = 8;
162
+ assert.strictEqual(yield b.value, 13);
163
+ });
164
+ });
165
+ it('old dependency changed while computing', function () {
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ let gate = 0;
168
+ const a = new Ref(5);
169
+ const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
170
+ gate++;
171
+ yield new Promise(resolve => setTimeout(resolve));
172
+ return value(a) + 2;
173
+ }));
174
+ assert.strictEqual(yield b.value, 7);
175
+ b.invalidate();
176
+ const promise = b.value;
177
+ a.value = 6;
178
+ assert.strictEqual(yield promise, 8);
179
+ assert.strictEqual(gate, 2);
180
+ });
181
+ });
182
+ it('new dependency changed while computing', function () {
183
+ return __awaiter(this, void 0, void 0, function* () {
184
+ let gate = 0;
185
+ const a = new Ref(5);
186
+ const b = new Ref(10);
187
+ const c = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
188
+ gate++;
189
+ yield new Promise(resolve => setTimeout(resolve, 50));
190
+ let sum = value(a);
191
+ yield new Promise(resolve => setTimeout(resolve, 50));
192
+ sum += value(b);
193
+ return sum;
194
+ }));
195
+ assert.strictEqual(yield c.value, 15);
196
+ c.invalidate();
197
+ const promise = c.value;
198
+ yield new Promise(resolve => setTimeout(resolve, 60));
199
+ a.value = 10;
200
+ assert.strictEqual(yield promise, 20);
201
+ assert.strictEqual(gate, 3);
202
+ });
203
+ });
204
+ it('fallback to primitive while computing', function () {
205
+ return __awaiter(this, void 0, void 0, function* () {
206
+ const a = new Ref(5);
207
+ const b = new Ref(10);
208
+ const c = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
209
+ if (value(a) < 10) {
210
+ yield new Promise(resolve => setTimeout(resolve, 50));
211
+ return value(b) + 5;
212
+ }
213
+ return 2;
214
+ }));
215
+ const promise = c.value;
216
+ yield new Promise(resolve => setTimeout(resolve, 20));
217
+ a.value = 10;
218
+ assert.strictEqual(yield promise, 2);
219
+ });
220
+ });
221
+ it('throw error', function () {
222
+ return __awaiter(this, void 0, void 0, function* () {
223
+ const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
224
+ yield new Promise(resolve => setTimeout(resolve));
225
+ throw new Error();
226
+ }));
227
+ assert.rejects(() => a.value);
228
+ });
229
+ });
230
+ it('dispose computed', function () {
231
+ return __awaiter(this, void 0, void 0, function* () {
232
+ const a = new Ref(5);
233
+ let gate = 0;
234
+ const b = new Computed(value => {
235
+ gate++;
236
+ return value(a) + 2;
237
+ });
238
+ b.value;
239
+ assert.strictEqual(gate, 1);
240
+ b.dispose();
241
+ b.value;
242
+ assert.strictEqual(gate, 2);
243
+ });
244
+ });
245
+ });
package/lib/ref.js CHANGED
@@ -1,13 +1,15 @@
1
1
  import Tracker from "./tracker.js";
2
+ const defaultIsEqual = (v1, v2) => v1 === v2;
2
3
  export default class Ref extends Tracker {
3
- constructor(_value) {
4
+ constructor(_value, isEqual = (defaultIsEqual)) {
4
5
  super();
5
6
  this._value = _value;
7
+ this.isEqual = isEqual;
6
8
  }
7
9
  set value(_value) {
8
10
  const lastValue = this._value;
9
11
  this._value = _value;
10
- if (lastValue !== _value) {
12
+ if (!this.isEqual(lastValue, _value)) {
11
13
  this.invalidate();
12
14
  }
13
15
  }
@@ -0,0 +1,21 @@
1
+ import 'mocha';
2
+ import assert from 'assert';
3
+ import { Ref, Watcher } from './index.js';
4
+ describe('ref', function () {
5
+ it('getter', function () {
6
+ const a = new Ref(5);
7
+ assert.strictEqual(a.value, 5);
8
+ });
9
+ it('setter', function () {
10
+ const a = new Ref(5);
11
+ a.value = 4;
12
+ assert.strictEqual(a.value, 4);
13
+ });
14
+ it('setter isEqual', function () {
15
+ const a = new Ref(5, () => true);
16
+ new Watcher(a, () => {
17
+ assert.fail();
18
+ }, false);
19
+ a.value = 4;
20
+ });
21
+ });
@@ -0,0 +1,73 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import 'mocha';
11
+ import assert from 'assert';
12
+ import { Computed, Ref, Watcher } from './index.js';
13
+ describe('watcher', function () {
14
+ it('sync', function () {
15
+ const a = new Ref(5);
16
+ new Watcher(a, (newValue, oldValue) => {
17
+ assert.strictEqual(oldValue, 5);
18
+ assert.strictEqual(newValue, 6);
19
+ }, false);
20
+ a.value = 6;
21
+ });
22
+ it('async', function () {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ const a = new Computed(() => __awaiter(this, void 0, void 0, function* () {
25
+ yield new Promise(resolve => setTimeout(resolve));
26
+ return 10;
27
+ }));
28
+ const result = yield new Promise(resolve => new Watcher(a, resolve));
29
+ assert.strictEqual(result, 10);
30
+ });
31
+ });
32
+ it('sync cancel', function () {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const a = new Ref(5);
35
+ const b = new Computed((value) => {
36
+ return value(a) % 2;
37
+ });
38
+ let gate = 0;
39
+ new Watcher(b, () => {
40
+ gate++;
41
+ }, false);
42
+ a.value = 6;
43
+ assert.strictEqual(gate, 1);
44
+ a.value = 7;
45
+ a.value = 9;
46
+ a.value = 11;
47
+ assert.strictEqual(gate, 2);
48
+ });
49
+ });
50
+ it('async sync cancel', function () {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const a = new Ref(5);
53
+ const b = new Computed((value) => __awaiter(this, void 0, void 0, function* () {
54
+ new Promise(resolve => setTimeout(resolve));
55
+ return value(a) % 2;
56
+ }));
57
+ let gate = 0;
58
+ new Watcher(b, () => {
59
+ gate++;
60
+ }, false);
61
+ yield new Promise(resolve => setTimeout(resolve, 10));
62
+ a.value = 6;
63
+ yield new Promise(resolve => setTimeout(resolve, 10));
64
+ assert.strictEqual(gate, 1);
65
+ a.value = 7;
66
+ yield new Promise(resolve => setTimeout(resolve, 10));
67
+ assert.strictEqual(gate, 2);
68
+ a.value = 9;
69
+ yield new Promise(resolve => setTimeout(resolve, 10));
70
+ assert.strictEqual(gate, 3);
71
+ });
72
+ });
73
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "async-reactivity",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "description": "",
5
5
  "main": "lib/index.js",
6
6
  "types": "types/index.d.ts",
@@ -22,9 +22,9 @@
22
22
  "author": "Donatas Lučiūnas",
23
23
  "license": "ISC",
24
24
  "devDependencies": {
25
- "@types/mocha": "^10.0.6",
26
- "@types/node": "^20.10.5",
27
- "mocha": "^10.2.0",
28
- "typescript": "^5.3.3"
25
+ "@types/mocha": "^10.0.7",
26
+ "@types/node": "^20.14.10",
27
+ "mocha": "^10.6.0",
28
+ "typescript": "^5.5.3"
29
29
  }
30
30
  }
@@ -0,0 +1,244 @@
1
+ import 'mocha';
2
+ import assert from 'assert';
3
+ import { Computed, Ref } from './index.js';
4
+
5
+ describe('computed', function () {
6
+ it('lazy compute', function () {
7
+ let gate = false;
8
+ const a = new Computed(() => {
9
+ gate = true;
10
+ return 5;
11
+ });
12
+
13
+ assert.strictEqual(gate, false);
14
+ assert.strictEqual(a.value, 5);
15
+ assert.strictEqual(gate, true);
16
+ });
17
+
18
+ it('cache', function () {
19
+ let gate = false;
20
+ const a = new Computed(() => {
21
+ gate = true;
22
+ return 5;
23
+ });
24
+
25
+ assert.strictEqual(a.value, 5);
26
+
27
+ gate = false;
28
+ assert.strictEqual(a.value, 5);
29
+ assert.strictEqual(gate, false);
30
+ });
31
+
32
+ it('invalidate dependents', function () {
33
+ const a = new Ref(5);
34
+ const b = new Computed((value) => {
35
+ return value(a) + 4;
36
+ });
37
+ assert.strictEqual(b.value, 9);
38
+ a.value = 6;
39
+ assert.strictEqual(b.value, 10);
40
+ });
41
+
42
+ it('dependents up-to-date', function () {
43
+ const a = new Ref(5);
44
+ const b = new Ref(10);
45
+ let gate;
46
+ const c = new Computed((value) => {
47
+ gate = true;
48
+ return value(a) < 10 ? value(b) : 0;
49
+ });
50
+ assert.strictEqual(c.value, 10);
51
+ a.value = 15;
52
+ assert.strictEqual(c.value, 0);
53
+ b.value = 15;
54
+ gate = false;
55
+ assert.strictEqual(c.value, 0);
56
+ assert.strictEqual(gate, false);
57
+ });
58
+
59
+ it('detect circular dependency', function () {
60
+ // @ts-expect-error
61
+ const a = new Computed((value) => {
62
+ return value(b);
63
+ });
64
+ // @ts-expect-error
65
+ const b = new Computed((value) => {
66
+ return value(a);
67
+ });
68
+ assert.throws(() => a.value);
69
+ });
70
+
71
+ xit('detect circular deeper dependency', function () {
72
+ // do not support for better performance
73
+ assert.fail('not implemented');
74
+ });
75
+
76
+ it('throw error', function () {
77
+ const a = new Computed(() => {
78
+ throw new Error();
79
+ });
80
+ assert.throws(() => a.value);
81
+ });
82
+
83
+ it('ignore same ref value', function () {
84
+ let gate = 0;
85
+ const a = new Ref(5);
86
+ const b = new Computed((value) => {
87
+ gate++;
88
+ return value(a);
89
+ });
90
+
91
+ assert.strictEqual(b.value, 5);
92
+
93
+ a.value = 5;
94
+ assert.strictEqual(b.value, 5);
95
+ assert.strictEqual(gate, 1);
96
+ });
97
+
98
+ it('ignore same computed value', function () {
99
+ let gate = 0;
100
+ const a = new Ref(5);
101
+ const b = new Computed((value) => {
102
+ return value(a) % 2;
103
+ });
104
+ const c = new Computed((value) => {
105
+ gate++;
106
+ return value(b) + 5;
107
+ });
108
+
109
+ assert.strictEqual(c.value, 6);
110
+
111
+ a.value = 7;
112
+ assert.strictEqual(c.value, 6);
113
+ assert.strictEqual(gate, 1);
114
+ });
115
+ });
116
+
117
+ describe('async computed', function () {
118
+ it('getter', async function () {
119
+ const a = new Computed(async () => {
120
+ await new Promise(resolve => setTimeout(resolve));
121
+ return 5;
122
+ });
123
+ assert.strictEqual(await a.value, 5);
124
+ });
125
+
126
+ it('tracks async dependencies', async function () {
127
+ const a = new Ref(5);
128
+ const b = new Computed(async (value) => {
129
+ await new Promise(resolve => setTimeout(resolve));
130
+ return value(a) + 5;
131
+ });
132
+ assert.strictEqual(await b.value, 10);
133
+ a.value = 6;
134
+ assert.strictEqual(await b.value, 11);
135
+ });
136
+
137
+ it('get value while computing', async function () {
138
+ const a = new Computed(async () => {
139
+ await new Promise(resolve => setTimeout(resolve));
140
+ return 5;
141
+ });
142
+
143
+ a.value;
144
+ assert.strictEqual(await a.value, 5);
145
+ });
146
+
147
+ it('detect circular dependency', async function () {
148
+ // @ts-expect-error
149
+ const a = new Computed(async (value) => {
150
+ await new Promise(resolve => setTimeout(resolve));
151
+ return value(b);
152
+ });
153
+ // @ts-expect-error
154
+ const b = new Computed(async (value) => {
155
+ await new Promise(resolve => setTimeout(resolve));
156
+ return value(a);
157
+ });
158
+
159
+ assert.rejects(async () => await a.value);
160
+ });
161
+
162
+ it('dependency changed while computing', async function () {
163
+ const a = new Ref(5);
164
+ const b = new Computed(async (value) => value(a) + 5);
165
+ b.value; // trigger compute
166
+ a.value = 8;
167
+ assert.strictEqual(await b.value, 13);
168
+ });
169
+
170
+ it('old dependency changed while computing', async function () {
171
+ let gate = 0;
172
+ const a = new Ref(5);
173
+ const b = new Computed(async (value) => {
174
+ gate++;
175
+ await new Promise(resolve => setTimeout(resolve));
176
+ return value(a) + 2;
177
+ });
178
+ assert.strictEqual(await b.value, 7);
179
+ b.invalidate();
180
+ const promise = b.value;
181
+ a.value = 6;
182
+ assert.strictEqual(await promise, 8);
183
+ assert.strictEqual(gate, 2);
184
+ });
185
+
186
+ it('new dependency changed while computing', async function () {
187
+ let gate = 0;
188
+ const a = new Ref(5);
189
+ const b = new Ref(10);
190
+ const c = new Computed(async (value) => {
191
+ gate++;
192
+ await new Promise(resolve => setTimeout(resolve, 50));
193
+ let sum = value(a);
194
+ await new Promise(resolve => setTimeout(resolve, 50));
195
+ sum += value(b);
196
+ return sum;
197
+ });
198
+ assert.strictEqual(await c.value, 15);
199
+ c.invalidate();
200
+ const promise = c.value;
201
+ await new Promise(resolve => setTimeout(resolve, 60));
202
+ a.value = 10;
203
+ assert.strictEqual(await promise, 20);
204
+ assert.strictEqual(gate, 3);
205
+ });
206
+
207
+ it('fallback to primitive while computing', async function () {
208
+ const a = new Ref(5);
209
+ const b = new Ref(10);
210
+ const c = new Computed(async (value) => {
211
+ if (value(a) < 10) {
212
+ await new Promise(resolve => setTimeout(resolve, 50));
213
+ return value(b) + 5;
214
+ }
215
+ return 2;
216
+ });
217
+ const promise = c.value;
218
+ await new Promise(resolve => setTimeout(resolve, 20));
219
+ a.value = 10;
220
+ assert.strictEqual(await promise, 2);
221
+ });
222
+
223
+ it('throw error', async function () {
224
+ const a = new Computed(async () => {
225
+ await new Promise(resolve => setTimeout(resolve));
226
+ throw new Error();
227
+ });
228
+ assert.rejects(() => a.value);
229
+ });
230
+
231
+ it('dispose computed', async function () {
232
+ const a = new Ref(5);
233
+ let gate = 0;
234
+ const b = new Computed(value => {
235
+ gate++;
236
+ return value(a) + 2;
237
+ });
238
+ b.value;
239
+ assert.strictEqual(gate, 1);
240
+ b.dispose();
241
+ b.value;
242
+ assert.strictEqual(gate, 2);
243
+ });
244
+ });
@@ -0,0 +1,24 @@
1
+ import 'mocha';
2
+ import assert from 'assert';
3
+ import { Ref, Watcher } from './index.js';
4
+
5
+ describe('ref', function () {
6
+ it('getter', function () {
7
+ const a = new Ref(5);
8
+ assert.strictEqual(a.value, 5);
9
+ });
10
+
11
+ it('setter', function () {
12
+ const a = new Ref(5);
13
+ a.value = 4;
14
+ assert.strictEqual(a.value, 4);
15
+ });
16
+
17
+ it('setter isEqual', function () {
18
+ const a = new Ref(5, () => true);
19
+ new Watcher(a, () => {
20
+ assert.fail();
21
+ }, false);
22
+ a.value = 4;
23
+ });
24
+ });
package/src/ref.ts CHANGED
@@ -1,17 +1,21 @@
1
1
  import Dependency from "./dependency.js";
2
2
  import Tracker from "./tracker.js";
3
3
 
4
+ const defaultIsEqual = <T>(v1?: T, v2?: T) => v1 === v2;
5
+
4
6
  export default class Ref<T> extends Tracker<T> implements Dependency<T> {
7
+ private isEqual: (a?: T, b?: T) => boolean;
5
8
 
6
- constructor(_value: T) {
9
+ constructor(_value: T, isEqual = defaultIsEqual<T>) {
7
10
  super();
8
11
  this._value = _value;
12
+ this.isEqual = isEqual;
9
13
  }
10
14
 
11
15
  public set value(_value: T) {
12
16
  const lastValue = this._value;
13
17
  this._value = _value;
14
- if (lastValue !== _value) {
18
+ if (!this.isEqual(lastValue, _value)) {
15
19
  this.invalidate();
16
20
  }
17
21
  }