Skip to content

Commit

Permalink
fix: debounce iframe updates and limit size of stored events (#959)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwootto authored Sep 18, 2024
1 parent 51b0ebc commit cea29d8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
26 changes: 25 additions & 1 deletion lib/web-debugger/src/initializeDevCycleDebugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class IframeManager {
position: string
debugLogs: boolean
client: DevCycleClient | NextClient
private debouncedUpdateIframeData: () => void

constructor(
client: DevCycleClient | NextClient,
Expand All @@ -68,6 +69,22 @@ class IframeManager {
this.debugLogs = debugLogs
this.mainIframe = document.createElement('iframe')
this.buttonIframe = document.createElement('iframe')
this.debouncedUpdateIframeData = this.debounce(() => {
this._updateIframeData()
}, 300)
}

private debounce(func: () => void, delay: number) {
let timeoutId: NodeJS.Timeout | null = null
return () => {
if (timeoutId) {
clearTimeout(timeoutId)
}
timeoutId = setTimeout(() => {
func()
timeoutId = null
}, delay)
}
}

createIframesWhenReady() {
Expand All @@ -79,7 +96,7 @@ class IframeManager {
})
}

updateIframeData() {
private _updateIframeData() {
clientData.current.config = this.client.config
clientData.current.user = this.client.user
this.synchronizeIframeUrl()
Expand All @@ -99,9 +116,16 @@ class IframeManager {
)
}

updateIframeData() {
this.debouncedUpdateIframeData()
}

setupSubscriptions() {
const addEvent = (event: any) => {
clientData.current.liveEvents.unshift(event)
if (clientData.current.liveEvents.length > 200) {
clientData.current.liveEvents.pop()
}
}

const onConfigUpdated = () => {
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"@devcycle/js-cloud-server-sdk": [
"sdk/js-cloud-server/src/index.ts"
],
"@devcycle/nextjs-sdk": ["sdk/nextjs/src/index.ts"],
"@devcycle/nextjs-sdk/pages": ["sdk/nextjs/src/pages.ts"],
"@devcycle/nextjs-sdk/server": ["sdk/nextjs/src/server.ts"],
"@devcycle/nextjs-sdk": ["sdk/nextjs/index.ts"],
"@devcycle/nextjs-sdk/pages": ["sdk/nextjs/pages.ts"],
"@devcycle/nextjs-sdk/server": ["sdk/nextjs/server.ts"],
"@devcycle/nodejs-server-sdk": ["sdk/nodejs/src/index.ts"],
"@devcycle/nodejs-server-sdk/openfeature-strategy": [
"sdk/nodejs/openfeature-strategy.ts"
Expand Down

0 comments on commit cea29d8

Please sign in to comment.