Skip to content

Commit

Permalink
disable next events serverside in the ClientSide provider
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto committed Oct 23, 2024
1 parent 5ff60b3 commit a45715c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
27 changes: 19 additions & 8 deletions sdk/js/src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class DevCycleClient<
[key: string]: { [key: string]: DVCVariable<any> }

Check warning on line 82 in sdk/js/src/Client.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
}
private store: CacheStore
private eventQueue: EventQueue<Variables, CustomData>
private eventQueue?: EventQueue<Variables, CustomData>
private requestConsolidator: ConfigRequestConsolidator
eventEmitter: EventEmitter
private streamingConnection?: StreamingConnection
Expand Down Expand Up @@ -125,7 +125,15 @@ export class DevCycleClient<

this.sdkKey = sdkKey
this.variableDefaultMap = {}
this.eventQueue = new EventQueue(sdkKey, this, options)

if (
!(
this.options.disableAutomaticEventLogging &&
this.options.disableCustomEventLogging
)
) {
this.eventQueue = new EventQueue(sdkKey, this, options)
}

this.eventEmitter = new EventEmitter()
if (!this.options.disableRealtimeUpdates) {
Expand Down Expand Up @@ -359,7 +367,7 @@ export class DevCycleClient<

try {
const variableFromConfig = this.config?.variables?.[variable.key]
this.eventQueue.queueAggregateEvent({
this.eventQueue?.queueAggregateEvent({
type: variable.isDefaulted
? EventTypes.variableDefaulted
: EventTypes.variableEvaluated,
Expand Down Expand Up @@ -439,7 +447,7 @@ export class DevCycleClient<
return this.config?.variables || {}
}

void this.eventQueue.flushEvents()
void this.eventQueue?.flushEvents()

try {
await this.onInitialized
Expand Down Expand Up @@ -489,7 +497,7 @@ export class DevCycleClient<
this.options,
)
const promise = new Promise<DVCVariableSet>((resolve, reject) => {
this.eventQueue.flushEvents()
this.eventQueue?.flushEvents()

this.onInitialized
.then(() => this.store.loadAnonUserId())
Expand Down Expand Up @@ -598,7 +606,7 @@ export class DevCycleClient<

checkParamDefined('type', event.type)
this.onInitialized.then(() => {
this.eventQueue.queueEvent(event)
this.eventQueue?.queueEvent(event)
})
}

Expand All @@ -608,7 +616,10 @@ export class DevCycleClient<
* @param callback
*/
flushEvents(callback?: () => void): Promise<void> {
return this.eventQueue.flushEvents().then(() => callback?.())
return (
this.eventQueue?.flushEvents().then(() => callback?.()) ??
Promise.resolve().then(() => callback?.())
)
}

/**
Expand Down Expand Up @@ -637,7 +648,7 @@ export class DevCycleClient<

this.streamingConnection?.close()

await this.eventQueue.close()
await this.eventQueue?.close()
}

/**
Expand Down
2 changes: 2 additions & 0 deletions sdk/js/src/EventQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export class EventQueue<
)
return
}
console.log('queueEvent', event)
this.eventQueue.push(event)
}

Expand All @@ -170,6 +171,7 @@ export class EventQueue<
)
return
}
console.log('queueAggregateEvent', event)

checkParamDefined('type', event.type)
checkParamDefined('target', event.target)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type DevCycleClientsideProviderProps = {
children: React.ReactNode
}

const isServer = typeof window === 'undefined'

/**
* Component which renders nothing, but runs code to keep client state in sync with server
* Also waits for the server's data promise with the `use` hook. This triggers the nearest suspense boundary,
Expand All @@ -36,7 +38,7 @@ export const SuspendedProviderInitialization = ({
}: Pick<
DevCycleClientContext,
'serverDataPromise' | 'userAgent'
>): React.ReactElement => {
>): React.ReactNode => {
const serverData = use(serverDataPromise)
const [previousContext, setPreviousContext] = useState<
DevCycleServerData | undefined
Expand All @@ -45,14 +47,16 @@ export const SuspendedProviderInitialization = ({
if (previousContext !== serverData) {
// change user and config data to match latest server data
// if the data has changed since the last invocation
context.client.synchronizeBootstrapData(
// assert this is a DevCycleClient, not a DevCycleNextClient, because it is. We expose a more limited type
// to the end user
(context.client as DevCycleClient).synchronizeBootstrapData(
serverData.config,
serverData.user,
userAgent,
)
setPreviousContext(serverData)
}
return <></>
return null
}

export const InternalDevCycleClientsideProvider = ({
Expand Down Expand Up @@ -102,6 +106,12 @@ export const InternalDevCycleClientsideProvider = ({
sdkPlatform: 'nextjs',
deferInitialization: true,
disableConfigCache: true,
...(isServer
? {
disableAutomaticEventLogging: true,
disableCustomEventLogging: true,
}
: {}),
next: {
configRefreshHandler: revalidateConfig,
},
Expand Down
19 changes: 17 additions & 2 deletions sdk/nextjs/src/client/internal/context.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
'use client'
import { DevCycleClient } from '@devcycle/js-client-sdk'
import {
DevCycleClient,
DVCCustomDataJSON,
VariableDefinitions,
} from '@devcycle/js-client-sdk'
import React from 'react'

export type DevCycleNextClient<
Variables extends VariableDefinitions = VariableDefinitions,
CustomData extends DVCCustomDataJSON = DVCCustomDataJSON,
> = Omit<
DevCycleClient<Variables, CustomData>,
| 'onClientInitialized'
| 'identifyUser'
| 'resetUser'
| 'synchronizeBootstrapData'
>

type ClientProviderContext = {
client: DevCycleClient
client: DevCycleNextClient
clientSDKKey: string
enableStreaming: boolean
serverDataPromise: Promise<unknown>
Expand Down
5 changes: 2 additions & 3 deletions sdk/nextjs/src/client/internal/useDevCycleClient.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useContext } from 'react'
import { DevCycleProviderContext } from './context'
import { DevCycleClient } from '@devcycle/js-client-sdk'
import { DevCycleNextClient, DevCycleProviderContext } from './context'

export const useDevCycleClient = (): DevCycleClient => {
export const useDevCycleClient = (): DevCycleNextClient => {
return useContext(DevCycleProviderContext).client
}
4 changes: 2 additions & 2 deletions sdk/nextjs/src/client/useDevCycleClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DevCycleNextClient } from './internal/context'
import { useDevCycleClient as internalUseClient } from './internal/useDevCycleClient'
import { DevCycleClient } from '@devcycle/js-client-sdk'

export const useDevCycleClient = (): DevCycleClient => {
export const useDevCycleClient = (): DevCycleNextClient => {
return internalUseClient()
}

0 comments on commit a45715c

Please sign in to comment.