Skip to content

Commit

Permalink
Remove all Maps
Browse files Browse the repository at this point in the history
Calls to get and set take 0.5ms (on my relatively fast MBP)
dtex committed Jul 7, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 95fc76a commit ff348b9
Showing 1 changed file with 27 additions and 39 deletions.
66 changes: 27 additions & 39 deletions lib/temporal.js
Original file line number Diff line number Diff line change
@@ -11,13 +11,6 @@ var queue = {};
// Actively processing queue
var isProcessing = false;

// Task details are stored as a plain object, privately in a Map
// to avoid being forced to expose the properties directly on the instance.
//
// Queue emitters are stored privately in a Map to avoid using
// |this| alias patterns.
var priv = new Map();

var tick = global.setImmediate || process.nextTick;

/**
@@ -32,19 +25,19 @@ function Task(entry) {
this.called = 0;
this.now = this.calledAt = Date.now();

priv.set(this, entry);

// Side table property definitions
entry.isRunnable = true;
entry.later = this.now + entry.time;


if (!queue[entry.later]) {
queue[entry.later] = [];
this.isRunnable = true;
this.later = this.now + entry.time;
this.task = entry.task;
this.type = entry.type;
this.time = entry.time;

if (!queue[this.later]) {
queue[this.later] = [];
}

// console.log( entry.later, this );
queue[entry.later].push(this);
queue[this.later].push(this);
}

// Inherit EventEmitter API
@@ -63,34 +56,30 @@ Task.deriveOp = function(p, v) {
* stop Stop the current behaviour
*/
Task.prototype.stop = function() {
priv.get(this).isRunnable = false;
this.isRunnable = false;
this.emit("stop");
};

function Queue(tasks) {
priv.set(this, []);

this.refs = [];
this.add(tasks);
}

util.inherits(Queue, Emitter);

Queue.prototype.stop = function() {
priv.get(this).forEach(function(ref) {
this.refs.forEach(function(ref) {
ref.stop();
});

this.emit("stop");
};

Queue.prototype.add = function(tasks) {
var thisq = this;
var op, item, task, ref, refs;
var op, item, task, ref;

this.cumulative = this.cumulative || 0;

refs = priv.get(this);

while (tasks.length) {
item = tasks.shift();
op = Object.keys(item).reduce(Task.deriveOp, "");
@@ -101,33 +90,33 @@ Queue.prototype.add = function(tasks) {
// emitted after the final callback is called.
if (tasks.length === 0) {
task = item.task;
item.task = function(temporald) {
task.call(thisq, temporald);
item.task = temporald => {
task.call(this, temporald);

// Emit the end event _from_ within the _last_ task
// defined in the Queue tasks. Use the |tasks| array
// object as the access key.
thisq.emit("end", temporald);
this.emit("end", temporald);

// Reset on last one in the queue
thisq.cumulative = 0;
this.cumulative = 0;
};
}

if (op === "loop" && tasks.length === 0) {
// When transitioning from a "delay" to a "loop", allow
// the loop to iterate the amount of time given,
// but still start at the correct offset.
ref = exportable.delay(this.cumulative - item[op], function() {
ref = exportable.delay(this.cumulative - item[op], () => {
ref = exportable.loop(item[op], item.task);

refs.push(ref);
this.refs.push(ref);
});
} else {
ref = exportable[op](this.cumulative, item.task);
}

refs.push(ref);
this.refs.push(ref);
}
};

@@ -172,19 +161,18 @@ function processQueue() {

if (entries.length) {

// console.log( now, entries );
// console.log(now, entries);
// console.log( entries );
while (entries.length) {
// Shift the entry out of the current list
temporald = entries.shift();
entry = priv.get(temporald);
entry = entries.shift();

// Execute the entry's callback, with
// "entry" as first arg
if (entry.isRunnable) {
temporald.called++;
temporald.calledAt = now;
entry.task.call(temporald, temporald);
entry.called++;
entry.calledAt = now;
entry.task.call(entry, entry);
}

// Additional "loop" handling
@@ -202,8 +190,8 @@ function processQueue() {
}

if (entry.isRunnable) {
// Push the entry into the cue
queue[entry.later].push(temporald);
// Push the entry into the queue
queue[entry.later].push(entry);
}
}
}

0 comments on commit ff348b9

Please sign in to comment.