feat(replay): Change stop() to flush and remove current session#7741
feat(replay): Change stop() to flush and remove current session#7741
stop() to flush and remove current session#7741Conversation
`stop()` will now flush the eventBuffer before clearing it, as well as removing the session from Session Storage. Due to the flushing, `stop()` is now async. Ref: #7738
size-limit report 📦
|
packages/replay/src/integration.ts
Outdated
| * does not support a teardown | ||
| */ | ||
| public stop(): void { | ||
| public async stop(): Promise<void> { |
There was a problem hiding this comment.
No need to make this async, as this will just create a new promise!
packages/replay/src/replay.ts
Outdated
| // Set this property so that it ignores `_isEnabled` = false | ||
| // We can't move `_isEnabled` after awaiting a flush, otherwise we can | ||
| // enter into an infinite loop when `stop()` is called while flushing. | ||
| this._shouldFinalFlush = true; |
There was a problem hiding this comment.
Hmm, I don't really like this flag - I would prefer we find a way to solve this by e.g. passing an argument to flushImmediate() or making a separate function for this type of flushing to ensure this. Seems prone to run out of sync, be accidentally true/false when we don't mean it to, etc.
One option I could see is to just do:
this._isEnabled = false;
this._removeListeners();
this._flushAfterStop();
function _flushAfterStop() {
this._debouncedFlush().cancel();
this._flush({ force: true });
}Or something along these lines? IMHO as we are stopping here anyhow, we don't necessarily need to run the debounced flush here at all, allowing us to separate this a bit better?
There was a problem hiding this comment.
updated, instead of creating a private method, I just kept the flush calls inline as it isn't used elsewhere.
packages/replay/src/types.ts
Outdated
| setInitialState(): void; | ||
| } | ||
|
|
||
| export interface ReplayFlushOptions { |
There was a problem hiding this comment.
l: Do we need to export this here, or can we just inline this into the one place where it is used? Maybe actually gives more context seeing it there?
| // or we ran into a problem we don't want to retry, like rate limiting. | ||
| // In this case, we want to completely stop the replay - otherwise, we may get inconsistent segments | ||
| this.stop('sendReplay'); | ||
| void this.stop('sendReplay'); |
There was a problem hiding this comment.
Just noticed this @mydea -- I think this is ok since stop() will attempt the flush, fail, and when it retries, this._isEnabled is false, so will fail.
There was a problem hiding this comment.
I think it should be fine since this._isEnabled = false should be set "synchronously" in the async stop function. So I would expect this to be false already in the next code line, basically.
stop()will now flush the eventBuffer before clearing it, as well as removing the session from Session Storage. Due to the flushing,stop()is now async.Closes: #7738