@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
@@ -1,181 +1,102 @@
1
- var __typeError = (msg) => {
2
- throw TypeError(msg);
3
- };
4
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
- var _indiced, _size;
1
+ import { noop, subscribe } from "../subscription.js";
8
2
  import { arrayName } from "../helpers/is.js";
9
- import { setValueInProxy, getReactiveValueInProxy } from "../helpers/proxy.js";
10
- import { getValue, equalArrays, emitValue } from "../helpers/value.js";
11
- import { subscribe, noop } from "../subscription.js";
12
- import { computed } from "./computed.js";
13
3
  import { Reactive } from "./reactive.js";
4
+ import { computed } from "./computed.js";
5
+ import { emitValue, equalArrays, getValue } from "../helpers/value.js";
6
+ import { getReactiveValueInProxy, setValueInProxy } from "../helpers/proxy.js";
14
7
  import { signal } from "./signal.js";
15
- class ReactiveArray extends Reactive {
16
- constructor(value, options) {
17
- super(
18
- arrayName,
19
- new Proxy(value, {
20
- get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, __privateGet(this, _size)) : Reflect.get(target, property),
21
- set: (target, property, value2) => setValueInProxy(
22
- target,
23
- property,
24
- value2,
25
- this.state,
26
- true,
27
- __privateGet(this, _size)
28
- )
29
- }),
30
- options
31
- );
32
- __privateAdd(this, _indiced, /* @__PURE__ */ new Map());
33
- __privateAdd(this, _size, signal(0));
34
- __privateGet(this, _size).set(value.length);
35
- }
36
- /**
37
- * The length of the array
38
- */
39
- get length() {
40
- return __privateGet(this, _size).get();
41
- }
42
- /**
43
- * Set the length of the array
44
- */
45
- set length(value) {
46
- if (typeof value === "number" && value >= 0 && value !== this.state.value.length) {
47
- this.get().length = value;
48
- }
49
- }
50
- /**
51
- * Clear the array
52
- */
53
- clear() {
54
- this.length = 0;
55
- }
56
- get(first) {
57
- if (typeof first === "number") {
58
- return getReactiveValueInProxy(this, __privateGet(this, _indiced), first, true).get();
59
- }
60
- if (first === "length") {
61
- return this.length;
62
- }
63
- return getValue(this.state);
64
- }
65
- /**
66
- * Create a computed, filtered array
67
- */
68
- filter(callback) {
69
- return computed(() => this.get().filter(callback));
70
- }
71
- /**
72
- * Create a computed, mapped array
73
- */
74
- map(callback) {
75
- return computed(() => this.get().map(callback));
76
- }
77
- peek(length) {
78
- return length === true ? __privateGet(this, _size).peek() : [...this.state.value];
79
- }
80
- /**
81
- * Remove and return the last item of the array
82
- */
83
- pop() {
84
- return this.state.value.pop();
85
- }
86
- /**
87
- * Add items to the end of the array
88
- */
89
- push(...items) {
90
- return this.state.value.push(...items);
91
- }
92
- set(first, second) {
93
- if (first == null || Array.isArray(first)) {
94
- this.state.value.splice(0, this.state.value.length, ...first ?? []);
95
- } else if (first === "length") {
96
- this.length = second;
97
- } else if (typeof first === "number") {
98
- this.state.value[first] = second;
99
- }
100
- }
101
- /**
102
- * Remove and return the first item of the array
103
- */
104
- shift() {
105
- return this.state.value.shift();
106
- }
107
- /**
108
- * Remove and return items from the array _(and optionally add new items)_
109
- */
110
- splice(from, to, ...items) {
111
- return this.state.value.splice(
112
- from,
113
- to ?? this.state.value.length,
114
- ...items
115
- );
116
- }
117
- subscribe(first, second) {
118
- if (typeof first === "number" && typeof second === "function") {
119
- return getReactiveValueInProxy(
120
- this,
121
- __privateGet(this, _indiced),
122
- first,
123
- true
124
- ).subscribe(second);
125
- }
126
- return typeof first === "function" ? subscribe(this.state, first) : noop;
127
- }
128
- /**
129
- * Add items to the beginning of the array
130
- */
131
- unshift(...items) {
132
- return this.state.value.unshift(...items);
133
- }
134
- /**
135
- * Update the value _(based on the current value)_
136
- */
137
- update(callback) {
138
- const updated = callback(this.state.value);
139
- if (updated == null || Array.isArray(updated)) {
140
- this.set(updated);
141
- }
142
- }
143
- }
144
- _indiced = new WeakMap();
145
- _size = new WeakMap();
8
+ var ReactiveArray = class extends Reactive {
9
+ #indiced = /* @__PURE__ */ new Map();
10
+ #size = signal(0);
11
+ get length() {
12
+ return this.#size.get();
13
+ }
14
+ set length(value) {
15
+ if (typeof value === "number" && value >= 0 && value !== this.state.value.length) this.get().length = value;
16
+ }
17
+ constructor(value, options) {
18
+ super(arrayName, new Proxy(value, {
19
+ get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, this.#size) : Reflect.get(target, property),
20
+ set: (target, property, value$1) => setValueInProxy(target, property, value$1, this.state, true, this.#size)
21
+ }), options);
22
+ this.#size.set(value.length);
23
+ }
24
+ clear() {
25
+ this.length = 0;
26
+ }
27
+ get(first) {
28
+ if (typeof first === "number") return getReactiveValueInProxy(this, this.#indiced, first, true).get();
29
+ if (first === "length") return this.length;
30
+ return getValue(this.state);
31
+ }
32
+ filter(callback) {
33
+ return computed(() => this.get().filter(callback));
34
+ }
35
+ map(callback) {
36
+ return computed(() => this.get().map(callback));
37
+ }
38
+ peek(value) {
39
+ if (value === true) return this.#size.peek();
40
+ if (typeof value === "number") return this.state.value.at(value);
41
+ return [...this.state.value];
42
+ }
43
+ pop() {
44
+ return this.state.value.pop();
45
+ }
46
+ push(...items) {
47
+ return this.state.value.push(...items);
48
+ }
49
+ set(first, second) {
50
+ if (first == null || Array.isArray(first)) this.state.value.splice(0, this.state.value.length, ...first ?? []);
51
+ else if (first === "length") this.length = second;
52
+ else if (typeof first === "number") this.state.value[first] = second;
53
+ }
54
+ shift() {
55
+ return this.state.value.shift();
56
+ }
57
+ splice(from, to, ...items) {
58
+ return this.state.value.splice(from, to ?? this.state.value.length, ...items);
59
+ }
60
+ subscribe(first, second) {
61
+ if (typeof first === "number" && typeof second === "function") return getReactiveValueInProxy(this, this.#indiced, first, true).subscribe(second);
62
+ return typeof first === "function" ? subscribe(this.state, first) : noop;
63
+ }
64
+ unshift(...items) {
65
+ return this.state.value.unshift(...items);
66
+ }
67
+ update(callback) {
68
+ const updated = callback(this.state.value);
69
+ if (updated == null || Array.isArray(updated)) this.set(updated);
70
+ }
71
+ };
146
72
  function array(value, options) {
147
- return new ReactiveArray(Array.isArray(value) ? value : [], options);
73
+ return new ReactiveArray(Array.isArray(value) ? value : [], options);
148
74
  }
149
- function updateArray(type, array2, state, length) {
150
- const affectsLength = lengthAffectingMethods.has(type);
151
- const previousArray = affectsLength ? [] : [...array2];
152
- const previousLength = array2.length;
153
- return (...args) => {
154
- const result = array2[type](
155
- ...args
156
- );
157
- if (affectsLength ? array2.length !== previousLength : !equalArrays(state, previousArray, array2)) {
158
- emitValue(state);
159
- length.set(array2.length);
160
- }
161
- return result;
162
- };
75
+ function updateArray(type, array$1, state, length) {
76
+ const affectsLength = lengthAffectingMethods.has(type);
77
+ const previousArray = affectsLength ? [] : [...array$1];
78
+ const previousLength = array$1.length;
79
+ return (...args) => {
80
+ const result = array$1[type](...args);
81
+ if (affectsLength ? array$1.length !== previousLength : !equalArrays(state, previousArray, array$1)) {
82
+ emitValue(state);
83
+ length.set(array$1.length);
84
+ }
85
+ return result;
86
+ };
163
87
  }
164
- const lengthAffectingMethods = /* @__PURE__ */ new Set([
165
- "pop",
166
- "push",
167
- "shift",
168
- "unshift"
88
+ var lengthAffectingMethods = new Set([
89
+ "pop",
90
+ "push",
91
+ "shift",
92
+ "unshift"
169
93
  ]);
170
- const updateMethods = /* @__PURE__ */ new Set([
171
- ...lengthAffectingMethods,
172
- "copyWithin",
173
- "fill",
174
- "reverse",
175
- "sort",
176
- "splice"
94
+ var updateMethods = new Set([
95
+ ...lengthAffectingMethods,
96
+ "copyWithin",
97
+ "fill",
98
+ "reverse",
99
+ "sort",
100
+ "splice"
177
101
  ]);
178
- export {
179
- ReactiveArray,
180
- array
181
- };
102
+ export { ReactiveArray, array };
@@ -1,61 +1,39 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { batchedHandlers, batchDepth } from "../batch.js";
5
- import { effect, activeEffect, runEffect } from "../effect.js";
6
1
  import { computedName } from "../helpers/is.js";
2
+ import { activeEffect, effect, runEffect } from "../effect.js";
3
+ import { batchDepth, batchedHandlers } from "../batch.js";
7
4
  import { Reactive } from "./reactive.js";
8
5
  let activeComputed;
9
- class Computed extends Reactive {
10
- constructor(callback, options) {
11
- super(computedName, void 0, options);
12
- __publicField(this, "effect", {
13
- dirty: true,
14
- instance: void 0
15
- });
16
- this.effect.instance = effect(() => {
17
- if (this.effect.dirty) {
18
- const previousComputed = activeComputed;
19
- activeComputed = this;
20
- const value = callback();
21
- activeComputed = previousComputed;
22
- if (!this.state.equal(this.state.value, value)) {
23
- this.state.value = value;
24
- for (const computed2 of this.state.computeds) {
25
- computed2.effect.dirty = true;
26
- }
27
- for (const effect2 of this.state.effects) {
28
- batchedHandlers.add(effect2);
29
- }
30
- for (const [, subscription] of this.state.subscriptions) {
31
- subscription.callback(value);
32
- }
33
- }
34
- this.effect.dirty = false;
35
- }
36
- });
37
- }
38
- /**
39
- * @inheritdoc
40
- */
41
- get() {
42
- if (activeComputed != null && this !== activeComputed) {
43
- this.state.computeds.add(activeComputed);
44
- }
45
- if (activeEffect != null && activeEffect !== this.effect.instance) {
46
- this.state.effects.add(activeEffect);
47
- }
48
- if (this.effect.dirty && batchDepth === 0) {
49
- runEffect(this.effect.instance);
50
- }
51
- return this.state.value;
52
- }
53
- }
6
+ var Computed = class extends Reactive {
7
+ effect = {
8
+ dirty: true,
9
+ instance: void 0
10
+ };
11
+ constructor(callback, options) {
12
+ super(computedName, void 0, options);
13
+ this.effect.instance = effect(() => {
14
+ if (this.effect.dirty) {
15
+ const previousComputed = activeComputed;
16
+ activeComputed = this;
17
+ const value = callback();
18
+ activeComputed = previousComputed;
19
+ if (!this.state.equal(this.state.value, value)) {
20
+ this.state.value = value;
21
+ for (const computed$1 of this.state.computeds) computed$1.effect.dirty = true;
22
+ for (const effect$1 of this.state.effects) batchedHandlers.add(effect$1);
23
+ for (const [, subscription] of this.state.subscriptions) subscription.callback(value);
24
+ }
25
+ this.effect.dirty = false;
26
+ }
27
+ });
28
+ }
29
+ get() {
30
+ if (activeComputed != null && this !== activeComputed) this.state.computeds.add(activeComputed);
31
+ if (activeEffect != null && activeEffect !== this.effect.instance) this.state.effects.add(activeEffect);
32
+ if (this.effect.dirty && batchDepth === 0) runEffect(this.effect.instance);
33
+ return this.state.value;
34
+ }
35
+ };
54
36
  function computed(callback, options) {
55
- return new Computed(callback, options);
37
+ return new Computed(callback, options);
56
38
  }
57
- export {
58
- Computed,
59
- activeComputed,
60
- computed
61
- };
39
+ export { Computed, activeComputed, computed };
@@ -1,55 +1,31 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
1
  import { subscribe, unsubscribe } from "../subscription.js";
5
- class Reactive {
6
- constructor(name, value, options) {
7
- __publicField(this, "state", {
8
- computeds: /* @__PURE__ */ new Set(),
9
- effects: /* @__PURE__ */ new Set(),
10
- equal: Object.is,
11
- subscriptions: /* @__PURE__ */ new Map(),
12
- value: void 0
13
- });
14
- this.state.value = value;
15
- if (typeof options === "object" && typeof options.equal === "function") {
16
- this.state.equal = options.equal;
17
- }
18
- Object.defineProperty(this, "$mora", {
19
- value: name
20
- });
21
- }
22
- /**
23
- * Get the value _(without reactivity)_
24
- */
25
- peek() {
26
- return this.state.value;
27
- }
28
- /**
29
- * Subscribe to changes
30
- */
31
- subscribe(callback) {
32
- return subscribe(this.state, callback);
33
- }
34
- /**
35
- * JSON representation of the value
36
- */
37
- toJSON() {
38
- return this.get();
39
- }
40
- /**
41
- * String representation of the value
42
- */
43
- toString() {
44
- return String(this.get());
45
- }
46
- /**
47
- * Unsubscribe from changes
48
- */
49
- unsubscribe(callback) {
50
- unsubscribe(this.state, callback);
51
- }
52
- }
53
- export {
54
- Reactive
2
+ var Reactive = class {
3
+ state = {
4
+ computeds: /* @__PURE__ */ new Set(),
5
+ effects: /* @__PURE__ */ new Set(),
6
+ equal: Object.is,
7
+ subscriptions: /* @__PURE__ */ new Map(),
8
+ value: void 0
9
+ };
10
+ constructor(name, value, options) {
11
+ this.state.value = value;
12
+ if (typeof options === "object" && typeof options.equal === "function") this.state.equal = options.equal;
13
+ Object.defineProperty(this, "$mora", { value: name });
14
+ }
15
+ peek() {
16
+ return this.state.value;
17
+ }
18
+ subscribe(callback) {
19
+ return subscribe(this.state, callback);
20
+ }
21
+ toJSON() {
22
+ return this.get();
23
+ }
24
+ toString() {
25
+ return String(this.get());
26
+ }
27
+ unsubscribe(callback) {
28
+ unsubscribe(this.state, callback);
29
+ }
55
30
  };
31
+ export { Reactive };
@@ -1,36 +1,24 @@
1
1
  import { signalName } from "../helpers/is.js";
2
- import { getValue, emitValue } from "../helpers/value.js";
3
2
  import { Reactive } from "./reactive.js";
4
- class Signal extends Reactive {
5
- constructor(value, options) {
6
- super(signalName, value, options);
7
- }
8
- /**
9
- * @inheritdoc
10
- */
11
- get() {
12
- return getValue(this.state);
13
- }
14
- /**
15
- * Set the value
16
- */
17
- set(value) {
18
- if (!this.state.equal(this.state.value, value)) {
19
- this.state.value = value;
20
- emitValue(this.state);
21
- }
22
- }
23
- /**
24
- * Update the value _(based on the current value)_
25
- */
26
- update(callback) {
27
- this.set(callback(this.state.value));
28
- }
29
- }
3
+ import { emitValue, getValue } from "../helpers/value.js";
4
+ var Signal = class extends Reactive {
5
+ constructor(value, options) {
6
+ super(signalName, value, options);
7
+ }
8
+ get() {
9
+ return getValue(this.state);
10
+ }
11
+ set(value) {
12
+ if (!this.state.equal(this.state.value, value)) {
13
+ this.state.value = value;
14
+ emitValue(this.state);
15
+ }
16
+ }
17
+ update(callback) {
18
+ this.set(callback(this.state.value));
19
+ }
20
+ };
30
21
  function signal(value, options) {
31
- return new Signal(value, options);
22
+ return new Signal(value, options);
32
23
  }
33
- export {
34
- Signal,
35
- signal
36
- };
24
+ export { Signal, signal };
@@ -1,63 +1,34 @@
1
- var __typeError = (msg) => {
2
- throw TypeError(msg);
3
- };
4
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
- var _keyed;
8
- import { isPlainObject, isKey } from "../node_modules/@oscarpalmer/atoms/dist/internal/is.js";
1
+ import { noop, subscribe } from "../subscription.js";
9
2
  import { storeName } from "../helpers/is.js";
10
- import { setValueInProxy, getReactiveValueInProxy, setProxyValue } from "../helpers/proxy.js";
11
- import { getValue } from "../helpers/value.js";
12
- import { subscribe, noop } from "../subscription.js";
13
3
  import { Reactive } from "./reactive.js";
14
- class Store extends Reactive {
15
- constructor(value, options) {
16
- super(
17
- storeName,
18
- new Proxy(value, {
19
- set: (target, property, value2) => setValueInProxy(target, property, value2, this.state, false)
20
- }),
21
- options
22
- );
23
- __privateAdd(this, _keyed, /* @__PURE__ */ new Map());
24
- }
25
- get(key) {
26
- return isKey(key) ? getReactiveValueInProxy(this, __privateGet(this, _keyed), key, false).get() : getValue(this.state);
27
- }
28
- peek(key) {
29
- return isKey(key) ? this.state.value[key] : { ...this.state.value };
30
- }
31
- set(first, second) {
32
- if (isKey(first)) {
33
- this.state.value[first] = second;
34
- } else if (first == null || isPlainObject(first)) {
35
- setProxyValue(this.state.value, first ?? {});
36
- }
37
- }
38
- subscribe(first, second) {
39
- if (isKey(first) && typeof second === "function") {
40
- return getReactiveValueInProxy(this, __privateGet(this, _keyed), first, false).subscribe(
41
- second
42
- );
43
- }
44
- return typeof first === "function" ? subscribe(this.state, first) : noop;
45
- }
46
- /**
47
- * Update the value _(based on the current value)_
48
- */
49
- update(callback) {
50
- const updated = callback({ ...this.state.value });
51
- if (updated == null || isPlainObject(updated)) {
52
- setProxyValue(this.state.value, updated ?? {});
53
- }
54
- }
55
- }
56
- _keyed = new WeakMap();
4
+ import { getValue } from "../helpers/value.js";
5
+ import { getReactiveValueInProxy, setProxyValue, setValueInProxy } from "../helpers/proxy.js";
6
+ import { isKey, isPlainObject } from "@oscarpalmer/atoms/is";
7
+ var Store = class extends Reactive {
8
+ #keyed = /* @__PURE__ */ new Map();
9
+ constructor(value, options) {
10
+ super(storeName, new Proxy(value, { set: (target, property, value$1) => setValueInProxy(target, property, value$1, this.state, false) }), options);
11
+ }
12
+ get(key) {
13
+ return isKey(key) ? getReactiveValueInProxy(this, this.#keyed, key, false).get() : getValue(this.state);
14
+ }
15
+ peek(key) {
16
+ return isKey(key) ? this.state.value[key] : { ...this.state.value };
17
+ }
18
+ set(first, second) {
19
+ if (isKey(first)) this.state.value[first] = second;
20
+ else if (first == null || isPlainObject(first)) setProxyValue(this.state.value, first ?? {});
21
+ }
22
+ subscribe(first, second) {
23
+ if (isKey(first) && typeof second === "function") return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(second);
24
+ return typeof first === "function" ? subscribe(this.state, first) : noop;
25
+ }
26
+ update(callback) {
27
+ const updated = callback({ ...this.state.value });
28
+ if (updated == null || isPlainObject(updated)) setProxyValue(this.state.value, updated ?? {});
29
+ }
30
+ };
57
31
  function store(value, options) {
58
- return new Store(isPlainObject(value) ? value : {}, options);
32
+ return new Store(isPlainObject(value) ? value : {}, options);
59
33
  }
60
- export {
61
- Store,
62
- store
63
- };
34
+ export { Store, store };