@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.
Files changed (42) 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/subscription.js +20 -28
  8. package/dist/value/array.js +94 -179
  9. package/dist/value/computed.js +34 -56
  10. package/dist/value/reactive.js +29 -53
  11. package/dist/value/signal.js +20 -32
  12. package/dist/value/store.js +30 -59
  13. package/package.json +12 -18
  14. package/src/helpers/value.ts +1 -1
  15. package/src/index.ts +0 -1
  16. package/src/value/array.ts +1 -1
  17. package/dist/batch.cjs +0 -32
  18. package/dist/effect.cjs +0 -30
  19. package/dist/helpers/is.cjs +0 -40
  20. package/dist/helpers/proxy.cjs +0 -56
  21. package/dist/helpers/value.cjs +0 -54
  22. package/dist/index.cjs +0 -21
  23. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
  24. package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
  25. package/dist/subscription.cjs +0 -32
  26. package/dist/value/array.cjs +0 -187
  27. package/dist/value/computed.cjs +0 -60
  28. package/dist/value/reactive.cjs +0 -55
  29. package/dist/value/signal.cjs +0 -36
  30. package/dist/value/store.cjs +0 -63
  31. package/types/batch.d.cts +0 -79
  32. package/types/effect.d.cts +0 -16
  33. package/types/helpers/is.d.cts +0 -259
  34. package/types/helpers/proxy.d.cts +0 -244
  35. package/types/helpers/value.d.cts +0 -71
  36. package/types/index.d.cts +0 -281
  37. package/types/subscription.d.cts +0 -71
  38. package/types/value/array.d.cts +0 -161
  39. package/types/value/computed.d.cts +0 -81
  40. package/types/value/reactive.d.cts +0 -68
  41. package/types/value/signal.d.cts +0 -87
  42. package/types/value/store.d.cts +0 -136
@@ -1,187 +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(value) {
78
- if (value === true) {
79
- return __privateGet(this, _size).peek();
80
- }
81
- if (typeof value === "number") {
82
- return this.state.value.at(value);
83
- }
84
- return [...this.state.value];
85
- }
86
- /**
87
- * Remove and return the last item of the array
88
- */
89
- pop() {
90
- return this.state.value.pop();
91
- }
92
- /**
93
- * Add items to the end of the array
94
- */
95
- push(...items) {
96
- return this.state.value.push(...items);
97
- }
98
- set(first, second) {
99
- if (first == null || Array.isArray(first)) {
100
- this.state.value.splice(0, this.state.value.length, ...first ?? []);
101
- } else if (first === "length") {
102
- this.length = second;
103
- } else if (typeof first === "number") {
104
- this.state.value[first] = second;
105
- }
106
- }
107
- /**
108
- * Remove and return the first item of the array
109
- */
110
- shift() {
111
- return this.state.value.shift();
112
- }
113
- /**
114
- * Remove and return items from the array _(and optionally add new items)_
115
- */
116
- splice(from, to, ...items) {
117
- return this.state.value.splice(
118
- from,
119
- to ?? this.state.value.length,
120
- ...items
121
- );
122
- }
123
- subscribe(first, second) {
124
- if (typeof first === "number" && typeof second === "function") {
125
- return getReactiveValueInProxy(
126
- this,
127
- __privateGet(this, _indiced),
128
- first,
129
- true
130
- ).subscribe(second);
131
- }
132
- return typeof first === "function" ? subscribe(this.state, first) : noop;
133
- }
134
- /**
135
- * Add items to the beginning of the array
136
- */
137
- unshift(...items) {
138
- return this.state.value.unshift(...items);
139
- }
140
- /**
141
- * Update the value _(based on the current value)_
142
- */
143
- update(callback) {
144
- const updated = callback(this.state.value);
145
- if (updated == null || Array.isArray(updated)) {
146
- this.set(updated);
147
- }
148
- }
149
- }
150
- _indiced = new WeakMap();
151
- _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
+ };
152
72
  function array(value, options) {
153
- return new ReactiveArray(Array.isArray(value) ? value : [], options);
73
+ return new ReactiveArray(Array.isArray(value) ? value : [], options);
154
74
  }
155
- function updateArray(type, array2, state, length) {
156
- const affectsLength = lengthAffectingMethods.has(type);
157
- const previousArray = affectsLength ? [] : [...array2];
158
- const previousLength = array2.length;
159
- return (...args) => {
160
- const result = array2[type](
161
- ...args
162
- );
163
- if (affectsLength ? array2.length !== previousLength : !equalArrays(state, previousArray, array2)) {
164
- emitValue(state);
165
- length.set(array2.length);
166
- }
167
- return result;
168
- };
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
+ };
169
87
  }
170
- const lengthAffectingMethods = /* @__PURE__ */ new Set([
171
- "pop",
172
- "push",
173
- "shift",
174
- "unshift"
88
+ var lengthAffectingMethods = new Set([
89
+ "pop",
90
+ "push",
91
+ "shift",
92
+ "unshift"
175
93
  ]);
176
- const updateMethods = /* @__PURE__ */ new Set([
177
- ...lengthAffectingMethods,
178
- "copyWithin",
179
- "fill",
180
- "reverse",
181
- "sort",
182
- "splice"
94
+ var updateMethods = new Set([
95
+ ...lengthAffectingMethods,
96
+ "copyWithin",
97
+ "fill",
98
+ "reverse",
99
+ "sort",
100
+ "splice"
183
101
  ]);
184
- export {
185
- ReactiveArray,
186
- array
187
- };
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 };