@oscarpalmer/mora 0.21.0 → 0.23.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.js +11 -25
- package/dist/constants.js +34 -0
- package/dist/effect.js +18 -28
- package/dist/helpers/is.js +9 -26
- package/dist/helpers/proxy.js +36 -48
- package/dist/helpers/value.js +21 -48
- package/dist/index.js +3 -16
- package/dist/models.js +0 -0
- package/dist/mora.full.js +218 -130
- package/dist/subscription.js +22 -28
- package/dist/value/array.js +97 -182
- package/dist/value/computed.js +33 -58
- package/dist/value/reactive.js +29 -53
- package/dist/value/signal.js +21 -33
- package/dist/value/store.js +37 -60
- package/package.json +17 -24
- package/src/batch.ts +11 -13
- package/src/constants.ts +49 -0
- package/src/effect.ts +11 -16
- package/src/helpers/is.ts +41 -19
- package/src/helpers/proxy.ts +34 -38
- package/src/helpers/value.ts +19 -14
- package/src/index.ts +1 -1
- package/src/models.ts +66 -0
- package/src/subscription.ts +14 -16
- package/src/value/array.ts +87 -45
- package/src/value/computed.ts +29 -39
- package/src/value/reactive.ts +9 -24
- package/src/value/signal.ts +9 -3
- package/src/value/store.ts +40 -9
- package/types/batch.d.ts +3 -5
- package/types/constants.d.ts +14 -0
- package/types/effect.d.ts +2 -1
- package/types/helpers/is.d.ts +23 -10
- package/types/helpers/proxy.d.ts +2 -3
- package/types/helpers/value.d.ts +1 -1
- package/types/index.d.ts +1 -0
- package/types/models.d.ts +56 -0
- package/types/subscription.d.ts +3 -6
- package/types/value/array.d.ts +48 -7
- package/types/value/computed.d.ts +2 -11
- package/types/value/reactive.d.ts +8 -17
- package/types/value/signal.d.ts +7 -1
- package/types/value/store.d.ts +28 -4
- package/dist/batch.cjs +0 -32
- package/dist/effect.cjs +0 -30
- package/dist/helpers/is.cjs +0 -40
- package/dist/helpers/proxy.cjs +0 -56
- package/dist/helpers/value.cjs +0 -54
- package/dist/index.cjs +0 -21
- package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
- package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
- package/dist/subscription.cjs +0 -32
- package/dist/value/array.cjs +0 -187
- package/dist/value/computed.cjs +0 -60
- package/dist/value/reactive.cjs +0 -55
- package/dist/value/signal.cjs +0 -36
- package/dist/value/store.cjs +0 -63
- package/types/batch.d.cts +0 -79
- package/types/effect.d.cts +0 -16
- package/types/helpers/is.d.cts +0 -259
- package/types/helpers/proxy.d.cts +0 -244
- package/types/helpers/value.d.cts +0 -71
- package/types/index.d.cts +0 -281
- package/types/subscription.d.cts +0 -71
- package/types/value/array.d.cts +0 -161
- package/types/value/computed.d.cts +0 -81
- package/types/value/reactive.d.cts +0 -68
- package/types/value/signal.d.cts +0 -87
- package/types/value/store.d.cts +0 -136
package/dist/batch.js
CHANGED
|
@@ -1,33 +1,19 @@
|
|
|
1
|
+
import { BATCH } from "./constants.js";
|
|
1
2
|
import { runEffect } from "./effect.js";
|
|
2
3
|
import { isEffect } from "./helpers/is.js";
|
|
3
4
|
function flushHandlers() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} else {
|
|
11
|
-
handler.callback(handler.state.value);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
5
|
+
while (BATCH.depth === 0 && BATCH.handlers.size > 0) {
|
|
6
|
+
const handlers = [...BATCH.handlers];
|
|
7
|
+
BATCH.handlers.clear();
|
|
8
|
+
for (const handler of handlers) if (isEffect(handler)) runEffect(handler);
|
|
9
|
+
else handler.callback(handler.state.value);
|
|
10
|
+
}
|
|
15
11
|
}
|
|
16
12
|
function startBatch() {
|
|
17
|
-
|
|
13
|
+
BATCH.depth += 1;
|
|
18
14
|
}
|
|
19
15
|
function stopBatch() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
flushHandlers();
|
|
16
|
+
if (BATCH.depth > 0) BATCH.depth -= 1;
|
|
17
|
+
flushHandlers();
|
|
24
18
|
}
|
|
25
|
-
|
|
26
|
-
let batchDepth = 0;
|
|
27
|
-
export {
|
|
28
|
-
batchDepth,
|
|
29
|
-
batchedHandlers,
|
|
30
|
-
flushHandlers,
|
|
31
|
-
startBatch,
|
|
32
|
-
stopBatch
|
|
33
|
-
};
|
|
19
|
+
export { flushHandlers, startBatch, stopBatch };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
const ACTIVE = {};
|
|
2
|
+
const ARRAY_THRESHOLD = 100;
|
|
3
|
+
const ARRAY_OFFSET = 25;
|
|
4
|
+
const ARRAY_PEEK = 10;
|
|
5
|
+
const BATCH = {
|
|
6
|
+
depth: 0,
|
|
7
|
+
handlers: /* @__PURE__ */ new Set()
|
|
8
|
+
};
|
|
9
|
+
const METHODS_AFFECTING_LENGTH = new Set([
|
|
10
|
+
"pop",
|
|
11
|
+
"push",
|
|
12
|
+
"shift",
|
|
13
|
+
"unshift"
|
|
14
|
+
]);
|
|
15
|
+
const METHODS_UPDATE = new Set([
|
|
16
|
+
...METHODS_AFFECTING_LENGTH,
|
|
17
|
+
"copyWithin",
|
|
18
|
+
"fill",
|
|
19
|
+
"reverse",
|
|
20
|
+
"sort",
|
|
21
|
+
"splice"
|
|
22
|
+
]);
|
|
23
|
+
const NAME_ARRAY = "array";
|
|
24
|
+
const NAME_COMPUTED = "computed";
|
|
25
|
+
const NAME_EFFECT = "effect";
|
|
26
|
+
const NAME_SIGNAL = "signal";
|
|
27
|
+
const NAME_STORE = "store";
|
|
28
|
+
const NAMES = new Set([
|
|
29
|
+
NAME_ARRAY,
|
|
30
|
+
NAME_COMPUTED,
|
|
31
|
+
NAME_SIGNAL,
|
|
32
|
+
NAME_STORE
|
|
33
|
+
]);
|
|
34
|
+
export { ACTIVE, ARRAY_OFFSET, ARRAY_PEEK, ARRAY_THRESHOLD, BATCH, METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAMES, NAME_ARRAY, NAME_COMPUTED, NAME_EFFECT, NAME_SIGNAL, NAME_STORE };
|
package/dist/effect.js
CHANGED
|
@@ -1,31 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
effect2.state.callback();
|
|
18
|
-
} finally {
|
|
19
|
-
activeEffect = previousEffect;
|
|
20
|
-
}
|
|
1
|
+
import { ACTIVE, NAME_EFFECT } from "./constants.js";
|
|
2
|
+
var Effect = class {
|
|
3
|
+
constructor(callback) {
|
|
4
|
+
Object.defineProperty(this, "$mora", { value: NAME_EFFECT });
|
|
5
|
+
this.state = { callback };
|
|
6
|
+
runEffect(this);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function runEffect(effect$1) {
|
|
10
|
+
const previousEffect = ACTIVE.effect;
|
|
11
|
+
ACTIVE.effect = effect$1;
|
|
12
|
+
try {
|
|
13
|
+
effect$1.state.callback();
|
|
14
|
+
} finally {
|
|
15
|
+
ACTIVE.effect = previousEffect;
|
|
16
|
+
}
|
|
21
17
|
}
|
|
22
18
|
function effect(callback) {
|
|
23
|
-
|
|
19
|
+
return typeof callback === "function" ? new Effect(callback) : void 0;
|
|
24
20
|
}
|
|
25
|
-
|
|
26
|
-
export {
|
|
27
|
-
Effect,
|
|
28
|
-
activeEffect,
|
|
29
|
-
effect,
|
|
30
|
-
runEffect
|
|
31
|
-
};
|
|
21
|
+
export { Effect, effect, runEffect };
|
package/dist/helpers/is.js
CHANGED
|
@@ -1,40 +1,23 @@
|
|
|
1
|
+
import { NAMES, NAME_ARRAY, NAME_COMPUTED, NAME_EFFECT, NAME_SIGNAL, NAME_STORE } from "../constants.js";
|
|
1
2
|
function isArray(value) {
|
|
2
|
-
|
|
3
|
+
return isMora(value, NAME_ARRAY);
|
|
3
4
|
}
|
|
4
5
|
function isComputed(value) {
|
|
5
|
-
|
|
6
|
+
return isMora(value, NAME_COMPUTED);
|
|
6
7
|
}
|
|
7
8
|
function isEffect(value) {
|
|
8
|
-
|
|
9
|
+
return isMora(value, NAME_EFFECT);
|
|
9
10
|
}
|
|
10
11
|
function isMora(value, name) {
|
|
11
|
-
|
|
12
|
+
return typeof value === "object" && value != null && "$mora" in value && (typeof name === "string" ? value.$mora === name : name.has(value.$mora));
|
|
12
13
|
}
|
|
13
14
|
function isReactive(value) {
|
|
14
|
-
|
|
15
|
+
return isMora(value, NAMES);
|
|
15
16
|
}
|
|
16
17
|
function isSignal(value) {
|
|
17
|
-
|
|
18
|
+
return isMora(value, NAME_SIGNAL);
|
|
18
19
|
}
|
|
19
20
|
function isStore(value) {
|
|
20
|
-
|
|
21
|
+
return isMora(value, NAME_STORE);
|
|
21
22
|
}
|
|
22
|
-
|
|
23
|
-
const computedName = "computed";
|
|
24
|
-
const effectName = "effect";
|
|
25
|
-
const signalName = "signal";
|
|
26
|
-
const storeName = "store";
|
|
27
|
-
const reactiveNames = /* @__PURE__ */ new Set([arrayName, computedName, signalName, storeName]);
|
|
28
|
-
export {
|
|
29
|
-
arrayName,
|
|
30
|
-
computedName,
|
|
31
|
-
effectName,
|
|
32
|
-
isArray,
|
|
33
|
-
isComputed,
|
|
34
|
-
isEffect,
|
|
35
|
-
isReactive,
|
|
36
|
-
isSignal,
|
|
37
|
-
isStore,
|
|
38
|
-
signalName,
|
|
39
|
-
storeName
|
|
40
|
-
};
|
|
23
|
+
export { isArray, isComputed, isEffect, isReactive, isSignal, isStore };
|
package/dist/helpers/proxy.js
CHANGED
|
@@ -2,55 +2,43 @@ import { startBatch, stopBatch } from "../batch.js";
|
|
|
2
2
|
import { computed } from "../value/computed.js";
|
|
3
3
|
import { emitValue } from "./value.js";
|
|
4
4
|
function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
let item = mapped.get(key);
|
|
6
|
+
if (item == null) {
|
|
7
|
+
item = computed(() => {
|
|
8
|
+
const value = reactive.get();
|
|
9
|
+
return isArray ? value.at(key) : value[key];
|
|
10
|
+
});
|
|
11
|
+
mapped.set(key, item);
|
|
12
|
+
}
|
|
13
|
+
return item;
|
|
14
14
|
}
|
|
15
15
|
function setProxyValue(proxy, value) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
stopBatch();
|
|
16
|
+
startBatch();
|
|
17
|
+
const proxyKeys = Object.keys(proxy);
|
|
18
|
+
const valueKeys = Object.keys(value);
|
|
19
|
+
let { length } = proxyKeys;
|
|
20
|
+
for (let index = 0; index < length; index += 1) {
|
|
21
|
+
const key = proxyKeys[index];
|
|
22
|
+
proxy[key] = valueKeys.includes(key) ? value[key] : void 0;
|
|
23
|
+
}
|
|
24
|
+
length = valueKeys.length;
|
|
25
|
+
for (let index = 0; index < length; index += 1) {
|
|
26
|
+
const key = valueKeys[index];
|
|
27
|
+
if (!proxyKeys.includes(key)) proxy[key] = value[key];
|
|
28
|
+
}
|
|
29
|
+
stopBatch();
|
|
33
30
|
}
|
|
34
|
-
function setValueInProxy(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (isArray) {
|
|
47
|
-
length == null ? void 0 : length.set(target.length);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return true;
|
|
31
|
+
function setValueInProxy(parameters) {
|
|
32
|
+
const { isArray, length, property, state, target, value } = parameters;
|
|
33
|
+
if (isArray) {
|
|
34
|
+
if (!(!Number.isNaN(Number(property)) || property === "length")) return Reflect.set(target, property, value);
|
|
35
|
+
}
|
|
36
|
+
const previous = Reflect.get(target, property);
|
|
37
|
+
if (!state.equal(previous, value)) {
|
|
38
|
+
Reflect.set(target, property, value);
|
|
39
|
+
emitValue(state);
|
|
40
|
+
if (isArray) length?.set(target.length);
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
51
43
|
}
|
|
52
|
-
export {
|
|
53
|
-
getReactiveValueInProxy,
|
|
54
|
-
setProxyValue,
|
|
55
|
-
setValueInProxy
|
|
56
|
-
};
|
|
44
|
+
export { getReactiveValueInProxy, setProxyValue, setValueInProxy };
|
package/dist/helpers/value.js
CHANGED
|
@@ -1,54 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { activeComputed } from "../value/computed.js";
|
|
1
|
+
import { ACTIVE, ARRAY_OFFSET, ARRAY_PEEK, ARRAY_THRESHOLD, BATCH } from "../constants.js";
|
|
2
|
+
import { flushHandlers } from "../batch.js";
|
|
4
3
|
function emitValue(state) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
batchedHandlers.add(effect);
|
|
10
|
-
}
|
|
11
|
-
for (const [, subscription] of state.subscriptions) {
|
|
12
|
-
batchedHandlers.add(subscription);
|
|
13
|
-
}
|
|
14
|
-
if (batchDepth === 0) {
|
|
15
|
-
flushHandlers();
|
|
16
|
-
}
|
|
4
|
+
for (const computed of state.computeds) computed.effect.dirty = true;
|
|
5
|
+
for (const effect of state.effects) BATCH.handlers.add(effect);
|
|
6
|
+
for (const [, subscription] of state.subscriptions) BATCH.handlers.add(subscription);
|
|
7
|
+
if (BATCH.depth === 0) flushHandlers();
|
|
17
8
|
}
|
|
18
9
|
function equalArrays(state, first, second) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
length -= offset;
|
|
34
|
-
for (let index = offset; index < length; index += 1) {
|
|
35
|
-
if (!state.equal(first[index], second[index])) {
|
|
36
|
-
return false;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
return true;
|
|
10
|
+
let { length } = first;
|
|
11
|
+
if (length !== second.length) return false;
|
|
12
|
+
let offset = 0;
|
|
13
|
+
if (length >= 100) {
|
|
14
|
+
offset = Math.round(length / 10);
|
|
15
|
+
offset = offset > 25 ? 25 : offset;
|
|
16
|
+
for (let index = 0; index < offset; index += 1) if (!state.equal(first[index], second[index])) return false;
|
|
17
|
+
}
|
|
18
|
+
length -= offset;
|
|
19
|
+
for (let index = offset; index < length; index += 1) if (!state.equal(first[index], second[index])) return false;
|
|
20
|
+
return true;
|
|
40
21
|
}
|
|
41
22
|
function getValue(state) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (activeEffect != null) {
|
|
46
|
-
state.effects.add(activeEffect);
|
|
47
|
-
}
|
|
48
|
-
return state.value;
|
|
23
|
+
if (ACTIVE.computed != null) state.computeds.add(ACTIVE.computed);
|
|
24
|
+
if (ACTIVE.effect != null) state.effects.add(ACTIVE.effect);
|
|
25
|
+
return state.value;
|
|
49
26
|
}
|
|
50
|
-
export {
|
|
51
|
-
emitValue,
|
|
52
|
-
equalArrays,
|
|
53
|
-
getValue
|
|
54
|
-
};
|
|
27
|
+
export { emitValue, equalArrays, getValue };
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
import { startBatch, stopBatch } from "./batch.js";
|
|
2
1
|
import { effect } from "./effect.js";
|
|
3
2
|
import { isArray, isComputed, isEffect, isReactive, isSignal } from "./helpers/is.js";
|
|
4
|
-
import {
|
|
3
|
+
import { startBatch, stopBatch } from "./batch.js";
|
|
5
4
|
import { computed } from "./value/computed.js";
|
|
6
5
|
import { signal } from "./value/signal.js";
|
|
6
|
+
import { array } from "./value/array.js";
|
|
7
7
|
import { store } from "./value/store.js";
|
|
8
|
-
export {
|
|
9
|
-
array,
|
|
10
|
-
computed,
|
|
11
|
-
effect,
|
|
12
|
-
isArray,
|
|
13
|
-
isComputed,
|
|
14
|
-
isEffect,
|
|
15
|
-
isReactive,
|
|
16
|
-
isSignal,
|
|
17
|
-
signal,
|
|
18
|
-
startBatch,
|
|
19
|
-
stopBatch,
|
|
20
|
-
store
|
|
21
|
-
};
|
|
8
|
+
export { array, computed, effect, isArray, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch, store };
|
package/dist/models.js
ADDED
|
File without changes
|