@oscarpalmer/mora 0.20.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.
Files changed (44) hide show
  1. package/dist/batch.js +11 -24
  2. package/dist/effect.js +17 -26
  3. package/dist/helpers/is.js +14 -21
  4. package/dist/helpers/proxy.js +34 -47
  5. package/dist/helpers/value.js +20 -46
  6. package/dist/index.js +4 -17
  7. package/dist/mora.full.js +8 -2
  8. package/dist/subscription.js +20 -28
  9. package/dist/value/array.js +94 -173
  10. package/dist/value/computed.js +34 -56
  11. package/dist/value/reactive.js +29 -53
  12. package/dist/value/signal.js +20 -32
  13. package/dist/value/store.js +30 -59
  14. package/package.json +12 -18
  15. package/src/helpers/value.ts +1 -1
  16. package/src/index.ts +0 -1
  17. package/src/value/array.ts +13 -3
  18. package/types/value/array.d.ts +1 -0
  19. package/dist/batch.cjs +0 -32
  20. package/dist/effect.cjs +0 -30
  21. package/dist/helpers/is.cjs +0 -40
  22. package/dist/helpers/proxy.cjs +0 -56
  23. package/dist/helpers/value.cjs +0 -54
  24. package/dist/index.cjs +0 -21
  25. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
  26. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
  27. package/dist/subscription.cjs +0 -32
  28. package/dist/value/array.cjs +0 -181
  29. package/dist/value/computed.cjs +0 -60
  30. package/dist/value/reactive.cjs +0 -55
  31. package/dist/value/signal.cjs +0 -36
  32. package/dist/value/store.cjs +0 -63
  33. package/types/batch.d.cts +0 -79
  34. package/types/effect.d.cts +0 -16
  35. package/types/helpers/is.d.cts +0 -258
  36. package/types/helpers/proxy.d.cts +0 -243
  37. package/types/helpers/value.d.cts +0 -71
  38. package/types/index.d.cts +0 -280
  39. package/types/subscription.d.cts +0 -71
  40. package/types/value/array.d.cts +0 -160
  41. package/types/value/computed.d.cts +0 -81
  42. package/types/value/reactive.d.cts +0 -68
  43. package/types/value/signal.d.cts +0 -87
  44. 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
- 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
- }
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
- batchDepth += 1;
12
+ batchDepth += 1;
18
13
  }
19
14
  function stopBatch() {
20
- if (batchDepth > 0) {
21
- batchDepth -= 1;
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
- 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
- }
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
- return new Effect(callback);
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 };
@@ -1,40 +1,33 @@
1
1
  function isArray(value) {
2
- return isMora(value, arrayName);
2
+ return isMora(value, arrayName);
3
3
  }
4
4
  function isComputed(value) {
5
- return isMora(value, computedName);
5
+ return isMora(value, computedName);
6
6
  }
7
7
  function isEffect(value) {
8
- return isMora(value, effectName);
8
+ return isMora(value, effectName);
9
9
  }
10
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));
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
- return isMora(value, reactiveNames);
14
+ return isMora(value, reactiveNames);
15
15
  }
16
16
  function isSignal(value) {
17
- return isMora(value, signalName);
17
+ return isMora(value, signalName);
18
18
  }
19
19
  function isStore(value) {
20
- return isMora(value, storeName);
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
- 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
- };
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 };
@@ -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
- 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
31
  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;
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 };
@@ -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
- 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
- }
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
- 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;
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
- if (activeComputed != null) {
43
- state.computeds.add(activeComputed);
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 { array } from "./value/array.js";
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/mora.full.js CHANGED
@@ -419,8 +419,14 @@ class ReactiveArray extends Reactive {
419
419
  map(callback) {
420
420
  return computed(() => this.get().map(callback));
421
421
  }
422
- peek(length) {
423
- return length === true ? this.#size.peek() : [...this.state.value];
422
+ peek(value) {
423
+ if (value === true) {
424
+ return this.#size.peek();
425
+ }
426
+ if (typeof value === 'number') {
427
+ return this.state.value.at(value);
428
+ }
429
+ return [...this.state.value];
424
430
  }
425
431
  /**
426
432
  * Remove and return the last item of the array
@@ -1,32 +1,24 @@
1
- class Subscription {
2
- constructor(state, callback) {
3
- this.state = state;
4
- this.callback = callback;
5
- callback(state.value);
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
- if (typeof callback !== "function" || state.subscriptions.has(callback)) {
12
- return noop;
13
- }
14
- state.subscriptions.set(callback, new Subscription(state, callback));
15
- return () => {
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
- const subscription = state.subscriptions.get(callback);
21
- state.subscriptions.delete(callback);
22
- if (subscription != null) {
23
- subscription.callback = noop;
24
- subscription.state = void 0;
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 };