@oscarpalmer/mora 0.32.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 (45) hide show
  1. package/dist/batch.mjs +2 -2
  2. package/dist/constants.d.mts +2 -0
  3. package/dist/constants.mjs +3 -1
  4. package/dist/effect.d.mts +1 -0
  5. package/dist/effect.mjs +8 -5
  6. package/dist/helpers/is.d.mts +6 -6
  7. package/dist/helpers/misc.d.mts +4 -0
  8. package/dist/helpers/misc.mjs +9 -0
  9. package/dist/helpers/proxy.d.mts +8 -10
  10. package/dist/helpers/proxy.mjs +71 -33
  11. package/dist/helpers/subscription.d.mts +4 -4
  12. package/dist/helpers/subscription.mjs +16 -13
  13. package/dist/helpers/value.d.mts +8 -7
  14. package/dist/helpers/value.mjs +35 -22
  15. package/dist/models.d.mts +47 -24
  16. package/dist/models.mjs +1 -0
  17. package/dist/mora.full.mjs +307 -236
  18. package/dist/value/array.d.mts +1 -0
  19. package/dist/value/array.mjs +69 -61
  20. package/dist/value/computed.d.mts +3 -1
  21. package/dist/value/computed.mjs +39 -25
  22. package/dist/value/readonly.d.mts +3 -3
  23. package/dist/value/readonly.mjs +31 -24
  24. package/dist/value/signal.d.mts +1 -0
  25. package/dist/value/signal.mjs +27 -20
  26. package/dist/value/store.d.mts +1 -0
  27. package/dist/value/store.mjs +23 -41
  28. package/package.json +1 -1
  29. package/src/batch.ts +2 -2
  30. package/src/constants.ts +4 -0
  31. package/src/effect.ts +16 -11
  32. package/src/helpers/is.ts +6 -6
  33. package/src/helpers/misc.ts +15 -0
  34. package/src/helpers/proxy.ts +116 -89
  35. package/src/helpers/subscription.ts +46 -35
  36. package/src/helpers/value.ts +61 -35
  37. package/src/models.ts +54 -28
  38. package/src/value/array.ts +103 -124
  39. package/src/value/computed.ts +70 -40
  40. package/src/value/readonly.ts +54 -41
  41. package/src/value/signal.ts +46 -35
  42. package/src/value/store.ts +36 -83
  43. package/dist/value/reactive.d.mts +0 -4
  44. package/dist/value/reactive.mjs +0 -15
  45. package/src/value/reactive.ts +0 -27
package/dist/models.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { PROPERTY_LENGTH } from "./constants.mjs";
1
+ import { PROPERTY_LENGTH, SYMBOL_EFFECT, SYMBOL_STATE } from "./constants.mjs";
2
2
  import { Subscription, Subscriptions } from "@oscarpalmer/atoms/subscription";
3
3
  import { GenericCallback, Key, PlainObject } from "@oscarpalmer/atoms/models";
4
4
  //#region src/models.d.ts
@@ -20,19 +20,32 @@ export type Effect = {};
20
20
  export type EffectState = {
21
21
  callback: GenericCallback;
22
22
  };
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
+ };
23
35
  export type Reactive<Value> = {
24
36
  /**
25
- * JSON representation of the value
37
+ * _JSON_ representation of the value
26
38
  *
27
- * @returns JSON value
39
+ * @returns _JSON_ value
28
40
  */
29
41
  toJSON(): Value;
30
42
  /**
31
43
  * String representation of the value
32
44
  *
45
+ * @param json When `true`, returns the _JSON_ string representation of the value _(defaults to `false`)_
33
46
  * @returns Value as string
34
47
  */
35
- toString(): string;
48
+ toString(json?: boolean): string;
36
49
  };
37
50
  export type ReactiveArray<Item> = {
38
51
  /**
@@ -187,20 +200,20 @@ export type ReactiveArray<Item> = {
187
200
  /**
188
201
  * Subscribe to changes
189
202
  *
190
- * @param callback Callback for changes
203
+ * @param subscriber Callback for changes
191
204
  * @param copy Copy the array? _(defaults to `false`)_
192
205
  * @returns Subscription
193
206
  */
194
- subscribe(callback: (value: Item[]) => void, copy?: boolean): Subscription;
207
+ subscribe(subscriber: Subscriber<Item[]>, copy?: boolean): Subscription;
195
208
  /**
196
209
  * Subscribe to changes at a specific index
197
210
  *
198
211
  * @param index Index of item to subscribe to
199
- * @param callback Callback for changes
212
+ * @param subscriber Callback for changes
200
213
  * @param copy If the item is an array or record, should it be copied? _(defaults to `false`)_
201
214
  * @returns Subscription
202
215
  */
203
- subscribe(index: number, callback: (value: Item | undefined) => void, copy?: boolean): Subscription;
216
+ subscribe(index: number, subscriber: Subscriber<Item | undefined>, copy?: boolean): Subscription;
204
217
  /**
205
218
  * Add items to the beginning of the array
206
219
  * @param items Items to add
@@ -224,14 +237,19 @@ export type ReactiveOptions<Value> = {
224
237
  */
225
238
  equal?: (first: Value, second: Value) => boolean;
226
239
  };
227
- export type ReactiveState<Value, Item = Value> = {
228
- computeds: Set<ComputedEffect>;
229
- effects: Set<EffectState>;
230
- equal: (first: Item, second: Item) => boolean;
231
- 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>;
232
250
  promises?: Map<Key, Promise<never>>;
233
251
  subscriptions?: Subscriptions<GenericCallback>;
234
- value: Value;
252
+ value: unknown;
235
253
  };
236
254
  export type ReactiveStore<Store> = {
237
255
  /**
@@ -321,29 +339,29 @@ export type ReactiveStore<Store> = {
321
339
  /**
322
340
  * Subscribe to changes
323
341
  *
324
- * @param callback Callback for changes
342
+ * @param subscriber Callback for changes
325
343
  * @param copy Copy the store? _(defaults to `false`)_
326
344
  * @returns Subscription
327
345
  */
328
- subscribe(callback: (value: Store) => void, copy?: boolean): Subscription;
346
+ subscribe(subscriber: Subscriber<Store>, copy?: boolean): Subscription;
329
347
  /**
330
348
  * Subscribe to changes for a specific key
331
349
  *
332
350
  * @param key Key of the value to subscribe to
333
- * @param callback Callback for changes
351
+ * @param subscriber Callback for changes
334
352
  * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
335
353
  * @returns Subscription
336
354
  */
337
- subscribe<Key extends keyof Store>(key: Key, callback: (value: Store[Key] | undefined) => void, copy?: boolean): Subscription;
355
+ subscribe<Key extends keyof Store>(key: Key, subscriber: Subscriber<Store[Key] | undefined>, copy?: boolean): Subscription;
338
356
  /**
339
357
  * Subscribe to changes for a specific key
340
358
  *
341
359
  * @param key Key of the value to subscribe to
342
- * @param callback Callback for changes
360
+ * @param subscriber Callback for changes
343
361
  * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
344
362
  * @returns Subscription
345
363
  */
346
- subscribe(key: Key, callback: (value: unknown) => void, copy?: boolean): Subscription;
364
+ subscribe(key: Key, subscriber: Subscriber<unknown>, copy?: boolean): Subscription;
347
365
  /**
348
366
  * Update the value _(based on the current value)_
349
367
  *
@@ -351,7 +369,7 @@ export type ReactiveStore<Store> = {
351
369
  */
352
370
  update(callback: (value: Store) => Store): void;
353
371
  } & Reactive<Store>;
354
- export type ReadonlyInstances<Value> = {
372
+ type ReadonlyInstances<Value> = {
355
373
  frozen?: ReadonlyFrozenSignal<Value>;
356
374
  original?: ReadonlySignal<Value>;
357
375
  };
@@ -391,6 +409,9 @@ export type Signal<Value> = {
391
409
  */
392
410
  update(callback: (value: Value) => Value): void;
393
411
  } & Reactive<Value> & SimpleReactive<Value>;
412
+ export type SignalState = {
413
+ readonlies?: ReadonlyInstances<unknown>;
414
+ } & ReactiveState;
394
415
  type SimpleReactive<Value> = {
395
416
  /**
396
417
  * Get the value
@@ -408,17 +429,19 @@ type SimpleReactive<Value> = {
408
429
  /**
409
430
  * Subscribe to changes
410
431
  *
411
- * @param callback Callback for changes
432
+ * @param subscriber Callback for changes
412
433
  * @param copy If the value is an array or record, should it be copied? _(defaults to `false`)_
413
434
  * @returns Subscription
414
435
  */
415
- subscribe(callback: (value: Value) => void, copy?: boolean): Subscription;
436
+ subscribe(subscriber: Subscriber<Value>, copy?: boolean): Subscription;
416
437
  };
417
438
  export type StoredSubscription = {
418
439
  callback: GenericCallback;
419
440
  copy: boolean;
420
441
  frozen: boolean;
421
- state: ReactiveState<unknown, never>;
442
+ instance: InternalSignal;
443
+ state: SignalState;
422
444
  };
445
+ export type Subscriber<Value> = (value: Value, subscription: Subscription) => void;
423
446
  export type SubscriptionType = 'copy' | 'frozen' | 'original' | 'readonly';
424
447
  //#endregion
package/dist/models.mjs CHANGED
@@ -1 +1,2 @@
1
+ import "./constants.mjs";
1
2
  export {};