Skip to content

Commit

Permalink
Fixed issue where the timeout is cleared before being set
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao Ribeiro committed Mar 19, 2015
1 parent 64b9e77 commit d53ecb3
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 21 deletions.
3 changes: 1 addition & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"trailing": true,
"maxparams": 4,
"maxdepth": 2,
"maxstatements": 6,
"maxcomplexity": 2,
"maxstatements": 8,
"maxlen": 120,
"node": true,
"globals": {
Expand Down
72 changes: 55 additions & 17 deletions lib/saferAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*
*/
(function saferAsync() {

'use strict';

/**
Expand Down Expand Up @@ -40,6 +41,9 @@
* @property {Function} timeoutCB - called if a given task timesout
*/
options: {

// ignoring the unused params for the function signatures until docs are written
/* jshint ignore:start */

/**
* Function to be called when a task starts. If a
Expand Down Expand Up @@ -88,6 +92,8 @@
* @returns {void}
*/
timeoutCB: function timeoutCB (task, wrappedCB) {}

/* jshint ignore:end */
},

/**
Expand Down Expand Up @@ -121,7 +127,22 @@
var task = internals.createTaskObject(options, taskArr, key);

taskArr[taskArr.length - 1] = internals.wrapTask(task);
tasks[key] = taskArr.length > 1 ? taskArr : taskArr[0];

tasks[key] = internals.normaliseTaskArray(taskArr, key, tasks);
},

/**
* Checks if the original definition was in function or array format and returns it in it's original format
*
* @param {Array} taskArr task definition in array notation
*
* @return {Mixed} array or function
*
* @private
*/
normaliseTaskArray: function normaliseTaskArray (taskArr) {

return taskArr.length > 1 ? taskArr : taskArr[0];
},

/**
Expand Down Expand Up @@ -163,7 +184,6 @@
* @returns {Function}
*/
wrapTask: function wrapTask (task) {
var counter = 0;

/**
* Function which will replace the original task function. It is responsible for creating another function
Expand All @@ -177,13 +197,6 @@
*/
return function wrappedTask (callback, results) {

if (counter === 1) {
internals.multipleFunctionCallsHandler(task, {}, results);
return;
}

counter += 1;

// call the start callback and save it's return value which will be passed to the endTaskCB
task.startObject = task.options.startTaskCB({
name: task.name,
Expand All @@ -192,8 +205,8 @@

var wrappedCB = internals.wrapCB(task, callback);

task.fn(wrappedCB, results);
internals.bindCallbackTimeout(task, wrappedCB);
task.fn(wrappedCB, results);
};
},

Expand All @@ -219,24 +232,41 @@
*/
return function wrappedCallback (error, params) {

if (counter === 1) {
internals.multipleFunctionCallsHandler(task, error, params);
if (!internals.isValidWrapperCall(task, counter, error, params)) {
return;
}

clearTimeout(task.timeout);
counter += 1;

// call the end callback
task.options.endTaskCB({
name: task.name,
deps: task.deps
}, task.startObject, arguments);

clearTimeout(task.timeout);
callback.apply(this, arguments);

task = null;
};
},

isValidWrapperCall: function (task, counter, error, params) {


if (!task || task.timedOut === true ) {

return false;
}

if (counter === 1) {

internals.multipleFunctionCallsHandler(task, error, params);
return false;
}

return true;
},

/**
* Binds the task timeout
*
Expand Down Expand Up @@ -269,7 +299,11 @@
timeoutHandler: function timeoutHandler (task, wrappedCB) {

clearTimeout(task.timeout);

task.options.timeoutCB(task, wrappedCB);
wrappedCB = function () {};

task.timedOut = true;
},

/**
Expand All @@ -281,7 +315,10 @@
*/
multipleFunctionCallsHandler: function (task, error, params) {

var errorObj = new Error('Error in task: ' + task.name + ' - Callback called multiple times. Aborting');
var errorObj = new Error(
'Error in task: ' + task.name + ' - Callback called multiple times. Aborting',
task
);
internals.options.errorCB(errorObj, error, params);
}
};
Expand All @@ -297,7 +334,8 @@
*/
defaults: function defaults (options) {

return internals.options = libs._.extend({}, internals.options, options);
internals.options = libs._.extend({}, internals.options, options);
return internals.options;
},

/**
Expand Down Expand Up @@ -335,7 +373,7 @@
* @param error
* @param result
*/
var finish = function (callback, domain, error, result) {
var finish = function (callback, domain) {

if (libs._.isFunction(callback)) {

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "safer-async",
"version": "0.0.4",
"version": "0.0.5",
"description": "Provides better error handling and profiling for the async library",
"main": "./lib/saferAsync",
"scripts": {
"test": "nodeunit test/saferAsync.js",
"lint": "jshint test/saferAsync.js"
"lint": "jshint lib/saferAsync.js"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit d53ecb3

Please sign in to comment.