@oscarpalmer/mora 0.21.0 → 0.22.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 -24
- package/dist/effect.js +17 -26
- package/dist/helpers/is.js +14 -21
- package/dist/helpers/proxy.js +34 -47
- package/dist/helpers/value.js +20 -46
- package/dist/index.js +4 -17
- package/dist/subscription.js +20 -28
- package/dist/value/array.js +94 -179
- package/dist/value/computed.js +34 -56
- package/dist/value/reactive.js +29 -53
- package/dist/value/signal.js +20 -32
- package/dist/value/store.js +30 -59
- package/package.json +12 -18
- package/src/helpers/value.ts +1 -1
- package/src/index.ts +0 -1
- package/src/value/array.ts +1 -1
- 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,20 @@
|
|
|
1
|
-
import { runEffect } from "./effect.js";
|
|
2
1
|
import { isEffect } from "./helpers/is.js";
|
|
2
|
+
import { runEffect } from "./effect.js";
|
|
3
3
|
function flushHandlers() {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} else {
|
|
11
|
-
handler.callback(handler.state.value);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
4
|
+
while (batchDepth === 0 && batchedHandlers.size > 0) {
|
|
5
|
+
const handlers = [...batchedHandlers];
|
|
6
|
+
batchedHandlers.clear();
|
|
7
|
+
for (const handler of handlers) if (isEffect(handler)) runEffect(handler);
|
|
8
|
+
else handler.callback(handler.state.value);
|
|
9
|
+
}
|
|
15
10
|
}
|
|
16
11
|
function startBatch() {
|
|
17
|
-
|
|
12
|
+
batchDepth += 1;
|
|
18
13
|
}
|
|
19
14
|
function stopBatch() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
flushHandlers();
|
|
15
|
+
if (batchDepth > 0) batchDepth -= 1;
|
|
16
|
+
flushHandlers();
|
|
24
17
|
}
|
|
25
18
|
const batchedHandlers = /* @__PURE__ */ new Set();
|
|
26
19
|
let batchDepth = 0;
|
|
27
|
-
export {
|
|
28
|
-
batchDepth,
|
|
29
|
-
batchedHandlers,
|
|
30
|
-
flushHandlers,
|
|
31
|
-
startBatch,
|
|
32
|
-
stopBatch
|
|
33
|
-
};
|
|
20
|
+
export { batchDepth, batchedHandlers, flushHandlers, startBatch, stopBatch };
|
package/dist/effect.js
CHANGED
|
@@ -1,31 +1,22 @@
|
|
|
1
1
|
import { effectName } from "./helpers/is.js";
|
|
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
|
-
}
|
|
2
|
+
var Effect = class {
|
|
3
|
+
constructor(callback) {
|
|
4
|
+
Object.defineProperty(this, "$mora", { value: effectName });
|
|
5
|
+
this.state = { callback };
|
|
6
|
+
runEffect(this);
|
|
7
|
+
}
|
|
8
|
+
};
|
|
9
|
+
function runEffect(effect$1) {
|
|
10
|
+
const previousEffect = activeEffect;
|
|
11
|
+
activeEffect = effect$1;
|
|
12
|
+
try {
|
|
13
|
+
effect$1.state.callback();
|
|
14
|
+
} finally {
|
|
15
|
+
activeEffect = previousEffect;
|
|
16
|
+
}
|
|
21
17
|
}
|
|
22
18
|
function effect(callback) {
|
|
23
|
-
|
|
19
|
+
return new Effect(callback);
|
|
24
20
|
}
|
|
25
21
|
let activeEffect;
|
|
26
|
-
export {
|
|
27
|
-
Effect,
|
|
28
|
-
activeEffect,
|
|
29
|
-
effect,
|
|
30
|
-
runEffect
|
|
31
|
-
};
|
|
22
|
+
export { Effect, activeEffect, effect, runEffect };
|
package/dist/helpers/is.js
CHANGED
|
@@ -1,40 +1,33 @@
|
|
|
1
1
|
function isArray(value) {
|
|
2
|
-
|
|
2
|
+
return isMora(value, arrayName);
|
|
3
3
|
}
|
|
4
4
|
function isComputed(value) {
|
|
5
|
-
|
|
5
|
+
return isMora(value, computedName);
|
|
6
6
|
}
|
|
7
7
|
function isEffect(value) {
|
|
8
|
-
|
|
8
|
+
return isMora(value, effectName);
|
|
9
9
|
}
|
|
10
10
|
function isMora(value, name) {
|
|
11
|
-
|
|
11
|
+
return typeof value === "object" && value != null && "$mora" in value && (typeof name === "string" ? value.$mora === name : name.has(value.$mora));
|
|
12
12
|
}
|
|
13
13
|
function isReactive(value) {
|
|
14
|
-
|
|
14
|
+
return isMora(value, reactiveNames);
|
|
15
15
|
}
|
|
16
16
|
function isSignal(value) {
|
|
17
|
-
|
|
17
|
+
return isMora(value, signalName);
|
|
18
18
|
}
|
|
19
19
|
function isStore(value) {
|
|
20
|
-
|
|
20
|
+
return isMora(value, storeName);
|
|
21
21
|
}
|
|
22
22
|
const arrayName = "array";
|
|
23
23
|
const computedName = "computed";
|
|
24
24
|
const effectName = "effect";
|
|
25
25
|
const signalName = "signal";
|
|
26
26
|
const storeName = "store";
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
isEffect,
|
|
35
|
-
isReactive,
|
|
36
|
-
isSignal,
|
|
37
|
-
isStore,
|
|
38
|
-
signalName,
|
|
39
|
-
storeName
|
|
40
|
-
};
|
|
27
|
+
var reactiveNames = new Set([
|
|
28
|
+
arrayName,
|
|
29
|
+
computedName,
|
|
30
|
+
signalName,
|
|
31
|
+
storeName
|
|
32
|
+
]);
|
|
33
|
+
export { arrayName, computedName, effectName, isArray, isComputed, isEffect, isReactive, isSignal, isStore, signalName, storeName };
|
package/dist/helpers/proxy.js
CHANGED
|
@@ -2,55 +2,42 @@ 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
31
|
function setValueInProxy(target, property, value, state, isArray, length) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
emitValue(state);
|
|
46
|
-
if (isArray) {
|
|
47
|
-
length == null ? void 0 : length.set(target.length);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
return true;
|
|
32
|
+
if (isArray) {
|
|
33
|
+
if (!!Number.isNaN(Number(property)) && !(property === "length")) return Reflect.set(target, property, value);
|
|
34
|
+
}
|
|
35
|
+
const previous = Reflect.get(target, property);
|
|
36
|
+
if (!state.equal(previous, value)) {
|
|
37
|
+
Reflect.set(target, property, value);
|
|
38
|
+
emitValue(state);
|
|
39
|
+
if (isArray) length?.set(target.length);
|
|
40
|
+
}
|
|
41
|
+
return true;
|
|
51
42
|
}
|
|
52
|
-
export {
|
|
53
|
-
getReactiveValueInProxy,
|
|
54
|
-
setProxyValue,
|
|
55
|
-
setValueInProxy
|
|
56
|
-
};
|
|
43
|
+
export { getReactiveValueInProxy, setProxyValue, setValueInProxy };
|
package/dist/helpers/value.js
CHANGED
|
@@ -1,54 +1,28 @@
|
|
|
1
|
-
import { batchedHandlers, batchDepth, flushHandlers } from "../batch.js";
|
|
2
1
|
import { activeEffect } from "../effect.js";
|
|
2
|
+
import { batchDepth, batchedHandlers, flushHandlers } from "../batch.js";
|
|
3
3
|
import { activeComputed } from "../value/computed.js";
|
|
4
4
|
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
|
-
}
|
|
5
|
+
for (const computed of state.computeds) computed.effect.dirty = true;
|
|
6
|
+
for (const effect of state.effects) batchedHandlers.add(effect);
|
|
7
|
+
for (const [, subscription] of state.subscriptions) batchedHandlers.add(subscription);
|
|
8
|
+
if (batchDepth === 0) flushHandlers();
|
|
17
9
|
}
|
|
18
10
|
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;
|
|
11
|
+
let { length } = first;
|
|
12
|
+
if (length !== second.length) return false;
|
|
13
|
+
let offset = 0;
|
|
14
|
+
if (length >= 100) {
|
|
15
|
+
offset = Math.round(length / 10);
|
|
16
|
+
offset = offset > 25 ? 25 : offset;
|
|
17
|
+
for (let index = 0; index < offset; index += 1) if (!state.equal(first[index], second[index])) return false;
|
|
18
|
+
}
|
|
19
|
+
length -= offset;
|
|
20
|
+
for (let index = offset; index < length; index += 1) if (!state.equal(first[index], second[index])) return false;
|
|
21
|
+
return true;
|
|
40
22
|
}
|
|
41
23
|
function getValue(state) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (activeEffect != null) {
|
|
46
|
-
state.effects.add(activeEffect);
|
|
47
|
-
}
|
|
48
|
-
return state.value;
|
|
24
|
+
if (activeComputed != null) state.computeds.add(activeComputed);
|
|
25
|
+
if (activeEffect != null) state.effects.add(activeEffect);
|
|
26
|
+
return state.value;
|
|
49
27
|
}
|
|
50
|
-
export {
|
|
51
|
-
emitValue,
|
|
52
|
-
equalArrays,
|
|
53
|
-
getValue
|
|
54
|
-
};
|
|
28
|
+
export { emitValue, equalArrays, getValue };
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,8 @@
|
|
|
1
|
-
import { startBatch, stopBatch } from "./batch.js";
|
|
2
|
-
import { effect } from "./effect.js";
|
|
3
1
|
import { isArray, isComputed, isEffect, isReactive, isSignal } from "./helpers/is.js";
|
|
4
|
-
import {
|
|
2
|
+
import { effect } from "./effect.js";
|
|
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/subscription.js
CHANGED
|
@@ -1,32 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
8
|
-
function noop() {
|
|
9
|
-
}
|
|
1
|
+
var Subscription = class {
|
|
2
|
+
constructor(state, callback) {
|
|
3
|
+
this.state = state;
|
|
4
|
+
this.callback = callback;
|
|
5
|
+
callback(state.value);
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
function noop() {}
|
|
10
9
|
function subscribe(state, callback) {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
unsubscribe(state, callback);
|
|
17
|
-
};
|
|
10
|
+
if (typeof callback !== "function" || state.subscriptions.has(callback)) return noop;
|
|
11
|
+
state.subscriptions.set(callback, new Subscription(state, callback));
|
|
12
|
+
return () => {
|
|
13
|
+
unsubscribe(state, callback);
|
|
14
|
+
};
|
|
18
15
|
}
|
|
19
16
|
function unsubscribe(state, callback) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
17
|
+
const subscription = state.subscriptions.get(callback);
|
|
18
|
+
state.subscriptions.delete(callback);
|
|
19
|
+
if (subscription != null) {
|
|
20
|
+
subscription.callback = noop;
|
|
21
|
+
subscription.state = void 0;
|
|
22
|
+
}
|
|
26
23
|
}
|
|
27
|
-
export {
|
|
28
|
-
Subscription,
|
|
29
|
-
noop,
|
|
30
|
-
subscribe,
|
|
31
|
-
unsubscribe
|
|
32
|
-
};
|
|
24
|
+
export { Subscription, noop, subscribe, unsubscribe };
|