-
Notifications
You must be signed in to change notification settings - Fork 206
feat: use yielding to main thread in more places #2585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pauldambra
wants to merge
1
commit into
main
Choose a base branch
from
feat/more-defer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| 'posthog-js': minor | ||
| --- | ||
|
|
||
| feat: introduce a task queue that will yield to the main thread periodically reducing the impact of long operations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ import { | |
| SURVEYS_REQUEST_TIMEOUT_MS, | ||
| USER_STATE, | ||
| } from './constants' | ||
| import { TaskQueue } from './utils/task-queue' | ||
| import { DeadClicksAutocapture, isDeadClicksEnabledForAutocapture } from './extensions/dead-clicks-autocapture' | ||
| import { ExceptionObserver } from './extensions/exception-autocapture' | ||
| import { HistoryAutocapture } from './extensions/history-autocapture' | ||
|
|
@@ -663,10 +664,6 @@ export class PostHog { | |
| } | ||
|
|
||
| private _initExtensions(startInCookielessMode: boolean): void { | ||
| // we don't support IE11 anymore, so performance.now is safe | ||
| // eslint-disable-next-line compat/compat | ||
| const initStartTime = performance.now() | ||
|
|
||
| this.historyAutocapture = new HistoryAutocapture(this) | ||
| this.historyAutocapture.startIfEnabled() | ||
|
|
||
|
|
@@ -733,52 +730,39 @@ export class PostHog { | |
| }) | ||
|
|
||
| // Process tasks with time-slicing to avoid blocking | ||
| this._processInitTaskQueue(initTasks, initStartTime) | ||
| } | ||
|
|
||
| private _processInitTaskQueue(queue: Array<() => void>, initStartTime: number): void { | ||
| const TIME_BUDGET_MS = 30 // Respect frame budget (~60fps = 16ms, but we're already deferred) | ||
|
|
||
| while (queue.length > 0) { | ||
| // Only time-slice if deferred init is enabled, otherwise run synchronously | ||
| if (this.config.__preview_deferred_init_extensions) { | ||
| // we don't support IE11 anymore, so performance.now is safe | ||
| // eslint-disable-next-line compat/compat | ||
| const elapsed = performance.now() - initStartTime | ||
|
|
||
| // Check if we've exceeded our time budget | ||
| if (elapsed >= TIME_BUDGET_MS && queue.length > 0) { | ||
| // Yield to browser, then continue processing | ||
| setTimeout(() => { | ||
| this._processInitTaskQueue(queue, initStartTime) | ||
| }, 0) | ||
| return | ||
| } | ||
| } | ||
|
|
||
| // Process next task | ||
| const task = queue.shift() | ||
| if (task) { | ||
| // Only use time-slicing if deferred init is enabled, otherwise process synchronously | ||
| if (this.config.__preview_deferred_init_extensions) { | ||
| const taskQueue = new TaskQueue({ | ||
| timeBudgetMs: 30, | ||
| onComplete: (totalTimeMs) => { | ||
| this.register_for_session({ | ||
| $sdk_debug_extensions_init_method: 'deferred', | ||
| $sdk_debug_extensions_init_time_ms: totalTimeMs, | ||
| }) | ||
| logger.info(`PostHog extensions initialized (${totalTimeMs}ms)`) | ||
| }, | ||
| onError: (error) => { | ||
| logger.error('Error initializing extension:', error) | ||
| }, | ||
| }) | ||
| taskQueue.enqueueAll(initTasks) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we not need to call a method to actually start the processing? |
||
| } else { | ||
| // we don't support IE11 anymore, so performance.now is safe | ||
| // eslint-disable-next-line compat/compat | ||
| const startTime = performance.now() | ||
| initTasks.forEach((task) => { | ||
| try { | ||
| task() | ||
| } catch (error) { | ||
| logger.error('Error initializing extension:', error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // All tasks complete - record timing for both sync and deferred modes | ||
| // we don't support IE11 anymore, so performance.now is safe | ||
| // eslint-disable-next-line compat/compat | ||
| const taskInitTiming = Math.round(performance.now() - initStartTime) | ||
| this.register_for_session({ | ||
| $sdk_debug_extensions_init_method: this.config.__preview_deferred_init_extensions | ||
| ? 'deferred' | ||
| : 'synchronous', | ||
| $sdk_debug_extensions_init_time_ms: taskInitTiming, | ||
| }) | ||
| if (this.config.__preview_deferred_init_extensions) { | ||
| logger.info(`PostHog extensions initialized (${taskInitTiming}ms)`) | ||
| }) | ||
| // eslint-disable-next-line compat/compat | ||
| const totalTimeMs = Math.round(performance.now() - startTime) | ||
| this.register_for_session({ | ||
| $sdk_debug_extensions_init_method: 'synchronous', | ||
| $sdk_debug_extensions_init_time_ms: totalTimeMs, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't look right, you're escaping the slash rather than the dot?
Shouldn't it be
\.d'\.ts$?