Skip to content

Commit

Permalink
fix: deferred init (#601)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Nov 7, 2023
1 parent 67c704c commit a91aa41
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
24 changes: 11 additions & 13 deletions sdk/react/src/DevCycleProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function DevCycleProvider(props: Props): React.ReactElement {
const [isInitialized, setIsInitialized] = useState(false)
const clientRef = useRef<DevCycleClient>()

const initializedWatcher = useRef<boolean>(isInitialized)
let sdkKey: string
if ('sdkKey' in config) {
sdkKey = config.sdkKey
Expand All @@ -27,19 +26,16 @@ export function DevCycleProvider(props: Props): React.ReactElement {
}

if (!clientRef.current) {
// if on server, set deferInitialization to true
clientRef.current = initializeDevCycleClient(sdkKey, user, {
...options,
deferInitialization: typeof window === 'undefined',
})
}

// ensure there is exactly one initialization event handler per provider
// use a ref so we don't re-render while setting this up
if (!initializedWatcher.current) {
initializedWatcher.current = true
clientRef.current
.onClientInitialized()
useEffect(() => {
// assert this is defined otherwise we have a bug
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
clientRef
.current!.onClientInitialized()
.then(() => {
setIsInitialized(true)
})
Expand All @@ -48,18 +44,20 @@ export function DevCycleProvider(props: Props): React.ReactElement {
console.log('Error initializing DevCycle.')
setIsInitialized(true)
})
}

useEffect(() => {
return () => {
clientRef.current?.close()
clientRef.current = undefined
}
}, [])

return (
<Provider value={{ client: clientRef.current }}>
<initializedContext.Provider value={{ isInitialized }}>
<initializedContext.Provider
value={{
isInitialized:
isInitialized || clientRef.current.isInitialized,
}}
>
{props.children}
</initializedContext.Provider>
</Provider>
Expand Down
2 changes: 1 addition & 1 deletion sdk/react/src/useVariableValue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useVariable } from './'
import { useVariable } from './useVariable'
import type { DVCVariableValue } from '@devcycle/js-client-sdk'
import { VariableTypeAlias } from '@devcycle/types'

Expand Down

3 comments on commit a91aa41

@vercel
Copy link

@vercel vercel bot commented on a91aa41 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a91aa41 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on a91aa41 Nov 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.