@reevit/react 0.4.7 → 0.4.8

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
@@ -428,6 +428,7 @@ function useReevit(options) {
428
428
  });
429
429
  const apiClientRef = react.useRef(null);
430
430
  const initializingRef = react.useRef(!!config.initialPaymentIntent);
431
+ const initRequestIdRef = react.useRef(0);
431
432
  react.useEffect(() => {
432
433
  if (config.initialPaymentIntent) {
433
434
  if (!state.paymentIntent || state.paymentIntent.id !== config.initialPaymentIntent.id) {
@@ -451,11 +452,11 @@ function useReevit(options) {
451
452
  return;
452
453
  }
453
454
  initializingRef.current = true;
455
+ const requestId = ++initRequestIdRef.current;
454
456
  dispatch({ type: "INIT_START" });
455
457
  try {
456
458
  const apiClient = apiClientRef.current;
457
459
  if (!apiClient) {
458
- initializingRef.current = false;
459
460
  throw new Error("API client not initialized");
460
461
  }
461
462
  const reference = config.reference || generateReference();
@@ -504,6 +505,9 @@ function useReevit(options) {
504
505
  data = result.data;
505
506
  error = result.error;
506
507
  }
508
+ if (requestId !== initRequestIdRef.current) {
509
+ return;
510
+ }
507
511
  if (error) {
508
512
  dispatch({ type: "INIT_ERROR", payload: error });
509
513
  onError?.(error);
@@ -523,6 +527,9 @@ function useReevit(options) {
523
527
  const paymentIntent = mapToPaymentIntent(data, { ...config, reference });
524
528
  dispatch({ type: "INIT_SUCCESS", payload: paymentIntent });
525
529
  } catch (err) {
530
+ if (requestId !== initRequestIdRef.current) {
531
+ return;
532
+ }
526
533
  const error = {
527
534
  code: "INIT_FAILED",
528
535
  message: err instanceof Error ? err.message : "Failed to initialize checkout",
@@ -604,6 +611,7 @@ function useReevit(options) {
604
611
  );
605
612
  const reset = react.useCallback(() => {
606
613
  initializingRef.current = false;
614
+ initRequestIdRef.current += 1;
607
615
  dispatch({ type: "RESET" });
608
616
  }, []);
609
617
  const close = react.useCallback(async () => {