Skip to content

Commit

Permalink
fix(VirtualAction): Block rescheduled VirtualActions from executing t…
Browse files Browse the repository at this point in the history
…heir scheduled work.

VirtualActions are immutable so they can be inspected by the TestScheduler. In order to mirror

rescheduled stateful Actions, rescheduled VirtualActions shouldn't execute if they've been

rescheduled before execution.
  • Loading branch information
trxcllnt authored and jayphelps committed Nov 20, 2016
1 parent d586808 commit ac443a7
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/scheduler/VirtualTimeScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export class VirtualTimeScheduler extends AsyncScheduler {
*/
export class VirtualAction<T> extends AsyncAction<T> {

protected active: boolean = true;

constructor(protected scheduler: VirtualTimeScheduler,
protected work: (this: VirtualAction<T>, state?: T) => void,
protected index: number = scheduler.index += 1) {
Expand All @@ -54,8 +56,11 @@ export class VirtualAction<T> extends AsyncAction<T> {
}

public schedule(state?: T, delay: number = 0): Subscription {
return !this.id ?
super.schedule(state, delay) : (
if (!this.id) {
return super.schedule(state, delay);
}
this.active = false;
return (
// If an action is rescheduled, we save allocations by mutating its state,
// pushing it to the end of the scheduler queue, and recycling the action.
// But since the VirtualTimeScheduler is used for testing, VirtualActions
Expand All @@ -77,6 +82,12 @@ export class VirtualAction<T> extends AsyncAction<T> {
return undefined;
}

protected _execute(state: T, delay: number): any {
if (this.active === true) {
return super._execute(state, delay);
}
}

public static sortActions<T>(a: VirtualAction<T>, b: VirtualAction<T>) {
if (a.delay === b.delay) {
if (a.index === b.index) {
Expand Down

0 comments on commit ac443a7

Please sign in to comment.