Skip to content

Commit 069ede4

Browse files
committed
fix(TestScheduler): properly schedule actions added dynamically
1 parent daa5f1d commit 069ede4

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

src/schedulers/TestScheduler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export default class TestScheduler extends VirtualTimeScheduler {
1515
let messages = TestScheduler.parseMarbles(marbles, values, error);
1616
return Observable.create(subscriber => {
1717
messages.forEach(({ notification, frame }) => {
18-
this.schedule(() => {
18+
subscriber.add(this.schedule(() => {
1919
notification.observe(subscriber);
20-
}, frame);
20+
}, frame));
2121
}, this);
2222
});
2323
}
@@ -67,6 +67,7 @@ export default class TestScheduler extends VirtualTimeScheduler {
6767
const flushTests = this.flushTests.filter(test => test.ready);
6868
while (flushTests.length > 0) {
6969
var test = flushTests.shift();
70+
test.actual.sort((a, b) => a.frame === b.frame ? 0 : (a.frame > b.frame ? 1 : -1));
7071
this.assertDeepEqual(test.actual, test.expected);
7172
}
7273
}

src/schedulers/VirtualTimeScheduler.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,7 @@ export default class VirtualTimeScheduler implements Scheduler {
1414
return 0;
1515
}
1616

17-
sortActions() {
18-
if (!this.sorted) {
19-
(<VirtualAction<any>[]>this.actions).sort((a, b) => {
20-
return a.delay === b.delay ? (a.index > b.index ? 1 : -1) : (a.delay > b.delay ? 1 : -1);
21-
});
22-
this.sorted = true;
23-
}
24-
}
25-
2617
flush() {
27-
this.sortActions();
2818
const actions = this.actions;
2919
while (actions.length > 0) {
3020
let action = actions.shift();
@@ -33,6 +23,20 @@ export default class VirtualTimeScheduler implements Scheduler {
3323
}
3424
this.frame = 0;
3525
}
26+
27+
addAction<T>(action: Action) {
28+
const findDelay = action.delay;
29+
const actions = this.actions;
30+
const len = actions.length;
31+
const vaction = <VirtualAction<T>>action;
32+
33+
34+
actions.push(action);
35+
36+
actions.sort((a:VirtualAction<T>, b:VirtualAction<T>) => {
37+
return (a.delay === b.delay) ? (a.index === b.index ? 0 : (a.index > b.index ? 1 : -1)) : (a.delay > b.delay ? 1 : -1);
38+
});
39+
}
3640

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

0 commit comments

Comments
 (0)