action-executor
Advanced tools
Comparing version
@@ -1,21 +0,8 @@ | ||
(function (root, factory) { | ||
if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
} else if (typeof define === 'function' && define.amd) { | ||
define(factory); | ||
} else { | ||
root.com = root.com || {}; | ||
root.com.one = root.com.one || {}; | ||
root.com.one.ActionCancelledError = factory(); | ||
} | ||
}(this, function () { | ||
/** | ||
* When executing an action recieving this error | ||
* indicates that the action was cancelled. | ||
*/ | ||
function ActionCancelledError() { | ||
} | ||
/** | ||
* When executing an action recieving this error | ||
* indicates that the action was cancelled. | ||
*/ | ||
function ActionCancelledError() {} | ||
ActionCancelledError.prototype = new Error(); | ||
ActionCancelledError.prototype = new Error(); | ||
return ActionCancelledError; | ||
})); | ||
module.exports = ActionCancelledError; |
@@ -1,147 +0,147 @@ | ||
(function (root, factory) { | ||
if (typeof exports === 'object') { | ||
module.exports = factory(require('./ActionNotReadyError')); | ||
} else if (typeof define === 'function' && define.amd) { | ||
define(['./ActionNotReadyError'], factory); | ||
} else { | ||
root.com = root.com || {}; | ||
root.com.one = root.com.one || {}; | ||
root.com.one.ActionExecutor = factory(root.com.one.ActionNotReadyError); | ||
} | ||
}(this, function (ActionNotReadyError) { | ||
function ActionExecutor(options) { | ||
var that = this; | ||
var ActionCancelledError = require("./ActionCancelledError"); | ||
var ActionNotReadyError = require("./ActionNotReadyError"); | ||
that.onEmptyQueue = options.onEmptyQueue; | ||
that.onStatusChange = options.onStatusChange; | ||
that.shouldRetryOnError = options.shouldRetryOnError; | ||
that.interceptor = options.interceptor; | ||
that.context = options.context; | ||
function ActionExecutor(options) { | ||
var that = this; | ||
that.queue = []; | ||
} | ||
that.onEmptyQueue = options.onEmptyQueue; | ||
that.onStatusChange = options.onStatusChange; | ||
that.shouldRetryOnError = options.shouldRetryOnError; | ||
that.interceptor = options.interceptor; | ||
that.context = options.context; | ||
ActionExecutor.prototype.setTaskStatus = function (task, status) { | ||
task.status = status; | ||
if (this.onStatusChange) { | ||
this.onStatusChange.apply(this, arguments); | ||
} | ||
}; | ||
that.queue = []; | ||
} | ||
ActionExecutor.prototype.executeQueuedActions = function () { | ||
var that = this; | ||
// Remove finished tasks | ||
this.queue = this.queue.filter(function (task) { | ||
return task.status !== 'done' && task.status !== 'failed'; | ||
}); | ||
ActionExecutor.prototype.setTaskStatus = function(task, status) { | ||
task.status = status; | ||
if (this.onStatusChange) { | ||
this.onStatusChange.apply(this, arguments); | ||
} | ||
}; | ||
if (this.queue.length === 0 && this.onEmptyQueue) { | ||
this.onEmptyQueue(); | ||
} | ||
ActionExecutor.prototype.executeQueuedActions = function() { | ||
var that = this; | ||
// Remove finished tasks | ||
this.queue = this.queue.filter(function(task) { | ||
return task.status !== "done" && task.status !== "failed"; | ||
}); | ||
// Restart tasks that yielded | ||
this.queue.filter(function (task) { | ||
return task.status === 'not ready' || task.status === 'retrying'; | ||
}).forEach(function (task) { | ||
that.execute(task); | ||
}); | ||
}; | ||
if (this.queue.length === 0 && this.onEmptyQueue) { | ||
this.onEmptyQueue(); | ||
} | ||
ActionExecutor.prototype.shouldRetry = function (task, err) { | ||
return err && typeof task.retries === 'number' && task.retries > 0 && | ||
this.shouldRetryOnError && this.shouldRetryOnError(err); | ||
}; | ||
// Restart tasks that yielded | ||
this.queue | ||
.filter(function(task) { | ||
return task.status === "not ready" || task.status === "retrying"; | ||
}) | ||
.forEach(function(task) { | ||
that.execute(task); | ||
}); | ||
}; | ||
ActionExecutor.prototype.intercept = function (task, args, cb) { | ||
if (this.interceptor) { | ||
this.interceptor.call(null, task.action, args, cb); | ||
} else { | ||
cb(); | ||
} | ||
}; | ||
ActionExecutor.prototype.shouldRetry = function(task, err) { | ||
return ( | ||
err && | ||
typeof task.retries === "number" && | ||
task.retries > 0 && | ||
this.shouldRetryOnError && | ||
this.shouldRetryOnError(err) | ||
); | ||
}; | ||
ActionExecutor.prototype.callTaskCallback = function (task, args, cb) { | ||
this.intercept(task, args, function () { | ||
if (task.callback) { | ||
task.callback.apply(null, args); | ||
} | ||
cb(); | ||
}); | ||
}; | ||
ActionExecutor.prototype.intercept = function(task, args, cb) { | ||
if (this.interceptor) { | ||
this.interceptor.call(null, task.action, args, cb); | ||
} else { | ||
cb(); | ||
} | ||
}; | ||
function fib(n) { | ||
var i; | ||
var fibTable = []; | ||
fibTable[0] = 0; | ||
fibTable[1] = 1; | ||
for (i = 2; i <= n; i += 1) { | ||
fibTable[i] = fibTable[i - 2] + fibTable[i - 1]; | ||
} | ||
return fibTable[n]; | ||
ActionExecutor.prototype.callTaskCallback = function(task, args, cb) { | ||
this.intercept(task, args, function() { | ||
if (task.callback) { | ||
task.callback.apply(null, args); | ||
} | ||
cb(); | ||
}); | ||
}; | ||
function fib(n) { | ||
var i; | ||
var fibTable = []; | ||
ActionExecutor.prototype.queueForRetryTask = function (task, err) { | ||
var that = this; | ||
var retriesLeft = task.action.retries - task.retries; | ||
var timeout = fib(retriesLeft + 14); | ||
this.setTaskStatus(task, 'queued for retrying', err); | ||
task.retries -= 1; | ||
setTimeout(function () { | ||
that.setTaskStatus(task, 'retrying'); | ||
that.executeQueuedActions(); | ||
}, timeout); | ||
}; | ||
fibTable[0] = 0; | ||
fibTable[1] = 1; | ||
for (i = 2; i <= n; i += 1) { | ||
fibTable[i] = fibTable[i - 2] + fibTable[i - 1]; | ||
} | ||
return fibTable[n]; | ||
} | ||
ActionExecutor.prototype.execute = function (task) { | ||
var that = this; | ||
this.setTaskStatus(task, 'running'); | ||
ActionExecutor.prototype.queueForRetryTask = function(task, err) { | ||
var that = this; | ||
var retriesLeft = task.action.retries - task.retries; | ||
var timeout = fib(retriesLeft + 14); | ||
this.setTaskStatus(task, "queued for retrying", err); | ||
task.retries -= 1; | ||
setTimeout(function() { | ||
that.setTaskStatus(task, "retrying"); | ||
that.executeQueuedActions(); | ||
}, timeout); | ||
}; | ||
task.action.execute(this.context, function (err) { | ||
var args = Array.prototype.slice.call(arguments); | ||
if (err instanceof ActionNotReadyError) { | ||
that.setTaskStatus(task, 'not ready'); | ||
} else if (that.shouldRetry(task, err)) { | ||
that.intercept(task, args, function () { | ||
that.queueForRetryTask(task, err); | ||
that.executeQueuedActions(); | ||
}); | ||
} else { | ||
that.callTaskCallback(task, args, function () { | ||
if (err) { | ||
that.setTaskStatus(task, 'failed', err); | ||
} else { | ||
that.setTaskStatus(task, 'done'); | ||
} | ||
ActionExecutor.prototype.execute = function(task) { | ||
var that = this; | ||
this.setTaskStatus(task, "running"); | ||
that.executeQueuedActions(); | ||
}); | ||
} | ||
}); | ||
}; | ||
ActionExecutor.prototype.validateAction = function (action) { | ||
if (action === null || typeof action !== 'object') { | ||
throw new Error('Expect actions to be objects. ' + action); | ||
task.action.execute(this.context, function(err) { | ||
var args = Array.prototype.slice.call(arguments); | ||
if (err instanceof ActionNotReadyError) { | ||
that.setTaskStatus(task, "not ready"); | ||
} else if (that.shouldRetry(task, err)) { | ||
that.intercept(task, args, function() { | ||
that.queueForRetryTask(task, err); | ||
that.executeQueuedActions(); | ||
}); | ||
} else { | ||
that.callTaskCallback(task, args, function() { | ||
if (err) { | ||
that.setTaskStatus(task, "failed", err); | ||
} else { | ||
that.setTaskStatus(task, "done"); | ||
} | ||
var name = action.name; | ||
if (typeof name !== 'string' || name.length === 0) { | ||
throw new Error('Expect actions to have an unique name. ' + action); | ||
} | ||
that.executeQueuedActions(); | ||
}); | ||
} | ||
}); | ||
}; | ||
if (typeof action.execute !== 'function') { | ||
throw new Error('Expect actions to have an execute method. ' + action.name); | ||
} | ||
}; | ||
ActionExecutor.prototype.validateAction = function(action) { | ||
if (action === null || typeof action !== "object") { | ||
throw new Error("Expect actions to be objects. " + action); | ||
} | ||
ActionExecutor.prototype.enqueue = function (action, cb) { | ||
this.validateAction(action); | ||
var task = { action: action, callback: cb, retries: action.retries }; | ||
this.queue.push(task); | ||
this.execute(task); | ||
}; | ||
var name = action.name; | ||
if (typeof name !== "string" || name.length === 0) { | ||
throw new Error("Expect actions to have an unique name. " + action); | ||
} | ||
return ActionExecutor; | ||
})); | ||
if (typeof action.execute !== "function") { | ||
throw new Error("Expect actions to have an execute method. " + action.name); | ||
} | ||
}; | ||
ActionExecutor.prototype.enqueue = function(action, cb) { | ||
this.validateAction(action); | ||
var task = { action: action, callback: cb, retries: action.retries }; | ||
this.queue.push(task); | ||
this.execute(task); | ||
}; | ||
ActionExecutor.ActionCancelledError = ActionCancelledError; | ||
ActionExecutor.ActionNotReadyError = ActionNotReadyError; | ||
module.exports = ActionExecutor; |
@@ -1,22 +0,11 @@ | ||
(function (root, factory) { | ||
if (typeof exports === 'object') { | ||
module.exports = factory(); | ||
} else if (typeof define === 'function' && define.amd) { | ||
define(factory); | ||
} else { | ||
root.com = root.com || {}; | ||
root.com.one = root.com.one || {}; | ||
root.com.one.ActionNotReadyError = factory(); | ||
} | ||
}(this, function () { | ||
/** | ||
* When executing an action recieving this error | ||
* indicates that the action is not able to get | ||
* a lock on all the objects it is interacting | ||
* with. Executing the action again will always | ||
* safe. | ||
*/ | ||
function ActionNotReadyError() { } | ||
ActionNotReadyError.prototype = new Error(); | ||
return ActionNotReadyError; | ||
})); | ||
/** | ||
* When executing an action recieving this error | ||
* indicates that the action is not able to get | ||
* a lock on all the objects it is interacting | ||
* with. Executing the action again will always | ||
* safe. | ||
*/ | ||
function ActionNotReadyError() {} | ||
ActionNotReadyError.prototype = new Error(); | ||
module.exports = ActionNotReadyError; |
{ | ||
"name": "action-executor", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"main": "lib/ActionExecutor.js", | ||
"files": [ | ||
"lib", | ||
"dist" | ||
], | ||
"devDependencies": { | ||
"mocha": "=1.12.0", | ||
"phantomjs": "=1.9.1-0", | ||
"mocha-phantomjs": "=3.1.0", | ||
"unexpected": "=3.1.5", | ||
"unexpected-sinon": "=3.0.1", | ||
"eslint": "^5.16.0", | ||
"eslint-config-pretty-standard": "^2.0.1", | ||
"eslint-plugin-import": "^2.9.0", | ||
"eslint-plugin-prettier": "^2.6.0", | ||
"mocha": "^6.2.0", | ||
"mocha-chrome": "^2.0.0", | ||
"nyc": "^13.3.0", | ||
"prettier": "~1.17.1", | ||
"rollup": "^1.21.2", | ||
"rollup-plugin-commonjs": "^10.1.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-terser": "^5.1.1", | ||
"sinon": "=1.9.1", | ||
"json2htmlcov": "*" | ||
"unexpected": "=10.40.2", | ||
"unexpected-sinon": "=10.11.2" | ||
}, | ||
"scripts": { | ||
"test": "make test" | ||
"build": "rollup -c", | ||
"coverage": "nyc --reporter lcov --reporter text npm test", | ||
"lint": "eslint .", | ||
"prepublishOnly": "npm run build", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test:browser": "npm run build && make test", | ||
"test:node": "mocha" | ||
}, | ||
@@ -16,0 +35,0 @@ "license": "BSD-3-Clause", |
@@ -298,2 +298,2 @@ # ActionExecutor | ||
Failboat is licensed under the BSD 3-clause license, as given at http://opensource.org/licenses/BSD-3-Clause | ||
ActionExecutor is licensed under the BSD 3-clause license, as given at http://opensource.org/licenses/BSD-3-Clause |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
21640
-36.42%15
114.29%7
-22.22%299
-37.84%2
Infinity%