@oscarpalmer/mora 0.4.0 → 0.5.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/batch.cjs +27 -0
- package/dist/batch.js +28 -0
- package/dist/computed.cjs +6 -4
- package/dist/computed.js +5 -3
- package/dist/effect.cjs +3 -1
- package/dist/effect.js +3 -1
- package/dist/index.cjs +3 -0
- package/dist/index.js +4 -1
- package/dist/mora.full.js +162 -0
- package/dist/signal.cjs +15 -11
- package/dist/signal.js +13 -9
- package/package.json +11 -19
- package/src/batch.ts +35 -0
- package/src/computed.ts +15 -4
- package/src/effect.ts +7 -2
- package/src/index.ts +1 -0
- package/src/signal.ts +13 -11
- package/types/batch.d.cts +14 -0
- package/types/batch.d.ts +10 -0
- package/types/computed.d.cts +7 -1
- package/types/computed.d.ts +7 -1
- package/types/effect.d.cts +5 -1
- package/types/effect.d.ts +5 -1
- package/types/index.d.cts +27 -1
- package/types/index.d.ts +1 -0
- package/types/signal.d.cts +13 -2
- package/types/signal.d.ts +9 -1
- package/dist/mora.iife.js +0 -129
package/dist/batch.cjs
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const effect = require("./effect.cjs");
|
|
4
|
+
function flushEffects() {
|
|
5
|
+
while (effect.dirtyEffects.size > 0) {
|
|
6
|
+
const effects = [...effect.dirtyEffects];
|
|
7
|
+
effect.dirtyEffects.clear();
|
|
8
|
+
for (const effect$1 of effects) {
|
|
9
|
+
effect.runEffect(effect$1);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function startBatch() {
|
|
14
|
+
exports.batchDepth += 1;
|
|
15
|
+
}
|
|
16
|
+
function stopBatch() {
|
|
17
|
+
if (exports.batchDepth > 0) {
|
|
18
|
+
exports.batchDepth -= 1;
|
|
19
|
+
}
|
|
20
|
+
if (exports.batchDepth === 0) {
|
|
21
|
+
flushEffects();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.batchDepth = 0;
|
|
25
|
+
exports.flushEffects = flushEffects;
|
|
26
|
+
exports.startBatch = startBatch;
|
|
27
|
+
exports.stopBatch = stopBatch;
|
package/dist/batch.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { dirtyEffects, runEffect } from "./effect.js";
|
|
2
|
+
function flushEffects() {
|
|
3
|
+
while (dirtyEffects.size > 0) {
|
|
4
|
+
const effects = [...dirtyEffects];
|
|
5
|
+
dirtyEffects.clear();
|
|
6
|
+
for (const effect of effects) {
|
|
7
|
+
runEffect(effect);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
function startBatch() {
|
|
12
|
+
batchDepth += 1;
|
|
13
|
+
}
|
|
14
|
+
function stopBatch() {
|
|
15
|
+
if (batchDepth > 0) {
|
|
16
|
+
batchDepth -= 1;
|
|
17
|
+
}
|
|
18
|
+
if (batchDepth === 0) {
|
|
19
|
+
flushEffects();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
let batchDepth = 0;
|
|
23
|
+
export {
|
|
24
|
+
batchDepth,
|
|
25
|
+
flushEffects,
|
|
26
|
+
startBatch,
|
|
27
|
+
stopBatch
|
|
28
|
+
};
|
package/dist/computed.cjs
CHANGED
|
@@ -4,14 +4,13 @@ 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
|
-
const signal = require("./signal.cjs");
|
|
8
7
|
exports.activeComputed = void 0;
|
|
9
8
|
class Computed {
|
|
10
9
|
constructor(callback) {
|
|
11
10
|
__publicField(this, "state");
|
|
12
11
|
this.state = {
|
|
13
|
-
dirty: true,
|
|
14
12
|
computeds: /* @__PURE__ */ new Set(),
|
|
13
|
+
dirty: true,
|
|
15
14
|
effect: void 0,
|
|
16
15
|
effects: /* @__PURE__ */ new Set(),
|
|
17
16
|
value: void 0
|
|
@@ -26,16 +25,19 @@ class Computed {
|
|
|
26
25
|
this.state.value = value;
|
|
27
26
|
for (const comp of this.state.computeds) {
|
|
28
27
|
comp.state.dirty = true;
|
|
29
|
-
|
|
28
|
+
effect.dirtyEffects.add(comp.state.effect);
|
|
30
29
|
}
|
|
31
30
|
for (const effect2 of this.state.effects) {
|
|
32
|
-
|
|
31
|
+
effect.dirtyEffects.add(effect2);
|
|
33
32
|
}
|
|
34
33
|
}
|
|
35
34
|
this.state.dirty = false;
|
|
36
35
|
}
|
|
37
36
|
});
|
|
38
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Get the value
|
|
40
|
+
*/
|
|
39
41
|
get() {
|
|
40
42
|
if (exports.activeComputed != null && exports.activeComputed !== this) {
|
|
41
43
|
this.state.computeds.add(exports.activeComputed);
|
package/dist/computed.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
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 { effect, activeEffect, runEffect } from "./effect.js";
|
|
5
|
-
import { dirtyEffects } from "./signal.js";
|
|
4
|
+
import { effect, dirtyEffects, activeEffect, runEffect } from "./effect.js";
|
|
6
5
|
let activeComputed;
|
|
7
6
|
class Computed {
|
|
8
7
|
constructor(callback) {
|
|
9
8
|
__publicField(this, "state");
|
|
10
9
|
this.state = {
|
|
11
|
-
dirty: true,
|
|
12
10
|
computeds: /* @__PURE__ */ new Set(),
|
|
11
|
+
dirty: true,
|
|
13
12
|
effect: void 0,
|
|
14
13
|
effects: /* @__PURE__ */ new Set(),
|
|
15
14
|
value: void 0
|
|
@@ -34,6 +33,9 @@ class Computed {
|
|
|
34
33
|
}
|
|
35
34
|
});
|
|
36
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Get the value
|
|
38
|
+
*/
|
|
37
39
|
get() {
|
|
38
40
|
if (activeComputed != null && activeComputed !== this) {
|
|
39
41
|
this.state.computeds.add(activeComputed);
|
package/dist/effect.cjs
CHANGED
|
@@ -3,7 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
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
|
-
exports.activeEffect = void 0;
|
|
7
6
|
class Effect {
|
|
8
7
|
constructor(callback) {
|
|
9
8
|
__publicField(this, "state");
|
|
@@ -25,6 +24,9 @@ function runEffect(effect2) {
|
|
|
25
24
|
function effect(callback) {
|
|
26
25
|
return new Effect(callback);
|
|
27
26
|
}
|
|
27
|
+
exports.activeEffect = void 0;
|
|
28
|
+
const dirtyEffects = /* @__PURE__ */ new Set();
|
|
28
29
|
exports.Effect = Effect;
|
|
30
|
+
exports.dirtyEffects = dirtyEffects;
|
|
29
31
|
exports.effect = effect;
|
|
30
32
|
exports.runEffect = runEffect;
|
package/dist/effect.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
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
|
-
let activeEffect;
|
|
5
4
|
class Effect {
|
|
6
5
|
constructor(callback) {
|
|
7
6
|
__publicField(this, "state");
|
|
@@ -23,9 +22,12 @@ function runEffect(effect2) {
|
|
|
23
22
|
function effect(callback) {
|
|
24
23
|
return new Effect(callback);
|
|
25
24
|
}
|
|
25
|
+
let activeEffect;
|
|
26
|
+
const dirtyEffects = /* @__PURE__ */ new Set();
|
|
26
27
|
export {
|
|
27
28
|
Effect,
|
|
28
29
|
activeEffect,
|
|
30
|
+
dirtyEffects,
|
|
29
31
|
effect,
|
|
30
32
|
runEffect
|
|
31
33
|
};
|
package/dist/index.cjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const batch = require("./batch.cjs");
|
|
3
4
|
const computed = require("./computed.cjs");
|
|
4
5
|
const effect = require("./effect.cjs");
|
|
5
6
|
const signal = require("./signal.cjs");
|
|
7
|
+
exports.startBatch = batch.startBatch;
|
|
8
|
+
exports.stopBatch = batch.stopBatch;
|
|
6
9
|
exports.computed = computed.computed;
|
|
7
10
|
exports.effect = effect.effect;
|
|
8
11
|
exports.signal = signal.signal;
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
+
import { startBatch, stopBatch } from "./batch.js";
|
|
1
2
|
import { computed } from "./computed.js";
|
|
2
3
|
import { effect } from "./effect.js";
|
|
3
4
|
import { signal } from "./signal.js";
|
|
4
5
|
export {
|
|
5
6
|
computed,
|
|
6
7
|
effect,
|
|
7
|
-
signal
|
|
8
|
+
signal,
|
|
9
|
+
startBatch,
|
|
10
|
+
stopBatch
|
|
8
11
|
};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
class Effect {
|
|
2
|
+
state;
|
|
3
|
+
constructor(callback) {
|
|
4
|
+
this.state = {
|
|
5
|
+
callback,
|
|
6
|
+
};
|
|
7
|
+
runEffect(this);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function runEffect(effect) {
|
|
11
|
+
const previousEffect = activeEffect;
|
|
12
|
+
activeEffect = effect;
|
|
13
|
+
try {
|
|
14
|
+
effect.state.callback();
|
|
15
|
+
}
|
|
16
|
+
finally {
|
|
17
|
+
activeEffect = previousEffect;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Create an effect
|
|
22
|
+
*/
|
|
23
|
+
function effect(callback) {
|
|
24
|
+
return new Effect(callback);
|
|
25
|
+
}
|
|
26
|
+
let activeEffect;
|
|
27
|
+
const dirtyEffects = new Set();
|
|
28
|
+
|
|
29
|
+
function flushEffects() {
|
|
30
|
+
while (dirtyEffects.size > 0) {
|
|
31
|
+
const effects = [...dirtyEffects];
|
|
32
|
+
dirtyEffects.clear();
|
|
33
|
+
for (const effect of effects) {
|
|
34
|
+
runEffect(effect);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
40
|
+
*/
|
|
41
|
+
function startBatch() {
|
|
42
|
+
batchDepth += 1;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Stop batching effects and flush _(run)_ them
|
|
46
|
+
*/
|
|
47
|
+
function stopBatch() {
|
|
48
|
+
if (batchDepth > 0) {
|
|
49
|
+
batchDepth -= 1;
|
|
50
|
+
}
|
|
51
|
+
if (batchDepth === 0) {
|
|
52
|
+
flushEffects();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
let batchDepth = 0;
|
|
56
|
+
|
|
57
|
+
let activeComputed;
|
|
58
|
+
class Computed {
|
|
59
|
+
state;
|
|
60
|
+
constructor(callback) {
|
|
61
|
+
this.state = {
|
|
62
|
+
computeds: new Set(),
|
|
63
|
+
dirty: true,
|
|
64
|
+
effect: undefined,
|
|
65
|
+
effects: new Set(),
|
|
66
|
+
value: undefined,
|
|
67
|
+
};
|
|
68
|
+
this.state.effect = effect(() => {
|
|
69
|
+
if (this.state.dirty) {
|
|
70
|
+
const previousComputed = activeComputed;
|
|
71
|
+
activeComputed = this;
|
|
72
|
+
const value = callback();
|
|
73
|
+
activeComputed = previousComputed;
|
|
74
|
+
if (!Object.is(this.state.value, value)) {
|
|
75
|
+
this.state.value = value;
|
|
76
|
+
for (const comp of this.state.computeds) {
|
|
77
|
+
comp.state.dirty = true;
|
|
78
|
+
dirtyEffects.add(comp.state.effect);
|
|
79
|
+
}
|
|
80
|
+
for (const effect of this.state.effects) {
|
|
81
|
+
dirtyEffects.add(effect);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
this.state.dirty = false;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Get the value
|
|
90
|
+
*/
|
|
91
|
+
get() {
|
|
92
|
+
if (activeComputed != null && activeComputed !== this) {
|
|
93
|
+
this.state.computeds.add(activeComputed);
|
|
94
|
+
}
|
|
95
|
+
if (activeEffect != null && activeEffect !== this.state.effect) {
|
|
96
|
+
this.state.effects.add(activeEffect);
|
|
97
|
+
}
|
|
98
|
+
if (this.state.dirty) {
|
|
99
|
+
runEffect(this.state.effect);
|
|
100
|
+
}
|
|
101
|
+
return this.state.value;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a computed value
|
|
106
|
+
*/
|
|
107
|
+
function computed(callback) {
|
|
108
|
+
return new Computed(callback);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
class Signal {
|
|
112
|
+
state;
|
|
113
|
+
constructor(value) {
|
|
114
|
+
this.state = {
|
|
115
|
+
value,
|
|
116
|
+
computeds: new Set(),
|
|
117
|
+
effects: new Set(),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get the value
|
|
122
|
+
*/
|
|
123
|
+
get() {
|
|
124
|
+
if (activeComputed != null) {
|
|
125
|
+
this.state.computeds.add(activeComputed);
|
|
126
|
+
}
|
|
127
|
+
if (activeEffect != null) {
|
|
128
|
+
this.state.effects.add(activeEffect);
|
|
129
|
+
}
|
|
130
|
+
return this.state.value;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Set the value
|
|
134
|
+
*/
|
|
135
|
+
set(value) {
|
|
136
|
+
if (Object.is(this.state.value, value)) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this.state.value = value;
|
|
140
|
+
for (const computed of this.state.computeds) {
|
|
141
|
+
computed.state.dirty = true;
|
|
142
|
+
dirtyEffects.add(computed.state.effect);
|
|
143
|
+
}
|
|
144
|
+
for (const effect of this.state.effects) {
|
|
145
|
+
dirtyEffects.add(effect);
|
|
146
|
+
}
|
|
147
|
+
if (batchDepth === 0) {
|
|
148
|
+
flushEffects();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Update the value
|
|
153
|
+
*/
|
|
154
|
+
update(callback) {
|
|
155
|
+
this.set(callback(this.state.value));
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function signal(value) {
|
|
159
|
+
return new Signal(value);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export { computed, effect, signal, startBatch, stopBatch };
|
package/dist/signal.cjs
CHANGED
|
@@ -3,9 +3,9 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
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
|
+
const batch = require("./batch.cjs");
|
|
6
7
|
const computed = require("./computed.cjs");
|
|
7
8
|
const effect = require("./effect.cjs");
|
|
8
|
-
const dirtyEffects = /* @__PURE__ */ new Set();
|
|
9
9
|
class Signal {
|
|
10
10
|
constructor(value) {
|
|
11
11
|
__publicField(this, "state");
|
|
@@ -15,6 +15,9 @@ class Signal {
|
|
|
15
15
|
effects: /* @__PURE__ */ new Set()
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Get the value
|
|
20
|
+
*/
|
|
18
21
|
get() {
|
|
19
22
|
if (computed.activeComputed != null) {
|
|
20
23
|
this.state.computeds.add(computed.activeComputed);
|
|
@@ -24,6 +27,9 @@ class Signal {
|
|
|
24
27
|
}
|
|
25
28
|
return this.state.value;
|
|
26
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Set the value
|
|
32
|
+
*/
|
|
27
33
|
set(value) {
|
|
28
34
|
if (Object.is(this.state.value, value)) {
|
|
29
35
|
return;
|
|
@@ -31,19 +37,18 @@ class Signal {
|
|
|
31
37
|
this.state.value = value;
|
|
32
38
|
for (const computed2 of this.state.computeds) {
|
|
33
39
|
computed2.state.dirty = true;
|
|
34
|
-
dirtyEffects.add(computed2.state.effect);
|
|
40
|
+
effect.dirtyEffects.add(computed2.state.effect);
|
|
35
41
|
}
|
|
36
|
-
for (const
|
|
37
|
-
dirtyEffects.add(
|
|
42
|
+
for (const effect$1 of this.state.effects) {
|
|
43
|
+
effect.dirtyEffects.add(effect$1);
|
|
38
44
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
dirtyEffects.clear();
|
|
42
|
-
for (const effect$1 of effects) {
|
|
43
|
-
effect.runEffect(effect$1);
|
|
44
|
-
}
|
|
45
|
+
if (batch.batchDepth === 0) {
|
|
46
|
+
batch.flushEffects();
|
|
45
47
|
}
|
|
46
48
|
}
|
|
49
|
+
/**
|
|
50
|
+
* Update the value
|
|
51
|
+
*/
|
|
47
52
|
update(callback) {
|
|
48
53
|
this.set(callback(this.state.value));
|
|
49
54
|
}
|
|
@@ -52,5 +57,4 @@ function signal(value) {
|
|
|
52
57
|
return new Signal(value);
|
|
53
58
|
}
|
|
54
59
|
exports.Signal = Signal;
|
|
55
|
-
exports.dirtyEffects = dirtyEffects;
|
|
56
60
|
exports.signal = signal;
|
package/dist/signal.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
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 { flushEffects, batchDepth } from "./batch.js";
|
|
4
5
|
import { activeComputed } from "./computed.js";
|
|
5
|
-
import { activeEffect,
|
|
6
|
-
const dirtyEffects = /* @__PURE__ */ new Set();
|
|
6
|
+
import { activeEffect, dirtyEffects } from "./effect.js";
|
|
7
7
|
class Signal {
|
|
8
8
|
constructor(value) {
|
|
9
9
|
__publicField(this, "state");
|
|
@@ -13,6 +13,9 @@ class Signal {
|
|
|
13
13
|
effects: /* @__PURE__ */ new Set()
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Get the value
|
|
18
|
+
*/
|
|
16
19
|
get() {
|
|
17
20
|
if (activeComputed != null) {
|
|
18
21
|
this.state.computeds.add(activeComputed);
|
|
@@ -22,6 +25,9 @@ class Signal {
|
|
|
22
25
|
}
|
|
23
26
|
return this.state.value;
|
|
24
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Set the value
|
|
30
|
+
*/
|
|
25
31
|
set(value) {
|
|
26
32
|
if (Object.is(this.state.value, value)) {
|
|
27
33
|
return;
|
|
@@ -34,14 +40,13 @@ class Signal {
|
|
|
34
40
|
for (const effect of this.state.effects) {
|
|
35
41
|
dirtyEffects.add(effect);
|
|
36
42
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
dirtyEffects.clear();
|
|
40
|
-
for (const effect of effects) {
|
|
41
|
-
runEffect(effect);
|
|
42
|
-
}
|
|
43
|
+
if (batchDepth === 0) {
|
|
44
|
+
flushEffects();
|
|
43
45
|
}
|
|
44
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Update the value
|
|
49
|
+
*/
|
|
45
50
|
update(callback) {
|
|
46
51
|
this.set(callback(this.state.value));
|
|
47
52
|
}
|
|
@@ -51,6 +56,5 @@ function signal(value) {
|
|
|
51
56
|
}
|
|
52
57
|
export {
|
|
53
58
|
Signal,
|
|
54
|
-
dirtyEffects,
|
|
55
59
|
signal
|
|
56
60
|
};
|
package/package.json
CHANGED
|
@@ -6,18 +6,18 @@
|
|
|
6
6
|
"description": "Signals and stuff…",
|
|
7
7
|
"devDependencies": {
|
|
8
8
|
"@biomejs/biome": "^1.9",
|
|
9
|
-
"@oscarpalmer/atoms": "^0.
|
|
9
|
+
"@oscarpalmer/atoms": "^0.99",
|
|
10
10
|
"@rollup/plugin-node-resolve": "^16",
|
|
11
11
|
"@rollup/plugin-typescript": "^12.1",
|
|
12
|
-
"@types/node": "^22.
|
|
13
|
-
"@vitest/coverage-istanbul": "^3",
|
|
12
|
+
"@types/node": "^22.15",
|
|
13
|
+
"@vitest/coverage-istanbul": "^3.1",
|
|
14
14
|
"dts-bundle-generator": "^9.5",
|
|
15
15
|
"glob": "^11",
|
|
16
|
-
"
|
|
16
|
+
"jsdom": "^26.1",
|
|
17
17
|
"tslib": "^2.8",
|
|
18
|
-
"typescript": "^5.
|
|
19
|
-
"vite": "^6",
|
|
20
|
-
"vitest": "^3"
|
|
18
|
+
"typescript": "^5.8",
|
|
19
|
+
"vite": "^6.3",
|
|
20
|
+
"vitest": "^3.1"
|
|
21
21
|
},
|
|
22
22
|
"exports": {
|
|
23
23
|
".": {
|
|
@@ -31,16 +31,8 @@
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
|
-
"files": [
|
|
35
|
-
|
|
36
|
-
"src",
|
|
37
|
-
"types"
|
|
38
|
-
],
|
|
39
|
-
"keywords": [
|
|
40
|
-
"signal",
|
|
41
|
-
"signals",
|
|
42
|
-
"reactive"
|
|
43
|
-
],
|
|
34
|
+
"files": ["dist", "src", "types"],
|
|
35
|
+
"keywords": ["signal", "signals", "reactive"],
|
|
44
36
|
"license": "MIT",
|
|
45
37
|
"main": "./dist/index.cjs",
|
|
46
38
|
"module": "./dist/index.js",
|
|
@@ -55,10 +47,10 @@
|
|
|
55
47
|
"clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
|
|
56
48
|
"rollup": "npx rollup -c",
|
|
57
49
|
"test": "npx vitest --coverage",
|
|
58
|
-
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts",
|
|
50
|
+
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts --silent",
|
|
59
51
|
"watch": "npx vite build --watch"
|
|
60
52
|
},
|
|
61
53
|
"type": "module",
|
|
62
54
|
"types": "./types/index.d.ts",
|
|
63
|
-
"version": "0.
|
|
55
|
+
"version": "0.5.0"
|
|
64
56
|
}
|
package/src/batch.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import {dirtyEffects, runEffect} from './effect';
|
|
2
|
+
|
|
3
|
+
export function flushEffects(): void {
|
|
4
|
+
while (dirtyEffects.size > 0) {
|
|
5
|
+
const effects = [...dirtyEffects];
|
|
6
|
+
|
|
7
|
+
dirtyEffects.clear();
|
|
8
|
+
|
|
9
|
+
for (const effect of effects) {
|
|
10
|
+
runEffect(effect);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
17
|
+
*/
|
|
18
|
+
export function startBatch(): void {
|
|
19
|
+
batchDepth += 1;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Stop batching effects and flush _(run)_ them
|
|
24
|
+
*/
|
|
25
|
+
export function stopBatch(): void {
|
|
26
|
+
if (batchDepth > 0) {
|
|
27
|
+
batchDepth -= 1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (batchDepth === 0) {
|
|
31
|
+
flushEffects();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export let batchDepth = 0;
|
package/src/computed.ts
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
type Effect,
|
|
3
|
+
activeEffect,
|
|
4
|
+
dirtyEffects,
|
|
5
|
+
effect,
|
|
6
|
+
runEffect,
|
|
7
|
+
} from './effect';
|
|
3
8
|
|
|
4
9
|
type ComputedState<Value> = {
|
|
5
|
-
dirty: boolean;
|
|
6
10
|
computeds: Set<Computed<unknown>>;
|
|
11
|
+
dirty: boolean;
|
|
7
12
|
effect: Effect;
|
|
8
13
|
effects: Set<Effect>;
|
|
9
14
|
value: Value;
|
|
@@ -16,8 +21,8 @@ export class Computed<Value> {
|
|
|
16
21
|
|
|
17
22
|
constructor(callback: () => Value) {
|
|
18
23
|
this.state = {
|
|
19
|
-
dirty: true,
|
|
20
24
|
computeds: new Set(),
|
|
25
|
+
dirty: true,
|
|
21
26
|
effect: undefined as never,
|
|
22
27
|
effects: new Set(),
|
|
23
28
|
value: undefined as never,
|
|
@@ -52,6 +57,9 @@ export class Computed<Value> {
|
|
|
52
57
|
});
|
|
53
58
|
}
|
|
54
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Get the value
|
|
62
|
+
*/
|
|
55
63
|
get(): Value {
|
|
56
64
|
if (activeComputed != null && activeComputed !== this) {
|
|
57
65
|
this.state.computeds.add(activeComputed);
|
|
@@ -69,6 +77,9 @@ export class Computed<Value> {
|
|
|
69
77
|
}
|
|
70
78
|
}
|
|
71
79
|
|
|
80
|
+
/**
|
|
81
|
+
* Create a computed value
|
|
82
|
+
*/
|
|
72
83
|
export function computed<Value>(callback: () => Value): Computed<Value> {
|
|
73
84
|
return new Computed(callback);
|
|
74
85
|
}
|
package/src/effect.ts
CHANGED
|
@@ -4,8 +4,6 @@ type EffectState = {
|
|
|
4
4
|
callback: GenericCallback;
|
|
5
5
|
};
|
|
6
6
|
|
|
7
|
-
export let activeEffect: Effect | undefined;
|
|
8
|
-
|
|
9
7
|
export class Effect {
|
|
10
8
|
readonly state: EffectState;
|
|
11
9
|
|
|
@@ -30,6 +28,13 @@ export function runEffect(effect: Effect): void {
|
|
|
30
28
|
}
|
|
31
29
|
}
|
|
32
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Create an effect
|
|
33
|
+
*/
|
|
33
34
|
export function effect(callback: GenericCallback): Effect {
|
|
34
35
|
return new Effect(callback);
|
|
35
36
|
}
|
|
37
|
+
|
|
38
|
+
export let activeEffect: Effect | undefined;
|
|
39
|
+
|
|
40
|
+
export const dirtyEffects = new Set<Effect>();
|
package/src/index.ts
CHANGED
package/src/signal.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import {batchDepth, flushEffects} from './batch';
|
|
1
2
|
import {type Computed, activeComputed} from './computed';
|
|
2
|
-
import {type Effect, activeEffect,
|
|
3
|
+
import {type Effect, activeEffect, dirtyEffects} from './effect';
|
|
3
4
|
|
|
4
5
|
type SignalState<Value> = {
|
|
5
6
|
computeds: Set<Computed<unknown>>;
|
|
@@ -7,8 +8,6 @@ type SignalState<Value> = {
|
|
|
7
8
|
value: Value;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
export const dirtyEffects = new Set<Effect>();
|
|
11
|
-
|
|
12
11
|
export class Signal<Value> {
|
|
13
12
|
readonly state: SignalState<Value>;
|
|
14
13
|
|
|
@@ -20,6 +19,9 @@ export class Signal<Value> {
|
|
|
20
19
|
};
|
|
21
20
|
}
|
|
22
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Get the value
|
|
24
|
+
*/
|
|
23
25
|
get(): Value {
|
|
24
26
|
if (activeComputed != null) {
|
|
25
27
|
this.state.computeds.add(activeComputed);
|
|
@@ -32,6 +34,9 @@ export class Signal<Value> {
|
|
|
32
34
|
return this.state.value;
|
|
33
35
|
}
|
|
34
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Set the value
|
|
39
|
+
*/
|
|
35
40
|
set(value: Value): void {
|
|
36
41
|
if (Object.is(this.state.value, value)) {
|
|
37
42
|
return;
|
|
@@ -49,17 +54,14 @@ export class Signal<Value> {
|
|
|
49
54
|
dirtyEffects.add(effect);
|
|
50
55
|
}
|
|
51
56
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
dirtyEffects.clear();
|
|
56
|
-
|
|
57
|
-
for (const effect of effects) {
|
|
58
|
-
runEffect(effect);
|
|
59
|
-
}
|
|
57
|
+
if (batchDepth === 0) {
|
|
58
|
+
flushEffects();
|
|
60
59
|
}
|
|
61
60
|
}
|
|
62
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Update the value
|
|
64
|
+
*/
|
|
63
65
|
update(callback: (value: Value) => Value): void {
|
|
64
66
|
this.set(callback(this.state.value));
|
|
65
67
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
export declare function flushEffects(): void;
|
|
4
|
+
/**
|
|
5
|
+
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
6
|
+
*/
|
|
7
|
+
export declare function startBatch(): void;
|
|
8
|
+
/**
|
|
9
|
+
* Stop batching effects and flush _(run)_ them
|
|
10
|
+
*/
|
|
11
|
+
export declare function stopBatch(): void;
|
|
12
|
+
export declare let batchDepth: number;
|
|
13
|
+
|
|
14
|
+
export {};
|
package/types/batch.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare function flushEffects(): void;
|
|
2
|
+
/**
|
|
3
|
+
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
4
|
+
*/
|
|
5
|
+
export declare function startBatch(): void;
|
|
6
|
+
/**
|
|
7
|
+
* Stop batching effects and flush _(run)_ them
|
|
8
|
+
*/
|
|
9
|
+
export declare function stopBatch(): void;
|
|
10
|
+
export declare let batchDepth: number;
|
package/types/computed.d.cts
CHANGED
|
@@ -9,8 +9,8 @@ declare class Effect {
|
|
|
9
9
|
constructor(callback: GenericCallback);
|
|
10
10
|
}
|
|
11
11
|
export type ComputedState<Value> = {
|
|
12
|
-
dirty: boolean;
|
|
13
12
|
computeds: Set<Computed<unknown>>;
|
|
13
|
+
dirty: boolean;
|
|
14
14
|
effect: Effect;
|
|
15
15
|
effects: Set<Effect>;
|
|
16
16
|
value: Value;
|
|
@@ -19,8 +19,14 @@ export declare let activeComputed: Computed<unknown> | undefined;
|
|
|
19
19
|
export declare class Computed<Value> {
|
|
20
20
|
readonly state: ComputedState<Value>;
|
|
21
21
|
constructor(callback: () => Value);
|
|
22
|
+
/**
|
|
23
|
+
* Get the value
|
|
24
|
+
*/
|
|
22
25
|
get(): Value;
|
|
23
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Create a computed value
|
|
29
|
+
*/
|
|
24
30
|
export declare function computed<Value>(callback: () => Value): Computed<Value>;
|
|
25
31
|
|
|
26
32
|
export {};
|
package/types/computed.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Effect } from './effect';
|
|
2
2
|
type ComputedState<Value> = {
|
|
3
|
-
dirty: boolean;
|
|
4
3
|
computeds: Set<Computed<unknown>>;
|
|
4
|
+
dirty: boolean;
|
|
5
5
|
effect: Effect;
|
|
6
6
|
effects: Set<Effect>;
|
|
7
7
|
value: Value;
|
|
@@ -10,7 +10,13 @@ export declare let activeComputed: Computed<unknown> | undefined;
|
|
|
10
10
|
export declare class Computed<Value> {
|
|
11
11
|
readonly state: ComputedState<Value>;
|
|
12
12
|
constructor(callback: () => Value);
|
|
13
|
+
/**
|
|
14
|
+
* Get the value
|
|
15
|
+
*/
|
|
13
16
|
get(): Value;
|
|
14
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Create a computed value
|
|
20
|
+
*/
|
|
15
21
|
export declare function computed<Value>(callback: () => Value): Computed<Value>;
|
|
16
22
|
export {};
|
package/types/effect.d.cts
CHANGED
|
@@ -4,12 +4,16 @@ export type GenericCallback = (...args: any[]) => any;
|
|
|
4
4
|
export type EffectState = {
|
|
5
5
|
callback: GenericCallback;
|
|
6
6
|
};
|
|
7
|
-
export declare let activeEffect: Effect | undefined;
|
|
8
7
|
export declare class Effect {
|
|
9
8
|
readonly state: EffectState;
|
|
10
9
|
constructor(callback: GenericCallback);
|
|
11
10
|
}
|
|
12
11
|
export declare function runEffect(effect: Effect): void;
|
|
12
|
+
/**
|
|
13
|
+
* Create an effect
|
|
14
|
+
*/
|
|
13
15
|
export declare function effect(callback: GenericCallback): Effect;
|
|
16
|
+
export declare let activeEffect: Effect | undefined;
|
|
17
|
+
export declare const dirtyEffects: Set<Effect>;
|
|
14
18
|
|
|
15
19
|
export {};
|
package/types/effect.d.ts
CHANGED
|
@@ -2,11 +2,15 @@ import type { GenericCallback } from '@oscarpalmer/atoms/models';
|
|
|
2
2
|
type EffectState = {
|
|
3
3
|
callback: GenericCallback;
|
|
4
4
|
};
|
|
5
|
-
export declare let activeEffect: Effect | undefined;
|
|
6
5
|
export declare class Effect {
|
|
7
6
|
readonly state: EffectState;
|
|
8
7
|
constructor(callback: GenericCallback);
|
|
9
8
|
}
|
|
10
9
|
export declare function runEffect(effect: Effect): void;
|
|
10
|
+
/**
|
|
11
|
+
* Create an effect
|
|
12
|
+
*/
|
|
11
13
|
export declare function effect(callback: GenericCallback): Effect;
|
|
14
|
+
export declare let activeEffect: Effect | undefined;
|
|
15
|
+
export declare const dirtyEffects: Set<Effect>;
|
|
12
16
|
export {};
|
package/types/index.d.cts
CHANGED
|
@@ -8,10 +8,13 @@ export declare class Effect {
|
|
|
8
8
|
readonly state: EffectState;
|
|
9
9
|
constructor(callback: GenericCallback);
|
|
10
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* Create an effect
|
|
13
|
+
*/
|
|
11
14
|
export declare function effect(callback: GenericCallback): Effect;
|
|
12
15
|
export type ComputedState<Value> = {
|
|
13
|
-
dirty: boolean;
|
|
14
16
|
computeds: Set<Computed<unknown>>;
|
|
17
|
+
dirty: boolean;
|
|
15
18
|
effect: Effect;
|
|
16
19
|
effects: Set<Effect>;
|
|
17
20
|
value: Value;
|
|
@@ -19,8 +22,14 @@ export type ComputedState<Value> = {
|
|
|
19
22
|
export declare class Computed<Value> {
|
|
20
23
|
readonly state: ComputedState<Value>;
|
|
21
24
|
constructor(callback: () => Value);
|
|
25
|
+
/**
|
|
26
|
+
* Get the value
|
|
27
|
+
*/
|
|
22
28
|
get(): Value;
|
|
23
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a computed value
|
|
32
|
+
*/
|
|
24
33
|
export declare function computed<Value>(callback: () => Value): Computed<Value>;
|
|
25
34
|
export type SignalState<Value> = {
|
|
26
35
|
computeds: Set<Computed<unknown>>;
|
|
@@ -30,10 +39,27 @@ export type SignalState<Value> = {
|
|
|
30
39
|
export declare class Signal<Value> {
|
|
31
40
|
readonly state: SignalState<Value>;
|
|
32
41
|
constructor(value: Value);
|
|
42
|
+
/**
|
|
43
|
+
* Get the value
|
|
44
|
+
*/
|
|
33
45
|
get(): Value;
|
|
46
|
+
/**
|
|
47
|
+
* Set the value
|
|
48
|
+
*/
|
|
34
49
|
set(value: Value): void;
|
|
50
|
+
/**
|
|
51
|
+
* Update the value
|
|
52
|
+
*/
|
|
35
53
|
update(callback: (value: Value) => Value): void;
|
|
36
54
|
}
|
|
37
55
|
export declare function signal<Value>(value: Value): Signal<Value>;
|
|
56
|
+
/**
|
|
57
|
+
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
58
|
+
*/
|
|
59
|
+
export declare function startBatch(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Stop batching effects and flush _(run)_ them
|
|
62
|
+
*/
|
|
63
|
+
export declare function stopBatch(): void;
|
|
38
64
|
|
|
39
65
|
export {};
|
package/types/index.d.ts
CHANGED
package/types/signal.d.cts
CHANGED
|
@@ -9,8 +9,8 @@ declare class Effect {
|
|
|
9
9
|
constructor(callback: GenericCallback);
|
|
10
10
|
}
|
|
11
11
|
export type ComputedState<Value> = {
|
|
12
|
-
dirty: boolean;
|
|
13
12
|
computeds: Set<Computed<unknown>>;
|
|
13
|
+
dirty: boolean;
|
|
14
14
|
effect: Effect;
|
|
15
15
|
effects: Set<Effect>;
|
|
16
16
|
value: Value;
|
|
@@ -18,6 +18,9 @@ export type ComputedState<Value> = {
|
|
|
18
18
|
declare class Computed<Value> {
|
|
19
19
|
readonly state: ComputedState<Value>;
|
|
20
20
|
constructor(callback: () => Value);
|
|
21
|
+
/**
|
|
22
|
+
* Get the value
|
|
23
|
+
*/
|
|
21
24
|
get(): Value;
|
|
22
25
|
}
|
|
23
26
|
export type SignalState<Value> = {
|
|
@@ -25,12 +28,20 @@ export type SignalState<Value> = {
|
|
|
25
28
|
effects: Set<Effect>;
|
|
26
29
|
value: Value;
|
|
27
30
|
};
|
|
28
|
-
export declare const dirtyEffects: Set<Effect>;
|
|
29
31
|
export declare class Signal<Value> {
|
|
30
32
|
readonly state: SignalState<Value>;
|
|
31
33
|
constructor(value: Value);
|
|
34
|
+
/**
|
|
35
|
+
* Get the value
|
|
36
|
+
*/
|
|
32
37
|
get(): Value;
|
|
38
|
+
/**
|
|
39
|
+
* Set the value
|
|
40
|
+
*/
|
|
33
41
|
set(value: Value): void;
|
|
42
|
+
/**
|
|
43
|
+
* Update the value
|
|
44
|
+
*/
|
|
34
45
|
update(callback: (value: Value) => Value): void;
|
|
35
46
|
}
|
|
36
47
|
export declare function signal<Value>(value: Value): Signal<Value>;
|
package/types/signal.d.ts
CHANGED
|
@@ -5,12 +5,20 @@ type SignalState<Value> = {
|
|
|
5
5
|
effects: Set<Effect>;
|
|
6
6
|
value: Value;
|
|
7
7
|
};
|
|
8
|
-
export declare const dirtyEffects: Set<Effect>;
|
|
9
8
|
export declare class Signal<Value> {
|
|
10
9
|
readonly state: SignalState<Value>;
|
|
11
10
|
constructor(value: Value);
|
|
11
|
+
/**
|
|
12
|
+
* Get the value
|
|
13
|
+
*/
|
|
12
14
|
get(): Value;
|
|
15
|
+
/**
|
|
16
|
+
* Set the value
|
|
17
|
+
*/
|
|
13
18
|
set(value: Value): void;
|
|
19
|
+
/**
|
|
20
|
+
* Update the value
|
|
21
|
+
*/
|
|
14
22
|
update(callback: (value: Value) => Value): void;
|
|
15
23
|
}
|
|
16
24
|
export declare function signal<Value>(value: Value): Signal<Value>;
|
package/dist/mora.iife.js
DELETED
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
var Mora = (function (exports) {
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
let activeEffect;
|
|
5
|
-
class Effect {
|
|
6
|
-
state;
|
|
7
|
-
constructor(callback) {
|
|
8
|
-
this.state = {
|
|
9
|
-
callback,
|
|
10
|
-
};
|
|
11
|
-
runEffect(this);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function runEffect(effect) {
|
|
15
|
-
const previousEffect = activeEffect;
|
|
16
|
-
activeEffect = effect;
|
|
17
|
-
try {
|
|
18
|
-
effect.state.callback();
|
|
19
|
-
}
|
|
20
|
-
finally {
|
|
21
|
-
activeEffect = previousEffect;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function effect(callback) {
|
|
25
|
-
return new Effect(callback);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const dirtyEffects = new Set();
|
|
29
|
-
class Signal {
|
|
30
|
-
state;
|
|
31
|
-
constructor(value) {
|
|
32
|
-
this.state = {
|
|
33
|
-
value,
|
|
34
|
-
computeds: new Set(),
|
|
35
|
-
effects: new Set(),
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
get() {
|
|
39
|
-
if (activeComputed != null) {
|
|
40
|
-
this.state.computeds.add(activeComputed);
|
|
41
|
-
}
|
|
42
|
-
if (activeEffect != null) {
|
|
43
|
-
this.state.effects.add(activeEffect);
|
|
44
|
-
}
|
|
45
|
-
return this.state.value;
|
|
46
|
-
}
|
|
47
|
-
set(value) {
|
|
48
|
-
if (Object.is(this.state.value, value)) {
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
this.state.value = value;
|
|
52
|
-
for (const computed of this.state.computeds) {
|
|
53
|
-
computed.state.dirty = true;
|
|
54
|
-
dirtyEffects.add(computed.state.effect);
|
|
55
|
-
}
|
|
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);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
update(callback) {
|
|
68
|
-
this.set(callback(this.state.value));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
function signal(value) {
|
|
72
|
-
return new Signal(value);
|
|
73
|
-
}
|
|
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
|
-
exports.computed = computed;
|
|
124
|
-
exports.effect = effect;
|
|
125
|
-
exports.signal = signal;
|
|
126
|
-
|
|
127
|
-
return exports;
|
|
128
|
-
|
|
129
|
-
})({});
|