Skip to content

Commit

Permalink
Optimize clearing by marking entry.isRunnable
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Oct 3, 2017
1 parent 0e2dd8c commit ebc9130
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 49 deletions.
5 changes: 4 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ module.exports = function(grunt) {
grunt.initConfig({
pkg: "<json:package.json>",
nodeunit: {
files: ["test/**/*.js"]
files: [
"test/common/bootstrap.js",
"test/**/*.js"
]
},
jshint: {
all: {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ temporal.delay(500, function() {

// Add a task with an id
temporal.delay({
id: "myTask",
time:500,
operation: function() {
task: function() {
console.log("500ms later...");
}),
id: "myTask"
});

// Selectively delete tasks
Expand Down
92 changes: 57 additions & 35 deletions lib/temporal.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var Emitter = require("events").EventEmitter;
var util = require("util");

Expand All @@ -17,9 +19,12 @@ var isProcessing = false;
// Queue emitters are stored privately in a Map to avoid using
// |this| alias patterns.
var priv = new Map();
var taskId = 0;

var tick = global.setImmediate || process.nextTick;
// Entries stored by id
var entriesById = new Map();

var tick = global.setImmediate || process.nextTick;
/**
* Task create a temporal task item
* @param {Object} entry Options for entry {time, task}
Expand All @@ -31,24 +36,25 @@ function Task(entry) {

this.called = 0;
this.now = this.calledAt = Date.now();

if (entry.id) {
this.id = entry.id;
}
this.id = entry.id || ++taskId;

priv.set(this, entry);

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


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

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

if (!entriesById.has(this.id)) {
entriesById.set(this.id, []);
}

entriesById.get(this.id).push(entry);
}

// Inherit EventEmitter API
Expand Down Expand Up @@ -231,33 +237,32 @@ function processQueue() {
}

["loop", "delay"].forEach(function(type) {
exportable[type] = function(time, operation) {

var opts;
exportable[type] = function(time, task) {
var options;

if (typeof time === "function") {
operation = time;
task = time;
time = 10;
}

if (typeof time === "object") {
opts = time;
opts.type = type;
options = time;
options.type = type;
} else {
opts = {
time: time,
type: type,
task: operation
options = {
time,
type,
task,
};
}

var task = new Task(opts);
var instance = new Task(options);

if (!isProcessing) {
processQueue();
}

return task;
return instance;
};
});

Expand All @@ -278,32 +283,49 @@ exportable.repeat = function(n, ms, callback) {
});
};

exportable.clear = function(id) {
exportable.clear = function(input) {

if (typeof id === "undefined") {
var id;

if (typeof input === "undefined") {
isProcessing = false;
queue = {};
} else {
queue = filterQueue(queue, id);
id = input instanceof Task ? input.id : input;

let entries = entriesById.get(id);

if (entries) {
for (let entry of entries) {
entry.isRunnable = false;
}
}
entriesById.delete(id);
}

if (Object.keys(queue).length === 0) {
exportable.removeAllListeners();
if (entriesById.size === 0) {
isProcessing = false;
queue = {};
}

if (isEmpty(queue)) {
exportable.removeAllListeners();
}
};

function filterQueue(queue, id) {
var filtered = {};
Object.keys(queue).forEach(function(key) {
queue[key] = queue[key].filter(function(entry) {
return entry.id !== id;
});
if (queue[key].length > 0) {
filtered[key] = queue[key];
}
});
return filtered;
function isEmpty(o) {
for (let p in o) {
return (p, false);
}
return true;
}

if (global.TEST_ENV) {
exportable.test = {
get entriesById() {
return entriesById;
},
};
}

module.exports = exportable;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"grunt-contrib-jshint": "latest",
"grunt-contrib-nodeunit": "latest",
"grunt-contrib-watch": "latest",
"grunt-jsbeautifier": "latest"
"grunt-jsbeautifier": "latest",
"sinon": "^4.0.0"
},
"keywords": [
"schedule",
Expand Down
3 changes: 3 additions & 0 deletions test/common/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use strict";

global.TEST_ENV = true;
Loading

0 comments on commit ebc9130

Please sign in to comment.