async-reactivity 1.1.2 → 1.1.3

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 CHANGED
@@ -1,129 +1,130 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const tracker_js_1 = __importDefault(require("./tracker.js"));
7
- var ComputedState;
8
- (function (ComputedState) {
9
- ComputedState[ComputedState["Invalid"] = 0] = "Invalid";
10
- ComputedState[ComputedState["Valid"] = 1] = "Valid";
11
- ComputedState[ComputedState["Uncertain"] = 2] = "Uncertain";
12
- ComputedState[ComputedState["Computing"] = 3] = "Computing";
13
- })(ComputedState || (ComputedState = {}));
14
- ;
15
- class CircularDependencyError extends Error {
16
- }
17
- class Computed extends tracker_js_1.default {
18
- constructor(getter) {
19
- super();
20
- this.state = ComputedState.Invalid;
21
- this.dependencies = new Map();
22
- this.trackDependency = this.innerTrackDependency.bind(this);
23
- this.getter = getter;
24
- this.prepareComputePromise();
25
- }
26
- prepareComputePromise() {
27
- this.computePromise = new Promise((resolve, reject) => {
28
- this.computePromiseActions = {
29
- resolve,
30
- reject
31
- };
32
- });
33
- }
34
- get value() {
35
- if (this.state === ComputedState.Invalid || this.state === ComputedState.Uncertain) {
36
- return this.compute();
37
- }
38
- return this._value;
39
- }
40
- compute() {
41
- if (this.state === ComputedState.Uncertain) {
42
- for (const dependency of this.dependencies.keys()) {
43
- this.dependencies.set(dependency, true);
44
- }
45
- if ([...this.dependencies.keys()].every(d => {
46
- d.value;
47
- return !this.dependencies.get(d);
48
- })) {
49
- this.finalizeComputing();
50
- this.validateDependents();
51
- return this._value;
52
- }
53
- }
54
- this.state = ComputedState.Computing;
55
- this.clearDependencies();
56
- const lastValue = this._value;
57
- this._value = this.getter(this.trackDependency);
58
- if (this._value instanceof Promise) {
59
- const computeAttemptPromise = this._value
60
- .then(result => this.handlePromiseThen(computeAttemptPromise, result))
61
- .catch(error => this.handlePromiseCatch(computeAttemptPromise, error));
62
- this.lastComputeAttemptPromise = computeAttemptPromise;
63
- this._value = this.computePromise;
64
- }
65
- else {
66
- this.handlePromiseThen(this.lastComputeAttemptPromise, this._value);
67
- if (lastValue === this._value) {
68
- this.validateDependents();
69
- }
70
- }
71
- return this._value;
72
- }
73
- clearDependencies() {
74
- for (const dependency of this.dependencies.keys()) {
75
- dependency.removeDependent(this);
76
- }
77
- this.dependencies.clear();
78
- }
79
- handlePromiseThen(computeAttemptPromise, result) {
80
- if (this.lastComputeAttemptPromise === computeAttemptPromise) {
81
- this.computePromiseActions.resolve(result);
82
- this.finalizeComputing();
83
- }
84
- }
85
- handlePromiseCatch(computeAttemptPromise, error) {
86
- if (this.lastComputeAttemptPromise === computeAttemptPromise) {
87
- this.computePromiseActions.reject(error);
88
- this.finalizeComputing();
89
- }
90
- }
91
- innerTrackDependency(dependency) {
92
- if (this.dependents.has(dependency)) {
93
- throw new CircularDependencyError();
94
- }
95
- this.dependencies.set(dependency, true);
96
- dependency.addDependent(this);
97
- return dependency.value;
98
- }
99
- finalizeComputing() {
100
- this.state = ComputedState.Valid;
101
- this.lastComputeAttemptPromise = undefined;
102
- this.prepareComputePromise();
103
- }
104
- invalidate() {
105
- if (this.state === ComputedState.Computing) {
106
- setTimeout(this.compute.bind(this));
107
- }
108
- else if (this.state === ComputedState.Valid) {
109
- this.state = ComputedState.Uncertain;
110
- super.invalidate();
111
- }
112
- }
113
- validate(dependency) {
114
- this.dependencies.set(dependency, false);
115
- }
116
- validateDependents() {
117
- for (const dependent of this.dependents.keys()) {
118
- dependent.validate(this);
119
- }
120
- }
121
- dispose() {
122
- this.clearDependencies();
123
- this.state = ComputedState.Invalid;
124
- this._value = undefined;
125
- this.lastComputeAttemptPromise = undefined;
126
- this.prepareComputePromise();
127
- }
128
- }
129
- exports.default = Computed;
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const tracker_js_1 = __importDefault(require("./tracker.js"));
7
+ var ComputedState;
8
+ (function (ComputedState) {
9
+ ComputedState[ComputedState["Invalid"] = 0] = "Invalid";
10
+ ComputedState[ComputedState["Valid"] = 1] = "Valid";
11
+ ComputedState[ComputedState["Uncertain"] = 2] = "Uncertain";
12
+ ComputedState[ComputedState["Computing"] = 3] = "Computing";
13
+ })(ComputedState || (ComputedState = {}));
14
+ ;
15
+ class CircularDependencyError extends Error {
16
+ }
17
+ class Computed extends tracker_js_1.default {
18
+ constructor(getter) {
19
+ super();
20
+ this.state = ComputedState.Invalid;
21
+ this.dependencies = new Map();
22
+ this.trackDependency = this.innerTrackDependency.bind(this);
23
+ this.getter = getter;
24
+ this.prepareComputePromise();
25
+ }
26
+ prepareComputePromise() {
27
+ this.computePromise = new Promise((resolve, reject) => {
28
+ this.computePromiseActions = {
29
+ resolve,
30
+ reject
31
+ };
32
+ });
33
+ }
34
+ get value() {
35
+ if (this.state === ComputedState.Invalid || this.state === ComputedState.Uncertain) {
36
+ return this.compute();
37
+ }
38
+ return this._value;
39
+ }
40
+ compute() {
41
+ if (this.state === ComputedState.Uncertain) {
42
+ for (const dependency of this.dependencies.keys()) {
43
+ this.dependencies.set(dependency, true);
44
+ }
45
+ if ([...this.dependencies.keys()].every(d => {
46
+ d.value;
47
+ return !this.dependencies.get(d);
48
+ })) {
49
+ this.finalizeComputing();
50
+ this.validateDependents();
51
+ return this._value;
52
+ }
53
+ }
54
+ this.state = ComputedState.Computing;
55
+ this.clearDependencies();
56
+ const lastValue = this._value;
57
+ this._value = this.getter(this.trackDependency);
58
+ if (this._value instanceof Promise) {
59
+ const computeAttemptPromise = this._value
60
+ .then(result => this.handlePromiseThen(computeAttemptPromise, result))
61
+ .catch(error => this.handlePromiseCatch(computeAttemptPromise, error));
62
+ this.lastComputeAttemptPromise = computeAttemptPromise;
63
+ this._value = this.computePromise;
64
+ }
65
+ else {
66
+ this.handlePromiseThen(this.lastComputeAttemptPromise, this._value);
67
+ if (lastValue === this._value) {
68
+ this.validateDependents();
69
+ }
70
+ }
71
+ return this._value;
72
+ }
73
+ clearDependencies() {
74
+ for (const dependency of this.dependencies.keys()) {
75
+ dependency.removeDependent(this);
76
+ }
77
+ this.dependencies.clear();
78
+ }
79
+ handlePromiseThen(computeAttemptPromise, result) {
80
+ if (this.lastComputeAttemptPromise === computeAttemptPromise) {
81
+ this.computePromiseActions.resolve(result);
82
+ this.finalizeComputing();
83
+ }
84
+ }
85
+ handlePromiseCatch(computeAttemptPromise, error) {
86
+ if (this.lastComputeAttemptPromise === computeAttemptPromise) {
87
+ this.computePromiseActions.reject(error);
88
+ this.finalizeComputing();
89
+ }
90
+ }
91
+ innerTrackDependency(dependency) {
92
+ if (this.dependents.has(dependency)) {
93
+ throw new CircularDependencyError();
94
+ }
95
+ this.dependencies.set(dependency, true);
96
+ dependency.addDependent(this);
97
+ return dependency.value;
98
+ }
99
+ finalizeComputing() {
100
+ this.state = ComputedState.Valid;
101
+ this.lastComputeAttemptPromise = undefined;
102
+ this.prepareComputePromise();
103
+ }
104
+ invalidate() {
105
+ if (this.state === ComputedState.Computing) {
106
+ this.lastComputeAttemptPromise = undefined; // prevent finalizeComputing if it is in the event loop already
107
+ Promise.resolve().then(this.compute.bind(this));
108
+ }
109
+ else if (this.state === ComputedState.Valid) {
110
+ this.state = ComputedState.Uncertain;
111
+ super.invalidate();
112
+ }
113
+ }
114
+ validate(dependency) {
115
+ this.dependencies.set(dependency, false);
116
+ }
117
+ validateDependents() {
118
+ for (const dependent of this.dependents.keys()) {
119
+ dependent.validate(this);
120
+ }
121
+ }
122
+ dispose() {
123
+ this.clearDependencies();
124
+ this.state = ComputedState.Invalid;
125
+ this._value = undefined;
126
+ this.lastComputeAttemptPromise = undefined;
127
+ this.prepareComputePromise();
128
+ }
129
+ }
130
+ exports.default = Computed;
package/lib/dependency.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/dependent.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/lib/index.js CHANGED
@@ -1,12 +1,12 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Watcher = exports.Ref = exports.Computed = void 0;
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; } });
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Watcher = exports.Ref = exports.Computed = void 0;
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; } });