@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.
Files changed (70) hide show
  1. package/dist/batch.js +11 -25
  2. package/dist/constants.js +34 -0
  3. package/dist/effect.js +18 -28
  4. package/dist/helpers/is.js +9 -26
  5. package/dist/helpers/proxy.js +36 -48
  6. package/dist/helpers/value.js +21 -48
  7. package/dist/index.js +3 -16
  8. package/dist/models.js +0 -0
  9. package/dist/mora.full.js +218 -130
  10. package/dist/subscription.js +22 -28
  11. package/dist/value/array.js +97 -182
  12. package/dist/value/computed.js +33 -58
  13. package/dist/value/reactive.js +29 -53
  14. package/dist/value/signal.js +21 -33
  15. package/dist/value/store.js +37 -60
  16. package/package.json +17 -24
  17. package/src/batch.ts +11 -13
  18. package/src/constants.ts +49 -0
  19. package/src/effect.ts +11 -16
  20. package/src/helpers/is.ts +41 -19
  21. package/src/helpers/proxy.ts +34 -38
  22. package/src/helpers/value.ts +19 -14
  23. package/src/index.ts +1 -1
  24. package/src/models.ts +66 -0
  25. package/src/subscription.ts +14 -16
  26. package/src/value/array.ts +87 -45
  27. package/src/value/computed.ts +29 -39
  28. package/src/value/reactive.ts +9 -24
  29. package/src/value/signal.ts +9 -3
  30. package/src/value/store.ts +40 -9
  31. package/types/batch.d.ts +3 -5
  32. package/types/constants.d.ts +14 -0
  33. package/types/effect.d.ts +2 -1
  34. package/types/helpers/is.d.ts +23 -10
  35. package/types/helpers/proxy.d.ts +2 -3
  36. package/types/helpers/value.d.ts +1 -1
  37. package/types/index.d.ts +1 -0
  38. package/types/models.d.ts +56 -0
  39. package/types/subscription.d.ts +3 -6
  40. package/types/value/array.d.ts +48 -7
  41. package/types/value/computed.d.ts +2 -11
  42. package/types/value/reactive.d.ts +8 -17
  43. package/types/value/signal.d.ts +7 -1
  44. package/types/value/store.d.ts +28 -4
  45. package/dist/batch.cjs +0 -32
  46. package/dist/effect.cjs +0 -30
  47. package/dist/helpers/is.cjs +0 -40
  48. package/dist/helpers/proxy.cjs +0 -56
  49. package/dist/helpers/value.cjs +0 -54
  50. package/dist/index.cjs +0 -21
  51. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
  52. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
  53. package/dist/subscription.cjs +0 -32
  54. package/dist/value/array.cjs +0 -187
  55. package/dist/value/computed.cjs +0 -60
  56. package/dist/value/reactive.cjs +0 -55
  57. package/dist/value/signal.cjs +0 -36
  58. package/dist/value/store.cjs +0 -63
  59. package/types/batch.d.cts +0 -79
  60. package/types/effect.d.cts +0 -16
  61. package/types/helpers/is.d.cts +0 -259
  62. package/types/helpers/proxy.d.cts +0 -244
  63. package/types/helpers/value.d.cts +0 -71
  64. package/types/index.d.cts +0 -281
  65. package/types/subscription.d.cts +0 -71
  66. package/types/value/array.d.cts +0 -161
  67. package/types/value/computed.d.cts +0 -81
  68. package/types/value/reactive.d.cts +0 -68
  69. package/types/value/signal.d.cts +0 -87
  70. 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
- while (batchDepth === 0 && batchedHandlers.size > 0) {
5
- const handlers = [...batchedHandlers];
6
- batchedHandlers.clear();
7
- for (const handler of handlers) {
8
- if (isEffect(handler)) {
9
- runEffect(handler);
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
- batchDepth += 1;
13
+ BATCH.depth += 1;
18
14
  }
19
15
  function stopBatch() {
20
- if (batchDepth > 0) {
21
- batchDepth -= 1;
22
- }
23
- flushHandlers();
16
+ if (BATCH.depth > 0) BATCH.depth -= 1;
17
+ flushHandlers();
24
18
  }
25
- const batchedHandlers = /* @__PURE__ */ new Set();
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 { effectName } from "./helpers/is.js";
2
- class Effect {
3
- constructor(callback) {
4
- Object.defineProperty(this, "$mora", {
5
- value: effectName
6
- });
7
- this.state = {
8
- callback
9
- };
10
- runEffect(this);
11
- }
12
- }
13
- function runEffect(effect2) {
14
- const previousEffect = activeEffect;
15
- activeEffect = effect2;
16
- try {
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
- return new Effect(callback);
19
+ return typeof callback === "function" ? new Effect(callback) : void 0;
24
20
  }
25
- let activeEffect;
26
- export {
27
- Effect,
28
- activeEffect,
29
- effect,
30
- runEffect
31
- };
21
+ export { Effect, effect, runEffect };
@@ -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
- return isMora(value, arrayName);
3
+ return isMora(value, NAME_ARRAY);
3
4
  }
4
5
  function isComputed(value) {
5
- return isMora(value, computedName);
6
+ return isMora(value, NAME_COMPUTED);
6
7
  }
7
8
  function isEffect(value) {
8
- return isMora(value, effectName);
9
+ return isMora(value, NAME_EFFECT);
9
10
  }
10
11
  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
+ 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
- return isMora(value, reactiveNames);
15
+ return isMora(value, NAMES);
15
16
  }
16
17
  function isSignal(value) {
17
- return isMora(value, signalName);
18
+ return isMora(value, NAME_SIGNAL);
18
19
  }
19
20
  function isStore(value) {
20
- return isMora(value, storeName);
21
+ return isMora(value, NAME_STORE);
21
22
  }
22
- const arrayName = "array";
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 };
@@ -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
- 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;
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
- 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)) {
28
- const keyedValue = value[key];
29
- proxy[key] = keyedValue;
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(target, property, value, state, isArray, length) {
35
- if (isArray) {
36
- const isIndex = !Number.isNaN(Number(property));
37
- const isLength = property === "length";
38
- if (!isIndex && !isLength) {
39
- return Reflect.set(target, property, value);
40
- }
41
- }
42
- const previous = Reflect.get(target, property);
43
- if (!state.equal(previous, value)) {
44
- Reflect.set(target, property, value);
45
- emitValue(state);
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 };
@@ -1,54 +1,27 @@
1
- import { batchedHandlers, batchDepth, flushHandlers } from "../batch.js";
2
- import { activeEffect } from "../effect.js";
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
- for (const computed of state.computeds) {
6
- computed.effect.dirty = true;
7
- }
8
- for (const effect of state.effects) {
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
- let { length } = first;
20
- if (length !== second.length) {
21
- return false;
22
- }
23
- let offset = 0;
24
- if (length >= 100) {
25
- offset = Math.round(length / 10);
26
- offset = offset > 25 ? 25 : offset;
27
- for (let index = 0; index < offset; index += 1) {
28
- if (!state.equal(first[index], second[index])) {
29
- return false;
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
- if (activeComputed != null) {
43
- state.computeds.add(activeComputed);
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 { array } from "./value/array.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/models.js ADDED
File without changes