@flurryx/store 0.7.3 → 0.7.5

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.
package/dist/index.js CHANGED
@@ -247,6 +247,11 @@ var BaseStore = class {
247
247
 
248
248
  // src/lazy-store.ts
249
249
  import { signal as signal2 } from "@angular/core";
250
+ import {
251
+ isAnyKeyLoading as isAnyKeyLoading2,
252
+ isKeyedResourceData as isKeyedResourceData2,
253
+ createKeyedResourceData as createKeyedResourceData2
254
+ } from "@flurryx/core";
250
255
  function createDefaultState() {
251
256
  return {
252
257
  data: void 0,
@@ -310,6 +315,96 @@ var LazyStore = class {
310
315
  })
311
316
  );
312
317
  }
318
+ updateKeyedOne(key, resourceKey, entity) {
319
+ const sig = this.getOrCreate(key);
320
+ const state = sig();
321
+ const data = isKeyedResourceData2(state.data) ? state.data : createKeyedResourceData2();
322
+ const nextErrors = { ...data.errors };
323
+ delete nextErrors[resourceKey];
324
+ const nextData = {
325
+ ...data,
326
+ entities: { ...data.entities, [resourceKey]: entity },
327
+ isLoading: { ...data.isLoading, [resourceKey]: false },
328
+ status: { ...data.status, [resourceKey]: "Success" },
329
+ errors: nextErrors
330
+ };
331
+ this.update(key, {
332
+ data: nextData,
333
+ isLoading: isAnyKeyLoading2(nextData.isLoading),
334
+ status: void 0,
335
+ errors: void 0
336
+ });
337
+ }
338
+ clearKeyedOne(key, resourceKey) {
339
+ const sig = this.getOrCreate(key);
340
+ const state = sig();
341
+ if (!isKeyedResourceData2(state.data)) {
342
+ return;
343
+ }
344
+ const data = state.data;
345
+ const previousState = state;
346
+ const nextEntities = { ...data.entities };
347
+ delete nextEntities[resourceKey];
348
+ const nextIsLoading = { ...data.isLoading };
349
+ delete nextIsLoading[resourceKey];
350
+ const nextStatus = { ...data.status };
351
+ delete nextStatus[resourceKey];
352
+ const nextErrors = { ...data.errors };
353
+ delete nextErrors[resourceKey];
354
+ const nextData = {
355
+ ...data,
356
+ entities: nextEntities,
357
+ isLoading: nextIsLoading,
358
+ status: nextStatus,
359
+ errors: nextErrors
360
+ };
361
+ sig.update(
362
+ (prev) => ({
363
+ ...prev,
364
+ data: nextData,
365
+ status: void 0,
366
+ isLoading: isAnyKeyLoading2(nextIsLoading),
367
+ errors: void 0
368
+ })
369
+ );
370
+ const updatedState = sig();
371
+ this.notifyHooks(key, updatedState, previousState);
372
+ }
373
+ startKeyedLoading(key, resourceKey) {
374
+ const sig = this.getOrCreate(key);
375
+ const state = sig();
376
+ if (!isKeyedResourceData2(state.data)) {
377
+ this.startLoading(key);
378
+ return;
379
+ }
380
+ const previousState = state;
381
+ const data = state.data;
382
+ const nextIsLoading = {
383
+ ...data.isLoading,
384
+ [resourceKey]: true
385
+ };
386
+ const nextStatus = { ...data.status };
387
+ delete nextStatus[resourceKey];
388
+ const nextErrors = { ...data.errors };
389
+ delete nextErrors[resourceKey];
390
+ const nextData = {
391
+ ...data,
392
+ isLoading: nextIsLoading,
393
+ status: nextStatus,
394
+ errors: nextErrors
395
+ };
396
+ sig.update(
397
+ (previous) => ({
398
+ ...previous,
399
+ data: nextData,
400
+ status: void 0,
401
+ isLoading: isAnyKeyLoading2(nextIsLoading),
402
+ errors: void 0
403
+ })
404
+ );
405
+ const updatedState = sig();
406
+ this.notifyHooks(key, updatedState, previousState);
407
+ }
313
408
  onUpdate(key, callback) {
314
409
  if (!this.hooks.has(key)) {
315
410
  this.hooks.set(key, []);
@@ -371,6 +466,93 @@ function mirrorKey(source, sourceKey, target, targetKeyOrOptions, options) {
371
466
  return cleanup;
372
467
  }
373
468
 
469
+ // src/collect-keyed.ts
470
+ import { createKeyedResourceData as createKeyedResourceData3, isAnyKeyLoading as isAnyKeyLoading3 } from "@flurryx/core";
471
+ function collectKeyed(source, sourceKey, target, targetKeyOrOptions, options) {
472
+ const resolvedTargetKey = typeof targetKeyOrOptions === "string" ? targetKeyOrOptions : sourceKey;
473
+ const resolvedOptions = typeof targetKeyOrOptions === "object" ? targetKeyOrOptions : options;
474
+ target.update(resolvedTargetKey, {
475
+ data: createKeyedResourceData3()
476
+ });
477
+ let previousId;
478
+ const cleanup = source.onUpdate(sourceKey, (state) => {
479
+ const resourceState = state;
480
+ const currentId = resolvedOptions.extractId(resourceState.data);
481
+ const currentTarget = target.get(resolvedTargetKey)();
482
+ const currentKeyed = currentTarget.data;
483
+ if (!currentKeyed) {
484
+ return;
485
+ }
486
+ if (resourceState.status === "Success" && currentId !== void 0) {
487
+ const newEntities = { ...currentKeyed.entities, [currentId]: resourceState.data };
488
+ const newIsLoading = { ...currentKeyed.isLoading, [currentId]: false };
489
+ const newStatus = { ...currentKeyed.status, [currentId]: resourceState.status };
490
+ const newErrors = { ...currentKeyed.errors };
491
+ delete newErrors[currentId];
492
+ const updatedKeyed = {
493
+ entities: newEntities,
494
+ isLoading: newIsLoading,
495
+ status: newStatus,
496
+ errors: newErrors
497
+ };
498
+ target.update(resolvedTargetKey, {
499
+ data: updatedKeyed,
500
+ isLoading: isAnyKeyLoading3(newIsLoading),
501
+ status: "Success"
502
+ });
503
+ previousId = currentId;
504
+ } else if (resourceState.status === "Error" && currentId !== void 0) {
505
+ const newIsLoading = { ...currentKeyed.isLoading, [currentId]: false };
506
+ const newStatus = { ...currentKeyed.status, [currentId]: resourceState.status };
507
+ const newErrors = { ...currentKeyed.errors, [currentId]: resourceState.errors };
508
+ const updatedKeyed = {
509
+ entities: { ...currentKeyed.entities },
510
+ isLoading: newIsLoading,
511
+ status: newStatus,
512
+ errors: newErrors
513
+ };
514
+ target.update(resolvedTargetKey, {
515
+ data: updatedKeyed,
516
+ isLoading: isAnyKeyLoading3(newIsLoading)
517
+ });
518
+ previousId = currentId;
519
+ } else if (resourceState.data === void 0 && previousId !== void 0) {
520
+ const { [previousId]: _removed, ...remainingEntities } = currentKeyed.entities;
521
+ const { [previousId]: _removedLoading, ...remainingLoading } = currentKeyed.isLoading;
522
+ const { [previousId]: _removedStatus, ...remainingStatus } = currentKeyed.status;
523
+ const { [previousId]: _removedErrors, ...remainingErrors } = currentKeyed.errors;
524
+ const updatedKeyed = {
525
+ entities: remainingEntities,
526
+ isLoading: remainingLoading,
527
+ status: remainingStatus,
528
+ errors: remainingErrors
529
+ };
530
+ target.update(resolvedTargetKey, {
531
+ data: updatedKeyed,
532
+ isLoading: isAnyKeyLoading3(remainingLoading)
533
+ });
534
+ previousId = void 0;
535
+ } else if (resourceState.isLoading && currentId !== void 0) {
536
+ const newIsLoading = { ...currentKeyed.isLoading, [currentId]: true };
537
+ const updatedKeyed = {
538
+ entities: { ...currentKeyed.entities },
539
+ isLoading: newIsLoading,
540
+ status: { ...currentKeyed.status },
541
+ errors: { ...currentKeyed.errors }
542
+ };
543
+ target.update(resolvedTargetKey, {
544
+ data: updatedKeyed,
545
+ isLoading: true
546
+ });
547
+ previousId = currentId;
548
+ }
549
+ });
550
+ if (resolvedOptions?.destroyRef) {
551
+ resolvedOptions.destroyRef.onDestroy(cleanup);
552
+ }
553
+ return cleanup;
554
+ }
555
+
374
556
  // src/resource.ts
375
557
  function resource() {
376
558
  return {};
@@ -384,7 +566,16 @@ function wireMirrors(store, mirrors) {
384
566
  }
385
567
  return store;
386
568
  }
387
- function createBuilder(accum, mirrors = []) {
569
+ function wireMirrorKeyed(store, defs) {
570
+ for (const def of defs) {
571
+ const sourceStore = inject(def.sourceToken);
572
+ collectKeyed(sourceStore, def.sourceKey, store, def.targetKey, {
573
+ extractId: def.extractId
574
+ });
575
+ }
576
+ return store;
577
+ }
578
+ function createBuilder(accum, mirrors = [], mirrorKeyedDefs = []) {
388
579
  return {
389
580
  resource(key) {
390
581
  return {
@@ -393,7 +584,7 @@ function createBuilder(accum, mirrors = []) {
393
584
  ...accum,
394
585
  [key]: resource()
395
586
  };
396
- return createBuilder(nextAccum, mirrors);
587
+ return createBuilder(nextAccum, mirrors, mirrorKeyedDefs);
397
588
  }
398
589
  };
399
590
  },
@@ -403,17 +594,31 @@ function createBuilder(accum, mirrors = []) {
403
594
  sourceKey,
404
595
  targetKey: targetKey ?? sourceKey
405
596
  };
406
- return createBuilder(accum, [...mirrors, def]);
597
+ return createBuilder(accum, [...mirrors, def], mirrorKeyedDefs);
598
+ },
599
+ mirrorKeyed(source, sourceKey, options, targetKey) {
600
+ const def = {
601
+ sourceToken: source,
602
+ sourceKey,
603
+ targetKey: targetKey ?? sourceKey,
604
+ extractId: options.extractId
605
+ };
606
+ return createBuilder(accum, mirrors, [...mirrorKeyedDefs, def]);
407
607
  },
408
608
  build() {
409
609
  return new InjectionToken("FlurryxStore", {
410
610
  providedIn: "root",
411
- factory: () => wireMirrors(new DynamicStore(accum), mirrors)
611
+ factory: () => {
612
+ const store = new DynamicStore(accum);
613
+ wireMirrors(store, mirrors);
614
+ wireMirrorKeyed(store, mirrorKeyedDefs);
615
+ return store;
616
+ }
412
617
  });
413
618
  }
414
619
  };
415
620
  }
416
- function createConstrainedBuilder(_enumObj, accum, mirrors = []) {
621
+ function createConstrainedBuilder(_enumObj, accum, mirrors = [], mirrorKeyedDefs = []) {
417
622
  return {
418
623
  resource(key) {
419
624
  return {
@@ -422,7 +627,12 @@ function createConstrainedBuilder(_enumObj, accum, mirrors = []) {
422
627
  ...accum,
423
628
  [key]: resource()
424
629
  };
425
- return createConstrainedBuilder(_enumObj, nextAccum, mirrors);
630
+ return createConstrainedBuilder(
631
+ _enumObj,
632
+ nextAccum,
633
+ mirrors,
634
+ mirrorKeyedDefs
635
+ );
426
636
  }
427
637
  };
428
638
  },
@@ -432,17 +642,39 @@ function createConstrainedBuilder(_enumObj, accum, mirrors = []) {
432
642
  sourceKey,
433
643
  targetKey: targetKey ?? sourceKey
434
644
  };
435
- return createConstrainedBuilder(_enumObj, accum, [...mirrors, def]);
645
+ return createConstrainedBuilder(
646
+ _enumObj,
647
+ accum,
648
+ [...mirrors, def],
649
+ mirrorKeyedDefs
650
+ );
651
+ },
652
+ mirrorKeyed(source, sourceKey, options, targetKey) {
653
+ const def = {
654
+ sourceToken: source,
655
+ sourceKey,
656
+ targetKey: targetKey ?? sourceKey,
657
+ extractId: options.extractId
658
+ };
659
+ return createConstrainedBuilder(_enumObj, accum, mirrors, [
660
+ ...mirrorKeyedDefs,
661
+ def
662
+ ]);
436
663
  },
437
664
  build() {
438
665
  return new InjectionToken("FlurryxStore", {
439
666
  providedIn: "root",
440
- factory: () => wireMirrors(new DynamicStore(accum), mirrors)
667
+ factory: () => {
668
+ const store = new DynamicStore(accum);
669
+ wireMirrors(store, mirrors);
670
+ wireMirrorKeyed(store, mirrorKeyedDefs);
671
+ return store;
672
+ }
441
673
  });
442
674
  }
443
675
  };
444
676
  }
445
- function createInterfaceBuilder(mirrors = []) {
677
+ function createInterfaceBuilder(mirrors = [], mirrorKeyedDefs = []) {
446
678
  return {
447
679
  mirror(source, sourceKey, targetKey) {
448
680
  const def = {
@@ -450,15 +682,32 @@ function createInterfaceBuilder(mirrors = []) {
450
682
  sourceKey,
451
683
  targetKey: targetKey ?? sourceKey
452
684
  };
453
- return createInterfaceBuilder([...mirrors, def]);
685
+ return createInterfaceBuilder(
686
+ [...mirrors, def],
687
+ mirrorKeyedDefs
688
+ );
689
+ },
690
+ mirrorKeyed(source, sourceKey, options, targetKey) {
691
+ const def = {
692
+ sourceToken: source,
693
+ sourceKey,
694
+ targetKey: targetKey ?? sourceKey,
695
+ extractId: options.extractId
696
+ };
697
+ return createInterfaceBuilder(mirrors, [
698
+ ...mirrorKeyedDefs,
699
+ def
700
+ ]);
454
701
  },
455
702
  build() {
456
703
  return new InjectionToken("FlurryxStore", {
457
704
  providedIn: "root",
458
- factory: () => wireMirrors(
459
- new LazyStore(),
460
- mirrors
461
- )
705
+ factory: () => {
706
+ const store = new LazyStore();
707
+ wireMirrors(store, mirrors);
708
+ wireMirrorKeyed(store, mirrorKeyedDefs);
709
+ return store;
710
+ }
462
711
  });
463
712
  }
464
713
  };
@@ -472,93 +721,6 @@ var Store = {
472
721
  return createConstrainedBuilder(enumObj, {});
473
722
  }
474
723
  };
475
-
476
- // src/collect-keyed.ts
477
- import { createKeyedResourceData as createKeyedResourceData2, isAnyKeyLoading as isAnyKeyLoading2 } from "@flurryx/core";
478
- function collectKeyed(source, sourceKey, target, targetKeyOrOptions, options) {
479
- const resolvedTargetKey = typeof targetKeyOrOptions === "string" ? targetKeyOrOptions : sourceKey;
480
- const resolvedOptions = typeof targetKeyOrOptions === "object" ? targetKeyOrOptions : options;
481
- target.update(resolvedTargetKey, {
482
- data: createKeyedResourceData2()
483
- });
484
- let previousId;
485
- const cleanup = source.onUpdate(sourceKey, (state) => {
486
- const resourceState = state;
487
- const currentId = resolvedOptions.extractId(resourceState.data);
488
- const currentTarget = target.get(resolvedTargetKey)();
489
- const currentKeyed = currentTarget.data;
490
- if (!currentKeyed) {
491
- return;
492
- }
493
- if (resourceState.status === "Success" && currentId !== void 0) {
494
- const newEntities = { ...currentKeyed.entities, [currentId]: resourceState.data };
495
- const newIsLoading = { ...currentKeyed.isLoading, [currentId]: false };
496
- const newStatus = { ...currentKeyed.status, [currentId]: resourceState.status };
497
- const newErrors = { ...currentKeyed.errors };
498
- delete newErrors[currentId];
499
- const updatedKeyed = {
500
- entities: newEntities,
501
- isLoading: newIsLoading,
502
- status: newStatus,
503
- errors: newErrors
504
- };
505
- target.update(resolvedTargetKey, {
506
- data: updatedKeyed,
507
- isLoading: isAnyKeyLoading2(newIsLoading),
508
- status: "Success"
509
- });
510
- previousId = currentId;
511
- } else if (resourceState.status === "Error" && currentId !== void 0) {
512
- const newIsLoading = { ...currentKeyed.isLoading, [currentId]: false };
513
- const newStatus = { ...currentKeyed.status, [currentId]: resourceState.status };
514
- const newErrors = { ...currentKeyed.errors, [currentId]: resourceState.errors };
515
- const updatedKeyed = {
516
- entities: { ...currentKeyed.entities },
517
- isLoading: newIsLoading,
518
- status: newStatus,
519
- errors: newErrors
520
- };
521
- target.update(resolvedTargetKey, {
522
- data: updatedKeyed,
523
- isLoading: isAnyKeyLoading2(newIsLoading)
524
- });
525
- previousId = currentId;
526
- } else if (resourceState.data === void 0 && previousId !== void 0) {
527
- const { [previousId]: _removed, ...remainingEntities } = currentKeyed.entities;
528
- const { [previousId]: _removedLoading, ...remainingLoading } = currentKeyed.isLoading;
529
- const { [previousId]: _removedStatus, ...remainingStatus } = currentKeyed.status;
530
- const { [previousId]: _removedErrors, ...remainingErrors } = currentKeyed.errors;
531
- const updatedKeyed = {
532
- entities: remainingEntities,
533
- isLoading: remainingLoading,
534
- status: remainingStatus,
535
- errors: remainingErrors
536
- };
537
- target.update(resolvedTargetKey, {
538
- data: updatedKeyed,
539
- isLoading: isAnyKeyLoading2(remainingLoading)
540
- });
541
- previousId = void 0;
542
- } else if (resourceState.isLoading && currentId !== void 0) {
543
- const newIsLoading = { ...currentKeyed.isLoading, [currentId]: true };
544
- const updatedKeyed = {
545
- entities: { ...currentKeyed.entities },
546
- isLoading: newIsLoading,
547
- status: { ...currentKeyed.status },
548
- errors: { ...currentKeyed.errors }
549
- };
550
- target.update(resolvedTargetKey, {
551
- data: updatedKeyed,
552
- isLoading: true
553
- });
554
- previousId = currentId;
555
- }
556
- });
557
- if (resolvedOptions?.destroyRef) {
558
- resolvedOptions.destroyRef.onDestroy(cleanup);
559
- }
560
- return cleanup;
561
- }
562
724
  export {
563
725
  BaseStore,
564
726
  LazyStore,