Skip to content

Commit

Permalink
Also put the this._sync assignment inside setImmediate
Browse files Browse the repository at this point in the history
  • Loading branch information
longnguyen2004 committed Jan 24, 2025
1 parent e34019b commit 7221fc3
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/media/BaseMediaStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,29 @@ export class BaseMediaStream extends Writable {
return this._sync;
}
set sync(val: boolean) {
this._sync = val;
if (val)
{
// Reset timestamp stats when enabling sync
/**
* Since _waitForOtherStream() is an async function, it can only
* return at the end of the event loop, while Node.js is processing
* the microtask queue. If this is run synchronously, it can set
* _startTime to undefined before _waitForOtherStream() returns,
* which will cause the timeout calculation below to return NaN.
* Use setImmediate to put this on the macrotask queue, which is
* processed after the microtask queue.
*/
setImmediate(() => {
/**
* Since _waitForOtherStream() is an async function, it can only
* return at the end of the event loop, while Node.js is processing
* the microtask queue. If this is run synchronously, it can interfere
* with the operation of _waitForOtherStream().
* Use setImmediate to put this on the macrotask queue, which is
* processed after the microtask queue. This comes with the side effect
* of the value being incorrect until the next turn of the event loop,
* but it avoids all data race issues
*/
setImmediate(() => {
this._sync = val;
if (val)
{
// Reset timestamp stats when enabling sync
this._startPts = this._startTime = undefined;
this._loggerSync.debug("Sync enabled");
});
}
else
{
this._loggerSync.debug("Sync disabled");
}
}
else
{
this._loggerSync.debug("Sync disabled");
}
});
}
get noSleep(): boolean {
return this._noSleep;
Expand Down

0 comments on commit 7221fc3

Please sign in to comment.