Skip to content

Commit

Permalink
fix(TestScheduler): properly schedule actions added dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Sep 15, 2015
1 parent daa5f1d commit 069ede4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/schedulers/TestScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export default class TestScheduler extends VirtualTimeScheduler {
let messages = TestScheduler.parseMarbles(marbles, values, error);
return Observable.create(subscriber => {
messages.forEach(({ notification, frame }) => {
this.schedule(() => {
subscriber.add(this.schedule(() => {
notification.observe(subscriber);
}, frame);
}, frame));
}, this);
});
}
Expand Down Expand Up @@ -67,6 +67,7 @@ export default class TestScheduler extends VirtualTimeScheduler {
const flushTests = this.flushTests.filter(test => test.ready);
while (flushTests.length > 0) {
var test = flushTests.shift();
test.actual.sort((a, b) => a.frame === b.frame ? 0 : (a.frame > b.frame ? 1 : -1));
this.assertDeepEqual(test.actual, test.expected);
}
}
Expand Down
26 changes: 15 additions & 11 deletions src/schedulers/VirtualTimeScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ export default class VirtualTimeScheduler implements Scheduler {
return 0;
}

sortActions() {
if (!this.sorted) {
(<VirtualAction<any>[]>this.actions).sort((a, b) => {
return a.delay === b.delay ? (a.index > b.index ? 1 : -1) : (a.delay > b.delay ? 1 : -1);
});
this.sorted = true;
}
}

flush() {
this.sortActions();
const actions = this.actions;
while (actions.length > 0) {
let action = actions.shift();
Expand All @@ -33,6 +23,20 @@ export default class VirtualTimeScheduler implements Scheduler {
}
this.frame = 0;
}

addAction<T>(action: Action) {
const findDelay = action.delay;
const actions = this.actions;
const len = actions.length;
const vaction = <VirtualAction<T>>action;


actions.push(action);

actions.sort((a:VirtualAction<T>, b:VirtualAction<T>) => {
return (a.delay === b.delay) ? (a.index === b.index ? 0 : (a.index > b.index ? 1 : -1)) : (a.delay > b.delay ? 1 : -1);
});
}

schedule<T>(work: (x?: any) => Subscription<T> | void, delay: number = 0, state?: any): Subscription<T> {
this.sorted = false;
Expand All @@ -59,7 +63,7 @@ class VirtualAction<T> extends Subscription<T> implements Action {
new VirtualAction(scheduler, this.work, scheduler.index += 1);
action.state = state;
action.delay = scheduler.frame + delay;
scheduler.actions.push(action);
scheduler.addAction(action);
return this;
}

Expand Down

0 comments on commit 069ede4

Please sign in to comment.