@aws-amplify/datastore 3.5.2 → 3.6.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.
- package/CHANGELOG.md +11 -0
- package/dist/aws-amplify-datastore.js +154 -15
- package/dist/aws-amplify-datastore.js.map +1 -1
- package/dist/aws-amplify-datastore.min.js +7 -7
- package/dist/aws-amplify-datastore.min.js.map +1 -1
- package/lib/datastore/datastore.d.ts +2 -2
- package/lib/datastore/datastore.js +20 -9
- package/lib/datastore/datastore.js.map +1 -1
- package/lib/types.d.ts +10 -0
- package/lib/types.js +5 -0
- package/lib/types.js.map +1 -1
- package/lib/util.d.ts +23 -1
- package/lib/util.js +77 -0
- package/lib/util.js.map +1 -1
- package/lib-esm/datastore/datastore.d.ts +2 -2
- package/lib-esm/datastore/datastore.js +21 -10
- package/lib-esm/datastore/datastore.js.map +1 -1
- package/lib-esm/types.d.ts +10 -0
- package/lib-esm/types.js +5 -0
- package/lib-esm/types.js.map +1 -1
- package/lib-esm/util.d.ts +23 -1
- package/lib-esm/util.js +78 -1
- package/lib-esm/util.js.map +1 -1
- package/package.json +6 -6
- package/src/datastore/datastore.ts +32 -16
- package/src/types.ts +16 -0
- package/src/util.ts +80 -0
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
|
+
}
|