Revert "getFrom is like getAfter, but also includes the event at the given time"

This reverts commit b4b924f2e0.
This commit is contained in:
Yotam Mann 2019-07-24 19:54:57 -04:00
parent 4858251ba6
commit 5fcb7df3cc
2 changed files with 14 additions and 55 deletions

View file

@ -215,33 +215,6 @@ describe("Timeline", () => {
sched.dispose();
});
it("can the next event from the given time", () => {
const sched = new Timeline<StateTimelineEvent>();
expect(sched.getFrom(0)).is.null;
sched.add({
state: "A",
time: 0.1,
});
sched.add({
state: "B",
time: 1.1,
});
sched.add({
state: "C",
time: 2.1,
});
const firstEvent = sched.getFrom(0.1);
const secondEvent = sched.getFrom(1);
if (firstEvent && secondEvent) {
expect(firstEvent.state).is.equal("A");
expect(secondEvent.state).is.equal("B");
} else {
throw new Error("should have 2 events");
}
expect(sched.getFrom(3)).is.null;
sched.dispose();
});
it("can the event before the event before the given time", () => {
const sched = new Timeline<StateTimelineEvent>();
expect(sched.getBefore(0)).is.null;

View file

@ -66,7 +66,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* Insert an event object onto the timeline. Events must have a "time" attribute.
* @param event The event object to insert into the timeline.
*/
add(event: GenericEvent): this {
add(event: GenericEvent): Timeline<GenericEvent> {
// the event needs to have a time attribute
this.assert(Reflect.has(event, "time"), "Timeline: events must have a time attribute");
event.time = event.time.valueOf();
@ -85,7 +85,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* @param {Object} event The event object to remove from the list.
* @returns {Timeline} this
*/
remove(event: GenericEvent): this {
remove(event: GenericEvent): Timeline<GenericEvent> {
const index = this._timeline.indexOf(event);
if (index !== -1) {
this._timeline.splice(index, 1);
@ -134,21 +134,6 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
}
}
/**
* Get the event which is scheduled at or after the given time.
* @param time The time to query.
*/
getFrom(time: number, param: TimelineSearchParam = "time"): GenericEvent | null {
const index = this._search(time, param);
if (index !== -1 && this._timeline[index].time === time) {
return this._timeline[index];
} else if (index + 1 < this._timeline.length) {
return this._timeline[index + 1];
} else {
return null;
}
}
/**
* Get the event before the event at the given time.
* @param time The time to query.
@ -171,7 +156,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* Cancel events at and after the given time
* @param time The time to query.
*/
cancel(after: number): this {
cancel(after: number): Timeline<GenericEvent> {
if (this._timeline.length > 1) {
let index = this._search(after);
if (index >= 0) {
@ -202,9 +187,10 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
/**
* Cancel events before or equal to the given time.
* @param time The time to cancel before.
* @param {Number} time The time to cancel before.
* @returns {Timeline} this
*/
cancelBefore(time): this {
cancelBefore(time): Timeline<GenericEvent> {
const index = this._search(time);
if (index >= 0) {
this._timeline = this._timeline.slice(index + 1);
@ -214,8 +200,8 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
/**
* Returns the previous event if there is one. null otherwise
* @param event The event to find the previous one of
* @return The event right before the given event
* @param {Object} event The event to find the previous one of
* @return {Object} The event right before the given event
*/
previousEvent(event): GenericEvent | null {
const index = this._timeline.indexOf(event);
@ -285,7 +271,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* Iterate over everything in the array
* @param callback The callback to invoke with every item
*/
forEach(callback: (event: GenericEvent) => void): this {
forEach(callback: (event: GenericEvent) => void): Timeline<GenericEvent> {
this._iterate(callback);
return this;
}
@ -295,7 +281,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* @param time The time to check if items are before
* @param callback The callback to invoke with every item
*/
forEachBefore(time, callback: (event: GenericEvent) => void): this {
forEachBefore(time, callback: (event: GenericEvent) => void): Timeline<GenericEvent> {
// iterate over the items in reverse so that removing an item doesn't break things
const upperBound = this._search(time);
if (upperBound !== -1) {
@ -309,7 +295,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* @param time The time to check if items are before
* @param callback The callback to invoke with every item
*/
forEachAfter(time, callback: (event: GenericEvent) => void): this {
forEachAfter(time, callback: (event: GenericEvent) => void): Timeline<GenericEvent> {
// iterate over the items in reverse so that removing an item doesn't break things
const lowerBound = this._search(time);
this._iterate(callback, lowerBound + 1);
@ -324,7 +310,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* @param endTime The end of the test interval.
* @param callback The callback to invoke with every item
*/
forEachBetween(startTime: number, endTime: number, callback: (event: GenericEvent) => void): this {
forEachBetween(startTime: number, endTime: number, callback: (event: GenericEvent) => void): Timeline<GenericEvent> {
let lowerBound = this._search(startTime);
let upperBound = this._search(endTime);
if (lowerBound !== -1 && upperBound !== -1) {
@ -348,7 +334,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* @param time The time to check if items are before
* @param callback The callback to invoke with every item
*/
forEachFrom(time: number, callback: (event: GenericEvent) => void): this {
forEachFrom(time: number, callback: (event: GenericEvent) => void): Timeline<GenericEvent> {
// iterate over the items in reverse so that removing an item doesn't break things
let lowerBound = this._search(time);
// work backwards until the event time is less than time
@ -364,7 +350,7 @@ export class Timeline<GenericEvent extends TimelineEvent> extends Tone {
* @param time The time to check if items are before
* @param callback The callback to invoke with every item
*/
forEachAtTime(time: number, callback: (event: GenericEvent) => void): this {
forEachAtTime(time: number, callback: (event: GenericEvent) => void): Timeline<GenericEvent> {
// iterate over the items in reverse so that removing an item doesn't break things
const upperBound = this._search(time);
if (upperBound !== -1) {