@oscarpalmer/mora 0.31.0 → 0.32.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 (54) hide show
  1. package/dist/batch.d.mts +4 -5
  2. package/dist/batch.mjs +4 -2
  3. package/dist/constants.d.mts +29 -19
  4. package/dist/constants.mjs +17 -2
  5. package/dist/effect.d.mts +4 -5
  6. package/dist/effect.mjs +2 -2
  7. package/dist/helpers/is.d.mts +14 -9
  8. package/dist/helpers/is.mjs +6 -0
  9. package/dist/helpers/proxy.d.mts +12 -10
  10. package/dist/helpers/proxy.mjs +32 -14
  11. package/dist/helpers/subscription.d.mts +8 -0
  12. package/dist/helpers/subscription.mjs +27 -0
  13. package/dist/helpers/value.d.mts +8 -6
  14. package/dist/helpers/value.mjs +30 -4
  15. package/dist/index.d.mts +3 -2
  16. package/dist/models.d.mts +45 -87
  17. package/dist/mora.full.mjs +461 -285
  18. package/dist/value/array.d.mts +4 -5
  19. package/dist/value/array.mjs +22 -69
  20. package/dist/value/computed.d.mts +3 -4
  21. package/dist/value/computed.mjs +14 -14
  22. package/dist/value/reactive.d.mts +2 -3
  23. package/dist/value/reactive.mjs +0 -1
  24. package/dist/value/readonly.d.mts +3 -5
  25. package/dist/value/readonly.mjs +15 -19
  26. package/dist/value/signal.d.mts +4 -5
  27. package/dist/value/signal.mjs +9 -22
  28. package/dist/value/store.d.mts +4 -5
  29. package/dist/value/store.mjs +16 -56
  30. package/package.json +6 -6
  31. package/src/batch.ts +15 -5
  32. package/src/constants.ts +32 -2
  33. package/src/effect.ts +6 -2
  34. package/src/helpers/is.ts +10 -0
  35. package/src/helpers/proxy.ts +99 -49
  36. package/src/helpers/subscription.ts +78 -0
  37. package/src/helpers/value.ts +69 -4
  38. package/src/index.ts +1 -1
  39. package/src/models.ts +38 -81
  40. package/src/value/array.ts +61 -150
  41. package/src/value/computed.ts +36 -13
  42. package/src/value/reactive.ts +8 -5
  43. package/src/value/readonly.ts +26 -25
  44. package/src/value/signal.ts +19 -22
  45. package/src/value/store.ts +35 -114
  46. package/types/constants.d.ts +1 -1
  47. package/types/index.d.ts +1 -1
  48. package/types/value/array.d.ts +1 -1
  49. package/types/value/computed.d.ts +0 -3
  50. package/types/value/signal.d.ts +1 -1
  51. package/types/value/store.d.ts +1 -1
  52. package/dist/subscription.d.mts +0 -7
  53. package/dist/subscription.mjs +0 -27
  54. package/src/subscription.ts +0 -45
package/dist/models.d.mts CHANGED
@@ -1,25 +1,26 @@
1
1
  import { PROPERTY_LENGTH } 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 Reactive<Value> = {
23
24
  /**
24
25
  * JSON representation of the value
25
26
  *
@@ -33,7 +34,7 @@ type Reactive<Value> = {
33
34
  */
34
35
  toString(): string;
35
36
  };
36
- type ReactiveArray<Item> = {
37
+ export type ReactiveArray<Item> = {
37
38
  /**
38
39
  * The length of the array
39
40
  */
@@ -187,36 +188,25 @@ type ReactiveArray<Item> = {
187
188
  * Subscribe to changes
188
189
  *
189
190
  * @param callback Callback for changes
190
- * @returns Unsubscribe callback
191
+ * @param copy Copy the array? _(defaults to `false`)_
192
+ * @returns Subscription
191
193
  */
192
- subscribe(callback: (value: Item[]) => void): Unsubscribe;
194
+ subscribe(callback: (value: Item[]) => void, copy?: boolean): Subscription;
193
195
  /**
194
196
  * Subscribe to changes at a specific index
195
197
  *
196
198
  * @param index Index of item to subscribe to
197
199
  * @param callback Callback for changes
198
- * @returns Unsubscribe callback
200
+ * @param copy If the item is an array or record, should it be copied? _(defaults to `false`)_
201
+ * @returns Subscription
199
202
  */
200
- subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
203
+ subscribe(index: number, callback: (value: Item | undefined) => void, copy?: boolean): Subscription;
201
204
  /**
202
205
  * Add items to the beginning of the array
203
206
  * @param items Items to add
204
207
  * @returns New array length
205
208
  */
206
209
  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
210
  /**
221
211
  * Update the value _(based on the current value)_
222
212
  *
@@ -224,7 +214,7 @@ type ReactiveArray<Item> = {
224
214
  */
225
215
  update(callback: (value: Item[]) => Item[]): void;
226
216
  } & Reactive<Item[]>;
227
- type ReactiveOptions<Value> = {
217
+ export type ReactiveOptions<Value> = {
228
218
  /**
229
219
  * Method for comparing values for equality
230
220
  * @param first First value
@@ -234,16 +224,16 @@ type ReactiveOptions<Value> = {
234
224
  */
235
225
  equal?: (first: Value, second: Value) => boolean;
236
226
  };
237
- type ReactiveState<Value, Equal> = {
227
+ export type ReactiveState<Value, Item = Value> = {
238
228
  computeds: Set<ComputedEffect>;
239
229
  effects: Set<EffectState>;
240
- equal: (first: Equal, second: Equal) => boolean;
230
+ equal: (first: Item, second: Item) => boolean;
241
231
  promise?: Promise<Value>;
242
232
  promises?: Map<Key, Promise<never>>;
243
- subscriptions: Map<GenericCallback, Subscription>;
233
+ subscriptions?: Subscriptions<GenericCallback>;
244
234
  value: Value;
245
235
  };
246
- type ReactiveStore<Store> = {
236
+ export type ReactiveStore<Store> = {
247
237
  /**
248
238
  * Get a readonly version of the reactive store
249
239
  *
@@ -332,45 +322,28 @@ type ReactiveStore<Store> = {
332
322
  * Subscribe to changes
333
323
  *
334
324
  * @param callback Callback for changes
335
- * @returns Unsubscribe callback
325
+ * @param copy Copy the store? _(defaults to `false`)_
326
+ * @returns Subscription
336
327
  */
337
- subscribe(callback: (value: Store) => void): Unsubscribe;
328
+ subscribe(callback: (value: Store) => void, copy?: boolean): Subscription;
338
329
  /**
339
330
  * Subscribe to changes for a specific key
340
331
  *
341
332
  * @param key Key of the value to subscribe to
342
333
  * @param callback Callback for changes
343
- * @returns Unsubscribe callback
334
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
335
+ * @returns Subscription
344
336
  */
345
- subscribe<Key extends keyof Store>(key: Key, callback: (value: Store[Key] | undefined) => void): Unsubscribe;
337
+ subscribe<Key extends keyof Store>(key: Key, callback: (value: Store[Key] | undefined) => void, copy?: boolean): Subscription;
346
338
  /**
347
339
  * Subscribe to changes for a specific key
348
340
  *
349
341
  * @param key Key of the value to subscribe to
350
342
  * @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
343
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
344
+ * @returns Subscription
372
345
  */
373
- unsubscribe(callback: (value: Store) => void): void;
346
+ subscribe(key: Key, callback: (value: unknown) => void, copy?: boolean): Subscription;
374
347
  /**
375
348
  * Update the value _(based on the current value)_
376
349
  *
@@ -378,27 +351,19 @@ type ReactiveStore<Store> = {
378
351
  */
379
352
  update(callback: (value: Store) => Store): void;
380
353
  } & Reactive<Store>;
381
- type ReadonlyInstances<Value> = {
354
+ export type ReadonlyInstances<Value> = {
382
355
  frozen?: ReadonlyFrozenSignal<Value>;
383
356
  original?: ReadonlySignal<Value>;
384
357
  };
385
- type ReadonlyFrozenSignal<Value> = ReadonlySignal<ReadonlySignalValue<Value>>;
386
- type ReadonlySignal<Value> = {
358
+ export type ReadonlyFrozenSignal<Value> = ReadonlySignal<ReadonlySignalValue<Value>>;
359
+ export type ReadonlySignal<Value> = {
387
360
  /**
388
361
  * Is the signal frozen?
389
362
  */
390
363
  get frozen(): boolean;
391
364
  } & 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> = {
365
+ export type ReadonlySignalValue<Value> = Value extends unknown[] ? Readonly<Value> : Value extends PlainObject ? Readonly<Value> : Value;
366
+ export type Signal<Value> = {
402
367
  /**
403
368
  * Get a readonly version of the signal
404
369
  *
@@ -436,31 +401,24 @@ type SimpleReactive<Value> = {
436
401
  /**
437
402
  * Get the value _(without reactivity)_
438
403
  *
404
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
439
405
  * @returns Current value
440
406
  */
441
- peek(): Value;
407
+ peek(copy?: boolean): Value;
442
408
  /**
443
409
  * Subscribe to changes
444
410
  *
445
411
  * @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
412
+ * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
413
+ * @returns Subscription
453
414
  */
454
- unsubscribe(callback: (value: Value) => void): void;
415
+ subscribe(callback: (value: Value) => void, copy?: boolean): Subscription;
455
416
  };
456
- type Subscription = {
417
+ export type StoredSubscription = {
457
418
  callback: GenericCallback;
419
+ copy: boolean;
420
+ frozen: boolean;
458
421
  state: ReactiveState<unknown, never>;
459
- destroy(): void;
460
422
  };
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 };
423
+ export type SubscriptionType = 'copy' | 'frozen' | 'original' | 'readonly';
424
+ //#endregion