Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Optimize stale schedules processing
Browse files Browse the repository at this point in the history
  • Loading branch information
surol committed Jan 16, 2020
1 parent 4f1dfa3 commit 8c02a10
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/animation-render-scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('animationRenderScheduler', () => {

schedule(() => {
schedule(nextRender1.mockImplementation(() => {
schedule2(nextRender3); // This schedule stale
schedule2(nextRender3); // This schedule is stale
schedule(nextRender2);
}));
try {
Expand Down
31 changes: 14 additions & 17 deletions src/custom-render-scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,18 @@ const ScheduledRenderQ__symbol = Symbol('scheduled-render-q');
*/
class ScheduledRenderQ {

readonly ref: [ScheduledRenderQ];
schedule: (this: ScheduledRenderQ, config: RenderScheduleConfig) => void;
private scheduled?: RenderScheduleConfig;
private _next?: ScheduledRenderQ;

static by(queue: ScheduledRenderQueue): ScheduledRenderQ {
static by(queue: ScheduledRenderQueue, ref?: [ScheduledRenderQ]): ScheduledRenderQ {
return (queue as any)[ScheduledRenderQ__symbol]
|| ((queue as any)[ScheduledRenderQ__symbol] = new ScheduledRenderQ(queue));
|| ((queue as any)[ScheduledRenderQ__symbol] = new ScheduledRenderQ(queue, ref));
}

private constructor(private readonly q: ScheduledRenderQueue) {
private constructor(private readonly q: ScheduledRenderQueue, ref?: [ScheduledRenderQ]) {
this.schedule = this.doSchedule;
}

get next(): ScheduledRenderQ {
return this._next ? (this._next = this._next.next) : this; // ensure `next` is actual for stale schedules
this.ref = ref || [this];
}

add(render: ScheduledRender) {
Expand Down Expand Up @@ -181,7 +178,7 @@ class ScheduledRenderQ {
}

private reset(): ScheduledRenderQ {
return this._next = ScheduledRenderQ.by(this.q.reset());
return this.ref[0] = ScheduledRenderQ.by(this.q.reset(), this.ref);
}

private suspend() {
Expand Down Expand Up @@ -214,21 +211,21 @@ export function customRenderScheduler(
return scheduleOptions => {

const config = RenderScheduleConfig.by(scheduleOptions);
let nextQueue = ScheduledRenderQ.by(options.newQueue(config));
let queued: [ScheduledRenderQ, ScheduledRender] | [] = [];
const queueRef = ScheduledRenderQ.by(options.newQueue(config)).ref;
let enqueued: [ScheduledRenderQ, ScheduledRender] | [] = [];

return render => {

const [queue] = queued;
const [lastQueue] = enqueued;
const [nextQueue] = queueRef;

nextQueue = nextQueue.next;
if (queue === nextQueue) {
queued[1] = render;
if (lastQueue === nextQueue) {
enqueued[1] = render;
} else {

const nextQueued = queued = [nextQueue, render];
const nextEnqueued = enqueued = [nextQueue, render];

nextQueue.add((execution: ScheduledRenderExecution) => nextQueued[1](execution));
nextQueue.add((execution: ScheduledRenderExecution) => nextEnqueued[1](execution));
}

nextQueue.schedule(config);
Expand Down
2 changes: 1 addition & 1 deletion src/immediate-render-scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('immediateRenderScheduler', () => {

schedule(() => {
schedule(nextRender1.mockImplementation(() => {
schedule2(nextRender3); // This schedule stale
schedule2(nextRender3); // This schedule is stale
schedule(nextRender2);
}));
try {
Expand Down

0 comments on commit 8c02a10

Please sign in to comment.