async-reactivity 1.0.2 → 1.1.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.
- package/lib/computed.js +4 -4
- package/lib/index.js +6 -6
- package/lib/index.test.js +96 -116
- package/lib/ref.js +2 -2
- package/lib/watcher.js +2 -2
- package/package.json +4 -3
- package/src/computed.ts +5 -5
- package/src/dependency.ts +1 -1
- package/src/dependent.ts +1 -1
- package/src/index.test.ts +4 -4
- package/src/index.ts +5 -5
- package/src/ref.ts +2 -2
- package/src/tracker.ts +2 -2
- package/src/tsconfig.json +2 -2
- package/src/watcher.ts +3 -3
- package/types/computed.d.ts +3 -3
- package/types/computed.d.ts.map +1 -1
- package/types/dependency.d.ts +1 -1
- package/types/dependency.d.ts.map +1 -1
- package/types/dependent.d.ts +1 -1
- package/types/dependent.d.ts.map +1 -1
- package/types/index.d.ts +5 -5
- package/types/index.d.ts.map +1 -1
- package/types/index.test.d.ts +1 -1
- package/types/index.test.d.ts.map +1 -1
- package/types/ref.d.ts +2 -2
- package/types/ref.d.ts.map +1 -1
- package/types/tracker.d.ts +3 -3
- package/types/tracker.d.ts.map +1 -1
- package/types/watcher.d.ts +4 -4
- package/types/watcher.d.ts.map +1 -1
package/lib/computed.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
6
|
+
const tracker_js_1 = __importDefault(require("./tracker.js"));
|
|
7
7
|
var ComputedState;
|
|
8
8
|
(function (ComputedState) {
|
|
9
9
|
ComputedState[ComputedState["Invalid"] = 0] = "Invalid";
|
|
@@ -14,7 +14,7 @@ var ComputedState;
|
|
|
14
14
|
;
|
|
15
15
|
class CircularDependencyError extends Error {
|
|
16
16
|
}
|
|
17
|
-
class Computed extends
|
|
17
|
+
class Computed extends tracker_js_1.default {
|
|
18
18
|
constructor(getter) {
|
|
19
19
|
super();
|
|
20
20
|
this.state = ComputedState.Invalid;
|
|
@@ -103,9 +103,9 @@ class Computed extends tracker_1.default {
|
|
|
103
103
|
}
|
|
104
104
|
invalidate() {
|
|
105
105
|
if (this.state === ComputedState.Computing) {
|
|
106
|
-
|
|
106
|
+
setTimeout(this.compute.bind(this));
|
|
107
107
|
}
|
|
108
|
-
if (this.state
|
|
108
|
+
else if (this.state === ComputedState.Valid) {
|
|
109
109
|
this.state = ComputedState.Uncertain;
|
|
110
110
|
super.invalidate();
|
|
111
111
|
}
|
package/lib/index.js
CHANGED
|
@@ -4,9 +4,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Watcher = exports.Ref = exports.Computed = void 0;
|
|
7
|
-
var
|
|
8
|
-
Object.defineProperty(exports, "Computed", { enumerable: true, get: function () { return __importDefault(
|
|
9
|
-
var
|
|
10
|
-
Object.defineProperty(exports, "Ref", { enumerable: true, get: function () { return __importDefault(
|
|
11
|
-
var
|
|
12
|
-
Object.defineProperty(exports, "Watcher", { enumerable: true, get: function () { return __importDefault(
|
|
7
|
+
var computed_js_1 = require("./computed.js");
|
|
8
|
+
Object.defineProperty(exports, "Computed", { enumerable: true, get: function () { return __importDefault(computed_js_1).default; } });
|
|
9
|
+
var ref_js_1 = require("./ref.js");
|
|
10
|
+
Object.defineProperty(exports, "Ref", { enumerable: true, get: function () { return __importDefault(ref_js_1).default; } });
|
|
11
|
+
var watcher_js_1 = require("./watcher.js");
|
|
12
|
+
Object.defineProperty(exports, "Watcher", { enumerable: true, get: function () { return __importDefault(watcher_js_1).default; } });
|
package/lib/index.test.js
CHANGED
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -31,187 +8,191 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
31
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
9
|
});
|
|
33
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
34
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
-
|
|
36
|
-
const
|
|
15
|
+
require("mocha");
|
|
16
|
+
const assert_1 = __importDefault(require("assert"));
|
|
17
|
+
const index_js_1 = require("./index.js");
|
|
37
18
|
describe('async reactivity', function () {
|
|
38
19
|
describe('ref', function () {
|
|
39
20
|
it('getter', function () {
|
|
40
|
-
const a = new
|
|
41
|
-
|
|
21
|
+
const a = new index_js_1.Ref(5);
|
|
22
|
+
assert_1.default.strictEqual(a.value, 5);
|
|
42
23
|
});
|
|
43
24
|
it('setter', function () {
|
|
44
|
-
const a = new
|
|
25
|
+
const a = new index_js_1.Ref(5);
|
|
45
26
|
a.value = 4;
|
|
46
|
-
|
|
27
|
+
assert_1.default.strictEqual(a.value, 4);
|
|
47
28
|
});
|
|
48
29
|
});
|
|
49
30
|
describe('computed', function () {
|
|
50
31
|
it('lazy compute', function () {
|
|
51
32
|
let gate = false;
|
|
52
|
-
const a = new
|
|
33
|
+
const a = new index_js_1.Computed(() => {
|
|
53
34
|
gate = true;
|
|
54
35
|
return 5;
|
|
55
36
|
});
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
37
|
+
assert_1.default.strictEqual(gate, false);
|
|
38
|
+
assert_1.default.strictEqual(a.value, 5);
|
|
39
|
+
assert_1.default.strictEqual(gate, true);
|
|
59
40
|
});
|
|
60
41
|
it('cache', function () {
|
|
61
42
|
let gate = false;
|
|
62
|
-
const a = new
|
|
43
|
+
const a = new index_js_1.Computed(() => {
|
|
63
44
|
gate = true;
|
|
64
45
|
return 5;
|
|
65
46
|
});
|
|
66
|
-
|
|
47
|
+
assert_1.default.strictEqual(a.value, 5);
|
|
67
48
|
gate = false;
|
|
68
|
-
|
|
69
|
-
|
|
49
|
+
assert_1.default.strictEqual(a.value, 5);
|
|
50
|
+
assert_1.default.strictEqual(gate, false);
|
|
70
51
|
});
|
|
71
52
|
it('invalidate dependents', function () {
|
|
72
|
-
const a = new
|
|
73
|
-
const b = new
|
|
53
|
+
const a = new index_js_1.Ref(5);
|
|
54
|
+
const b = new index_js_1.Computed((value) => {
|
|
74
55
|
return value(a) + 4;
|
|
75
56
|
});
|
|
76
|
-
|
|
57
|
+
assert_1.default.strictEqual(b.value, 9);
|
|
77
58
|
a.value = 6;
|
|
78
|
-
|
|
59
|
+
assert_1.default.strictEqual(b.value, 10);
|
|
79
60
|
});
|
|
80
61
|
it('dependents up-to-date', function () {
|
|
81
|
-
const a = new
|
|
82
|
-
const b = new
|
|
62
|
+
const a = new index_js_1.Ref(5);
|
|
63
|
+
const b = new index_js_1.Ref(10);
|
|
83
64
|
let gate;
|
|
84
|
-
const c = new
|
|
65
|
+
const c = new index_js_1.Computed((value) => {
|
|
85
66
|
gate = true;
|
|
86
67
|
return value(a) < 10 ? value(b) : 0;
|
|
87
68
|
});
|
|
88
|
-
|
|
69
|
+
assert_1.default.strictEqual(c.value, 10);
|
|
89
70
|
a.value = 15;
|
|
90
|
-
|
|
71
|
+
assert_1.default.strictEqual(c.value, 0);
|
|
91
72
|
b.value = 15;
|
|
92
73
|
gate = false;
|
|
93
|
-
|
|
94
|
-
|
|
74
|
+
assert_1.default.strictEqual(c.value, 0);
|
|
75
|
+
assert_1.default.strictEqual(gate, false);
|
|
95
76
|
});
|
|
96
77
|
it('detect circular dependency', function () {
|
|
97
78
|
// @ts-expect-error
|
|
98
|
-
const a = new
|
|
79
|
+
const a = new index_js_1.Computed((value) => {
|
|
99
80
|
return value(b);
|
|
100
81
|
});
|
|
101
82
|
// @ts-expect-error
|
|
102
|
-
const b = new
|
|
83
|
+
const b = new index_js_1.Computed((value) => {
|
|
103
84
|
return value(a);
|
|
104
85
|
});
|
|
105
|
-
|
|
86
|
+
assert_1.default.throws(() => a.value);
|
|
106
87
|
});
|
|
107
88
|
xit('detect circular deeper dependency', function () {
|
|
108
89
|
// do not support for better performance
|
|
109
|
-
|
|
90
|
+
assert_1.default.fail('not implemented');
|
|
110
91
|
});
|
|
111
92
|
it('throw error', function () {
|
|
112
|
-
const a = new
|
|
93
|
+
const a = new index_js_1.Computed(() => {
|
|
113
94
|
throw new Error();
|
|
114
95
|
});
|
|
115
|
-
|
|
96
|
+
assert_1.default.throws(() => a.value);
|
|
116
97
|
});
|
|
117
98
|
it('ignore same ref value', function () {
|
|
118
99
|
let gate = 0;
|
|
119
|
-
const a = new
|
|
120
|
-
const b = new
|
|
100
|
+
const a = new index_js_1.Ref(5);
|
|
101
|
+
const b = new index_js_1.Computed((value) => {
|
|
121
102
|
gate++;
|
|
122
103
|
return value(a);
|
|
123
104
|
});
|
|
124
|
-
|
|
105
|
+
assert_1.default.strictEqual(b.value, 5);
|
|
125
106
|
a.value = 5;
|
|
126
|
-
|
|
127
|
-
|
|
107
|
+
assert_1.default.strictEqual(b.value, 5);
|
|
108
|
+
assert_1.default.strictEqual(gate, 1);
|
|
128
109
|
});
|
|
129
110
|
it('ignore same computed value', function () {
|
|
130
111
|
let gate = 0;
|
|
131
|
-
const a = new
|
|
132
|
-
const b = new
|
|
112
|
+
const a = new index_js_1.Ref(5);
|
|
113
|
+
const b = new index_js_1.Computed((value) => {
|
|
133
114
|
return value(a) % 2;
|
|
134
115
|
});
|
|
135
|
-
const c = new
|
|
116
|
+
const c = new index_js_1.Computed((value) => {
|
|
136
117
|
gate++;
|
|
137
118
|
return value(b) + 5;
|
|
138
119
|
});
|
|
139
|
-
|
|
120
|
+
assert_1.default.strictEqual(c.value, 6);
|
|
140
121
|
a.value = 7;
|
|
141
|
-
|
|
142
|
-
|
|
122
|
+
assert_1.default.strictEqual(c.value, 6);
|
|
123
|
+
assert_1.default.strictEqual(gate, 1);
|
|
143
124
|
});
|
|
144
125
|
});
|
|
145
126
|
describe('async computed', function () {
|
|
146
127
|
it('getter', function () {
|
|
147
128
|
return __awaiter(this, void 0, void 0, function* () {
|
|
148
|
-
const a = new
|
|
129
|
+
const a = new index_js_1.Computed(() => __awaiter(this, void 0, void 0, function* () {
|
|
149
130
|
yield new Promise(resolve => setTimeout(resolve));
|
|
150
131
|
return 5;
|
|
151
132
|
}));
|
|
152
|
-
|
|
133
|
+
assert_1.default.strictEqual(yield a.value, 5);
|
|
153
134
|
});
|
|
154
135
|
});
|
|
155
136
|
it('tracks async dependencies', function () {
|
|
156
137
|
return __awaiter(this, void 0, void 0, function* () {
|
|
157
|
-
const a = new
|
|
158
|
-
const b = new
|
|
138
|
+
const a = new index_js_1.Ref(5);
|
|
139
|
+
const b = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
159
140
|
yield new Promise(resolve => setTimeout(resolve));
|
|
160
141
|
return value(a) + 5;
|
|
161
142
|
}));
|
|
162
|
-
|
|
143
|
+
assert_1.default.strictEqual(yield b.value, 10);
|
|
163
144
|
a.value = 6;
|
|
164
|
-
|
|
145
|
+
assert_1.default.strictEqual(yield b.value, 11);
|
|
165
146
|
});
|
|
166
147
|
});
|
|
167
148
|
it('get value while computing', function () {
|
|
168
149
|
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
-
const a = new
|
|
150
|
+
const a = new index_js_1.Computed(() => __awaiter(this, void 0, void 0, function* () {
|
|
170
151
|
yield new Promise(resolve => setTimeout(resolve));
|
|
171
152
|
return 5;
|
|
172
153
|
}));
|
|
173
154
|
a.value;
|
|
174
|
-
|
|
155
|
+
assert_1.default.strictEqual(yield a.value, 5);
|
|
175
156
|
});
|
|
176
157
|
});
|
|
177
158
|
it('detect circular dependency', function () {
|
|
178
159
|
return __awaiter(this, void 0, void 0, function* () {
|
|
179
160
|
// @ts-expect-error
|
|
180
|
-
const a = new
|
|
161
|
+
const a = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
181
162
|
yield new Promise(resolve => setTimeout(resolve));
|
|
182
163
|
return value(b);
|
|
183
164
|
}));
|
|
184
165
|
// @ts-expect-error
|
|
185
|
-
const b = new
|
|
166
|
+
const b = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
186
167
|
yield new Promise(resolve => setTimeout(resolve));
|
|
187
168
|
return value(a);
|
|
188
169
|
}));
|
|
189
|
-
|
|
170
|
+
assert_1.default.rejects(() => __awaiter(this, void 0, void 0, function* () { return yield a.value; }));
|
|
190
171
|
});
|
|
191
172
|
});
|
|
192
173
|
it('old dependency changed while computing', function () {
|
|
193
174
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194
175
|
let gate = 0;
|
|
195
|
-
const a = new
|
|
196
|
-
const b = new
|
|
176
|
+
const a = new index_js_1.Ref(5);
|
|
177
|
+
const b = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
197
178
|
gate++;
|
|
198
179
|
yield new Promise(resolve => setTimeout(resolve));
|
|
199
180
|
return value(a) + 2;
|
|
200
181
|
}));
|
|
201
|
-
|
|
182
|
+
assert_1.default.strictEqual(yield b.value, 7);
|
|
202
183
|
b.invalidate();
|
|
203
184
|
const promise = b.value;
|
|
204
185
|
a.value = 6;
|
|
205
|
-
|
|
206
|
-
|
|
186
|
+
assert_1.default.strictEqual(yield promise, 8);
|
|
187
|
+
assert_1.default.strictEqual(gate, 2);
|
|
207
188
|
});
|
|
208
189
|
});
|
|
209
190
|
it('new dependency changed while computing', function () {
|
|
210
191
|
return __awaiter(this, void 0, void 0, function* () {
|
|
211
192
|
let gate = 0;
|
|
212
|
-
const a = new
|
|
213
|
-
const b = new
|
|
214
|
-
const c = new
|
|
193
|
+
const a = new index_js_1.Ref(5);
|
|
194
|
+
const b = new index_js_1.Ref(10);
|
|
195
|
+
const c = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
215
196
|
gate++;
|
|
216
197
|
yield new Promise(resolve => setTimeout(resolve, 50));
|
|
217
198
|
let sum = value(a);
|
|
@@ -219,20 +200,20 @@ describe('async reactivity', function () {
|
|
|
219
200
|
sum += value(b);
|
|
220
201
|
return sum;
|
|
221
202
|
}));
|
|
222
|
-
|
|
203
|
+
assert_1.default.strictEqual(yield c.value, 15);
|
|
223
204
|
c.invalidate();
|
|
224
205
|
const promise = c.value;
|
|
225
206
|
yield new Promise(resolve => setTimeout(resolve, 60));
|
|
226
207
|
a.value = 10;
|
|
227
|
-
|
|
228
|
-
|
|
208
|
+
assert_1.default.strictEqual(yield promise, 20);
|
|
209
|
+
assert_1.default.strictEqual(gate, 3);
|
|
229
210
|
});
|
|
230
211
|
});
|
|
231
212
|
it('fallback to primitive while computing', function () {
|
|
232
213
|
return __awaiter(this, void 0, void 0, function* () {
|
|
233
|
-
const a = new
|
|
234
|
-
const b = new
|
|
235
|
-
const c = new
|
|
214
|
+
const a = new index_js_1.Ref(5);
|
|
215
|
+
const b = new index_js_1.Ref(10);
|
|
216
|
+
const c = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
236
217
|
if (value(a) < 10) {
|
|
237
218
|
yield new Promise(resolve => setTimeout(resolve, 50));
|
|
238
219
|
return value(b) + 5;
|
|
@@ -242,93 +223,92 @@ describe('async reactivity', function () {
|
|
|
242
223
|
const promise = c.value;
|
|
243
224
|
yield new Promise(resolve => setTimeout(resolve, 20));
|
|
244
225
|
a.value = 10;
|
|
245
|
-
|
|
226
|
+
assert_1.default.strictEqual(yield promise, 2);
|
|
246
227
|
});
|
|
247
228
|
});
|
|
248
229
|
it('throw error', function () {
|
|
249
230
|
return __awaiter(this, void 0, void 0, function* () {
|
|
250
|
-
const a = new
|
|
231
|
+
const a = new index_js_1.Computed(() => __awaiter(this, void 0, void 0, function* () {
|
|
251
232
|
yield new Promise(resolve => setTimeout(resolve));
|
|
252
233
|
throw new Error();
|
|
253
234
|
}));
|
|
254
|
-
|
|
235
|
+
assert_1.default.rejects(() => a.value);
|
|
255
236
|
});
|
|
256
237
|
});
|
|
257
238
|
it('dispose computed', function () {
|
|
258
239
|
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
-
const a = new
|
|
240
|
+
const a = new index_js_1.Ref(5);
|
|
260
241
|
let gate = 0;
|
|
261
|
-
const b = new
|
|
242
|
+
const b = new index_js_1.Computed(value => {
|
|
262
243
|
gate++;
|
|
263
244
|
return value(a) + 2;
|
|
264
245
|
});
|
|
265
246
|
b.value;
|
|
266
|
-
|
|
247
|
+
assert_1.default.strictEqual(gate, 1);
|
|
267
248
|
b.dispose();
|
|
268
|
-
a.value = 6;
|
|
269
249
|
b.value;
|
|
270
|
-
|
|
250
|
+
assert_1.default.strictEqual(gate, 2);
|
|
271
251
|
});
|
|
272
252
|
});
|
|
273
253
|
});
|
|
274
254
|
describe('watcher', function () {
|
|
275
255
|
it('sync', function () {
|
|
276
|
-
const a = new
|
|
277
|
-
new
|
|
278
|
-
|
|
279
|
-
|
|
256
|
+
const a = new index_js_1.Ref(5);
|
|
257
|
+
new index_js_1.Watcher(a, (newValue, oldValue) => {
|
|
258
|
+
assert_1.default.strictEqual(oldValue, 5);
|
|
259
|
+
assert_1.default.strictEqual(newValue, 6);
|
|
280
260
|
}, false);
|
|
281
261
|
a.value = 6;
|
|
282
262
|
});
|
|
283
263
|
it('async', function () {
|
|
284
264
|
return __awaiter(this, void 0, void 0, function* () {
|
|
285
|
-
const a = new
|
|
265
|
+
const a = new index_js_1.Computed(() => __awaiter(this, void 0, void 0, function* () {
|
|
286
266
|
yield new Promise(resolve => setTimeout(resolve));
|
|
287
267
|
return 10;
|
|
288
268
|
}));
|
|
289
|
-
const result = yield new Promise(resolve => new
|
|
290
|
-
|
|
269
|
+
const result = yield new Promise(resolve => new index_js_1.Watcher(a, resolve));
|
|
270
|
+
assert_1.default.strictEqual(result, 10);
|
|
291
271
|
});
|
|
292
272
|
});
|
|
293
273
|
it('sync cancel', function () {
|
|
294
274
|
return __awaiter(this, void 0, void 0, function* () {
|
|
295
|
-
const a = new
|
|
296
|
-
const b = new
|
|
275
|
+
const a = new index_js_1.Ref(5);
|
|
276
|
+
const b = new index_js_1.Computed((value) => {
|
|
297
277
|
return value(a) % 2;
|
|
298
278
|
});
|
|
299
279
|
let gate = 0;
|
|
300
|
-
new
|
|
280
|
+
new index_js_1.Watcher(b, () => {
|
|
301
281
|
gate++;
|
|
302
282
|
}, false);
|
|
303
283
|
a.value = 6;
|
|
304
|
-
|
|
284
|
+
assert_1.default.strictEqual(gate, 1);
|
|
305
285
|
a.value = 7;
|
|
306
286
|
a.value = 9;
|
|
307
287
|
a.value = 11;
|
|
308
|
-
|
|
288
|
+
assert_1.default.strictEqual(gate, 2);
|
|
309
289
|
});
|
|
310
290
|
});
|
|
311
291
|
it('async sync cancel', function () {
|
|
312
292
|
return __awaiter(this, void 0, void 0, function* () {
|
|
313
|
-
const a = new
|
|
314
|
-
const b = new
|
|
293
|
+
const a = new index_js_1.Ref(5);
|
|
294
|
+
const b = new index_js_1.Computed((value) => __awaiter(this, void 0, void 0, function* () {
|
|
315
295
|
new Promise(resolve => setTimeout(resolve));
|
|
316
296
|
return value(a) % 2;
|
|
317
297
|
}));
|
|
318
298
|
let gate = 0;
|
|
319
|
-
new
|
|
299
|
+
new index_js_1.Watcher(b, () => {
|
|
320
300
|
gate++;
|
|
321
301
|
}, false);
|
|
322
302
|
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
323
303
|
a.value = 6;
|
|
324
304
|
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
325
|
-
|
|
305
|
+
assert_1.default.strictEqual(gate, 1);
|
|
326
306
|
a.value = 7;
|
|
327
307
|
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
328
|
-
|
|
308
|
+
assert_1.default.strictEqual(gate, 2);
|
|
329
309
|
a.value = 9;
|
|
330
310
|
yield new Promise(resolve => setTimeout(resolve, 10));
|
|
331
|
-
|
|
311
|
+
assert_1.default.strictEqual(gate, 3);
|
|
332
312
|
});
|
|
333
313
|
});
|
|
334
314
|
});
|
package/lib/ref.js
CHANGED
|
@@ -3,8 +3,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
7
|
-
class Ref extends
|
|
6
|
+
const tracker_js_1 = __importDefault(require("./tracker.js"));
|
|
7
|
+
class Ref extends tracker_js_1.default {
|
|
8
8
|
constructor(_value) {
|
|
9
9
|
super();
|
|
10
10
|
this._value = _value;
|
package/lib/watcher.js
CHANGED
|
@@ -3,14 +3,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
const
|
|
6
|
+
const tracker_js_1 = __importDefault(require("./tracker.js"));
|
|
7
7
|
var WatchState;
|
|
8
8
|
(function (WatchState) {
|
|
9
9
|
WatchState[WatchState["Uncertain"] = 0] = "Uncertain";
|
|
10
10
|
WatchState[WatchState["Valid"] = 1] = "Valid";
|
|
11
11
|
})(WatchState || (WatchState = {}));
|
|
12
12
|
;
|
|
13
|
-
class Watcher extends
|
|
13
|
+
class Watcher extends tracker_js_1.default {
|
|
14
14
|
constructor(dependency, onChange, immediate = true) {
|
|
15
15
|
super();
|
|
16
16
|
this.state = WatchState.Valid;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "async-reactivity",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
"types"
|
|
11
11
|
],
|
|
12
12
|
"scripts": {
|
|
13
|
-
"dev": "
|
|
14
|
-
"
|
|
13
|
+
"dev": "tsc -w -p src",
|
|
14
|
+
"build": "tsc -p src",
|
|
15
|
+
"test": "mocha \"./{,!(node_modules)/**/}*.test.js\""
|
|
15
16
|
},
|
|
16
17
|
"author": "Donatas Lučiūnas",
|
|
17
18
|
"license": "ISC",
|
package/src/computed.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Dependency from "./dependency";
|
|
2
|
-
import Dependent from "./dependent";
|
|
3
|
-
import Tracker from "./tracker";
|
|
1
|
+
import Dependency from "./dependency.js";
|
|
2
|
+
import Dependent from "./dependent.js";
|
|
3
|
+
import Tracker from "./tracker.js";
|
|
4
4
|
|
|
5
5
|
declare type TrackValue = <T>(dependency: Dependency<T>) => T;
|
|
6
6
|
export declare type ComputeFunc<T> = (value: TrackValue) => T;
|
|
@@ -121,8 +121,8 @@ export default class Computed<T> extends Tracker<T> implements Dependent, Depend
|
|
|
121
121
|
|
|
122
122
|
public invalidate() {
|
|
123
123
|
if (this.state === ComputedState.Computing) {
|
|
124
|
-
|
|
125
|
-
} if (this.state
|
|
124
|
+
setTimeout(this.compute.bind(this));
|
|
125
|
+
} else if (this.state === ComputedState.Valid) {
|
|
126
126
|
this.state = ComputedState.Uncertain;
|
|
127
127
|
super.invalidate();
|
|
128
128
|
}
|
package/src/dependency.ts
CHANGED
package/src/dependent.ts
CHANGED
package/src/index.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import 'mocha';
|
|
2
|
+
import assert from 'assert';
|
|
3
|
+
import { Computed, Ref, Watcher } from './index.js';
|
|
3
4
|
|
|
4
5
|
describe('async reactivity', function () {
|
|
5
6
|
describe('ref', function () {
|
|
@@ -243,9 +244,8 @@ describe('async reactivity', function () {
|
|
|
243
244
|
b.value;
|
|
244
245
|
assert.strictEqual(gate, 1);
|
|
245
246
|
b.dispose();
|
|
246
|
-
a.value = 6;
|
|
247
247
|
b.value;
|
|
248
|
-
assert.strictEqual(gate,
|
|
248
|
+
assert.strictEqual(gate, 2);
|
|
249
249
|
});
|
|
250
250
|
});
|
|
251
251
|
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { default as Computed, ComputeFunc, ComputeFuncScoped } from './computed';
|
|
2
|
-
export { default as Ref } from './ref';
|
|
3
|
-
export { default as Watcher } from './watcher';
|
|
4
|
-
export { default as Dependency } from './dependency';
|
|
5
|
-
export { default as Dependent } from './dependent';
|
|
1
|
+
export { default as Computed, ComputeFunc, ComputeFuncScoped } from './computed.js';
|
|
2
|
+
export { default as Ref } from './ref.js';
|
|
3
|
+
export { default as Watcher } from './watcher.js';
|
|
4
|
+
export { default as Dependency } from './dependency.js';
|
|
5
|
+
export { default as Dependent } from './dependent.js';
|
package/src/ref.ts
CHANGED
package/src/tracker.ts
CHANGED
package/src/tsconfig.json
CHANGED
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
23
23
|
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
24
24
|
/* Modules */
|
|
25
|
-
"module": "
|
|
25
|
+
"module": "CommonJS", /* Specify what module code is generated. */
|
|
26
26
|
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
27
|
-
|
|
27
|
+
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
28
28
|
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
29
29
|
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
30
30
|
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
package/src/watcher.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Dependency from "./dependency";
|
|
2
|
-
import Dependent from "./dependent";
|
|
3
|
-
import Tracker from "./tracker";
|
|
1
|
+
import Dependency from "./dependency.js";
|
|
2
|
+
import Dependent from "./dependent.js";
|
|
3
|
+
import Tracker from "./tracker.js";
|
|
4
4
|
|
|
5
5
|
enum WatchState {
|
|
6
6
|
Uncertain,
|
package/types/computed.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Dependency from "./dependency";
|
|
2
|
-
import Dependent from "./dependent";
|
|
3
|
-
import Tracker from "./tracker";
|
|
1
|
+
import Dependency from "./dependency.js";
|
|
2
|
+
import Dependent from "./dependent.js";
|
|
3
|
+
import Tracker from "./tracker.js";
|
|
4
4
|
declare type TrackValue = <T>(dependency: Dependency<T>) => T;
|
|
5
5
|
export declare type ComputeFunc<T> = (value: TrackValue) => T;
|
|
6
6
|
export declare type ComputeFuncScoped<T1, T2> = (value: TrackValue, scope: T1) => T2;
|
package/types/computed.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../src/computed.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"computed.d.ts","sourceRoot":"","sources":["../src/computed.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,OAAO,MAAM,UAAU,GAAG,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;AAC9D,MAAM,CAAC,OAAO,MAAM,WAAW,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE,UAAU,KAAK,CAAC,CAAC;AAC9D,MAAM,CAAC,OAAO,MAAM,iBAAiB,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,EAAE,KAAK,EAAE,CAAC;AAWrF,MAAM,CAAC,OAAO,OAAO,QAAQ,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,SAAS,EAAE,UAAU,CAAC,CAAC,CAAC;IACnF,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC,CAAC;IACvB,OAAO,CAAC,KAAK,CAAyB;IACtC,OAAO,CAAC,YAAY,CAAuC;IAC3D,OAAO,CAAC,cAAc,CAAC,CAAI;IAC3B,OAAO,CAAC,qBAAqB,CAAC,CAA0C;IACxE,OAAO,CAAC,yBAAyB,CAAC,CAAgB;gBAEtC,MAAM,EAAE,WAAW,CAAC,CAAC,CAAC;IAMlC,OAAO,CAAC,qBAAqB;IAS7B,IAAW,KAAK,IAAI,CAAC,CAMpB;IAED,OAAO,CAAC,OAAO;IAmCf,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,iBAAiB;IAOzB,OAAO,CAAC,kBAAkB;IAO1B,OAAO,CAAC,oBAAoB;IAS5B,OAAO,CAAC,eAAe,CAAwC;IAE/D,OAAO,CAAC,iBAAiB;IAMlB,UAAU;IASV,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC;IAI3C,OAAO,CAAC,kBAAkB;IAMnB,OAAO;CAOjB"}
|
package/types/dependency.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dependency.d.ts","sourceRoot":"","sources":["../src/dependency.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,
|
|
1
|
+
{"version":3,"file":"dependency.d.ts","sourceRoot":"","sources":["../src/dependency.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,gBAAgB,CAAC;AAEvC,MAAM,CAAC,OAAO,WAAW,UAAU,CAAC,CAAC;IACjC,IAAI,KAAK,IAAI,CAAC,CAAC;IACf,YAAY,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI,CAAC;IACzC,eAAe,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI,CAAC;CAC/C"}
|
package/types/dependent.d.ts
CHANGED
package/types/dependent.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dependent.d.ts","sourceRoot":"","sources":["../src/dependent.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"dependent.d.ts","sourceRoot":"","sources":["../src/dependent.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AAEzC,MAAM,CAAC,OAAO,WAAW,SAAS;IAC9B,UAAU,IAAI,IAAI,CAAC;IACnB,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC;IAC5C,OAAO,IAAI,IAAI,CAAC;CACnB"}
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { default as Computed, ComputeFunc, ComputeFuncScoped } from './computed';
|
|
2
|
-
export { default as Ref } from './ref';
|
|
3
|
-
export { default as Watcher } from './watcher';
|
|
4
|
-
export { default as Dependency } from './dependency';
|
|
5
|
-
export { default as Dependent } from './dependent';
|
|
1
|
+
export { default as Computed, ComputeFunc, ComputeFuncScoped } from './computed.js';
|
|
2
|
+
export { default as Ref } from './ref.js';
|
|
3
|
+
export { default as Watcher } from './watcher.js';
|
|
4
|
+
export { default as Dependency } from './dependency.js';
|
|
5
|
+
export { default as Dependent } from './dependent.js';
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
package/types/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,IAAI,QAAQ,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AACpF,OAAO,EAAE,OAAO,IAAI,GAAG,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAE,OAAO,IAAI,UAAU,EAAE,MAAM,iBAAiB,CAAC;AACxD,OAAO,EAAE,OAAO,IAAI,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
|
package/types/index.test.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import 'mocha';
|
|
2
2
|
//# sourceMappingURL=index.test.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../src/index.test.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,CAAC"}
|
package/types/ref.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import Dependency from "./dependency";
|
|
2
|
-
import Tracker from "./tracker";
|
|
1
|
+
import Dependency from "./dependency.js";
|
|
2
|
+
import Tracker from "./tracker.js";
|
|
3
3
|
export default class Ref<T> extends Tracker<T> implements Dependency<T> {
|
|
4
4
|
constructor(_value: T);
|
|
5
5
|
set value(_value: T);
|
package/types/ref.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"ref.d.ts","sourceRoot":"","sources":["../src/ref.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,MAAM,CAAC,OAAO,OAAO,GAAG,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,UAAU,CAAC,CAAC,CAAC;gBAEvD,MAAM,EAAE,CAAC;IAKrB,IAAW,KAAK,CAAC,MAAM,EAAE,CAAC,EAMzB;IAED,IAAW,KAAK,IAAI,CAAC,CAEpB;CACJ"}
|
package/types/tracker.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import Computed from "./computed";
|
|
2
|
-
import Watcher from "./watcher";
|
|
3
|
-
|
|
1
|
+
import Computed from "./computed.js";
|
|
2
|
+
import Watcher from "./watcher.js";
|
|
3
|
+
type Dependent = Computed<any> | Watcher<any>;
|
|
4
4
|
export default class Tracker<T> {
|
|
5
5
|
protected dependents: Set<Dependent>;
|
|
6
6
|
protected _value?: T;
|
package/types/tracker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../src/tracker.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,
|
|
1
|
+
{"version":3,"file":"tracker.d.ts","sourceRoot":"","sources":["../src/tracker.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,eAAe,CAAC;AACrC,OAAO,OAAO,MAAM,cAAc,CAAC;AAEnC,KAAK,SAAS,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;AAE9C,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"}
|
package/types/watcher.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import Dependency from "./dependency";
|
|
2
|
-
import Dependent from "./dependent";
|
|
3
|
-
import Tracker from "./tracker";
|
|
4
|
-
|
|
1
|
+
import Dependency from "./dependency.js";
|
|
2
|
+
import Dependent from "./dependent.js";
|
|
3
|
+
import Tracker from "./tracker.js";
|
|
4
|
+
type onChangeFunc<T> = (newValue: T, oldValue?: T) => void;
|
|
5
5
|
export default class Watcher<T> extends Tracker<T> implements Dependent {
|
|
6
6
|
private onChange;
|
|
7
7
|
private dependency;
|
package/types/watcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watcher.d.ts","sourceRoot":"","sources":["../src/watcher.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"watcher.d.ts","sourceRoot":"","sources":["../src/watcher.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,iBAAiB,CAAC;AACzC,OAAO,SAAS,MAAM,gBAAgB,CAAC;AACvC,OAAO,OAAO,MAAM,cAAc,CAAC;AAOnC,KAAK,YAAY,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,KAAK,IAAI,CAAC;AAE3D,MAAM,CAAC,OAAO,OAAO,OAAO,CAAC,CAAC,CAAE,SAAQ,OAAO,CAAC,CAAC,CAAE,YAAW,SAAS;IAEnE,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,UAAU,CAAgB;IAClC,OAAO,CAAC,KAAK,CAAoB;gBAErB,UAAU,EAAE,UAAU,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,SAAS,GAAE,OAAc;IAYpF,UAAU;IAUV,QAAQ;IAIR,OAAO;CAGjB"}
|