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