Skip to content

Commit

Permalink
fix: use a useEffect for suspended data synchronization (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Nov 7, 2024
1 parent 06390d0 commit 1b4b138
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
'use client'
import React, { Suspense, use, useContext, useRef, useState } from 'react'
import React, {
Suspense,
use,
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react'
import { DevCycleClient, initializeDevCycle } from '@devcycle/js-client-sdk'
import { invalidateConfig } from '../../common/invalidateConfig'
import { DevCycleNextOptions, DevCycleServerData } from '../../common/types'
Expand Down Expand Up @@ -38,18 +46,31 @@ export const SuspendedProviderInitialization = ({
DevCycleServerData | undefined
>()
const context = useContext(DevCycleProviderContext)
if (previousContext !== serverData) {
// change user and config data to match latest server data
// if the data has changed since the last invocation
// assert this is a DevCycleClient, not a DevCycleNextClient, because it is. We expose a more limited type
// to the end user

const synchronizeData = useCallback(() => {
;(context.client as DevCycleClient).synchronizeBootstrapData(
serverData.config,
serverData.user,
serverData.userAgent,
)
setPreviousContext(serverData)
}, [context.client, serverData])

if (!previousContext) {
synchronizeData()
}

useEffect(() => {
if (previousContext !== serverData) {
// change user and config data to match latest server data
// if the data has changed since the last invocation
// assert this is a DevCycleClient, not a DevCycleNextClient, because it is. We expose a more limited type
// to the end user
if (previousContext) {
synchronizeData()
}
setPreviousContext(serverData)
}
}, [synchronizeData, serverData, previousContext])
return null
}

Expand Down

0 comments on commit 1b4b138

Please sign in to comment.