@oscarpalmer/mora 0.3.0 → 0.4.0
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/dist/computed.cjs +37 -16
- package/dist/computed.js +39 -17
- package/dist/effect.cjs +8 -12
- package/dist/effect.js +8 -12
- package/dist/mora.iife.js +74 -74
- package/dist/signal.cjs +19 -33
- package/dist/signal.js +20 -34
- package/package.json +1 -1
- package/src/computed.ts +56 -18
- package/src/effect.ts +15 -18
- package/src/signal.ts +23 -38
- package/types/computed.d.cts +12 -14
- package/types/computed.d.ts +9 -2
- package/types/effect.d.cts +5 -20
- package/types/effect.d.ts +6 -3
- package/types/index.d.cts +13 -4
- package/types/signal.d.cts +14 -4
- package/types/signal.d.ts +4 -2
package/dist/computed.cjs
CHANGED
|
@@ -4,28 +4,49 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
5
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
6
6
|
const effect = require("./effect.cjs");
|
|
7
|
-
|
|
7
|
+
const signal = require("./signal.cjs");
|
|
8
|
+
exports.activeComputed = void 0;
|
|
9
|
+
class Computed {
|
|
8
10
|
constructor(callback) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
this.dirty = false;
|
|
17
|
-
}
|
|
11
|
+
__publicField(this, "state");
|
|
12
|
+
this.state = {
|
|
13
|
+
dirty: true,
|
|
14
|
+
computeds: /* @__PURE__ */ new Set(),
|
|
15
|
+
effect: void 0,
|
|
16
|
+
effects: /* @__PURE__ */ new Set(),
|
|
17
|
+
value: void 0
|
|
18
18
|
};
|
|
19
|
-
effect.
|
|
19
|
+
this.state.effect = effect.effect(() => {
|
|
20
|
+
if (this.state.dirty) {
|
|
21
|
+
const previousComputed = exports.activeComputed;
|
|
22
|
+
exports.activeComputed = this;
|
|
23
|
+
const value = callback();
|
|
24
|
+
exports.activeComputed = previousComputed;
|
|
25
|
+
if (!Object.is(this.state.value, value)) {
|
|
26
|
+
this.state.value = value;
|
|
27
|
+
for (const comp of this.state.computeds) {
|
|
28
|
+
comp.state.dirty = true;
|
|
29
|
+
signal.dirtyEffects.add(comp.state.effect);
|
|
30
|
+
}
|
|
31
|
+
for (const effect2 of this.state.effects) {
|
|
32
|
+
signal.dirtyEffects.add(effect2);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
this.state.dirty = false;
|
|
36
|
+
}
|
|
37
|
+
});
|
|
20
38
|
}
|
|
21
39
|
get() {
|
|
22
|
-
if (
|
|
23
|
-
this.
|
|
40
|
+
if (exports.activeComputed != null && exports.activeComputed !== this) {
|
|
41
|
+
this.state.computeds.add(exports.activeComputed);
|
|
42
|
+
}
|
|
43
|
+
if (effect.activeEffect != null && effect.activeEffect !== this.state.effect) {
|
|
44
|
+
this.state.effects.add(effect.activeEffect);
|
|
24
45
|
}
|
|
25
|
-
if (this.dirty) {
|
|
26
|
-
effect.runEffect(this);
|
|
46
|
+
if (this.state.dirty) {
|
|
47
|
+
effect.runEffect(this.state.effect);
|
|
27
48
|
}
|
|
28
|
-
return this.value;
|
|
49
|
+
return this.state.value;
|
|
29
50
|
}
|
|
30
51
|
}
|
|
31
52
|
function computed(callback) {
|
package/dist/computed.js
CHANGED
|
@@ -1,29 +1,50 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import { effect, activeEffect, runEffect } from "./effect.js";
|
|
5
|
+
import { dirtyEffects } from "./signal.js";
|
|
6
|
+
let activeComputed;
|
|
7
|
+
class Computed {
|
|
6
8
|
constructor(callback) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.dirty = false;
|
|
15
|
-
}
|
|
9
|
+
__publicField(this, "state");
|
|
10
|
+
this.state = {
|
|
11
|
+
dirty: true,
|
|
12
|
+
computeds: /* @__PURE__ */ new Set(),
|
|
13
|
+
effect: void 0,
|
|
14
|
+
effects: /* @__PURE__ */ new Set(),
|
|
15
|
+
value: void 0
|
|
16
16
|
};
|
|
17
|
-
|
|
17
|
+
this.state.effect = effect(() => {
|
|
18
|
+
if (this.state.dirty) {
|
|
19
|
+
const previousComputed = activeComputed;
|
|
20
|
+
activeComputed = this;
|
|
21
|
+
const value = callback();
|
|
22
|
+
activeComputed = previousComputed;
|
|
23
|
+
if (!Object.is(this.state.value, value)) {
|
|
24
|
+
this.state.value = value;
|
|
25
|
+
for (const comp of this.state.computeds) {
|
|
26
|
+
comp.state.dirty = true;
|
|
27
|
+
dirtyEffects.add(comp.state.effect);
|
|
28
|
+
}
|
|
29
|
+
for (const effect2 of this.state.effects) {
|
|
30
|
+
dirtyEffects.add(effect2);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
this.state.dirty = false;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
18
36
|
}
|
|
19
37
|
get() {
|
|
20
|
-
if (
|
|
21
|
-
this.
|
|
38
|
+
if (activeComputed != null && activeComputed !== this) {
|
|
39
|
+
this.state.computeds.add(activeComputed);
|
|
40
|
+
}
|
|
41
|
+
if (activeEffect != null && activeEffect !== this.state.effect) {
|
|
42
|
+
this.state.effects.add(activeEffect);
|
|
22
43
|
}
|
|
23
|
-
if (this.dirty) {
|
|
24
|
-
runEffect(this);
|
|
44
|
+
if (this.state.dirty) {
|
|
45
|
+
runEffect(this.state.effect);
|
|
25
46
|
}
|
|
26
|
-
return this.value;
|
|
47
|
+
return this.state.value;
|
|
27
48
|
}
|
|
28
49
|
}
|
|
29
50
|
function computed(callback) {
|
|
@@ -31,5 +52,6 @@ function computed(callback) {
|
|
|
31
52
|
}
|
|
32
53
|
export {
|
|
33
54
|
Computed,
|
|
55
|
+
activeComputed,
|
|
34
56
|
computed
|
|
35
57
|
};
|
package/dist/effect.cjs
CHANGED
|
@@ -5,29 +5,25 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
5
5
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
6
6
|
exports.activeEffect = void 0;
|
|
7
7
|
class Effect {
|
|
8
|
-
constructor() {
|
|
9
|
-
__publicField(this, "
|
|
10
|
-
|
|
8
|
+
constructor(callback) {
|
|
9
|
+
__publicField(this, "state");
|
|
10
|
+
this.state = {
|
|
11
|
+
callback
|
|
12
|
+
};
|
|
13
|
+
runEffect(this);
|
|
11
14
|
}
|
|
12
15
|
}
|
|
13
16
|
function runEffect(effect2) {
|
|
14
|
-
for (const signal of effect2.signals) {
|
|
15
|
-
signal.state.observers.delete(effect2);
|
|
16
|
-
}
|
|
17
|
-
effect2.signals.clear();
|
|
18
17
|
const previousEffect = exports.activeEffect;
|
|
19
18
|
exports.activeEffect = effect2;
|
|
20
19
|
try {
|
|
21
|
-
effect2.callback();
|
|
20
|
+
effect2.state.callback();
|
|
22
21
|
} finally {
|
|
23
22
|
exports.activeEffect = previousEffect;
|
|
24
23
|
}
|
|
25
24
|
}
|
|
26
25
|
function effect(callback) {
|
|
27
|
-
|
|
28
|
-
instance.callback = callback;
|
|
29
|
-
runEffect(instance);
|
|
30
|
-
return instance;
|
|
26
|
+
return new Effect(callback);
|
|
31
27
|
}
|
|
32
28
|
exports.Effect = Effect;
|
|
33
29
|
exports.effect = effect;
|
package/dist/effect.js
CHANGED
|
@@ -3,29 +3,25 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
let activeEffect;
|
|
5
5
|
class Effect {
|
|
6
|
-
constructor() {
|
|
7
|
-
__publicField(this, "
|
|
8
|
-
|
|
6
|
+
constructor(callback) {
|
|
7
|
+
__publicField(this, "state");
|
|
8
|
+
this.state = {
|
|
9
|
+
callback
|
|
10
|
+
};
|
|
11
|
+
runEffect(this);
|
|
9
12
|
}
|
|
10
13
|
}
|
|
11
14
|
function runEffect(effect2) {
|
|
12
|
-
for (const signal of effect2.signals) {
|
|
13
|
-
signal.state.observers.delete(effect2);
|
|
14
|
-
}
|
|
15
|
-
effect2.signals.clear();
|
|
16
15
|
const previousEffect = activeEffect;
|
|
17
16
|
activeEffect = effect2;
|
|
18
17
|
try {
|
|
19
|
-
effect2.callback();
|
|
18
|
+
effect2.state.callback();
|
|
20
19
|
} finally {
|
|
21
20
|
activeEffect = previousEffect;
|
|
22
21
|
}
|
|
23
22
|
}
|
|
24
23
|
function effect(callback) {
|
|
25
|
-
|
|
26
|
-
instance.callback = callback;
|
|
27
|
-
runEffect(instance);
|
|
28
|
-
return instance;
|
|
24
|
+
return new Effect(callback);
|
|
29
25
|
}
|
|
30
26
|
export {
|
|
31
27
|
Effect,
|
package/dist/mora.iife.js
CHANGED
|
@@ -3,72 +3,44 @@ var Mora = (function (exports) {
|
|
|
3
3
|
|
|
4
4
|
let activeEffect;
|
|
5
5
|
class Effect {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
state;
|
|
7
|
+
constructor(callback) {
|
|
8
|
+
this.state = {
|
|
9
|
+
callback,
|
|
10
|
+
};
|
|
11
|
+
runEffect(this);
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
function runEffect(effect) {
|
|
10
|
-
for (const signal of effect.signals) {
|
|
11
|
-
signal.state.observers.delete(effect);
|
|
12
|
-
}
|
|
13
|
-
effect.signals.clear();
|
|
14
15
|
const previousEffect = activeEffect;
|
|
15
16
|
activeEffect = effect;
|
|
16
17
|
try {
|
|
17
|
-
effect.callback();
|
|
18
|
+
effect.state.callback();
|
|
18
19
|
}
|
|
19
20
|
finally {
|
|
20
21
|
activeEffect = previousEffect;
|
|
21
22
|
}
|
|
22
23
|
}
|
|
23
24
|
function effect(callback) {
|
|
24
|
-
|
|
25
|
-
instance.callback = callback;
|
|
26
|
-
runEffect(instance);
|
|
27
|
-
return instance;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
class Computed extends Effect {
|
|
31
|
-
dirty = true;
|
|
32
|
-
effects = new Set();
|
|
33
|
-
value;
|
|
34
|
-
constructor(callback) {
|
|
35
|
-
super();
|
|
36
|
-
this.callback = () => {
|
|
37
|
-
if (this.dirty) {
|
|
38
|
-
this.value = callback();
|
|
39
|
-
this.dirty = false;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
runEffect(this);
|
|
43
|
-
}
|
|
44
|
-
get() {
|
|
45
|
-
if (activeEffect != null && activeEffect !== this) {
|
|
46
|
-
this.effects.add(activeEffect);
|
|
47
|
-
}
|
|
48
|
-
if (this.dirty) {
|
|
49
|
-
runEffect(this);
|
|
50
|
-
}
|
|
51
|
-
return this.value;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
function computed(callback) {
|
|
55
|
-
return new Computed(callback);
|
|
25
|
+
return new Effect(callback);
|
|
56
26
|
}
|
|
57
27
|
|
|
58
28
|
const dirtyEffects = new Set();
|
|
59
|
-
let batchDepth = 0;
|
|
60
29
|
class Signal {
|
|
61
30
|
state;
|
|
62
31
|
constructor(value) {
|
|
63
32
|
this.state = {
|
|
64
33
|
value,
|
|
65
|
-
|
|
34
|
+
computeds: new Set(),
|
|
35
|
+
effects: new Set(),
|
|
66
36
|
};
|
|
67
37
|
}
|
|
68
38
|
get() {
|
|
39
|
+
if (activeComputed != null) {
|
|
40
|
+
this.state.computeds.add(activeComputed);
|
|
41
|
+
}
|
|
69
42
|
if (activeEffect != null) {
|
|
70
|
-
this.state.
|
|
71
|
-
activeEffect.signals.add(this);
|
|
43
|
+
this.state.effects.add(activeEffect);
|
|
72
44
|
}
|
|
73
45
|
return this.state.value;
|
|
74
46
|
}
|
|
@@ -77,38 +49,18 @@ var Mora = (function (exports) {
|
|
|
77
49
|
return;
|
|
78
50
|
}
|
|
79
51
|
this.state.value = value;
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
for (const observer of this.state.observers) {
|
|
84
|
-
if (observer instanceof Computed) {
|
|
85
|
-
observer.dirty = true;
|
|
86
|
-
dirtyEffects.add(observer);
|
|
87
|
-
for (const effect of observer.effects) {
|
|
88
|
-
dirtyEffects.add(effect);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
dirtyEffects.add(observer);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
if (isOutermostBatch) {
|
|
96
|
-
queueMicrotask(() => {
|
|
97
|
-
try {
|
|
98
|
-
for (const effect of dirtyEffects) {
|
|
99
|
-
runEffect(effect);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
finally {
|
|
103
|
-
dirtyEffects.clear();
|
|
104
|
-
batchDepth = 0;
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
}
|
|
52
|
+
for (const computed of this.state.computeds) {
|
|
53
|
+
computed.state.dirty = true;
|
|
54
|
+
dirtyEffects.add(computed.state.effect);
|
|
108
55
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
56
|
+
for (const effect of this.state.effects) {
|
|
57
|
+
dirtyEffects.add(effect);
|
|
58
|
+
}
|
|
59
|
+
while (dirtyEffects.size > 0) {
|
|
60
|
+
const effects = [...dirtyEffects];
|
|
61
|
+
dirtyEffects.clear();
|
|
62
|
+
for (const effect of effects) {
|
|
63
|
+
runEffect(effect);
|
|
112
64
|
}
|
|
113
65
|
}
|
|
114
66
|
}
|
|
@@ -120,6 +72,54 @@ var Mora = (function (exports) {
|
|
|
120
72
|
return new Signal(value);
|
|
121
73
|
}
|
|
122
74
|
|
|
75
|
+
let activeComputed;
|
|
76
|
+
class Computed {
|
|
77
|
+
state;
|
|
78
|
+
constructor(callback) {
|
|
79
|
+
this.state = {
|
|
80
|
+
dirty: true,
|
|
81
|
+
computeds: new Set(),
|
|
82
|
+
effect: undefined,
|
|
83
|
+
effects: new Set(),
|
|
84
|
+
value: undefined,
|
|
85
|
+
};
|
|
86
|
+
this.state.effect = effect(() => {
|
|
87
|
+
if (this.state.dirty) {
|
|
88
|
+
const previousComputed = activeComputed;
|
|
89
|
+
activeComputed = this;
|
|
90
|
+
const value = callback();
|
|
91
|
+
activeComputed = previousComputed;
|
|
92
|
+
if (!Object.is(this.state.value, value)) {
|
|
93
|
+
this.state.value = value;
|
|
94
|
+
for (const comp of this.state.computeds) {
|
|
95
|
+
comp.state.dirty = true;
|
|
96
|
+
dirtyEffects.add(comp.state.effect);
|
|
97
|
+
}
|
|
98
|
+
for (const effect of this.state.effects) {
|
|
99
|
+
dirtyEffects.add(effect);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
this.state.dirty = false;
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
get() {
|
|
107
|
+
if (activeComputed != null && activeComputed !== this) {
|
|
108
|
+
this.state.computeds.add(activeComputed);
|
|
109
|
+
}
|
|
110
|
+
if (activeEffect != null && activeEffect !== this.state.effect) {
|
|
111
|
+
this.state.effects.add(activeEffect);
|
|
112
|
+
}
|
|
113
|
+
if (this.state.dirty) {
|
|
114
|
+
runEffect(this.state.effect);
|
|
115
|
+
}
|
|
116
|
+
return this.state.value;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function computed(callback) {
|
|
120
|
+
return new Computed(callback);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
123
|
exports.computed = computed;
|
|
124
124
|
exports.effect = effect;
|
|
125
125
|
exports.signal = signal;
|
package/dist/signal.cjs
CHANGED
|
@@ -6,19 +6,21 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
6
6
|
const computed = require("./computed.cjs");
|
|
7
7
|
const effect = require("./effect.cjs");
|
|
8
8
|
const dirtyEffects = /* @__PURE__ */ new Set();
|
|
9
|
-
let batchDepth = 0;
|
|
10
9
|
class Signal {
|
|
11
10
|
constructor(value) {
|
|
12
11
|
__publicField(this, "state");
|
|
13
12
|
this.state = {
|
|
14
13
|
value,
|
|
15
|
-
|
|
14
|
+
computeds: /* @__PURE__ */ new Set(),
|
|
15
|
+
effects: /* @__PURE__ */ new Set()
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
get() {
|
|
19
|
+
if (computed.activeComputed != null) {
|
|
20
|
+
this.state.computeds.add(computed.activeComputed);
|
|
21
|
+
}
|
|
19
22
|
if (effect.activeEffect != null) {
|
|
20
|
-
this.state.
|
|
21
|
-
effect.activeEffect.signals.add(this);
|
|
23
|
+
this.state.effects.add(effect.activeEffect);
|
|
22
24
|
}
|
|
23
25
|
return this.state.value;
|
|
24
26
|
}
|
|
@@ -27,35 +29,18 @@ class Signal {
|
|
|
27
29
|
return;
|
|
28
30
|
}
|
|
29
31
|
this.state.value = value;
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
if (isOutermostBatch) {
|
|
45
|
-
queueMicrotask(() => {
|
|
46
|
-
try {
|
|
47
|
-
for (const effect$1 of dirtyEffects) {
|
|
48
|
-
effect.runEffect(effect$1);
|
|
49
|
-
}
|
|
50
|
-
} finally {
|
|
51
|
-
dirtyEffects.clear();
|
|
52
|
-
batchDepth = 0;
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
} finally {
|
|
57
|
-
if (isOutermostBatch) {
|
|
58
|
-
batchDepth -= 1;
|
|
32
|
+
for (const computed2 of this.state.computeds) {
|
|
33
|
+
computed2.state.dirty = true;
|
|
34
|
+
dirtyEffects.add(computed2.state.effect);
|
|
35
|
+
}
|
|
36
|
+
for (const effect2 of this.state.effects) {
|
|
37
|
+
dirtyEffects.add(effect2);
|
|
38
|
+
}
|
|
39
|
+
while (dirtyEffects.size > 0) {
|
|
40
|
+
const effects = [...dirtyEffects];
|
|
41
|
+
dirtyEffects.clear();
|
|
42
|
+
for (const effect$1 of effects) {
|
|
43
|
+
effect.runEffect(effect$1);
|
|
59
44
|
}
|
|
60
45
|
}
|
|
61
46
|
}
|
|
@@ -67,4 +52,5 @@ function signal(value) {
|
|
|
67
52
|
return new Signal(value);
|
|
68
53
|
}
|
|
69
54
|
exports.Signal = Signal;
|
|
55
|
+
exports.dirtyEffects = dirtyEffects;
|
|
70
56
|
exports.signal = signal;
|
package/dist/signal.js
CHANGED
|
@@ -1,22 +1,24 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
import {
|
|
4
|
+
import { activeComputed } from "./computed.js";
|
|
5
5
|
import { activeEffect, runEffect } from "./effect.js";
|
|
6
6
|
const dirtyEffects = /* @__PURE__ */ new Set();
|
|
7
|
-
let batchDepth = 0;
|
|
8
7
|
class Signal {
|
|
9
8
|
constructor(value) {
|
|
10
9
|
__publicField(this, "state");
|
|
11
10
|
this.state = {
|
|
12
11
|
value,
|
|
13
|
-
|
|
12
|
+
computeds: /* @__PURE__ */ new Set(),
|
|
13
|
+
effects: /* @__PURE__ */ new Set()
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
get() {
|
|
17
|
+
if (activeComputed != null) {
|
|
18
|
+
this.state.computeds.add(activeComputed);
|
|
19
|
+
}
|
|
17
20
|
if (activeEffect != null) {
|
|
18
|
-
this.state.
|
|
19
|
-
activeEffect.signals.add(this);
|
|
21
|
+
this.state.effects.add(activeEffect);
|
|
20
22
|
}
|
|
21
23
|
return this.state.value;
|
|
22
24
|
}
|
|
@@ -25,35 +27,18 @@ class Signal {
|
|
|
25
27
|
return;
|
|
26
28
|
}
|
|
27
29
|
this.state.value = value;
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
if (isOutermostBatch) {
|
|
43
|
-
queueMicrotask(() => {
|
|
44
|
-
try {
|
|
45
|
-
for (const effect of dirtyEffects) {
|
|
46
|
-
runEffect(effect);
|
|
47
|
-
}
|
|
48
|
-
} finally {
|
|
49
|
-
dirtyEffects.clear();
|
|
50
|
-
batchDepth = 0;
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
} finally {
|
|
55
|
-
if (isOutermostBatch) {
|
|
56
|
-
batchDepth -= 1;
|
|
30
|
+
for (const computed of this.state.computeds) {
|
|
31
|
+
computed.state.dirty = true;
|
|
32
|
+
dirtyEffects.add(computed.state.effect);
|
|
33
|
+
}
|
|
34
|
+
for (const effect of this.state.effects) {
|
|
35
|
+
dirtyEffects.add(effect);
|
|
36
|
+
}
|
|
37
|
+
while (dirtyEffects.size > 0) {
|
|
38
|
+
const effects = [...dirtyEffects];
|
|
39
|
+
dirtyEffects.clear();
|
|
40
|
+
for (const effect of effects) {
|
|
41
|
+
runEffect(effect);
|
|
57
42
|
}
|
|
58
43
|
}
|
|
59
44
|
}
|
|
@@ -66,5 +51,6 @@ function signal(value) {
|
|
|
66
51
|
}
|
|
67
52
|
export {
|
|
68
53
|
Signal,
|
|
54
|
+
dirtyEffects,
|
|
69
55
|
signal
|
|
70
56
|
};
|
package/package.json
CHANGED
package/src/computed.ts
CHANGED
|
@@ -1,33 +1,71 @@
|
|
|
1
|
-
import {Effect, activeEffect, runEffect} from './effect';
|
|
1
|
+
import {type Effect, activeEffect, effect, runEffect} from './effect';
|
|
2
|
+
import {dirtyEffects} from './signal';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
dirty
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
type ComputedState<Value> = {
|
|
5
|
+
dirty: boolean;
|
|
6
|
+
computeds: Set<Computed<unknown>>;
|
|
7
|
+
effect: Effect;
|
|
8
|
+
effects: Set<Effect>;
|
|
9
|
+
value: Value;
|
|
10
|
+
};
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
super();
|
|
12
|
+
export let activeComputed: Computed<unknown> | undefined;
|
|
10
13
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
export class Computed<Value> {
|
|
15
|
+
readonly state: ComputedState<Value>;
|
|
16
|
+
|
|
17
|
+
constructor(callback: () => Value) {
|
|
18
|
+
this.state = {
|
|
19
|
+
dirty: true,
|
|
20
|
+
computeds: new Set(),
|
|
21
|
+
effect: undefined as never,
|
|
22
|
+
effects: new Set(),
|
|
23
|
+
value: undefined as never,
|
|
16
24
|
};
|
|
17
25
|
|
|
18
|
-
|
|
26
|
+
this.state.effect = effect(() => {
|
|
27
|
+
if (this.state.dirty) {
|
|
28
|
+
const previousComputed = activeComputed;
|
|
29
|
+
|
|
30
|
+
activeComputed = this;
|
|
31
|
+
|
|
32
|
+
const value = callback();
|
|
33
|
+
|
|
34
|
+
activeComputed = previousComputed;
|
|
35
|
+
|
|
36
|
+
if (!Object.is(this.state.value, value)) {
|
|
37
|
+
this.state.value = value;
|
|
38
|
+
|
|
39
|
+
for (const comp of this.state.computeds) {
|
|
40
|
+
comp.state.dirty = true;
|
|
41
|
+
|
|
42
|
+
dirtyEffects.add(comp.state.effect);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for (const effect of this.state.effects) {
|
|
46
|
+
dirtyEffects.add(effect);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.state.dirty = false;
|
|
51
|
+
}
|
|
52
|
+
});
|
|
19
53
|
}
|
|
20
54
|
|
|
21
55
|
get(): Value {
|
|
22
|
-
if (
|
|
23
|
-
this.
|
|
56
|
+
if (activeComputed != null && activeComputed !== this) {
|
|
57
|
+
this.state.computeds.add(activeComputed);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (activeEffect != null && activeEffect !== this.state.effect) {
|
|
61
|
+
this.state.effects.add(activeEffect);
|
|
24
62
|
}
|
|
25
63
|
|
|
26
|
-
if (this.dirty) {
|
|
27
|
-
runEffect(this);
|
|
64
|
+
if (this.state.dirty) {
|
|
65
|
+
runEffect(this.state.effect);
|
|
28
66
|
}
|
|
29
67
|
|
|
30
|
-
return this.value;
|
|
68
|
+
return this.state.value;
|
|
31
69
|
}
|
|
32
70
|
}
|
|
33
71
|
|
package/src/effect.ts
CHANGED
|
@@ -1,38 +1,35 @@
|
|
|
1
1
|
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
type EffectState = {
|
|
4
|
+
callback: GenericCallback;
|
|
5
|
+
};
|
|
3
6
|
|
|
4
7
|
export let activeEffect: Effect | undefined;
|
|
5
8
|
|
|
6
9
|
export class Effect {
|
|
7
|
-
|
|
8
|
-
signals = new Set<Signal<unknown>>();
|
|
9
|
-
}
|
|
10
|
+
readonly state: EffectState;
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
constructor(callback: GenericCallback) {
|
|
13
|
+
this.state = {
|
|
14
|
+
callback,
|
|
15
|
+
};
|
|
15
16
|
|
|
16
|
-
|
|
17
|
+
runEffect(this);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
17
20
|
|
|
21
|
+
export function runEffect(effect: Effect): void {
|
|
18
22
|
const previousEffect = activeEffect;
|
|
19
23
|
|
|
20
24
|
activeEffect = effect;
|
|
21
25
|
|
|
22
26
|
try {
|
|
23
|
-
effect.callback();
|
|
27
|
+
effect.state.callback();
|
|
24
28
|
} finally {
|
|
25
29
|
activeEffect = previousEffect;
|
|
26
30
|
}
|
|
27
31
|
}
|
|
28
32
|
|
|
29
33
|
export function effect(callback: GenericCallback): Effect {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
instance.callback = callback;
|
|
33
|
-
|
|
34
|
-
runEffect(instance);
|
|
35
|
-
|
|
36
|
-
return instance;
|
|
34
|
+
return new Effect(callback);
|
|
37
35
|
}
|
|
38
|
-
|
package/src/signal.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import {Computed} from './computed';
|
|
1
|
+
import {type Computed, activeComputed} from './computed';
|
|
2
2
|
import {type Effect, activeEffect, runEffect} from './effect';
|
|
3
3
|
|
|
4
4
|
type SignalState<Value> = {
|
|
5
|
-
|
|
5
|
+
computeds: Set<Computed<unknown>>;
|
|
6
|
+
effects: Set<Effect>;
|
|
6
7
|
value: Value;
|
|
7
8
|
};
|
|
8
9
|
|
|
9
|
-
const dirtyEffects = new Set<Effect>();
|
|
10
|
-
|
|
11
|
-
let batchDepth = 0;
|
|
10
|
+
export const dirtyEffects = new Set<Effect>();
|
|
12
11
|
|
|
13
12
|
export class Signal<Value> {
|
|
14
13
|
readonly state: SignalState<Value>;
|
|
@@ -16,14 +15,18 @@ export class Signal<Value> {
|
|
|
16
15
|
constructor(value: Value) {
|
|
17
16
|
this.state = {
|
|
18
17
|
value,
|
|
19
|
-
|
|
18
|
+
computeds: new Set(),
|
|
19
|
+
effects: new Set(),
|
|
20
20
|
};
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
get(): Value {
|
|
24
|
+
if (activeComputed != null) {
|
|
25
|
+
this.state.computeds.add(activeComputed);
|
|
26
|
+
}
|
|
27
|
+
|
|
24
28
|
if (activeEffect != null) {
|
|
25
|
-
this.state.
|
|
26
|
-
activeEffect.signals.add(this as never);
|
|
29
|
+
this.state.effects.add(activeEffect);
|
|
27
30
|
}
|
|
28
31
|
|
|
29
32
|
return this.state.value;
|
|
@@ -36,41 +39,23 @@ export class Signal<Value> {
|
|
|
36
39
|
|
|
37
40
|
this.state.value = value;
|
|
38
41
|
|
|
39
|
-
const
|
|
42
|
+
for (const computed of this.state.computeds) {
|
|
43
|
+
computed.state.dirty = true;
|
|
40
44
|
|
|
41
|
-
|
|
45
|
+
dirtyEffects.add(computed.state.effect);
|
|
46
|
+
}
|
|
42
47
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
observer.dirty = true;
|
|
48
|
+
for (const effect of this.state.effects) {
|
|
49
|
+
dirtyEffects.add(effect);
|
|
50
|
+
}
|
|
47
51
|
|
|
48
|
-
|
|
52
|
+
while (dirtyEffects.size > 0) {
|
|
53
|
+
const effects = [...dirtyEffects];
|
|
49
54
|
|
|
50
|
-
|
|
51
|
-
dirtyEffects.add(effect);
|
|
52
|
-
}
|
|
53
|
-
} else {
|
|
54
|
-
dirtyEffects.add(observer);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
55
|
+
dirtyEffects.clear();
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
for (const effect of dirtyEffects) {
|
|
62
|
-
runEffect(effect);
|
|
63
|
-
}
|
|
64
|
-
} finally {
|
|
65
|
-
dirtyEffects.clear();
|
|
66
|
-
|
|
67
|
-
batchDepth = 0;
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
} finally {
|
|
72
|
-
if (isOutermostBatch) {
|
|
73
|
-
batchDepth -= 1;
|
|
57
|
+
for (const effect of effects) {
|
|
58
|
+
runEffect(effect);
|
|
74
59
|
}
|
|
75
60
|
}
|
|
76
61
|
}
|
package/types/computed.d.cts
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
3
|
export type GenericCallback = (...args: any[]) => any;
|
|
4
|
-
|
|
4
|
+
export type EffectState = {
|
|
5
5
|
callback: GenericCallback;
|
|
6
|
-
|
|
6
|
+
};
|
|
7
|
+
declare class Effect {
|
|
8
|
+
readonly state: EffectState;
|
|
9
|
+
constructor(callback: GenericCallback);
|
|
7
10
|
}
|
|
8
|
-
export
|
|
11
|
+
export type ComputedState<Value> = {
|
|
9
12
|
dirty: boolean;
|
|
13
|
+
computeds: Set<Computed<unknown>>;
|
|
14
|
+
effect: Effect;
|
|
10
15
|
effects: Set<Effect>;
|
|
11
16
|
value: Value;
|
|
17
|
+
};
|
|
18
|
+
export declare let activeComputed: Computed<unknown> | undefined;
|
|
19
|
+
export declare class Computed<Value> {
|
|
20
|
+
readonly state: ComputedState<Value>;
|
|
12
21
|
constructor(callback: () => Value);
|
|
13
22
|
get(): Value;
|
|
14
23
|
}
|
|
15
24
|
export declare function computed<Value>(callback: () => Value): Computed<Value>;
|
|
16
|
-
export type SignalState<Value> = {
|
|
17
|
-
observers: Set<Computed<never> | Effect>;
|
|
18
|
-
value: Value;
|
|
19
|
-
};
|
|
20
|
-
declare class Signal<Value> {
|
|
21
|
-
readonly state: SignalState<Value>;
|
|
22
|
-
constructor(value: Value);
|
|
23
|
-
get(): Value;
|
|
24
|
-
set(value: Value): void;
|
|
25
|
-
update(callback: (value: Value) => Value): void;
|
|
26
|
-
}
|
|
27
25
|
|
|
28
26
|
export {};
|
package/types/computed.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
import { Effect } from './effect';
|
|
2
|
-
|
|
1
|
+
import { type Effect } from './effect';
|
|
2
|
+
type ComputedState<Value> = {
|
|
3
3
|
dirty: boolean;
|
|
4
|
+
computeds: Set<Computed<unknown>>;
|
|
5
|
+
effect: Effect;
|
|
4
6
|
effects: Set<Effect>;
|
|
5
7
|
value: Value;
|
|
8
|
+
};
|
|
9
|
+
export declare let activeComputed: Computed<unknown> | undefined;
|
|
10
|
+
export declare class Computed<Value> {
|
|
11
|
+
readonly state: ComputedState<Value>;
|
|
6
12
|
constructor(callback: () => Value);
|
|
7
13
|
get(): Value;
|
|
8
14
|
}
|
|
9
15
|
export declare function computed<Value>(callback: () => Value): Computed<Value>;
|
|
16
|
+
export {};
|
package/types/effect.d.cts
CHANGED
|
@@ -1,30 +1,15 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
3
|
export type GenericCallback = (...args: any[]) => any;
|
|
4
|
+
export type EffectState = {
|
|
5
|
+
callback: GenericCallback;
|
|
6
|
+
};
|
|
4
7
|
export declare let activeEffect: Effect | undefined;
|
|
5
8
|
export declare class Effect {
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
readonly state: EffectState;
|
|
10
|
+
constructor(callback: GenericCallback);
|
|
8
11
|
}
|
|
9
12
|
export declare function runEffect(effect: Effect): void;
|
|
10
13
|
export declare function effect(callback: GenericCallback): Effect;
|
|
11
|
-
declare class Computed<Value> extends Effect {
|
|
12
|
-
dirty: boolean;
|
|
13
|
-
effects: Set<Effect>;
|
|
14
|
-
value: Value;
|
|
15
|
-
constructor(callback: () => Value);
|
|
16
|
-
get(): Value;
|
|
17
|
-
}
|
|
18
|
-
export type SignalState<Value> = {
|
|
19
|
-
observers: Set<Computed<never> | Effect>;
|
|
20
|
-
value: Value;
|
|
21
|
-
};
|
|
22
|
-
declare class Signal<Value> {
|
|
23
|
-
readonly state: SignalState<Value>;
|
|
24
|
-
constructor(value: Value);
|
|
25
|
-
get(): Value;
|
|
26
|
-
set(value: Value): void;
|
|
27
|
-
update(callback: (value: Value) => Value): void;
|
|
28
|
-
}
|
|
29
14
|
|
|
30
15
|
export {};
|
package/types/effect.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import type { GenericCallback } from '@oscarpalmer/atoms/models';
|
|
2
|
-
|
|
2
|
+
type EffectState = {
|
|
3
|
+
callback: GenericCallback;
|
|
4
|
+
};
|
|
3
5
|
export declare let activeEffect: Effect | undefined;
|
|
4
6
|
export declare class Effect {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
readonly state: EffectState;
|
|
8
|
+
constructor(callback: GenericCallback);
|
|
7
9
|
}
|
|
8
10
|
export declare function runEffect(effect: Effect): void;
|
|
9
11
|
export declare function effect(callback: GenericCallback): Effect;
|
|
12
|
+
export {};
|
package/types/index.d.cts
CHANGED
|
@@ -1,21 +1,30 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
3
|
export type GenericCallback = (...args: any[]) => any;
|
|
4
|
-
export
|
|
4
|
+
export type EffectState = {
|
|
5
5
|
callback: GenericCallback;
|
|
6
|
-
|
|
6
|
+
};
|
|
7
|
+
export declare class Effect {
|
|
8
|
+
readonly state: EffectState;
|
|
9
|
+
constructor(callback: GenericCallback);
|
|
7
10
|
}
|
|
8
11
|
export declare function effect(callback: GenericCallback): Effect;
|
|
9
|
-
export
|
|
12
|
+
export type ComputedState<Value> = {
|
|
10
13
|
dirty: boolean;
|
|
14
|
+
computeds: Set<Computed<unknown>>;
|
|
15
|
+
effect: Effect;
|
|
11
16
|
effects: Set<Effect>;
|
|
12
17
|
value: Value;
|
|
18
|
+
};
|
|
19
|
+
export declare class Computed<Value> {
|
|
20
|
+
readonly state: ComputedState<Value>;
|
|
13
21
|
constructor(callback: () => Value);
|
|
14
22
|
get(): Value;
|
|
15
23
|
}
|
|
16
24
|
export declare function computed<Value>(callback: () => Value): Computed<Value>;
|
|
17
25
|
export type SignalState<Value> = {
|
|
18
|
-
|
|
26
|
+
computeds: Set<Computed<unknown>>;
|
|
27
|
+
effects: Set<Effect>;
|
|
19
28
|
value: Value;
|
|
20
29
|
};
|
|
21
30
|
export declare class Signal<Value> {
|
package/types/signal.d.cts
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
3
|
export type GenericCallback = (...args: any[]) => any;
|
|
4
|
-
|
|
4
|
+
export type EffectState = {
|
|
5
5
|
callback: GenericCallback;
|
|
6
|
-
|
|
6
|
+
};
|
|
7
|
+
declare class Effect {
|
|
8
|
+
readonly state: EffectState;
|
|
9
|
+
constructor(callback: GenericCallback);
|
|
7
10
|
}
|
|
8
|
-
|
|
11
|
+
export type ComputedState<Value> = {
|
|
9
12
|
dirty: boolean;
|
|
13
|
+
computeds: Set<Computed<unknown>>;
|
|
14
|
+
effect: Effect;
|
|
10
15
|
effects: Set<Effect>;
|
|
11
16
|
value: Value;
|
|
17
|
+
};
|
|
18
|
+
declare class Computed<Value> {
|
|
19
|
+
readonly state: ComputedState<Value>;
|
|
12
20
|
constructor(callback: () => Value);
|
|
13
21
|
get(): Value;
|
|
14
22
|
}
|
|
15
23
|
export type SignalState<Value> = {
|
|
16
|
-
|
|
24
|
+
computeds: Set<Computed<unknown>>;
|
|
25
|
+
effects: Set<Effect>;
|
|
17
26
|
value: Value;
|
|
18
27
|
};
|
|
28
|
+
export declare const dirtyEffects: Set<Effect>;
|
|
19
29
|
export declare class Signal<Value> {
|
|
20
30
|
readonly state: SignalState<Value>;
|
|
21
31
|
constructor(value: Value);
|
package/types/signal.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import { Computed } from './computed';
|
|
1
|
+
import { type Computed } from './computed';
|
|
2
2
|
import { type Effect } from './effect';
|
|
3
3
|
type SignalState<Value> = {
|
|
4
|
-
|
|
4
|
+
computeds: Set<Computed<unknown>>;
|
|
5
|
+
effects: Set<Effect>;
|
|
5
6
|
value: Value;
|
|
6
7
|
};
|
|
8
|
+
export declare const dirtyEffects: Set<Effect>;
|
|
7
9
|
export declare class Signal<Value> {
|
|
8
10
|
readonly state: SignalState<Value>;
|
|
9
11
|
constructor(value: Value);
|