-
Notifications
You must be signed in to change notification settings - Fork 149
/
index.ts
38 lines (32 loc) · 1012 Bytes
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import {StripeConstructor} from '../types';
import {loadScript, initStripe, LoadStripe} from './shared';
let stripePromise: Promise<StripeConstructor | null> | null;
let loadCalled = false;
const getStripePromise: () => Promise<StripeConstructor | null> = () => {
if (stripePromise) {
return stripePromise;
}
stripePromise = loadScript(null).catch((error) => {
// clear cache on error
stripePromise = null;
return Promise.reject(error);
});
return stripePromise;
};
// Execute our own script injection after a tick to give users time to do their
// own script injection.
Promise.resolve()
.then(() => getStripePromise())
.catch((error) => {
if (!loadCalled) {
console.warn(error);
}
});
export const loadStripe: LoadStripe = (...args) => {
loadCalled = true;
const startTime = Date.now();
// if previous attempts are unsuccessful, will re-load script
return getStripePromise().then((maybeStripe) =>
initStripe(maybeStripe, args, startTime)
);
};