Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add startTime to measure loadStripe call timing #86

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ stripePromise.catch((err: Error) => {

export const loadStripe: LoadStripe = (...args) => {
loadCalled = true;
const startTime = Date.now();

return stripePromise.then((maybeStripe) => initStripe(maybeStripe, args));
return stripePromise.then((maybeStripe) =>
initStripe(maybeStripe, args, startTime)
);
};
3 changes: 2 additions & 1 deletion src/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export const loadStripe: LoadStripe & {setLoadParameters: SetLoadParams} = (
...args
) => {
loadStripeCalled = true;
const startTime = Date.now();

return loadScript(loadParams).then((maybeStripe) =>
initStripe(maybeStripe, args)
initStripe(maybeStripe, args, startTime)
);
};

Expand Down
9 changes: 5 additions & 4 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ const injectScript = (params: null | LoadParams): HTMLScriptElement => {
return script;
};

const registerWrapper = (stripe: any): void => {
const registerWrapper = (stripe: any, startTime: number): void => {
if (!stripe || !stripe._registerWrapper) {
return;
}

stripe._registerWrapper({name: 'stripe-js', version: _VERSION});
stripe._registerWrapper({name: 'stripe-js', version: _VERSION, startTime});
};

let stripePromise: Promise<StripeConstructor | null> | null = null;
Expand Down Expand Up @@ -121,14 +121,15 @@ export const loadScript = (

export const initStripe = (
maybeStripe: StripeConstructor | null,
args: Parameters<StripeConstructor>
args: Parameters<StripeConstructor>,
startTime: number
): Stripe | null => {
if (maybeStripe === null) {
return null;
}

const stripe = maybeStripe.apply(undefined, args);
registerWrapper(stripe);
registerWrapper(stripe, startTime);
return stripe;
};

Expand Down