frame-scheduling
Advanced tools
Comparing version 0.3.3 to 0.4.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var context = typeof window !== "undefined" ? window : global; | ||
var defer; | ||
var context = typeof window !== "undefined" ? window : global; | ||
if ("requestAnimationFrame" in context) { | ||
@@ -11,3 +11,3 @@ defer = requestAnimationFrame.bind(context); | ||
} | ||
var timeLifeFrame = 16; // 16ms === 60fps | ||
var TIME_LIFE_FRAME = 16; // 16ms === 60fps | ||
exports.P_LOWER = 1; | ||
@@ -18,2 +18,36 @@ exports.P_LOW = 3; | ||
exports.P_IMPORTANT = 10; | ||
var LinkedList = /** @class */ (function () { | ||
function LinkedList() { | ||
this.head = null; | ||
this.length = 0; | ||
} | ||
LinkedList.prototype.push = function (value) { | ||
var node = { | ||
value: value, | ||
next: null | ||
}; | ||
if (this.length === 0) { | ||
this.head = node; | ||
this.last = node; | ||
} | ||
else { | ||
this.last.next = node; | ||
this.last = node; | ||
} | ||
this.length++; | ||
}; | ||
LinkedList.prototype.shift = function () { | ||
if (this.length === 0 || !this.head) { | ||
return; | ||
} | ||
var value = this.head.value; | ||
this.head = this.head.next; | ||
this.length--; | ||
return value; | ||
}; | ||
LinkedList.prototype.isEmpty = function () { | ||
return this.length === 0; | ||
}; | ||
return LinkedList; | ||
}()); | ||
var frameScheduling = function () { | ||
@@ -39,3 +73,3 @@ var listJobs = {}; | ||
if (!listJobs[priority]) { | ||
listJobs[priority] = []; | ||
listJobs[priority] = new LinkedList(); | ||
jobsSortActual = false; | ||
@@ -56,14 +90,14 @@ } | ||
var timeRun = Date.now(); | ||
var keys = sortJobsByNumber(listJobs); | ||
var keysJobs = sortJobsByNumber(listJobs); | ||
var empty = false; | ||
while (true) { | ||
if (!keys.length) { | ||
if (!keysJobs.length) { | ||
empty = true; | ||
} | ||
if (empty || Date.now() - timeRun > timeLifeFrame) { | ||
if (empty || Date.now() - timeRun > TIME_LIFE_FRAME) { | ||
break; | ||
} | ||
else { | ||
var key = keys[keys.length - 1]; | ||
var jobs = listJobs[key]; | ||
var keyJob = keysJobs[keysJobs.length - 1]; | ||
var jobs = listJobs[keyJob]; | ||
var job = jobs.shift(); | ||
@@ -76,5 +110,5 @@ try { | ||
} | ||
if (!jobs.length) { | ||
delete listJobs[key]; | ||
keys.length = keys.length - 1; | ||
if (jobs.isEmpty()) { | ||
delete listJobs[keyJob]; | ||
keysJobs.length = keysJobs.length - 1; | ||
jobsSortActual = false; | ||
@@ -81,0 +115,0 @@ } |
{ | ||
"name": "frame-scheduling", | ||
"version": "0.3.3", | ||
"version": "0.4.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", |
@@ -1,4 +0,4 @@ | ||
let defer: Function; | ||
const context = typeof window !== "undefined" ? window : global; | ||
let defer: Function; | ||
if ("requestAnimationFrame" in context) { | ||
@@ -10,3 +10,3 @@ defer = requestAnimationFrame.bind(context); | ||
const timeLifeFrame = 16; // 16ms === 60fps | ||
const TIME_LIFE_FRAME = 16; // 16ms === 60fps | ||
@@ -19,5 +19,56 @@ export const P_LOWER = 1; | ||
interface listNode { | ||
value: Function; | ||
next: listNode | null; | ||
} | ||
class LinkedList { | ||
private length: number; | ||
private head: listNode | null; | ||
private last: listNode; | ||
constructor() { | ||
this.head = null; | ||
this.length = 0; | ||
} | ||
push(value: Function) { | ||
const node: listNode = { | ||
value: value, | ||
next: null | ||
}; | ||
if (this.length === 0) { | ||
this.head = node; | ||
this.last = node; | ||
} else { | ||
this.last.next = node; | ||
this.last = node; | ||
} | ||
this.length++; | ||
} | ||
shift() { | ||
if (this.length === 0 || !this.head) { | ||
return; | ||
} | ||
const value = this.head.value; | ||
this.head = this.head.next; | ||
this.length--; | ||
return value; | ||
} | ||
isEmpty() { | ||
return this.length === 0; | ||
} | ||
} | ||
const frameScheduling = () => { | ||
const listJobs: { [l: string]: Function[] } = {}; | ||
const listJobs: { [l: string]: LinkedList } = {}; | ||
let deferScheduled = false; | ||
let jobsSortCached: string[]; | ||
@@ -47,3 +98,3 @@ let jobsSortActual = false; | ||
if (!listJobs[priority]) { | ||
listJobs[priority] = []; | ||
listJobs[priority] = new LinkedList(); | ||
jobsSortActual = false; | ||
@@ -69,14 +120,14 @@ } | ||
const timeRun = Date.now(); | ||
const keys = sortJobsByNumber(listJobs); | ||
const keysJobs = sortJobsByNumber(listJobs); | ||
let empty = false; | ||
while (true) { | ||
if (!keys.length) { | ||
if (!keysJobs.length) { | ||
empty = true; | ||
} | ||
if (empty || Date.now() - timeRun > timeLifeFrame) { | ||
if (empty || Date.now() - timeRun > TIME_LIFE_FRAME) { | ||
break; | ||
} else { | ||
const key = keys[keys.length - 1]; | ||
const jobs = listJobs[key]; | ||
const keyJob = keysJobs[keysJobs.length - 1]; | ||
const jobs = listJobs[keyJob]; | ||
const job = jobs.shift(); | ||
@@ -90,5 +141,5 @@ | ||
if (!jobs.length) { | ||
delete listJobs[key]; | ||
keys.length = keys.length - 1; | ||
if (jobs.isEmpty()) { | ||
delete listJobs[keyJob]; | ||
keysJobs.length = keysJobs.length - 1; | ||
jobsSortActual = false; | ||
@@ -95,0 +146,0 @@ } |
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
15161
11
422