@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,55 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
- const subscription = require("../subscription.cjs");
7
- class Reactive {
8
- constructor(name, value, options) {
9
- __publicField(this, "state", {
10
- computeds: /* @__PURE__ */ new Set(),
11
- effects: /* @__PURE__ */ new Set(),
12
- equal: Object.is,
13
- subscriptions: /* @__PURE__ */ new Map(),
14
- value: void 0
15
- });
16
- this.state.value = value;
17
- if (typeof options === "object" && typeof options.equal === "function") {
18
- this.state.equal = options.equal;
19
- }
20
- Object.defineProperty(this, "$mora", {
21
- value: name
22
- });
23
- }
24
- /**
25
- * Get the value _(without reactivity)_
26
- */
27
- peek() {
28
- return this.state.value;
29
- }
30
- /**
31
- * Subscribe to changes
32
- */
33
- subscribe(callback) {
34
- return subscription.subscribe(this.state, callback);
35
- }
36
- /**
37
- * JSON representation of the value
38
- */
39
- toJSON() {
40
- return this.get();
41
- }
42
- /**
43
- * String representation of the value
44
- */
45
- toString() {
46
- return String(this.get());
47
- }
48
- /**
49
- * Unsubscribe from changes
50
- */
51
- unsubscribe(callback) {
52
- subscription.unsubscribe(this.state, callback);
53
- }
54
- }
55
- exports.Reactive = Reactive;
@@ -1,36 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const helpers_is = require("../helpers/is.cjs");
4
- const helpers_value = require("../helpers/value.cjs");
5
- const value_reactive = require("./reactive.cjs");
6
- class Signal extends value_reactive.Reactive {
7
- constructor(value, options) {
8
- super(helpers_is.signalName, value, options);
9
- }
10
- /**
11
- * @inheritdoc
12
- */
13
- get() {
14
- return helpers_value.getValue(this.state);
15
- }
16
- /**
17
- * Set the value
18
- */
19
- set(value) {
20
- if (!this.state.equal(this.state.value, value)) {
21
- this.state.value = value;
22
- helpers_value.emitValue(this.state);
23
- }
24
- }
25
- /**
26
- * Update the value _(based on the current value)_
27
- */
28
- update(callback) {
29
- this.set(callback(this.state.value));
30
- }
31
- }
32
- function signal(value, options) {
33
- return new Signal(value, options);
34
- }
35
- exports.Signal = Signal;
36
- exports.signal = signal;
@@ -1,63 +0,0 @@
1
- "use strict";
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
6
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
7
- 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);
8
- var _keyed;
9
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
10
- const is = require("../node_modules/@oscarpalmer/atoms/dist/internal/is.cjs");
11
- const helpers_is = require("../helpers/is.cjs");
12
- const helpers_proxy = require("../helpers/proxy.cjs");
13
- const helpers_value = require("../helpers/value.cjs");
14
- const subscription = require("../subscription.cjs");
15
- const value_reactive = require("./reactive.cjs");
16
- class Store extends value_reactive.Reactive {
17
- constructor(value, options) {
18
- super(
19
- helpers_is.storeName,
20
- new Proxy(value, {
21
- set: (target, property, value2) => helpers_proxy.setValueInProxy(target, property, value2, this.state, false)
22
- }),
23
- options
24
- );
25
- __privateAdd(this, _keyed, /* @__PURE__ */ new Map());
26
- }
27
- get(key) {
28
- return is.isKey(key) ? helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _keyed), key, false).get() : helpers_value.getValue(this.state);
29
- }
30
- peek(key) {
31
- return is.isKey(key) ? this.state.value[key] : { ...this.state.value };
32
- }
33
- set(first, second) {
34
- if (is.isKey(first)) {
35
- this.state.value[first] = second;
36
- } else if (first == null || is.isPlainObject(first)) {
37
- helpers_proxy.setProxyValue(this.state.value, first ?? {});
38
- }
39
- }
40
- subscribe(first, second) {
41
- if (is.isKey(first) && typeof second === "function") {
42
- return helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _keyed), first, false).subscribe(
43
- second
44
- );
45
- }
46
- return typeof first === "function" ? subscription.subscribe(this.state, first) : subscription.noop;
47
- }
48
- /**
49
- * Update the value _(based on the current value)_
50
- */
51
- update(callback) {
52
- const updated = callback({ ...this.state.value });
53
- if (updated == null || is.isPlainObject(updated)) {
54
- helpers_proxy.setProxyValue(this.state.value, updated ?? {});
55
- }
56
- }
57
- }
58
- _keyed = new WeakMap();
59
- function store(value, options) {
60
- return new Store(is.isPlainObject(value) ? value : {}, options);
61
- }
62
- exports.Store = Store;
63
- exports.store = store;
package/types/batch.d.cts DELETED
@@ -1,79 +0,0 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- export type GenericCallback = (...args: any[]) => any;
4
- declare class Effect {
5
- private readonly $mora;
6
- private readonly state;
7
- constructor(callback: GenericCallback);
8
- }
9
- declare class Computed<Value> extends Reactive<Value> {
10
- private readonly effect;
11
- constructor(callback: () => Value, options?: ReactiveOptions<Value>);
12
- /**
13
- * @inheritdoc
14
- */
15
- get(): Value;
16
- }
17
- declare abstract class Reactive<Value, Equal = Value> {
18
- protected readonly state: ReactiveState<Value, Equal>;
19
- constructor(name: string, value: Value, options?: ReactiveOptions<Equal>);
20
- /**
21
- * Get the value
22
- */
23
- abstract get(): Value;
24
- /**
25
- * Get the value _(without reactivity)_
26
- */
27
- peek(): Value;
28
- /**
29
- * Subscribe to changes
30
- */
31
- subscribe(callback: (value: Value) => void): Unsubscribe;
32
- /**
33
- * JSON representation of the value
34
- */
35
- toJSON(): Value;
36
- /**
37
- * String representation of the value
38
- */
39
- toString(): string;
40
- /**
41
- * Unsubscribe from changes
42
- */
43
- unsubscribe(callback: (value: Value) => void): void;
44
- }
45
- export type ReactiveOptions<Value> = {
46
- /**
47
- * Method to compare values for equality
48
- */
49
- equal?: (first: Value, second: Value) => boolean;
50
- };
51
- export type ReactiveState<Value, Equal> = {
52
- computeds: Set<Computed<unknown>>;
53
- effects: Set<Effect>;
54
- equal: (first: Equal, second: Equal) => boolean;
55
- subscriptions: Map<GenericCallback, Subscription>;
56
- value: Value;
57
- };
58
- declare class Subscription {
59
- state: ReactiveState<unknown, never>;
60
- callback: GenericCallback;
61
- constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
62
- }
63
- /**
64
- * Unsubscribe from changes
65
- */
66
- export type Unsubscribe = () => void;
67
- export declare function flushHandlers(): void;
68
- /**
69
- * Start batching effects _(use `stopBatch` to flush and run batched effects)_
70
- */
71
- export declare function startBatch(): void;
72
- /**
73
- * Stop batching effects and flush _(run)_ them
74
- */
75
- export declare function stopBatch(): void;
76
- export declare const batchedHandlers: Set<Effect | Subscription>;
77
- export declare let batchDepth: number;
78
-
79
- export {};
@@ -1,16 +0,0 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- export type GenericCallback = (...args: any[]) => any;
4
- export declare class Effect {
5
- private readonly $mora;
6
- private readonly state;
7
- constructor(callback: GenericCallback);
8
- }
9
- export declare function runEffect(effect: Effect): void;
10
- /**
11
- * Create an effect
12
- */
13
- export declare function effect(callback: GenericCallback): Effect;
14
- export declare let activeEffect: Effect | undefined;
15
-
16
- export {};
@@ -1,259 +0,0 @@
1
- // Generated by dts-bundle-generator v9.5.1
2
-
3
- export type GenericCallback = (...args: any[]) => any;
4
- /**
5
- * A useful key type
6
- */
7
- export type Key = number | string;
8
- /**
9
- * A generic object
10
- */
11
- export type PlainObject = Record<PropertyKey, unknown>;
12
- declare class Effect {
13
- private readonly $mora;
14
- private readonly state;
15
- constructor(callback: GenericCallback);
16
- }
17
- declare class Computed<Value> extends Reactive<Value> {
18
- private readonly effect;
19
- constructor(callback: () => Value, options?: ReactiveOptions<Value>);
20
- /**
21
- * @inheritdoc
22
- */
23
- get(): Value;
24
- }
25
- declare abstract class Reactive<Value, Equal = Value> {
26
- protected readonly state: ReactiveState<Value, Equal>;
27
- constructor(name: string, value: Value, options?: ReactiveOptions<Equal>);
28
- /**
29
- * Get the value
30
- */
31
- abstract get(): Value;
32
- /**
33
- * Get the value _(without reactivity)_
34
- */
35
- peek(): Value;
36
- /**
37
- * Subscribe to changes
38
- */
39
- subscribe(callback: (value: Value) => void): Unsubscribe;
40
- /**
41
- * JSON representation of the value
42
- */
43
- toJSON(): Value;
44
- /**
45
- * String representation of the value
46
- */
47
- toString(): string;
48
- /**
49
- * Unsubscribe from changes
50
- */
51
- unsubscribe(callback: (value: Value) => void): void;
52
- }
53
- export type ReactiveOptions<Value> = {
54
- /**
55
- * Method to compare values for equality
56
- */
57
- equal?: (first: Value, second: Value) => boolean;
58
- };
59
- export type ReactiveState<Value, Equal> = {
60
- computeds: Set<Computed<unknown>>;
61
- effects: Set<Effect>;
62
- equal: (first: Equal, second: Equal) => boolean;
63
- subscriptions: Map<GenericCallback, Subscription>;
64
- value: Value;
65
- };
66
- declare class Subscription {
67
- state: ReactiveState<unknown, never>;
68
- callback: GenericCallback;
69
- constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
70
- }
71
- /**
72
- * Unsubscribe from changes
73
- */
74
- export type Unsubscribe = () => void;
75
- declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
76
- #private;
77
- /**
78
- * The length of the array
79
- */
80
- get length(): number;
81
- /**
82
- * Set the length of the array
83
- */
84
- set length(value: number);
85
- constructor(value: Item[], options?: ReactiveOptions<Item>);
86
- /**
87
- * Clear the array
88
- */
89
- clear(): void;
90
- /**
91
- * @inheritdoc
92
- */
93
- get(): Item[];
94
- /**
95
- * Get the value at an index
96
- */
97
- get(index: number): Item | undefined;
98
- /**
99
- * Get the length of the array
100
- */
101
- get(property: "length"): number;
102
- /**
103
- * Create a computed, filtered array
104
- */
105
- filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
106
- /**
107
- * Create a computed, mapped array
108
- */
109
- map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
110
- /**
111
- * @inheritdoc
112
- */
113
- peek(): Item[];
114
- peek(index: number): Item | undefined;
115
- /**
116
- * Get the length of the array _(without reactivity)_
117
- */
118
- peek(length: true): number;
119
- /**
120
- * Remove and return the last item of the array
121
- */
122
- pop(): Item | undefined;
123
- /**
124
- * Add items to the end of the array
125
- */
126
- push(...items: Item[]): number;
127
- /**
128
- * Set the value
129
- */
130
- set(value?: Item[]): void;
131
- /**
132
- * Set the value at an index
133
- */
134
- set(index: number, value: Item): void;
135
- /**
136
- * Set the length of the array
137
- */
138
- set(property: "length", value: number): void;
139
- /**
140
- * Remove and return the first item of the array
141
- */
142
- shift(): Item | undefined;
143
- /**
144
- * Remove and return items from the array _(and optionally add new items)_
145
- */
146
- splice(from: number, to?: number, ...items: Item[]): Item[];
147
- /**
148
- * @inheritdoc
149
- */
150
- subscribe(callback: (value: Item[]) => void): Unsubscribe;
151
- /**
152
- * Subscribe to changes at a specific index
153
- */
154
- subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
155
- /**
156
- * Add items to the beginning of the array
157
- */
158
- unshift(...items: Item[]): number;
159
- /**
160
- * Update the value _(based on the current value)_
161
- */
162
- update(callback: (value: Item[]) => Item[]): void;
163
- }
164
- declare class Signal<Value> extends Reactive<Value> {
165
- constructor(value: Value, options?: ReactiveOptions<Value>);
166
- /**
167
- * @inheritdoc
168
- */
169
- get(): Value;
170
- /**
171
- * Set the value
172
- */
173
- set(value: Value): void;
174
- /**
175
- * Update the value _(based on the current value)_
176
- */
177
- update(callback: (value: Value) => Value): void;
178
- }
179
- declare class Store<Value extends PlainObject> extends Reactive<Value, Value> {
180
- #private;
181
- constructor(value: Value, options?: ReactiveOptions<Value>);
182
- /**
183
- * @inheritdoc
184
- */
185
- get(): Value;
186
- /**
187
- * Get a value by key _(without reactivity)_
188
- */
189
- get(key: keyof Value): Value[keyof Value];
190
- /**
191
- * Get a value by key _(without reactivity)_
192
- */
193
- get(key: Key): unknown;
194
- /**
195
- * Get the value _(without reactivity)_
196
- */
197
- peek(): Value;
198
- /**
199
- * Get a value by key _(without reactivity)_
200
- */
201
- peek(key: keyof Value): Value[keyof Value];
202
- /**
203
- * Get a value by key _(without reactivity)_
204
- */
205
- peek(key: Key): unknown;
206
- /**
207
- * Set the value
208
- */
209
- set(value?: Value): void;
210
- /**
211
- * Set a value by key
212
- */
213
- set(key: keyof Value, value: Value[keyof Value]): void;
214
- /**
215
- * Set a value by key
216
- */
217
- set(key: Key, value: unknown): void;
218
- /**
219
- * @inheritdoc
220
- */
221
- subscribe(callback: (value: Value) => void): Unsubscribe;
222
- /**
223
- * Subscribe to changes for a specific key
224
- */
225
- subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
226
- /**
227
- * Subscribe to changes for a specific key
228
- */
229
- subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
230
- /**
231
- * Update the value _(based on the current value)_
232
- */
233
- update(callback: (value: Value) => Value): void;
234
- }
235
- /**
236
- * Is the value a reactive array?
237
- */
238
- export declare function isArray(value: unknown): value is ReactiveArray<unknown>;
239
- /**
240
- * Is the value a computed signal?
241
- */
242
- export declare function isComputed(value: unknown): value is Computed<unknown>;
243
- /**
244
- * Is the value an effect?
245
- */
246
- export declare function isEffect(value: unknown): value is Effect;
247
- export declare function isReactive(value: unknown): value is Reactive<unknown>;
248
- /**
249
- * Is the value a signal?
250
- */
251
- export declare function isSignal(value: unknown): value is Signal<unknown>;
252
- export declare function isStore(value: unknown): value is Store<PlainObject>;
253
- export declare const arrayName = "array";
254
- export declare const computedName = "computed";
255
- export declare const effectName = "effect";
256
- export declare const signalName = "signal";
257
- export declare const storeName = "store";
258
-
259
- export {};