@oscarpalmer/mora 0.31.0 → 0.33.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 (58) hide show
  1. package/dist/batch.d.mts +4 -5
  2. package/dist/batch.mjs +4 -2
  3. package/dist/constants.d.mts +31 -19
  4. package/dist/constants.mjs +19 -2
  5. package/dist/effect.d.mts +5 -5
  6. package/dist/effect.mjs +8 -5
  7. package/dist/helpers/is.d.mts +14 -9
  8. package/dist/helpers/is.mjs +6 -0
  9. package/dist/helpers/misc.d.mts +4 -0
  10. package/dist/helpers/misc.mjs +9 -0
  11. package/dist/helpers/proxy.d.mts +10 -10
  12. package/dist/helpers/proxy.mjs +83 -27
  13. package/dist/helpers/subscription.d.mts +8 -0
  14. package/dist/helpers/subscription.mjs +30 -0
  15. package/dist/helpers/value.d.mts +10 -7
  16. package/dist/helpers/value.mjs +56 -17
  17. package/dist/index.d.mts +3 -2
  18. package/dist/models.d.mts +82 -101
  19. package/dist/models.mjs +1 -0
  20. package/dist/mora.full.mjs +659 -412
  21. package/dist/value/array.d.mts +5 -5
  22. package/dist/value/array.mjs +70 -109
  23. package/dist/value/computed.d.mts +5 -4
  24. package/dist/value/computed.mjs +45 -31
  25. package/dist/value/readonly.d.mts +4 -6
  26. package/dist/value/readonly.mjs +34 -31
  27. package/dist/value/signal.d.mts +5 -5
  28. package/dist/value/signal.mjs +28 -34
  29. package/dist/value/store.d.mts +5 -5
  30. package/dist/value/store.mjs +25 -83
  31. package/package.json +6 -6
  32. package/src/batch.ts +15 -5
  33. package/src/constants.ts +36 -2
  34. package/src/effect.ts +20 -11
  35. package/src/helpers/is.ts +16 -6
  36. package/src/helpers/misc.ts +15 -0
  37. package/src/helpers/proxy.ts +151 -74
  38. package/src/helpers/subscription.ts +89 -0
  39. package/src/helpers/value.ts +121 -30
  40. package/src/index.ts +1 -1
  41. package/src/models.ts +81 -98
  42. package/src/value/array.ts +116 -226
  43. package/src/value/computed.ts +96 -43
  44. package/src/value/readonly.ts +68 -54
  45. package/src/value/signal.ts +54 -46
  46. package/src/value/store.ts +42 -168
  47. package/types/constants.d.ts +1 -1
  48. package/types/index.d.ts +1 -1
  49. package/types/value/array.d.ts +1 -1
  50. package/types/value/computed.d.ts +0 -3
  51. package/types/value/signal.d.ts +1 -1
  52. package/types/value/store.d.ts +1 -1
  53. package/dist/subscription.d.mts +0 -7
  54. package/dist/subscription.mjs +0 -27
  55. package/dist/value/reactive.d.mts +0 -5
  56. package/dist/value/reactive.mjs +0 -16
  57. package/src/subscription.ts +0 -45
  58. package/src/value/reactive.ts +0 -24
package/dist/models.d.mts CHANGED
@@ -1,39 +1,53 @@
1
- import { PROPERTY_LENGTH } from "./constants.mjs";
1
+ import { PROPERTY_LENGTH, SYMBOL_EFFECT, SYMBOL_STATE } from "./constants.mjs";
2
+ import { Subscription, Subscriptions } from "@oscarpalmer/atoms/subscription";
2
3
  import { GenericCallback, Key, PlainObject } from "@oscarpalmer/atoms/models";
3
4
  //#region src/models.d.ts
4
- type Active = {
5
+ export type Active = {
5
6
  computed?: ComputedEffect;
6
7
  effect?: EffectState;
7
8
  };
8
- type Batch = {
9
+ export type Batch = {
9
10
  depth: number;
10
11
  flushing: boolean;
11
- handlers: Set<EffectState | Subscription>;
12
+ handlers: Map<EffectState | Subscription, EffectState | StoredSubscription>;
12
13
  };
13
- type Computed<Value> = Reactive<Value> & SimpleReactive<Value>;
14
- type ComputedEffect = {
14
+ export type Computed<Value> = Reactive<Value> & SimpleReactive<Value>;
15
+ export type ComputedEffect = {
15
16
  dirty: boolean;
16
17
  instance: EffectState;
17
18
  };
18
- type Effect = {};
19
- type EffectState = {
19
+ export type Effect = {};
20
+ export type EffectState = {
20
21
  callback: GenericCallback;
21
22
  };
22
- type Reactive<Value> = {
23
+ export type InternalComputed = {
24
+ [SYMBOL_EFFECT]: ComputedEffect;
25
+ } & InternalSignal;
26
+ export type InternalProxy = {
27
+ [SYMBOL_STATE]: ReactiveProxyState;
28
+ };
29
+ export type InternalReadonly = {
30
+ frozen: boolean;
31
+ } & InternalSignal;
32
+ export type InternalSignal = {
33
+ [SYMBOL_STATE]: SignalState;
34
+ };
35
+ export type Reactive<Value> = {
23
36
  /**
24
- * JSON representation of the value
37
+ * _JSON_ representation of the value
25
38
  *
26
- * @returns JSON value
39
+ * @returns _JSON_ value
27
40
  */
28
41
  toJSON(): Value;
29
42
  /**
30
43
  * String representation of the value
31
44
  *
45
+ * @param json When `true`, returns the _JSON_ string representation of the value _(defaults to `false`)_
32
46
  * @returns Value as string
33
47
  */
34
- toString(): string;
48
+ toString(json?: boolean): string;
35
49
  };
36
- type ReactiveArray<Item> = {
50
+ export type ReactiveArray<Item> = {
37
51
  /**
38
52
  * The length of the array
39
53
  */
@@ -186,37 +200,26 @@ type ReactiveArray<Item> = {
186
200
  /**
187
201
  * Subscribe to changes
188
202
  *
189
- * @param callback Callback for changes
190
- * @returns Unsubscribe callback
203
+ * @param subscriber Callback for changes
204
+ * @param copy Copy the array? _(defaults to `false`)_
205
+ * @returns Subscription
191
206
  */
192
- subscribe(callback: (value: Item[]) => void): Unsubscribe;
207
+ subscribe(subscriber: Subscriber<Item[]>, copy?: boolean): Subscription;
193
208
  /**
194
209
  * Subscribe to changes at a specific index
195
210
  *
196
211
  * @param index Index of item to subscribe to
197
- * @param callback Callback for changes
198
- * @returns Unsubscribe callback
212
+ * @param subscriber Callback for changes
213
+ * @param copy If the item is an array or record, should it be copied? _(defaults to `false`)_
214
+ * @returns Subscription
199
215
  */
200
- subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
216
+ subscribe(index: number, subscriber: Subscriber<Item | undefined>, copy?: boolean): Subscription;
201
217
  /**
202
218
  * Add items to the beginning of the array
203
219
  * @param items Items to add
204
220
  * @returns New array length
205
221
  */
206
222
  unshift(...items: Item[]): number;
207
- /**
208
- * Unsubscribe from changes for a specific index
209
- *
210
- * @param index Index of the value to unsubscribe from
211
- * @param callback Callback to unsubscribe
212
- */
213
- unsubscribe(index: number, callback: (item: Item | undefined) => void): void;
214
- /**
215
- * Unsubscribe from changes
216
- *
217
- * @param callback Callback to unsubscribe
218
- */
219
- unsubscribe(callback: (array: Item[]) => void): void;
220
223
  /**
221
224
  * Update the value _(based on the current value)_
222
225
  *
@@ -224,7 +227,7 @@ type ReactiveArray<Item> = {
224
227
  */
225
228
  update(callback: (value: Item[]) => Item[]): void;
226
229
  } & Reactive<Item[]>;
227
- type ReactiveOptions<Value> = {
230
+ export type ReactiveOptions<Value> = {
228
231
  /**
229
232
  * Method for comparing values for equality
230
233
  * @param first First value
@@ -234,16 +237,21 @@ type ReactiveOptions<Value> = {
234
237
  */
235
238
  equal?: (first: Value, second: Value) => boolean;
236
239
  };
237
- type ReactiveState<Value, Equal> = {
238
- computeds: Set<ComputedEffect>;
239
- effects: Set<EffectState>;
240
- equal: (first: Equal, second: Equal) => boolean;
241
- promise?: Promise<Value>;
240
+ export type ReactiveProxyState = {
241
+ isArray: boolean;
242
+ length?: Signal<number>;
243
+ mapped?: Map<string, [Computed<unknown>, ComputedEffect]>;
244
+ } & SignalState;
245
+ export type ReactiveState = {
246
+ computeds?: Set<ComputedEffect>;
247
+ effects?: Set<EffectState>;
248
+ equal?: (first: unknown, second: unknown) => boolean;
249
+ promise?: Promise<unknown>;
242
250
  promises?: Map<Key, Promise<never>>;
243
- subscriptions: Map<GenericCallback, Subscription>;
244
- value: Value;
251
+ subscriptions?: Subscriptions<GenericCallback>;
252
+ value: unknown;
245
253
  };
246
- type ReactiveStore<Store> = {
254
+ export type ReactiveStore<Store> = {
247
255
  /**
248
256
  * Get a readonly version of the reactive store
249
257
  *
@@ -331,46 +339,29 @@ type ReactiveStore<Store> = {
331
339
  /**
332
340
  * Subscribe to changes
333
341
  *
334
- * @param callback Callback for changes
335
- * @returns Unsubscribe callback
342
+ * @param subscriber Callback for changes
343
+ * @param copy Copy the store? _(defaults to `false`)_
344
+ * @returns Subscription
336
345
  */
337
- subscribe(callback: (value: Store) => void): Unsubscribe;
346
+ subscribe(subscriber: Subscriber<Store>, copy?: boolean): Subscription;
338
347
  /**
339
348
  * Subscribe to changes for a specific key
340
349
  *
341
350
  * @param key Key of the value to subscribe to
342
- * @param callback Callback for changes
343
- * @returns Unsubscribe callback
351
+ * @param subscriber Callback for changes
352
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
353
+ * @returns Subscription
344
354
  */
345
- subscribe<Key extends keyof Store>(key: Key, callback: (value: Store[Key] | undefined) => void): Unsubscribe;
355
+ subscribe<Key extends keyof Store>(key: Key, subscriber: Subscriber<Store[Key] | undefined>, copy?: boolean): Subscription;
346
356
  /**
347
357
  * Subscribe to changes for a specific key
348
358
  *
349
359
  * @param key Key of the value to subscribe to
350
- * @param callback Callback for changes
351
- * @returns Unsubscribe callback
352
- */
353
- subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
354
- /**
355
- * Unsubscribe from changes for a specific key
356
- *
357
- * @param key Key of the value to unsubscribe from
358
- * @param callback Callback to unsubscribe
359
- */
360
- unsubscribe<Key extends keyof Store>(key: Key, callback: (value: Store[Key] | undefined) => void): void;
361
- /**
362
- * Unsubscribe from changes for a specific key
363
- *
364
- * @param key Key of the value to unsubscribe from
365
- * @param callback Callback to unsubscribe
366
- */
367
- unsubscribe(key: Key, callback: (value: unknown | undefined) => void): void;
368
- /**
369
- * Unsubscribe from changes
370
- *
371
- * @param callback Callback to unsubscribe
360
+ * @param subscriber Callback for changes
361
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
362
+ * @returns Subscription
372
363
  */
373
- unsubscribe(callback: (value: Store) => void): void;
364
+ subscribe(key: Key, subscriber: Subscriber<unknown>, copy?: boolean): Subscription;
374
365
  /**
375
366
  * Update the value _(based on the current value)_
376
367
  *
@@ -382,23 +373,15 @@ type ReadonlyInstances<Value> = {
382
373
  frozen?: ReadonlyFrozenSignal<Value>;
383
374
  original?: ReadonlySignal<Value>;
384
375
  };
385
- type ReadonlyFrozenSignal<Value> = ReadonlySignal<ReadonlySignalValue<Value>>;
386
- type ReadonlySignal<Value> = {
376
+ export type ReadonlyFrozenSignal<Value> = ReadonlySignal<ReadonlySignalValue<Value>>;
377
+ export type ReadonlySignal<Value> = {
387
378
  /**
388
379
  * Is the signal frozen?
389
380
  */
390
381
  get frozen(): boolean;
391
382
  } & Reactive<Value> & SimpleReactive<Value>;
392
- type ReadonlySignalValue<Value> = Value extends unknown[] ? Readonly<Value> : Value extends PlainObject ? Readonly<Value> : Value;
393
- type SetValueInProxyParameters<Value, Equal> = {
394
- target: Value;
395
- property: PropertyKey;
396
- value: unknown;
397
- state: ReactiveState<Value, Equal>;
398
- isArray: boolean;
399
- length?: Signal<number>;
400
- };
401
- type Signal<Value> = {
383
+ export type ReadonlySignalValue<Value> = Value extends unknown[] ? Readonly<Value> : Value extends PlainObject ? Readonly<Value> : Value;
384
+ export type Signal<Value> = {
402
385
  /**
403
386
  * Get a readonly version of the signal
404
387
  *
@@ -426,6 +409,9 @@ type Signal<Value> = {
426
409
  */
427
410
  update(callback: (value: Value) => Value): void;
428
411
  } & Reactive<Value> & SimpleReactive<Value>;
412
+ export type SignalState = {
413
+ readonlies?: ReadonlyInstances<unknown>;
414
+ } & ReactiveState;
429
415
  type SimpleReactive<Value> = {
430
416
  /**
431
417
  * Get the value
@@ -436,31 +422,26 @@ type SimpleReactive<Value> = {
436
422
  /**
437
423
  * Get the value _(without reactivity)_
438
424
  *
425
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
439
426
  * @returns Current value
440
427
  */
441
- peek(): Value;
428
+ peek(copy?: boolean): Value;
442
429
  /**
443
430
  * Subscribe to changes
444
431
  *
445
- * @param callback Callback for changes
446
- * @returns Unsubscribe callback
447
- */
448
- subscribe(callback: (value: Value) => void): Unsubscribe;
449
- /**
450
- * Unsubscribe from changes
451
- *
452
- * @param callback Callback to unsubscribe
432
+ * @param subscriber Callback for changes
433
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
434
+ * @returns Subscription
453
435
  */
454
- unsubscribe(callback: (value: Value) => void): void;
436
+ subscribe(subscriber: Subscriber<Value>, copy?: boolean): Subscription;
455
437
  };
456
- type Subscription = {
438
+ export type StoredSubscription = {
457
439
  callback: GenericCallback;
458
- state: ReactiveState<unknown, never>;
459
- destroy(): void;
440
+ copy: boolean;
441
+ frozen: boolean;
442
+ instance: InternalSignal;
443
+ state: SignalState;
460
444
  };
461
- /**
462
- * Unsubscribe from changes
463
- */
464
- type Unsubscribe = () => void;
465
- //#endregion
466
- export { Active, Batch, Computed, ComputedEffect, Effect, EffectState, Reactive, ReactiveArray, ReactiveOptions, ReactiveState, ReactiveStore, ReadonlyFrozenSignal, ReadonlyInstances, ReadonlySignal, ReadonlySignalValue, SetValueInProxyParameters, Signal, Subscription, Unsubscribe };
445
+ export type Subscriber<Value> = (value: Value, subscription: Subscription) => void;
446
+ export type SubscriptionType = 'copy' | 'frozen' | 'original' | 'readonly';
447
+ //#endregion
package/dist/models.mjs CHANGED
@@ -1 +1,2 @@
1
+ import "./constants.mjs";
1
2
  export {};