-
Notifications
You must be signed in to change notification settings - Fork 44
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
fix: shutdownAsync flushes the whole queue #191
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3234457
chore: release rn 2.11.6
marandaneto 0e1207c
Merge branch 'main' of github.com:PostHog/posthog-js-lite
marandaneto 3071bc6
fix: shutdown flush all
marandaneto 38cef79
fix
marandaneto 7cc3c4b
escape hatch
marandaneto 2a35dd8
fix
marandaneto 6e541a9
docs
marandaneto c496356
prettier
marandaneto 66d8d1a
test
marandaneto e6d8ecc
fix
marandaneto 781667e
Merge branch 'main' of github.com:PostHog/posthog-js-lite
marandaneto 5bf9180
fix
marandaneto 9acea7f
fix
marandaneto a455a54
rename
marandaneto 162faf1
fix snapshot
marandaneto b434404
changelog
marandaneto 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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -616,23 +616,40 @@ export abstract class PostHogCoreStateless { | |
) | ||
} | ||
|
||
async shutdownAsync(): Promise<void> { | ||
async shutdownAsync(shutdownTimeoutMs?: number): Promise<void> { | ||
await this._initPromise | ||
|
||
clearTimeout(this._flushTimer) | ||
try { | ||
await this.flushAsync() | ||
await Promise.all( | ||
Object.values(this.pendingPromises).map((x) => | ||
x.catch(() => { | ||
// ignore errors as we are shutting down and can't deal with them anyways. | ||
}) | ||
) | ||
) | ||
// flush again to make sure we send all events, some of which might've been added | ||
// while we were waiting for the pending promises to resolve | ||
// For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture | ||
await this.flushAsync() | ||
|
||
const timeout = shutdownTimeoutMs ?? 30000 | ||
const startTimeWithDelay = Date.now() + timeout | ||
|
||
while (true) { | ||
marandaneto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const queue = this.getPersistedProperty<PostHogQueueItem[]>(PostHogPersistedProperty.Queue) || [] | ||
|
||
if (queue.length === 0) { | ||
break | ||
} | ||
|
||
// flush again to make sure we send all events, some of which might've been added | ||
// while we were waiting for the pending promises to resolve | ||
// For example, see sendFeatureFlags in posthog-node/src/posthog-node.ts::capture | ||
await this.flushAsync() | ||
|
||
// If we've been waiting for more than the shutdownTimeoutMs, stop it | ||
const now = Date.now() | ||
if (startTimeWithDelay < now) { | ||
break | ||
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. will silently break instead of throwing an error or something since its an intended behavior. |
||
} | ||
} | ||
} catch (e) { | ||
if (!isPostHogFetchError(e)) { | ||
throw e | ||
|
@@ -641,8 +658,8 @@ export abstract class PostHogCoreStateless { | |
} | ||
} | ||
|
||
shutdown(): void { | ||
void this.shutdownAsync() | ||
shutdown(shutdownTimeoutMs?: number): void { | ||
void this.shutdownAsync(shutdownTimeoutMs) | ||
} | ||
} | ||
|
||
|
This file contains 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,26 @@ | ||
import { createTestClient, PostHogCoreTestClient, PostHogCoreTestClientMocks } from './test-utils/PostHogCoreTestClient' | ||
|
||
describe('PostHog Core', () => { | ||
let posthog: PostHogCoreTestClient | ||
let mocks: PostHogCoreTestClientMocks | ||
|
||
describe('shutdown', () => { | ||
beforeEach(() => { | ||
jest.useRealTimers() | ||
;[posthog, mocks] = createTestClient('TEST_API_KEY', { | ||
flushAt: 10, | ||
preloadFeatureFlags: false, | ||
captureMode: 'json', | ||
}) | ||
}) | ||
|
||
it('flush messsages once called', async () => { | ||
for (let i = 0; i < 5; i++) { | ||
posthog.capture('test-event') | ||
} | ||
|
||
await posthog.shutdownAsync() | ||
expect(mocks.fetch).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |
This file contains 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 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 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 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
50 changes: 25 additions & 25 deletions
50
posthog-react-native/test/__snapshots__/autocapture.spec.ts.snap
This file contains 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 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.
Relates to jestjs/jest#14741 (comment) otherwise I cannot run tests locally