@@ -5,7 +5,11 @@ import type { ReactNode } from 'react'
55import { useDeepCompareEffectNoCheck } from 'use-deep-compare-effect'
66import { destSDKBaseURL , pluginsSDKBaseURL } from '../constants'
77import type { CategoryKind } from '../types'
8- import { defaultConsentOptions , defaultLoadOptions , defaultTimeout } from './constants'
8+ import {
9+ defaultConsentOptions ,
10+ defaultLoadOptions ,
11+ defaultTimeout ,
12+ } from './constants'
913import { normalizeIdsMigration } from './normalizeIdsMigration'
1014
1115type Analytics = RudderAnalytics
@@ -92,17 +96,25 @@ export function AnalyticsProvider<T extends Events>({
9296 // This effect will unlock the case where we have a failure with the load of the analytics.load as rudderstack doesn't provider any solution for this case.
9397 useEffect ( ( ) => {
9498 let timer : ReturnType < typeof setTimeout > | undefined
95- if ( ! isAnalyticsReady && ( Number . isFinite ( timeout ) || shouldRenderOnlyWhenReady ) ) {
96- timer = setTimeout ( ( ) => setIsAnalyticsReady ( true ) , timeout ?? defaultTimeout )
97- onError ?.( new Error ( 'Analytics Setup Timeout' ) )
99+ if (
100+ ! isAnalyticsReady &&
101+ ( Number . isFinite ( timeout ) || shouldRenderOnlyWhenReady )
102+ ) {
103+ timer = setTimeout ( ( ) => {
104+ setIsAnalyticsReady ( true )
105+ onError ?.( new Error ( 'Timeout' ) )
106+ } , timeout ?? defaultTimeout )
107+ }
108+
109+ if ( isAnalyticsReady ) {
110+ clearTimeout ( timer )
98111 }
99112
100113 return ( ) => {
101114 clearTimeout ( timer )
102115 }
103116 } , [
104117 isAnalyticsReady ,
105- internalAnalytics ,
106118 setIsAnalyticsReady ,
107119 shouldRenderOnlyWhenReady ,
108120 timeout ,
@@ -144,7 +156,13 @@ export function AnalyticsProvider<T extends Events>({
144156 } )
145157
146158 analytics . ready ( ( ) => {
147- setAnalytics ( analytics )
159+ /**
160+ * this will wait for client destination to be ready, but analytics is ready.
161+ * we can listen for RSA_Ready event to know when the analytics is ready if we don't want to wait for the client destination to be ready.
162+ * document.addEventListener('RSA_Ready', function(e) {
163+ * console.log('RSA_Ready', e.detail.analyticsInstance);
164+ * });
165+ */
148166 setIsAnalyticsReady ( true )
149167 } )
150168 }
0 commit comments