@oscarpalmer/mora 0.13.0 → 0.14.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 +2 -2
- package/dist/batch.js +1 -1
- package/dist/effect.cjs +2 -1
- package/dist/effect.js +2 -1
- package/dist/helpers/emit.cjs +42 -0
- package/dist/helpers/emit.js +42 -0
- package/dist/helpers/is.cjs +34 -0
- package/dist/helpers/is.js +34 -0
- package/dist/helpers/value.cjs +22 -0
- package/dist/helpers/value.js +22 -0
- package/dist/index.cjs +12 -11
- package/dist/index.js +5 -4
- package/dist/mora.full.js +152 -140
- package/dist/{array.cjs → value/array.cjs} +27 -49
- package/dist/{array.js → value/array.js} +9 -31
- package/dist/{computed.cjs → value/computed.cjs} +6 -5
- package/dist/{computed.js → value/computed.js} +4 -3
- package/dist/{reactive.cjs → value/reactive.cjs} +1 -1
- package/dist/{reactive.js → value/reactive.js} +1 -1
- package/dist/value/signal.cjs +33 -0
- package/dist/{signal.js → value/signal.js} +3 -2
- package/package.json +1 -1
- package/src/batch.ts +1 -1
- package/src/effect.ts +12 -11
- package/src/helpers/emit.ts +52 -0
- package/src/helpers/is.ts +56 -0
- package/src/helpers/value.ts +27 -0
- package/src/index.ts +11 -5
- package/src/subscription.ts +1 -1
- package/src/{array.ts → value/array.ts} +10 -41
- package/src/{computed.ts → value/computed.ts} +4 -3
- package/src/{reactive.ts → value/reactive.ts} +3 -3
- package/src/{signal.ts → value/signal.ts} +3 -2
- package/types/batch.d.cts +9 -9
- package/types/helpers/emit.d.cts +63 -0
- package/types/helpers/emit.d.ts +3 -0
- package/types/{is.d.cts → helpers/is.d.cts} +53 -9
- package/types/helpers/is.d.ts +26 -0
- package/types/{value.d.cts → helpers/value.d.cts} +9 -10
- package/types/{value.d.ts → helpers/value.d.ts} +1 -2
- package/types/index.d.cts +42 -38
- package/types/index.d.ts +5 -5
- package/types/subscription.d.cts +12 -12
- package/types/subscription.d.ts +1 -1
- package/types/{computed.d.ts → value/computed.d.ts} +1 -1
- package/types/{reactive.d.ts → value/reactive.d.ts} +2 -2
- package/dist/is.cjs +0 -21
- package/dist/is.js +0 -21
- package/dist/signal.cjs +0 -32
- package/dist/value.cjs +0 -38
- package/dist/value.js +0 -38
- package/src/is.ts +0 -39
- package/src/value.ts +0 -47
- package/types/is.d.ts +0 -17
- package/types/{array.d.cts → value/array.d.cts} +9 -9
- package/types/{array.d.ts → value/array.d.ts} +0 -0
- package/types/{computed.d.cts → value/computed.d.cts} +9 -9
- package/types/{reactive.d.cts → value/reactive.d.cts} +9 -9
- package/types/{signal.d.cts → value/signal.d.cts} +9 -9
- /package/types/{signal.d.ts → value/signal.d.ts} +0 -0
package/dist/batch.cjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const effect = require("./effect.cjs");
|
|
4
|
-
const
|
|
4
|
+
const helpers_is = require("./helpers/is.cjs");
|
|
5
5
|
function flushEffects() {
|
|
6
6
|
while (exports.batchDepth === 0 && batchedHandlers.size > 0) {
|
|
7
7
|
const handlers = [...batchedHandlers];
|
|
8
8
|
batchedHandlers.clear();
|
|
9
9
|
for (const handler of handlers) {
|
|
10
|
-
if (
|
|
10
|
+
if (helpers_is.isEffect(handler)) {
|
|
11
11
|
effect.runEffect(handler);
|
|
12
12
|
} else {
|
|
13
13
|
handler.callback(handler.state.value);
|
package/dist/batch.js
CHANGED
package/dist/effect.cjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const helpers_is = require("./helpers/is.cjs");
|
|
3
4
|
class Effect {
|
|
4
5
|
constructor(callback) {
|
|
5
6
|
Object.defineProperty(this, "$mora", {
|
|
6
|
-
value:
|
|
7
|
+
value: helpers_is.effectName
|
|
7
8
|
});
|
|
8
9
|
this.state = {
|
|
9
10
|
callback
|
package/dist/effect.js
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const batch = require("../batch.cjs");
|
|
4
|
+
function emitArrayChanges(first, second) {
|
|
5
|
+
let { length } = first;
|
|
6
|
+
if (length !== second.length) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
let offset = 0;
|
|
10
|
+
if (length >= 100) {
|
|
11
|
+
offset = Math.round(length / 10);
|
|
12
|
+
offset = offset > 25 ? 25 : offset;
|
|
13
|
+
for (let index = 0; index < offset; index += 1) {
|
|
14
|
+
if (!Object.is(first[index], second[index])) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
length -= offset;
|
|
20
|
+
for (let index = offset; index < length; index += 1) {
|
|
21
|
+
if (!Object.is(first[index], second[index])) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function emitValue(state) {
|
|
28
|
+
for (const computed of state.computeds) {
|
|
29
|
+
computed.effect.dirty = true;
|
|
30
|
+
}
|
|
31
|
+
for (const effect of state.effects) {
|
|
32
|
+
batch.batchedHandlers.add(effect);
|
|
33
|
+
}
|
|
34
|
+
for (const [, subscription] of state.subscriptions) {
|
|
35
|
+
batch.batchedHandlers.add(subscription);
|
|
36
|
+
}
|
|
37
|
+
if (batch.batchDepth === 0) {
|
|
38
|
+
batch.flushEffects();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
exports.emitArrayChanges = emitArrayChanges;
|
|
42
|
+
exports.emitValue = emitValue;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { batchedHandlers, batchDepth, flushEffects } from "../batch.js";
|
|
2
|
+
function emitArrayChanges(first, second) {
|
|
3
|
+
let { length } = first;
|
|
4
|
+
if (length !== second.length) {
|
|
5
|
+
return true;
|
|
6
|
+
}
|
|
7
|
+
let offset = 0;
|
|
8
|
+
if (length >= 100) {
|
|
9
|
+
offset = Math.round(length / 10);
|
|
10
|
+
offset = offset > 25 ? 25 : offset;
|
|
11
|
+
for (let index = 0; index < offset; index += 1) {
|
|
12
|
+
if (!Object.is(first[index], second[index])) {
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
length -= offset;
|
|
18
|
+
for (let index = offset; index < length; index += 1) {
|
|
19
|
+
if (!Object.is(first[index], second[index])) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
function emitValue(state) {
|
|
26
|
+
for (const computed of state.computeds) {
|
|
27
|
+
computed.effect.dirty = true;
|
|
28
|
+
}
|
|
29
|
+
for (const effect of state.effects) {
|
|
30
|
+
batchedHandlers.add(effect);
|
|
31
|
+
}
|
|
32
|
+
for (const [, subscription] of state.subscriptions) {
|
|
33
|
+
batchedHandlers.add(subscription);
|
|
34
|
+
}
|
|
35
|
+
if (batchDepth === 0) {
|
|
36
|
+
flushEffects();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export {
|
|
40
|
+
emitArrayChanges,
|
|
41
|
+
emitValue
|
|
42
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
function isArray(value) {
|
|
4
|
+
return isMora(value, arrayName);
|
|
5
|
+
}
|
|
6
|
+
function isComputed(value) {
|
|
7
|
+
return isMora(value, computedName);
|
|
8
|
+
}
|
|
9
|
+
function isEffect(value) {
|
|
10
|
+
return isMora(value, effectName);
|
|
11
|
+
}
|
|
12
|
+
function isMora(value, name) {
|
|
13
|
+
return typeof value === "object" && value != null && "$mora" in value && (typeof name === "string" ? value.$mora === name : name.has(value.$mora));
|
|
14
|
+
}
|
|
15
|
+
function isReactive(value) {
|
|
16
|
+
return isMora(value, reactiveNames);
|
|
17
|
+
}
|
|
18
|
+
function isSignal(value) {
|
|
19
|
+
return isMora(value, signalName);
|
|
20
|
+
}
|
|
21
|
+
const arrayName = "array";
|
|
22
|
+
const computedName = "computed";
|
|
23
|
+
const effectName = "effect";
|
|
24
|
+
const signalName = "signal";
|
|
25
|
+
const reactiveNames = /* @__PURE__ */ new Set([arrayName, computedName, signalName]);
|
|
26
|
+
exports.arrayName = arrayName;
|
|
27
|
+
exports.computedName = computedName;
|
|
28
|
+
exports.effectName = effectName;
|
|
29
|
+
exports.isArray = isArray;
|
|
30
|
+
exports.isComputed = isComputed;
|
|
31
|
+
exports.isEffect = isEffect;
|
|
32
|
+
exports.isReactive = isReactive;
|
|
33
|
+
exports.isSignal = isSignal;
|
|
34
|
+
exports.signalName = signalName;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
function isArray(value) {
|
|
2
|
+
return isMora(value, arrayName);
|
|
3
|
+
}
|
|
4
|
+
function isComputed(value) {
|
|
5
|
+
return isMora(value, computedName);
|
|
6
|
+
}
|
|
7
|
+
function isEffect(value) {
|
|
8
|
+
return isMora(value, effectName);
|
|
9
|
+
}
|
|
10
|
+
function isMora(value, name) {
|
|
11
|
+
return typeof value === "object" && value != null && "$mora" in value && (typeof name === "string" ? value.$mora === name : name.has(value.$mora));
|
|
12
|
+
}
|
|
13
|
+
function isReactive(value) {
|
|
14
|
+
return isMora(value, reactiveNames);
|
|
15
|
+
}
|
|
16
|
+
function isSignal(value) {
|
|
17
|
+
return isMora(value, signalName);
|
|
18
|
+
}
|
|
19
|
+
const arrayName = "array";
|
|
20
|
+
const computedName = "computed";
|
|
21
|
+
const effectName = "effect";
|
|
22
|
+
const signalName = "signal";
|
|
23
|
+
const reactiveNames = /* @__PURE__ */ new Set([arrayName, computedName, signalName]);
|
|
24
|
+
export {
|
|
25
|
+
arrayName,
|
|
26
|
+
computedName,
|
|
27
|
+
effectName,
|
|
28
|
+
isArray,
|
|
29
|
+
isComputed,
|
|
30
|
+
isEffect,
|
|
31
|
+
isReactive,
|
|
32
|
+
isSignal,
|
|
33
|
+
signalName
|
|
34
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const effect = require("../effect.cjs");
|
|
4
|
+
const value_computed = require("../value/computed.cjs");
|
|
5
|
+
const helpers_emit = require("./emit.cjs");
|
|
6
|
+
function getValue(state) {
|
|
7
|
+
if (value_computed.activeComputed != null) {
|
|
8
|
+
state.computeds.add(value_computed.activeComputed);
|
|
9
|
+
}
|
|
10
|
+
if (effect.activeEffect != null) {
|
|
11
|
+
state.effects.add(effect.activeEffect);
|
|
12
|
+
}
|
|
13
|
+
return state.value;
|
|
14
|
+
}
|
|
15
|
+
function setValue(state, value) {
|
|
16
|
+
if (!Object.is(state.value, value)) {
|
|
17
|
+
state.value = value;
|
|
18
|
+
helpers_emit.emitValue(state);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.getValue = getValue;
|
|
22
|
+
exports.setValue = setValue;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { activeEffect } from "../effect.js";
|
|
2
|
+
import { activeComputed } from "../value/computed.js";
|
|
3
|
+
import { emitValue } from "./emit.js";
|
|
4
|
+
function getValue(state) {
|
|
5
|
+
if (activeComputed != null) {
|
|
6
|
+
state.computeds.add(activeComputed);
|
|
7
|
+
}
|
|
8
|
+
if (activeEffect != null) {
|
|
9
|
+
state.effects.add(activeEffect);
|
|
10
|
+
}
|
|
11
|
+
return state.value;
|
|
12
|
+
}
|
|
13
|
+
function setValue(state, value) {
|
|
14
|
+
if (!Object.is(state.value, value)) {
|
|
15
|
+
state.value = value;
|
|
16
|
+
emitValue(state);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export {
|
|
20
|
+
getValue,
|
|
21
|
+
setValue
|
|
22
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const array = require("./array.cjs");
|
|
4
3
|
const batch = require("./batch.cjs");
|
|
5
|
-
const computed = require("./computed.cjs");
|
|
6
4
|
const effect = require("./effect.cjs");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
5
|
+
const helpers_is = require("./helpers/is.cjs");
|
|
6
|
+
const value_array = require("./value/array.cjs");
|
|
7
|
+
const value_computed = require("./value/computed.cjs");
|
|
8
|
+
const value_signal = require("./value/signal.cjs");
|
|
10
9
|
exports.startBatch = batch.startBatch;
|
|
11
10
|
exports.stopBatch = batch.stopBatch;
|
|
12
|
-
exports.computed = computed.computed;
|
|
13
11
|
exports.effect = effect.effect;
|
|
14
|
-
exports.
|
|
15
|
-
exports.
|
|
16
|
-
exports.
|
|
17
|
-
exports.
|
|
18
|
-
exports.
|
|
12
|
+
exports.isArray = helpers_is.isArray;
|
|
13
|
+
exports.isComputed = helpers_is.isComputed;
|
|
14
|
+
exports.isEffect = helpers_is.isEffect;
|
|
15
|
+
exports.isReactive = helpers_is.isReactive;
|
|
16
|
+
exports.isSignal = helpers_is.isSignal;
|
|
17
|
+
exports.array = value_array.array;
|
|
18
|
+
exports.computed = value_computed.computed;
|
|
19
|
+
exports.signal = value_signal.signal;
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { array } from "./array.js";
|
|
2
1
|
import { startBatch, stopBatch } from "./batch.js";
|
|
3
|
-
import { computed } from "./computed.js";
|
|
4
2
|
import { effect } from "./effect.js";
|
|
5
|
-
import { isComputed, isEffect, isReactive, isSignal } from "./is.js";
|
|
6
|
-
import {
|
|
3
|
+
import { isArray, isComputed, isEffect, isReactive, isSignal } from "./helpers/is.js";
|
|
4
|
+
import { array } from "./value/array.js";
|
|
5
|
+
import { computed } from "./value/computed.js";
|
|
6
|
+
import { signal } from "./value/signal.js";
|
|
7
7
|
export {
|
|
8
8
|
array,
|
|
9
9
|
computed,
|
|
10
10
|
effect,
|
|
11
|
+
isArray,
|
|
11
12
|
isComputed,
|
|
12
13
|
isEffect,
|
|
13
14
|
isReactive,
|
package/dist/mora.full.js
CHANGED
|
@@ -1,3 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Is the value a reactive array?
|
|
3
|
+
*/
|
|
4
|
+
function isArray(value) {
|
|
5
|
+
return isMora(value, arrayName);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Is the value a computed signal?
|
|
9
|
+
*/
|
|
10
|
+
function isComputed(value) {
|
|
11
|
+
return isMora(value, computedName);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Is the value an effect?
|
|
15
|
+
*/
|
|
16
|
+
function isEffect(value) {
|
|
17
|
+
return isMora(value, effectName);
|
|
18
|
+
}
|
|
19
|
+
function isMora(value, name) {
|
|
20
|
+
return (typeof value === 'object' &&
|
|
21
|
+
value != null &&
|
|
22
|
+
'$mora' in value &&
|
|
23
|
+
(typeof name === 'string'
|
|
24
|
+
? value.$mora === name
|
|
25
|
+
: name.has(value.$mora)));
|
|
26
|
+
}
|
|
27
|
+
function isReactive(value) {
|
|
28
|
+
return isMora(value, reactiveNames);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Is the value a signal?
|
|
32
|
+
*/
|
|
33
|
+
function isSignal(value) {
|
|
34
|
+
return isMora(value, signalName);
|
|
35
|
+
}
|
|
36
|
+
const arrayName = 'array';
|
|
37
|
+
const computedName = 'computed';
|
|
38
|
+
const effectName = 'effect';
|
|
39
|
+
const signalName = 'signal';
|
|
40
|
+
const reactiveNames = new Set([arrayName, computedName, signalName]);
|
|
41
|
+
|
|
42
|
+
class Effect {
|
|
43
|
+
constructor(callback) {
|
|
44
|
+
Object.defineProperty(this, '$mora', {
|
|
45
|
+
value: effectName,
|
|
46
|
+
});
|
|
47
|
+
this.state = {
|
|
48
|
+
callback,
|
|
49
|
+
};
|
|
50
|
+
runEffect(this);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function runEffect(effect) {
|
|
54
|
+
const previousEffect = activeEffect;
|
|
55
|
+
activeEffect = effect;
|
|
56
|
+
try {
|
|
57
|
+
effect.state.callback();
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
activeEffect = previousEffect;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create an effect
|
|
65
|
+
*/
|
|
66
|
+
function effect(callback) {
|
|
67
|
+
return new Effect(callback);
|
|
68
|
+
}
|
|
69
|
+
let activeEffect;
|
|
70
|
+
|
|
71
|
+
function flushEffects() {
|
|
72
|
+
while (batchDepth === 0 && batchedHandlers.size > 0) {
|
|
73
|
+
const handlers = [...batchedHandlers];
|
|
74
|
+
batchedHandlers.clear();
|
|
75
|
+
for (const handler of handlers) {
|
|
76
|
+
if (isEffect(handler)) {
|
|
77
|
+
runEffect(handler);
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
handler.callback(handler.state.value);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
87
|
+
*/
|
|
88
|
+
function startBatch() {
|
|
89
|
+
batchDepth += 1;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Stop batching effects and flush _(run)_ them
|
|
93
|
+
*/
|
|
94
|
+
function stopBatch() {
|
|
95
|
+
if (batchDepth > 0) {
|
|
96
|
+
batchDepth -= 1;
|
|
97
|
+
}
|
|
98
|
+
flushEffects();
|
|
99
|
+
}
|
|
100
|
+
const batchedHandlers = new Set();
|
|
101
|
+
let batchDepth = 0;
|
|
102
|
+
|
|
103
|
+
function emitArrayChanges(first, second) {
|
|
104
|
+
let { length } = first;
|
|
105
|
+
if (length !== second.length) {
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
let offset = 0;
|
|
109
|
+
if (length >= 100) {
|
|
110
|
+
offset = Math.round(length / 10);
|
|
111
|
+
offset = offset > 25 ? 25 : offset;
|
|
112
|
+
for (let index = 0; index < offset; index += 1) {
|
|
113
|
+
if (!Object.is(first[index], second[index])) {
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
length -= offset;
|
|
119
|
+
for (let index = offset; index < length; index += 1) {
|
|
120
|
+
if (!Object.is(first[index], second[index])) {
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
function emitValue(state) {
|
|
127
|
+
for (const computed of state.computeds) {
|
|
128
|
+
computed.effect.dirty = true;
|
|
129
|
+
}
|
|
130
|
+
for (const effect of state.effects) {
|
|
131
|
+
batchedHandlers.add(effect);
|
|
132
|
+
}
|
|
133
|
+
for (const [, subscription] of state.subscriptions) {
|
|
134
|
+
batchedHandlers.add(subscription);
|
|
135
|
+
}
|
|
136
|
+
if (batchDepth === 0) {
|
|
137
|
+
flushEffects();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
1
141
|
class Subscription {
|
|
2
142
|
state;
|
|
3
143
|
callback;
|
|
@@ -71,95 +211,6 @@ class Reactive {
|
|
|
71
211
|
}
|
|
72
212
|
}
|
|
73
213
|
|
|
74
|
-
class Effect {
|
|
75
|
-
constructor(callback) {
|
|
76
|
-
Object.defineProperty(this, '$mora', {
|
|
77
|
-
value: 'effect',
|
|
78
|
-
});
|
|
79
|
-
this.state = {
|
|
80
|
-
callback,
|
|
81
|
-
};
|
|
82
|
-
runEffect(this);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
function runEffect(effect) {
|
|
86
|
-
const previousEffect = activeEffect;
|
|
87
|
-
activeEffect = effect;
|
|
88
|
-
try {
|
|
89
|
-
effect.state.callback();
|
|
90
|
-
}
|
|
91
|
-
finally {
|
|
92
|
-
activeEffect = previousEffect;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Create an effect
|
|
97
|
-
*/
|
|
98
|
-
function effect(callback) {
|
|
99
|
-
return new Effect(callback);
|
|
100
|
-
}
|
|
101
|
-
let activeEffect;
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Is the value a computed signal?
|
|
105
|
-
*/
|
|
106
|
-
function isComputed(value) {
|
|
107
|
-
return isMora(value, 'computed');
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Is the value an effect?
|
|
111
|
-
*/
|
|
112
|
-
function isEffect(value) {
|
|
113
|
-
return isMora(value, 'effect');
|
|
114
|
-
}
|
|
115
|
-
function isMora(value, name) {
|
|
116
|
-
return (typeof value === 'object' &&
|
|
117
|
-
value != null &&
|
|
118
|
-
'$mora' in value &&
|
|
119
|
-
value.$mora === name);
|
|
120
|
-
}
|
|
121
|
-
function isReactive(value) {
|
|
122
|
-
return isComputed(value) || isSignal(value);
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Is the value a signal?
|
|
126
|
-
*/
|
|
127
|
-
function isSignal(value) {
|
|
128
|
-
return isMora(value, 'signal');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function flushEffects() {
|
|
132
|
-
while (batchDepth === 0 && batchedHandlers.size > 0) {
|
|
133
|
-
const handlers = [...batchedHandlers];
|
|
134
|
-
batchedHandlers.clear();
|
|
135
|
-
for (const handler of handlers) {
|
|
136
|
-
if (isEffect(handler)) {
|
|
137
|
-
runEffect(handler);
|
|
138
|
-
}
|
|
139
|
-
else {
|
|
140
|
-
handler.callback(handler.state.value);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Start batching effects _(use `stopBatch` to flush and run batched effects)_
|
|
147
|
-
*/
|
|
148
|
-
function startBatch() {
|
|
149
|
-
batchDepth += 1;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Stop batching effects and flush _(run)_ them
|
|
153
|
-
*/
|
|
154
|
-
function stopBatch() {
|
|
155
|
-
if (batchDepth > 0) {
|
|
156
|
-
batchDepth -= 1;
|
|
157
|
-
}
|
|
158
|
-
flushEffects();
|
|
159
|
-
}
|
|
160
|
-
const batchedHandlers = new Set();
|
|
161
|
-
let batchDepth = 0;
|
|
162
|
-
|
|
163
214
|
let activeComputed;
|
|
164
215
|
class Computed extends Reactive {
|
|
165
216
|
effect = {
|
|
@@ -167,7 +218,7 @@ class Computed extends Reactive {
|
|
|
167
218
|
instance: undefined,
|
|
168
219
|
};
|
|
169
220
|
constructor(callback) {
|
|
170
|
-
super(
|
|
221
|
+
super(computedName, undefined);
|
|
171
222
|
this.effect.instance = effect(() => {
|
|
172
223
|
if (this.effect.dirty) {
|
|
173
224
|
const previousComputed = activeComputed;
|
|
@@ -213,20 +264,6 @@ function computed(callback) {
|
|
|
213
264
|
return new Computed(callback);
|
|
214
265
|
}
|
|
215
266
|
|
|
216
|
-
function emitValue(state) {
|
|
217
|
-
for (const computed of state.computeds) {
|
|
218
|
-
computed.effect.dirty = true;
|
|
219
|
-
}
|
|
220
|
-
for (const effect of state.effects) {
|
|
221
|
-
batchedHandlers.add(effect);
|
|
222
|
-
}
|
|
223
|
-
for (const [, subscription] of state.subscriptions) {
|
|
224
|
-
batchedHandlers.add(subscription);
|
|
225
|
-
}
|
|
226
|
-
if (batchDepth === 0) {
|
|
227
|
-
flushEffects();
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
267
|
function getValue(state) {
|
|
231
268
|
if (activeComputed != null) {
|
|
232
269
|
state.computeds.add(activeComputed);
|
|
@@ -237,16 +274,15 @@ function getValue(state) {
|
|
|
237
274
|
return state.value;
|
|
238
275
|
}
|
|
239
276
|
function setValue$1(state, value) {
|
|
240
|
-
if (Object.is(state.value, value)) {
|
|
241
|
-
|
|
277
|
+
if (!Object.is(state.value, value)) {
|
|
278
|
+
state.value = value;
|
|
279
|
+
emitValue(state);
|
|
242
280
|
}
|
|
243
|
-
state.value = value;
|
|
244
|
-
emitValue(state);
|
|
245
281
|
}
|
|
246
282
|
|
|
247
283
|
class Signal extends Reactive {
|
|
248
284
|
constructor(value) {
|
|
249
|
-
super(
|
|
285
|
+
super(signalName, value);
|
|
250
286
|
}
|
|
251
287
|
/**
|
|
252
288
|
* @inheritdoc
|
|
@@ -293,7 +329,7 @@ class ReactiveArray extends Reactive {
|
|
|
293
329
|
}
|
|
294
330
|
}
|
|
295
331
|
constructor(value) {
|
|
296
|
-
super(
|
|
332
|
+
super(arrayName, new Proxy(value, {
|
|
297
333
|
get: (target, property) => updateMethods.has(property)
|
|
298
334
|
? updateArray(property, target, this.state, this.#size)
|
|
299
335
|
: Reflect.get(target, property),
|
|
@@ -328,29 +364,6 @@ class ReactiveArray extends Reactive {
|
|
|
328
364
|
function array(value) {
|
|
329
365
|
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
330
366
|
}
|
|
331
|
-
function emit(first, second) {
|
|
332
|
-
let { length } = first;
|
|
333
|
-
if (length !== second.length) {
|
|
334
|
-
return true;
|
|
335
|
-
}
|
|
336
|
-
let offset = 0;
|
|
337
|
-
if (length >= 100) {
|
|
338
|
-
offset = Math.round(length / 10);
|
|
339
|
-
offset = offset > 25 ? 25 : offset;
|
|
340
|
-
for (let index = 0; index < offset; index += 1) {
|
|
341
|
-
if (!Object.is(first[index], second[index])) {
|
|
342
|
-
return true;
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
length -= offset;
|
|
347
|
-
for (let index = offset; index < length; index += 1) {
|
|
348
|
-
if (!Object.is(first[index], second[index])) {
|
|
349
|
-
return true;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
return false;
|
|
353
|
-
}
|
|
354
367
|
function setValue(target, property, value, state, length) {
|
|
355
368
|
const isIndex = !Number.isNaN(Number(property));
|
|
356
369
|
const isLength = property === 'length';
|
|
@@ -358,12 +371,11 @@ function setValue(target, property, value, state, length) {
|
|
|
358
371
|
return Reflect.set(target, property, value);
|
|
359
372
|
}
|
|
360
373
|
const previous = Reflect.get(target, property);
|
|
361
|
-
if (Object.is(previous, value)) {
|
|
362
|
-
|
|
374
|
+
if (!Object.is(previous, value)) {
|
|
375
|
+
Reflect.set(target, property, value);
|
|
376
|
+
emitValue(state);
|
|
377
|
+
length.set(target.length);
|
|
363
378
|
}
|
|
364
|
-
Reflect.set(target, property, value);
|
|
365
|
-
emitValue(state);
|
|
366
|
-
length.set(target.length);
|
|
367
379
|
return true;
|
|
368
380
|
}
|
|
369
381
|
function updateArray(type, array, state, length) {
|
|
@@ -374,7 +386,7 @@ function updateArray(type, array, state, length) {
|
|
|
374
386
|
const result = array[type](...args);
|
|
375
387
|
if (affectsLength
|
|
376
388
|
? array.length !== previousLength
|
|
377
|
-
:
|
|
389
|
+
: emitArrayChanges(previousArray, array)) {
|
|
378
390
|
emitValue(state);
|
|
379
391
|
length.set(array.length);
|
|
380
392
|
}
|
|
@@ -396,4 +408,4 @@ const updateMethods = new Set([
|
|
|
396
408
|
'splice',
|
|
397
409
|
]);
|
|
398
410
|
|
|
399
|
-
export { array, computed, effect, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch };
|
|
411
|
+
export { array, computed, effect, isArray, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch };
|