-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: isolate reliance on SchedulerAction
this
context (#7322)
This goes through and moves all of the reliance on the `this` context for scheduling to a single point, `executeSchedule`. It also moves common complexities to that helper function. All of this is done in preparation for creating newer, simplified schedulers
- Loading branch information
Showing
7 changed files
with
101 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
import { Observable } from '../Observable.js'; | ||
import type { SchedulerLike } from '../types.js'; | ||
import { executeSchedule } from '../util/executeSchedule.js'; | ||
|
||
export function scheduleArray<T>(input: ArrayLike<T>, scheduler: SchedulerLike) { | ||
return new Observable<T>((subscriber) => { | ||
// The current array index. | ||
let i = 0; | ||
// Start iterating over the array like on a schedule. | ||
return scheduler.schedule(function () { | ||
const emit = () => { | ||
// If we have hit the end of the array, complete. | ||
if (i === input.length) { | ||
// If we have hit the end of the array like in the | ||
// previous job, we can complete. | ||
subscriber.complete(); | ||
} else { | ||
// Otherwise let's next the value at the current index, | ||
// Otherwise, next the value at the current index, | ||
// then increment our index. | ||
subscriber.next(input[i++]); | ||
// If the last emission didn't cause us to close the subscriber | ||
// (via take or some side effect), reschedule the job and we'll | ||
// make another pass. | ||
if (!subscriber.closed) { | ||
this.schedule(); | ||
} | ||
executeSchedule(subscriber, scheduler, emit); | ||
} | ||
}); | ||
}; | ||
|
||
// Start iterating over the array like on a schedule. | ||
return executeSchedule(subscriber, scheduler, emit); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,31 @@ | ||
import type { Subscription } from '../Observable.js'; | ||
import type { SchedulerAction, SchedulerLike } from '../types.js'; | ||
|
||
export function executeSchedule( | ||
parentSubscription: Subscription, | ||
scheduler: SchedulerLike, | ||
work: () => void, | ||
delay: number, | ||
repeat: true | ||
): void; | ||
export function executeSchedule( | ||
parentSubscription: Subscription, | ||
scheduler: SchedulerLike, | ||
work: () => void, | ||
delay?: number, | ||
repeat?: false | ||
): Subscription; | ||
|
||
export function executeSchedule( | ||
parentSubscription: Subscription, | ||
scheduler: SchedulerLike, | ||
work: () => void, | ||
delay = 0, | ||
repeat = false | ||
): Subscription | void { | ||
const scheduleSubscription = scheduler.schedule(function (this: SchedulerAction<any>) { | ||
work(); | ||
if (repeat) { | ||
parentSubscription.add(this.schedule(null, delay)); | ||
} else { | ||
this.unsubscribe(); | ||
} | ||
}, delay); | ||
if (!parentSubscription.closed) { | ||
const scheduleSubscription = scheduler.schedule(function (this: SchedulerAction<any>) { | ||
work(); | ||
if (repeat) { | ||
parentSubscription.add(this.schedule(null, delay)); | ||
} else { | ||
this.unsubscribe(); | ||
} | ||
}, delay); | ||
|
||
parentSubscription.add(scheduleSubscription); | ||
parentSubscription.add(scheduleSubscription); | ||
|
||
if (!repeat) { | ||
// Because user-land scheduler implementations are unlikely to properly reuse | ||
// Actions for repeat scheduling, we can't trust that the returned subscription | ||
// will control repeat subscription scenarios. So we're trying to avoid using them | ||
// incorrectly within this library. | ||
return scheduleSubscription; | ||
if (!repeat) { | ||
// Because user-land scheduler implementations are unlikely to properly reuse | ||
// Actions for repeat scheduling, we can't trust that the returned subscription | ||
// will control repeat subscription scenarios. So we're trying to avoid using them | ||
// incorrectly within this library. | ||
return scheduleSubscription; | ||
} | ||
} | ||
} |