Skip to content

Commit

Permalink
Timeline cancelled within setTimeout callback (#1315)
Browse files Browse the repository at this point in the history
* Working on getting puppeteer to pass

* Handles string inputs to Reverb

* adding scratch path

* Using forEachBefore instead of peek/shift while loop

* removing events after they are invoked
  • Loading branch information
tambien authored Feb 17, 2025
1 parent 71230b6 commit ca417aa
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
15 changes: 15 additions & 0 deletions Tone/core/context/Context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,21 @@ describe("Context", () => {
}, 0.01);
}, 0.05);
});

it("is robust against altering the timeline within the callback fn", (done) => {
let invokeCount = 0;
function checkDone(id: number) {
// clearing the current event alters the timeline and should not cause an issue
ctx.clearTimeout(id);
invokeCount++;
if (invokeCount === 3) {
done();
}
}
const id0 = ctx.setTimeout(() => checkDone(id0), 0.01);
const id1 = ctx.setTimeout(() => checkDone(id1), 0.01);
const id2 = ctx.setTimeout(() => checkDone(id2), 0.01);
});
});

context("setInterval", () => {
Expand Down
12 changes: 4 additions & 8 deletions Tone/core/context/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,11 @@ export class Context extends BaseContext {
*/
private _timeoutLoop(): void {
const now = this.now();
let firstEvent = this._timeouts.peek();
while (this._timeouts.length && firstEvent && firstEvent.time <= now) {
this._timeouts.forEachBefore(now, (event) => {
// invoke the callback
firstEvent.callback();
// shift the first event off
this._timeouts.shift();
// get the next one
firstEvent = this._timeouts.peek();
}
event.callback();
this._timeouts.remove(event);
});
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Tone/core/util/Draw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ describe("Draw", () => {
draw.dispose();
});

afterEach(() => {
draw.cancel(0);
});

it("can schedule a callback at a AudioContext time", (done) => {
const scheduledTime = draw.now() + 0.2;
draw.schedule(() => {
Expand Down
11 changes: 4 additions & 7 deletions Tone/core/util/Draw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,12 @@ export class DrawClass extends ToneWithContext<ToneWithContextOptions> {
*/
private _drawLoop(): void {
const now = this.context.currentTime;
while (
this._events.length &&
(this._events.peek() as DrawEvent).time - this.anticipation <= now
) {
const event = this._events.shift();
if (event && now - event.time <= this.expiration) {
this._events.forEachBefore(now + this.anticipation, (event) => {
if (now - event.time <= this.expiration) {
event.callback();
}
}
this._events.remove(event);
});
if (this._events.length > 0) {
this._animationFrame = requestAnimationFrame(this._boundDrawLoop);
}
Expand Down
2 changes: 2 additions & 0 deletions Tone/core/util/Timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
/**
* Return the first event in the timeline without removing it
* @returns {Object} The first event object
* @deprecated
*/
peek(): GenericEvent | undefined {
return this._timeline[0];
}

/**
* Return the first event in the timeline and remove it
* @deprecated
*/
shift(): GenericEvent | undefined {
return this._timeline.shift();
Expand Down
28 changes: 14 additions & 14 deletions Tone/source/OneShotSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,20 +196,20 @@ export abstract class OneShotSource<
* Invoke the onended callback
*/
protected _onended(): void {
if (this.onended !== noOp) {
this.onended(this);
// overwrite onended to make sure it only is called once
this.onended = noOp;
// dispose when it's ended to free up for garbage collection only in the online context
if (!this.context.isOffline) {
const disposeCallback = () => this.dispose();
// @ts-ignore
if (typeof window.requestIdleCallback !== "undefined") {
// @ts-ignore
window.requestIdleCallback(disposeCallback);
} else {
setTimeout(disposeCallback, 1000);
}
if (this.onended === noOp) {
return;
}

this.onended(this);
// overwrite onended to make sure it only is called once
this.onended = noOp;
// dispose when it's ended to free up for garbage collection only in the online context
if (!this.context.isOffline) {
const disposeCallback = () => this.dispose();
if (typeof requestIdleCallback !== "undefined") {
requestIdleCallback(disposeCallback);
} else {
setTimeout(disposeCallback, 10);
}
}
}
Expand Down

0 comments on commit ca417aa

Please sign in to comment.