Skip to content

Commit

Permalink
use raf's time for smoother transitions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkishi committed Nov 29, 2019
1 parent 26de6eb commit c18f066
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
41 changes: 22 additions & 19 deletions src/runtime/internal/loop.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
import { now, raf } from './environment';
import { raf } from './environment';

export interface Task { abort(): void; promise: Promise<void> }

const tasks = new Set();
let running = false;
type TaskCallback = (now: number) => boolean | void;
type TaskEntry = { c: TaskCallback; f: () => void };

function run_tasks() {
const tasks = new Set<TaskEntry>();

function run_tasks(now: number) {
tasks.forEach(task => {
if (!task[0](now())) {
if (!task.c(now)) {
tasks.delete(task);
task[1]();
task.f();
}
});

running = tasks.size > 0;
if (running) raf(run_tasks);
if (tasks.size !== 0) raf(run_tasks);
}

/**
* For testing purposes only!
*/
export function clear_loops() {
// for testing...
tasks.forEach(task => tasks.delete(task));
running = false;
tasks.clear();
}

export function loop(fn: (number) => void): Task {
let task;
/**
* Creates a new task that runs on each raf frame
* until it returns a falsy value or is aborted
*/
export function loop(callback: TaskCallback): Task {
let task: TaskEntry;

if (!running) {
running = true;
raf(run_tasks);
}
if (tasks.size === 0) raf(run_tasks);

return {
promise: new Promise<void>(fulfil => {
tasks.add(task = [fn, fulfil]);
promise: new Promise(fulfill => {
tasks.add(task = { c: callback, f: fulfill });
}),
abort() {
tasks.delete(task);
Expand Down
2 changes: 1 addition & 1 deletion test/runtime/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe("runtime", () => {
set_raf(cb => {
raf.callback = () => {
raf.callback = null;
cb();
cb(raf.time);
flush();
};
});
Expand Down

0 comments on commit c18f066

Please sign in to comment.