frame-scheduling
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -12,10 +12,2 @@ "use strict"; | ||
var timeLifeFrame = 16; // 16ms === 60fps | ||
var Job = /** @class */ (function () { | ||
function Job(callback, priority) { | ||
this.fn = callback; | ||
this.priority = priority; | ||
this.iterations = 0; | ||
} | ||
return Job; | ||
}()); | ||
exports.P_LOWER = 1; | ||
@@ -26,9 +18,7 @@ exports.P_LOW = 3; | ||
exports.P_IMPORTANT = 10; | ||
var sortByPriority = function (list) { | ||
return list.sort(function (left, right) { | ||
return right.priority - right.iterations - left.priority - left.iterations; | ||
}); | ||
var sortJobsByNumber = function (jobs) { | ||
return Object.keys(jobs).sort(function (left, right) { return Number(left) - Number(right); }); | ||
}; | ||
var frameScheduling = function () { | ||
var listJobs = []; | ||
var listJobs = {}; | ||
var deferScheduled = false; | ||
@@ -41,19 +31,41 @@ var runDefer = function () { | ||
}; | ||
var addJob = function (callback, priority) { | ||
if (!listJobs[priority]) { | ||
listJobs[priority] = []; | ||
} | ||
listJobs[priority].push(callback); | ||
}; | ||
var raisingOfJob = function () { | ||
var keys = sortJobsByNumber(listJobs); | ||
for (var i = keys.length; i > 0; i--) { | ||
var key = keys[i - 1]; | ||
listJobs[Number(key) + 1] = listJobs[key]; | ||
delete listJobs[key]; | ||
} | ||
}; | ||
var runJobs = function () { | ||
var timeRun = Date.now(); | ||
sortByPriority(listJobs); | ||
var keys = sortJobsByNumber(listJobs); | ||
var empty = false; | ||
while (true) { | ||
if (listJobs.length === 0 || Date.now() - timeRun > timeLifeFrame) { | ||
if (!keys.length) { | ||
empty = true; | ||
} | ||
if (empty || Date.now() - timeRun > timeLifeFrame) { | ||
break; | ||
} | ||
else { | ||
var job = listJobs.shift(); | ||
job && job.fn(); | ||
var key = keys[keys.length - 1]; | ||
var jobs = listJobs[key]; | ||
var job = jobs.shift(); | ||
job && job(); | ||
if (!jobs.length) { | ||
delete listJobs[key]; | ||
keys.length = keys.length - 1; | ||
} | ||
} | ||
} | ||
deferScheduled = false; | ||
if (listJobs.length !== 0) { | ||
for (var i = 0; i < listJobs.length; i++) { | ||
listJobs[i].iterations++; | ||
} | ||
if (!empty) { | ||
raisingOfJob(); | ||
runDefer(); | ||
@@ -64,3 +76,3 @@ } | ||
var _b = (_a === void 0 ? {} : _a).priority, priority = _b === void 0 ? exports.P_NORMAL : _b; | ||
listJobs.push(new Job(callback, priority)); | ||
addJob(callback, priority); | ||
runDefer(); | ||
@@ -67,0 +79,0 @@ }; |
{ | ||
"name": "frame-scheduling", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Asynchronous start of functions in JS. Supports priority and interrupt execution every 16 milliseconds, to achieve 60fps.", | ||
@@ -5,0 +5,0 @@ "main": "lib/frameScheduling.js", |
@@ -12,13 +12,2 @@ let defer: Function; | ||
class Job { | ||
fn: Function; | ||
priority: number; | ||
iterations: number; | ||
constructor(callback: Function, priority: number) { | ||
this.fn = callback; | ||
this.priority = priority; | ||
this.iterations = 0; | ||
} | ||
} | ||
export const P_LOWER = 1; | ||
@@ -30,11 +19,9 @@ export const P_LOW = 3; | ||
const sortByPriority = (list: Job[]) => { | ||
return list.sort( | ||
(left: Job, right: Job) => | ||
right.priority - right.iterations - left.priority - left.iterations | ||
const sortJobsByNumber = (jobs: Object) => | ||
Object.keys(jobs).sort( | ||
(left: string, right: string) => Number(left) - Number(right) | ||
); | ||
}; | ||
const frameScheduling = () => { | ||
const listJobs: Job[] = []; | ||
const listJobs: { [l: string]: Function[] } = {}; | ||
let deferScheduled = false; | ||
@@ -50,13 +37,41 @@ | ||
const addJob = (callback: Function, priority: number) => { | ||
if (!listJobs[priority]) { | ||
listJobs[priority] = []; | ||
} | ||
listJobs[priority].push(callback); | ||
}; | ||
const raisingOfJob = () => { | ||
const keys = sortJobsByNumber(listJobs); | ||
for (var i = keys.length; i > 0; i--) { | ||
const key = keys[i - 1]; | ||
listJobs[Number(key) + 1] = listJobs[key]; | ||
delete listJobs[key]; | ||
} | ||
}; | ||
const runJobs = () => { | ||
const timeRun = Date.now(); | ||
sortByPriority(listJobs); | ||
const keys = sortJobsByNumber(listJobs); | ||
let empty = false; | ||
while (true) { | ||
if (listJobs.length === 0 || Date.now() - timeRun > timeLifeFrame) { | ||
if (!keys.length) { | ||
empty = true; | ||
} | ||
if (empty || Date.now() - timeRun > timeLifeFrame) { | ||
break; | ||
} else { | ||
const job = listJobs.shift(); | ||
const key = keys[keys.length - 1]; | ||
const jobs = listJobs[key]; | ||
const job = jobs.shift(); | ||
job && job(); | ||
job && job.fn(); | ||
if (!jobs.length) { | ||
delete listJobs[key]; | ||
keys.length = keys.length - 1; | ||
} | ||
} | ||
@@ -67,6 +82,4 @@ } | ||
if (listJobs.length !== 0) { | ||
for (var i = 0; i < listJobs.length; i++) { | ||
listJobs[i].iterations++; | ||
} | ||
if (!empty) { | ||
raisingOfJob(); | ||
@@ -78,3 +91,3 @@ runDefer(); | ||
return (callback: Function, { priority = P_NORMAL } = {}) => { | ||
listJobs.push(new Job(callback, priority)); | ||
addJob(callback, priority); | ||
@@ -81,0 +94,0 @@ runDefer(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8380
190