Skip to content

Commit e8c0f0a

Browse files
committed
single q
1 parent f70d3e5 commit e8c0f0a

File tree

12 files changed

+87
-86
lines changed

12 files changed

+87
-86
lines changed

packages/browser/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ We have 2 options for linking this project to your local version: via [pnpm link
8484
- run `pnpm build` and `pnpm package` in the root of this repo to generate a tarball of this project.
8585
- run `pnpm -r update posthog-js@file:[ABSOLUTE_PATH_TO_POSTHOG_JS_REPO]/target/posthog-js.tgz` in the root of the repo that you want to link to (e.g. the posthog main repo).
8686
- run `pnpm install` in that same repo
87-
- run `cd frontend && pnpm run copy-scripts` if the repo that you want to link to is the posthog main repo.
87+
- run `pnpm --filter=frontend copy-scripts` if the repo that you want to link to is the posthog main repo.
8888

8989
Then, once this link has been created, any time you need to make a change to `posthog-js`, you can run `pnpm build && pnpm package` from the `posthog-js` root and the changes will appear in the other repo.
9090

packages/browser/src/__tests__/deferred-init-extensions.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createPosthogInstance } from './helpers/posthog-instance'
22
import { uuidv7 } from '../uuidv7'
33
import { RemoteConfig } from '../types'
4+
import { scheduler } from '../utils/scheduler'
45

56
jest.mock('../utils/globals', () => {
67
const orig = jest.requireActual('../utils/globals')
@@ -39,6 +40,7 @@ describe('deferred extension initialization', () => {
3940
console.error = jest.fn()
4041
mockReferrerGetter.mockReturnValue('https://referrer.com')
4142
mockURLGetter.mockReturnValue('https://example.com')
43+
scheduler._reset()
4244
})
4345

4446
describe('race condition handling', () => {
@@ -121,7 +123,7 @@ describe('deferred extension initialization', () => {
121123

122124
const posthog = await createPosthogInstance(token, {
123125
__preview_deferred_init_extensions: true,
124-
advanced_disable_decide: false,
126+
advanced_disable_decide: true,
125127
capture_pageview: false,
126128
disable_session_recording: true,
127129
})

packages/browser/src/__tests__/site-apps.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ describe('SiteApps', () => {
382382
expect(siteAppsInstance.apps['1']).toEqual({
383383
errored: false,
384384
loaded: true,
385-
processedBuffer: false,
385+
processedBuffer: true, // true because there are no buffered events to process
386386
id: '1',
387387
processEvent: expect.any(Function),
388388
})

packages/browser/src/entrypoints/array.full.es5.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import 'core-js/features/object/entries'
99
import 'core-js/features/object/from-entries'
10-
import 'core-js/features/promise'
1110

1211
if (typeof performance === 'undefined' || typeof performance.now !== 'function') {
1312
const perf = typeof performance !== 'undefined' ? performance : ({} as any)

packages/browser/src/extensions/replay/external/lazy-loaded-session-recorder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ export class LazyLoadedSessionRecording implements LazyLoadedSessionRecordingInt
10991099

11001100
if (this._buffer.data.length > 0) {
11011101
const snapshotEvents = splitBuffer(this._buffer)
1102-
void scheduler.processEach(snapshotEvents, (snapshotBuffer) => {
1102+
scheduler.processEach(snapshotEvents, (snapshotBuffer) => {
11031103
this._flushedSizeTracker?.trackSize(snapshotBuffer.size)
11041104
this._captureSnapshot({
11051105
$snapshot_bytes: snapshotBuffer.size,

packages/browser/src/extensions/replay/external/network-plugin.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { formDataToQuery } from '../../../utils/request-utils'
1818
import { patch } from '../rrweb-plugins/patch'
1919
import { isHostOnDenyList } from '../../../extensions/replay/external/denylist'
2020
import { defaultNetworkOptions } from './config'
21-
import { scheduler } from '../../../utils/scheduler'
2221

2322
const logger = createLogger('[Recorder]')
2423

@@ -63,17 +62,12 @@ function initPerformanceObserver(cb: networkCallback, win: IWindow, options: Req
6362
(isResourceTiming(entry) && options.initiatorTypes.includes(entry.initiatorType as InitiatorType))
6463
)
6564

66-
// Process initial performance entries with yielding for large sets
67-
scheduler
68-
.processEach(initialPerformanceEntries, (entry) =>
65+
cb({
66+
requests: initialPerformanceEntries.flatMap((entry) =>
6967
prepareRequest({ entry, method: undefined, status: undefined, networkRequest: {}, isInitial: true })
70-
)
71-
.then((requests) => {
72-
cb({
73-
requests: requests.flat(),
74-
isInitial: true,
75-
})
76-
})
68+
),
69+
isInitial: true,
70+
})
7771
}
7872
const observer = new win.PerformanceObserver((entries) => {
7973
// if recordBody or recordHeaders is true then we don't want to record fetch or xhr here

packages/browser/src/posthog-core.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -736,22 +736,17 @@ export class PostHog {
736736
// we don't support IE11 anymore, so performance.now is safe
737737
// eslint-disable-next-line compat/compat
738738
const startTime = performance.now()
739-
scheduler
740-
.processEach(initTasks, (task) => task())
741-
.then(
742-
() => {
743-
// eslint-disable-next-line compat/compat
744-
const totalTimeMs = Math.round(performance.now() - startTime)
745-
this.register_for_session({
746-
$sdk_debug_extensions_init_method: 'deferred',
747-
$sdk_debug_extensions_init_time_ms: totalTimeMs,
748-
})
749-
logger.info(`PostHog extensions initialized (${totalTimeMs}ms)`)
750-
},
751-
(error) => {
752-
logger.error('Error initializing extension:', error)
753-
}
754-
)
739+
scheduler.processEach(initTasks, (task) => task(), {
740+
onComplete: () => {
741+
// eslint-disable-next-line compat/compat
742+
const totalTimeMs = Math.round(performance.now() - startTime)
743+
this.register_for_session({
744+
$sdk_debug_extensions_init_method: 'deferred',
745+
$sdk_debug_extensions_init_time_ms: totalTimeMs,
746+
})
747+
logger.info(`PostHog extensions initialized (${totalTimeMs}ms)`)
748+
},
749+
})
755750
} else {
756751
// we don't support IE11 anymore, so performance.now is safe
757752
// eslint-disable-next-line compat/compat

packages/browser/src/request-queue.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,19 @@ export class RequestQueue {
5858
if (this._isPaused) {
5959
return
6060
}
61-
this._flushTimeout = setTimeout(async () => {
61+
this._flushTimeout = setTimeout(() => {
6262
this._clearFlushTimeout()
6363
if (this._queue.length > 0) {
6464
const requests = this._formatQueue()
6565
const requestEntries = Object.entries(requests)
66-
const now = new Date().getTime()
66+
const flushStartTime = new Date().getTime()
6767

68-
// Process timestamp updates with yielding for large batches
69-
await scheduler.processEach(
68+
scheduler.processEach(
7069
requestEntries,
7170
([, req]) => {
7271
if (req.data && isArray(req.data)) {
7372
each(req.data, (data) => {
74-
data['offset'] = Math.abs(data['timestamp'] - now)
73+
data['offset'] = Math.abs(data['timestamp'] - flushStartTime)
7574
delete data['timestamp']
7675
})
7776
}

packages/browser/src/retry-queue.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ export class RetryQueue {
121121
return
122122
}
123123

124-
this._poller = setTimeout(async () => {
124+
this._poller = setTimeout(() => {
125125
if (this._areWeOnline && this._queue.length > 0) {
126-
await this._flush()
126+
this._flush()
127127
}
128128
this._poll()
129129
}, this._pollIntervalMs) as any as number
130130
}
131131

132-
private async _flush(): Promise<void> {
132+
private _flush(): void {
133133
const now = Date.now()
134134
const notToFlush: RetryQueueElement[] = []
135135
const toFlush = this._queue.filter((item) => {
@@ -143,7 +143,7 @@ export class RetryQueue {
143143
this._queue = notToFlush
144144

145145
if (toFlush.length > 0) {
146-
await scheduler.processEach(toFlush, ({ requestOptions }) => this.retriableRequest(requestOptions), {
146+
scheduler.processEach(toFlush, ({ requestOptions }) => this.retriableRequest(requestOptions), {
147147
priority: 'high',
148148
})
149149
}

packages/browser/src/site-apps.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,21 @@ export class SiteApps {
9393
const processBufferedEvents = () => {
9494
if (!app.errored && this._bufferedInvocations.length) {
9595
logger.info(`Processing ${this._bufferedInvocations.length} events for site app with id ${loader.id}`)
96-
void scheduler
97-
.processEach(this._bufferedInvocations, (globals) => app.processEvent?.(globals))
98-
.then(() => {
96+
scheduler.processEach(this._bufferedInvocations, (globals) => app.processEvent?.(globals), {
97+
onComplete: () => {
9998
app.processedBuffer = true
10099
if (Object.values(this.apps).every((app) => app.processedBuffer || app.errored)) {
101100
this._stopBuffering?.()
102101
}
103-
})
102+
},
103+
})
104+
return
104105
}
105106

107+
// Either app errored or no buffered invocations - handle synchronously
108+
if (!app.errored) {
109+
app.processedBuffer = true
110+
}
106111
if (Object.values(this.apps).every((app) => app.processedBuffer || app.errored)) {
107112
this._stopBuffering?.()
108113
}

0 commit comments

Comments
 (0)