Skip to content

Commit

Permalink
feat(scheduler): remove frame task queue after
Browse files Browse the repository at this point in the history
Reduce API surface. Use cases that were solved by `after` frame tasks
can be easily solved with a new `afterUpdate()` task queue.

BREAKING CHANGE: `currentFrameAfter()` and `nextFrameAfter()` functions
were removed.
  • Loading branch information
localvoid committed Jun 11, 2018
1 parent f599405 commit d3c4f72
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 75 deletions.
5 changes: 1 addition & 4 deletions documentation/advanced/scheduler.md
Expand Up @@ -21,23 +21,20 @@ is looking like this:
4. Update dirty components.
5. Check that `write` tasks queue is empty, otherwise go to step 3.
6. Check that `read` tasks queue is empty, otherwise go to step 2.
7. Execute `after` tasks until after task queue is empty.
8. Execute `afterUpdate` tasks once per frame.
7. Execute `afterUpdate` tasks once per frame.

There are three functions to add tasks into different task queues in the current frame:

```ts
function currentFrameRead(task: () => void): void;
function currentFrameWrite(task: () => void): void;
function currentFrameAfter(task: () => void): void;
```

And three functions for the next frame:

```ts
function nextFrameRead(task: () => void): void;
function nextFrameWrite(task: () => void): void;
function nextFrameAfter(task: () => void): void;
```

## beforeUpdate / afterUpdate
Expand Down
33 changes: 0 additions & 33 deletions packages/ivi-scheduler/src/index.ts
Expand Up @@ -28,10 +28,6 @@ const enum FrameTasksGroupFlags {
* Group contains "read" tasks".
*/
Read = 1 << 2,
/**
* Group contains "after" tasks.
*/
After = 1 << 3,
}

interface TaskList {
Expand Down Expand Up @@ -74,18 +70,13 @@ interface FrameTasksGroup {
* Read DOM task queue.
*/
r: TaskList;
/**
* Tasks that should be executed when all other frame tasks are finished.
*/
a: TaskList;
}

function createFrameTasksGroup(): FrameTasksGroup {
return {
f: 0,
w: createTaskList(),
r: createTaskList(),
a: createTaskList(),
};
}

Expand Down Expand Up @@ -306,12 +297,6 @@ const _handleNextFrame = catchError((time: number) => {

runRepeatableTasks(_afterUpdate);

// Perform tasks that should be executed when all DOM ops are finished.
while ((frame.f & FrameTasksGroupFlags.After)) {
frame.f ^= FrameTasksGroupFlags.After;
run(frame.a);
}

if (_autofocusedElement !== null) {
(_autofocusedElement as HTMLElement).focus();
_autofocusedElement = null;
Expand All @@ -334,11 +319,6 @@ function addFrameTaskRead(frame: FrameTasksGroup, task: () => void): void {
frame.r.a.push(task);
}

function addFrameTaskAfter(frame: FrameTasksGroup, task: () => void): void {
frame.f |= FrameTasksGroupFlags.After;
frame.a.a.push(task);
}

export function nextFrameUpdate(): void {
requestNextFrame();
addFrameTaskUpdate(_nextFrame);
Expand All @@ -354,11 +334,6 @@ export function nextFrameRead(task: () => void): void {
addFrameTaskRead(_nextFrame, task);
}

export function nextFrameAfter(task: () => void): void {
requestNextFrame();
addFrameTaskAfter(_nextFrame, task);
}

export function currentFrameUpdate(): void {
if (_flags & SchedulerFlags.CurrentFrameReady) {
addFrameTaskUpdate(_currentFrame);
Expand All @@ -383,14 +358,6 @@ export function currentFrameRead(task: () => void): void {
}
}

export function currentFrameAfter(task: () => void): void {
if (_flags & SchedulerFlags.CurrentFrameReady) {
addFrameTaskAfter(_currentFrame, task);
} else {
nextFrameAfter(task);
}
}

/**
* triggerNextFrame triggers an update for the next frame.
*/
Expand Down
38 changes: 0 additions & 38 deletions packages/ivi-test-scheduler/src/index.ts
Expand Up @@ -28,10 +28,6 @@ const enum FrameTasksGroupFlags {
* Group contains "read" tasks".
*/
Read = 1 << 2,
/**
* Group contains "after" tasks.
*/
After = 1 << 3,
}

/**
Expand All @@ -53,18 +49,13 @@ interface FrameTasksGroup {
* Read DOM task queue.
*/
read: (() => void)[] | null;
/**
* Tasks that should be executed when all other frame tasks are finished.
*/
after: (() => void)[] | null;
}

function createFrameTasksGroup(): FrameTasksGroup {
return {
flags: 0,
write: null,
read: null,
after: null,
};
}

Expand Down Expand Up @@ -220,11 +211,6 @@ function addFrameTaskRead(frame: FrameTasksGroup, task: () => void): void {
frame.read = append(frame.read, task);
}

function addFrameTaskAfter(frame: FrameTasksGroup, task: () => void): void {
frame.flags |= FrameTasksGroupFlags.After;
frame.after = append(frame.after, task);
}

export function nextFrameUpdate(): void {
requestNextFrame();
addFrameTaskUpdate(_nextFrame);
Expand All @@ -240,11 +226,6 @@ export function nextFrameRead(task: () => void): void {
addFrameTaskRead(_nextFrame, task);
}

export function nextFrameAfter(task: () => void): void {
requestNextFrame();
addFrameTaskAfter(_nextFrame, task);
}

export function currentFrameUpdate(): void {
if ((_flags & SchedulerFlags.CurrentFrameReady) !== 0) {
addFrameTaskUpdate(_currentFrame);
Expand All @@ -269,14 +250,6 @@ export function currentFrameRead(task: () => void): void {
}
}

export function currentFrameAfter(task: () => void): void {
if ((_flags & SchedulerFlags.CurrentFrameReady) !== 0) {
addFrameTaskAfter(_currentFrame, task);
} else {
nextFrameAfter(task);
}
}

export function runMicrotasks(): void {
if ((_flags & SchedulerFlags.MicrotaskPending) !== 0) {
while (_microtasks.length > 0) {
Expand Down Expand Up @@ -368,17 +341,6 @@ export function triggerNextFrame(time?: number): void {

runRepeatableTasks(_afterUpdate);

// Perform tasks that should be executed when all DOM ops are finished.
while ((frame.flags & FrameTasksGroupFlags.After) !== 0) {
frame.flags ^= FrameTasksGroupFlags.After;

tasks = frame.after!;
frame.after = null;
for (i = 0; i < tasks.length; ++i) {
tasks[i]();
}
}

if (_autofocusedElement !== null) {
(_autofocusedElement as HTMLElement).focus();
_autofocusedElement = null;
Expand Down

0 comments on commit d3c4f72

Please sign in to comment.