@aws-amplify/datastore 3.4.8 → 3.4.9-in-app-messaging.48

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 (48) hide show
  1. package/CHANGELOG.md +104 -445
  2. package/dist/aws-amplify-datastore.js +28167 -13268
  3. package/dist/aws-amplify-datastore.js.map +1 -1
  4. package/dist/aws-amplify-datastore.min.js +8 -8
  5. package/dist/aws-amplify-datastore.min.js.map +1 -1
  6. package/lib/.tsbuildinfo +1 -1
  7. package/lib/datastore/datastore.d.ts +4 -1
  8. package/lib/datastore/datastore.js +129 -4
  9. package/lib/datastore/datastore.js.map +1 -1
  10. package/lib/sync/index.d.ts +3 -1
  11. package/lib/sync/index.js +10 -1
  12. package/lib/sync/index.js.map +1 -1
  13. package/lib/sync/processors/subscription.js +2 -2
  14. package/lib/sync/processors/subscription.js.map +1 -1
  15. package/lib/sync/processors/sync.d.ts +1 -3
  16. package/lib/sync/processors/sync.js +8 -19
  17. package/lib/sync/processors/sync.js.map +1 -1
  18. package/lib/types.d.ts +14 -0
  19. package/lib/types.js +5 -0
  20. package/lib/types.js.map +1 -1
  21. package/lib/util.d.ts +23 -1
  22. package/lib/util.js +77 -0
  23. package/lib/util.js.map +1 -1
  24. package/lib-esm/.tsbuildinfo +1 -1
  25. package/lib-esm/datastore/datastore.d.ts +4 -1
  26. package/lib-esm/datastore/datastore.js +130 -5
  27. package/lib-esm/datastore/datastore.js.map +1 -1
  28. package/lib-esm/sync/index.d.ts +3 -1
  29. package/lib-esm/sync/index.js +11 -2
  30. package/lib-esm/sync/index.js.map +1 -1
  31. package/lib-esm/sync/processors/subscription.js +2 -2
  32. package/lib-esm/sync/processors/subscription.js.map +1 -1
  33. package/lib-esm/sync/processors/sync.d.ts +1 -3
  34. package/lib-esm/sync/processors/sync.js +8 -19
  35. package/lib-esm/sync/processors/sync.js.map +1 -1
  36. package/lib-esm/types.d.ts +14 -0
  37. package/lib-esm/types.js +5 -0
  38. package/lib-esm/types.js.map +1 -1
  39. package/lib-esm/util.d.ts +23 -1
  40. package/lib-esm/util.js +78 -1
  41. package/lib-esm/util.js.map +1 -1
  42. package/package.json +7 -7
  43. package/src/datastore/datastore.ts +267 -121
  44. package/src/sync/index.ts +19 -3
  45. package/src/sync/processors/subscription.ts +1 -1
  46. package/src/sync/processors/sync.ts +1 -17
  47. package/src/types.ts +31 -12
  48. package/src/util.ts +80 -0
package/src/types.ts CHANGED
@@ -398,19 +398,23 @@ export type SubscriptionMessage<T extends PersistentModel> = {
398
398
  model: PersistentModelConstructor<T>;
399
399
  condition: PredicatesGroup<T> | null;
400
400
  };
401
+
402
+ export type DataStoreSnapshot<T extends PersistentModel> = {
403
+ items: T[];
404
+ isSynced: boolean;
405
+ };
401
406
  //#endregion
402
407
 
403
408
  //#region Predicates
404
409
 
405
- export type PredicateExpression<M extends PersistentModel, FT> = TypeName<
410
+ export type PredicateExpression<
411
+ M extends PersistentModel,
406
412
  FT
407
- > extends keyof MapTypeToOperands<FT>
413
+ > = TypeName<FT> extends keyof MapTypeToOperands<FT>
408
414
  ? (
409
415
  operator: keyof MapTypeToOperands<FT>[TypeName<FT>],
410
416
  // make the operand type match the type they're trying to filter on
411
- operand: MapTypeToOperands<FT>[TypeName<FT>][keyof MapTypeToOperands<
412
- FT
413
- >[TypeName<FT>]]
417
+ operand: MapTypeToOperands<FT>[TypeName<FT>][keyof MapTypeToOperands<FT>[TypeName<FT>]]
414
418
  ) => ModelPredicate<M>
415
419
  : never;
416
420
 
@@ -478,8 +482,7 @@ export type PredicateGroups<T extends PersistentModel> = {
478
482
 
479
483
  export type ModelPredicate<M extends PersistentModel> = {
480
484
  [K in keyof M]-?: PredicateExpression<M, NonNullable<M[K]>>;
481
- } &
482
- PredicateGroups<M>;
485
+ } & PredicateGroups<M>;
483
486
 
484
487
  export type ProducerModelPredicate<M extends PersistentModel> = (
485
488
  condition: ModelPredicate<M>
@@ -550,6 +553,11 @@ export type ProducerPaginationInput<T extends PersistentModel> = {
550
553
  page?: number;
551
554
  };
552
555
 
556
+ export type ObserveQueryOptions<T extends PersistentModel> = Pick<
557
+ ProducerPaginationInput<T>,
558
+ 'sort'
559
+ >;
560
+
553
561
  export type PaginationInput<T extends PersistentModel> = {
554
562
  sort?: SortPredicate<T>;
555
563
  limit?: number;
@@ -564,9 +572,10 @@ export type SortPredicate<T extends PersistentModel> = {
564
572
  [K in keyof T]-?: SortPredicateExpression<T, NonNullable<T[K]>>;
565
573
  };
566
574
 
567
- export type SortPredicateExpression<M extends PersistentModel, FT> = TypeName<
575
+ export type SortPredicateExpression<
576
+ M extends PersistentModel,
568
577
  FT
569
- > extends keyof MapTypeToOperands<FT>
578
+ > = TypeName<FT> extends keyof MapTypeToOperands<FT>
570
579
  ? (sortDirection: keyof typeof SortDirection) => SortPredicate<M>
571
580
  : never;
572
581
 
@@ -575,9 +584,8 @@ export enum SortDirection {
575
584
  DESCENDING = 'DESCENDING',
576
585
  }
577
586
 
578
- export type SortPredicatesGroup<
579
- T extends PersistentModel
580
- > = SortPredicateObject<T>[];
587
+ export type SortPredicatesGroup<T extends PersistentModel> =
588
+ SortPredicateObject<T>[];
581
589
 
582
590
  export type SortPredicateObject<T extends PersistentModel> = {
583
591
  field: keyof T;
@@ -779,4 +787,15 @@ export type ConflictHandler = (
779
787
  | PersistentModel
780
788
  | typeof DISCARD;
781
789
  export type ErrorHandler = (error: SyncError) => void;
790
+
791
+ export type DeferredCallbackResolverOptions = {
792
+ callback: () => void;
793
+ maxInterval?: number;
794
+ errorHandler?: (error: string) => void;
795
+ };
796
+
797
+ export enum LimitTimerRaceResolvedValues {
798
+ LIMIT = 'LIMIT',
799
+ TIMER = 'TIMER',
800
+ }
782
801
  //#endregion
package/src/util.ts CHANGED
@@ -23,6 +23,8 @@ import {
23
23
  isModelAttributePrimaryKey,
24
24
  isModelAttributeCompositeKey,
25
25
  NonModelTypeConstructor,
26
+ DeferredCallbackResolverOptions,
27
+ LimitTimerRaceResolvedValues,
26
28
  } from './types';
27
29
  import { WordArray } from 'amazon-cognito-identity-js';
28
30
 
@@ -681,3 +683,81 @@ export const isAWSIPAddress = (val: string): boolean => {
681
683
  val
682
684
  );
683
685
  };
686
+
687
+ export class DeferredPromise {
688
+ public promise: Promise<string>;
689
+ public resolve: (value: string | PromiseLike<string>) => void;
690
+ public reject: () => void;
691
+ constructor() {
692
+ const self = this;
693
+ this.promise = new Promise(
694
+ (resolve: (value: string | PromiseLike<string>) => void, reject) => {
695
+ self.resolve = resolve;
696
+ self.reject = reject;
697
+ }
698
+ );
699
+ }
700
+ }
701
+
702
+ export class DeferredCallbackResolver {
703
+ private limitPromise = new DeferredPromise();
704
+ private timerPromise: Promise<string>;
705
+ private maxInterval: number;
706
+ private timer: ReturnType<typeof setTimeout>;
707
+ private raceInFlight = false;
708
+ private callback = () => {};
709
+ private errorHandler: (error: string) => void;
710
+ private defaultErrorHandler = (
711
+ msg = 'DeferredCallbackResolver error'
712
+ ): void => {
713
+ throw new Error(msg);
714
+ };
715
+
716
+ constructor(options: DeferredCallbackResolverOptions) {
717
+ this.callback = options.callback;
718
+ this.errorHandler = options.errorHandler || this.defaultErrorHandler;
719
+ this.maxInterval = options.maxInterval || 2000;
720
+ }
721
+
722
+ private startTimer(): void {
723
+ this.timerPromise = new Promise((resolve, reject) => {
724
+ this.timer = setTimeout(() => {
725
+ resolve(LimitTimerRaceResolvedValues.TIMER);
726
+ }, this.maxInterval);
727
+ });
728
+ }
729
+
730
+ private async racePromises(): Promise<string> {
731
+ let winner: string;
732
+ try {
733
+ this.raceInFlight = true;
734
+ this.startTimer();
735
+ winner = await Promise.race([
736
+ this.timerPromise,
737
+ this.limitPromise.promise,
738
+ ]);
739
+ this.callback();
740
+ } catch (err) {
741
+ this.errorHandler(err);
742
+ } finally {
743
+ // reset for the next race
744
+ this.clear();
745
+ this.raceInFlight = false;
746
+ this.limitPromise = new DeferredPromise();
747
+
748
+ return winner;
749
+ }
750
+ }
751
+
752
+ public start(): void {
753
+ if (!this.raceInFlight) this.racePromises();
754
+ }
755
+
756
+ public clear(): void {
757
+ clearTimeout(this.timer);
758
+ }
759
+
760
+ public resolve(): void {
761
+ this.limitPromise.resolve(LimitTimerRaceResolvedValues.LIMIT);
762
+ }
763
+ }