@oscarpalmer/mora 0.13.0 → 0.15.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 (62) hide show
  1. package/dist/batch.cjs +2 -2
  2. package/dist/batch.js +1 -1
  3. package/dist/effect.cjs +2 -1
  4. package/dist/effect.js +2 -1
  5. package/dist/helpers/emit.cjs +42 -0
  6. package/dist/helpers/emit.js +42 -0
  7. package/dist/helpers/is.cjs +34 -0
  8. package/dist/helpers/is.js +34 -0
  9. package/dist/helpers/value.cjs +22 -0
  10. package/dist/helpers/value.js +22 -0
  11. package/dist/index.cjs +12 -11
  12. package/dist/index.js +5 -4
  13. package/dist/mora.full.js +200 -140
  14. package/dist/value/array.cjs +161 -0
  15. package/dist/{array.js → value/array.js} +62 -31
  16. package/dist/{computed.cjs → value/computed.cjs} +6 -5
  17. package/dist/{computed.js → value/computed.js} +4 -3
  18. package/dist/{reactive.cjs → value/reactive.cjs} +1 -1
  19. package/dist/{reactive.js → value/reactive.js} +1 -1
  20. package/dist/value/signal.cjs +33 -0
  21. package/dist/{signal.js → value/signal.js} +3 -2
  22. package/package.json +1 -1
  23. package/src/batch.ts +1 -1
  24. package/src/effect.ts +12 -11
  25. package/src/helpers/emit.ts +52 -0
  26. package/src/helpers/is.ts +56 -0
  27. package/src/helpers/value.ts +27 -0
  28. package/src/index.ts +11 -5
  29. package/src/subscription.ts +1 -1
  30. package/src/{array.ts → value/array.ts} +75 -41
  31. package/src/{computed.ts → value/computed.ts} +4 -3
  32. package/src/{reactive.ts → value/reactive.ts} +3 -3
  33. package/src/{signal.ts → value/signal.ts} +3 -2
  34. package/types/batch.d.cts +9 -9
  35. package/types/helpers/emit.d.cts +63 -0
  36. package/types/helpers/emit.d.ts +3 -0
  37. package/types/{is.d.cts → helpers/is.d.cts} +85 -9
  38. package/types/helpers/is.d.ts +26 -0
  39. package/types/{value.d.cts → helpers/value.d.cts} +9 -10
  40. package/types/{value.d.ts → helpers/value.d.ts} +1 -2
  41. package/types/index.d.cts +74 -38
  42. package/types/index.d.ts +5 -5
  43. package/types/subscription.d.cts +12 -12
  44. package/types/subscription.d.ts +1 -1
  45. package/types/{array.d.cts → value/array.d.cts} +41 -9
  46. package/types/value/array.d.ts +74 -0
  47. package/types/{computed.d.ts → value/computed.d.ts} +1 -1
  48. package/types/{reactive.d.ts → value/reactive.d.ts} +2 -2
  49. package/dist/array.cjs +0 -130
  50. package/dist/is.cjs +0 -21
  51. package/dist/is.js +0 -21
  52. package/dist/signal.cjs +0 -32
  53. package/dist/value.cjs +0 -38
  54. package/dist/value.js +0 -38
  55. package/src/is.ts +0 -39
  56. package/src/value.ts +0 -47
  57. package/types/array.d.ts +0 -41
  58. package/types/is.d.ts +0 -17
  59. package/types/{computed.d.cts → value/computed.d.cts} +9 -9
  60. package/types/{reactive.d.cts → value/reactive.d.cts} +9 -9
  61. package/types/{signal.d.cts → value/signal.d.cts} +9 -9
  62. /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 is = require("./is.cjs");
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 (is.isEffect(handler)) {
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
@@ -1,5 +1,5 @@
1
1
  import { runEffect } from "./effect.js";
2
- import { isEffect } from "./is.js";
2
+ import { isEffect } from "./helpers/is.js";
3
3
  function flushEffects() {
4
4
  while (batchDepth === 0 && batchedHandlers.size > 0) {
5
5
  const handlers = [...batchedHandlers];
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: "effect"
7
+ value: helpers_is.effectName
7
8
  });
8
9
  this.state = {
9
10
  callback
package/dist/effect.js CHANGED
@@ -1,7 +1,8 @@
1
+ import { effectName } from "./helpers/is.js";
1
2
  class Effect {
2
3
  constructor(callback) {
3
4
  Object.defineProperty(this, "$mora", {
4
- value: "effect"
5
+ value: effectName
5
6
  });
6
7
  this.state = {
7
8
  callback
@@ -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 is = require("./is.cjs");
8
- const signal = require("./signal.cjs");
9
- exports.array = array.array;
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.isComputed = is.isComputed;
15
- exports.isEffect = is.isEffect;
16
- exports.isReactive = is.isReactive;
17
- exports.isSignal = is.isSignal;
18
- exports.signal = signal.signal;
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 { signal } from "./signal.js";
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,