async-listener
Advanced tools
Comparing version 0.1.2 to 0.2.0
42
glue.js
@@ -40,33 +40,29 @@ var wrap = require('shimmer').wrap; | ||
function runSetup(list, length) { | ||
var data = new Array(length); | ||
function asyncWrap(original, list, length) { | ||
// setup | ||
var data = []; | ||
for (var i = 0; i < length; ++i) { | ||
data[i] = list[i].listener.call(this); | ||
} | ||
return data; | ||
} | ||
function runBefore(data, list, length, context) { | ||
for (var i = 0; i < length; ++i) { | ||
var callbacks = list[i].callbacks; | ||
if (callbacks && callbacks.before) callbacks.before(context, data[i]); | ||
} | ||
} | ||
return function () { | ||
var i, callbacks, returned; | ||
function runAfter(data, list, length, context) { | ||
for (var i = 0; i < length; ++i) { | ||
var callbacks = list[i].callbacks; | ||
if (callbacks && callbacks.after) callbacks.after(context, data[i]); | ||
} | ||
} | ||
// call `before` | ||
for (i = 0; i < length; ++i) { | ||
callbacks = list[i].callbacks; | ||
if (callbacks && callbacks.before) callbacks.before(this, data[i]); | ||
} | ||
function normalWrap(original, list, length) { | ||
var data = runSetup(list, length); | ||
return function () { | ||
runBefore(data, list, length, this); | ||
try { | ||
return original.apply(this, arguments); | ||
// save returned to pass to `after` | ||
returned = original.apply(this, arguments); | ||
return returned; | ||
} | ||
finally { | ||
runAfter(data, list, length, this); | ||
// call `after` | ||
for (i = 0; i < length; ++i) { | ||
callbacks = list[i].callbacks; | ||
if (callbacks && callbacks.after) callbacks.after(this, data[i], returned); | ||
} | ||
} | ||
@@ -85,3 +81,3 @@ }; | ||
for (var i = 0; i < length; ++i) { | ||
if (list[i].callbacks) return normalWrap(original, list, length); | ||
if (list[i].callbacks) return asyncWrap(original, list, length); | ||
} | ||
@@ -88,0 +84,0 @@ |
{ | ||
"name": "async-listener", | ||
"version": "0.1.2", | ||
"description": "Polyfill exporting trevnorris's 0.11+ process.addAsynListener API.", | ||
"version": "0.2.0", | ||
"description": "Polyfill exporting trevnorris's 0.11+ asyncListener API.", | ||
"author": "Forrest L Norvell <ogd@aoaioxxysz.net>", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -22,5 +22,3 @@ # process.addAsyncListener polyfill | ||
// will be discarded. | ||
// The context (i.e. "this") of the calling method will be passed as the | ||
// first argument. | ||
function onAsync(context) { | ||
function onAsync() { | ||
return new Domain(); | ||
@@ -32,6 +30,8 @@ } | ||
// multiple are queued. | ||
// "context" is the "this" of the request object. | ||
var callbackObject = { | ||
before: function asyncBefore(domain) { | ||
before: function asyncBefore(context, domain) { | ||
}, | ||
after: function asyncAfter(domain) { | ||
// "returnValue" is the value returned from the callback. | ||
after: function asyncAfter(context, domain, returnValue) { | ||
}, | ||
@@ -42,6 +42,6 @@ // If this callback returns "true" then the error handler will assume | ||
// true, then Node will assume the error was properly handled. | ||
error: function asyncError(err, domain) { | ||
}, | ||
// Useful to cleanup any resources. | ||
done: function asyncDone(domain) { | ||
// This will not currently be passed the context (or "this") of the | ||
// callback that threw. A simple way of achieving this is currently | ||
// being investigated, and the feature will be added when one is found. | ||
error: function asyncError(domain, err) { | ||
} | ||
@@ -51,3 +51,3 @@ }; | ||
/** | ||
* process.addAsyncListener([callback][, object]); | ||
* process.addAsyncListener(callback[, object[, domain]]); | ||
* | ||
@@ -57,19 +57,9 @@ * Arguments: | ||
* callback - Function that will be run when an asynchronous job is | ||
* queued. The "context" argument is the "this" of the function | ||
* where the callback is being queued (e.g. EventEmitter instance). | ||
* Though this will not do much for callbacks passed to | ||
* process.nextTick(), since there's no associated context. | ||
* queued. | ||
* | ||
* object - Object with the optional callbacks set on: | ||
* before - Callback called just before the asynchronous callback | ||
* will be called. Passed will be any data that was returned | ||
* from the associated callback event. If no callback was | ||
* passed, then no data is sent. | ||
* will be called. | ||
* after - Callback called directly after the asynchronous callback. | ||
* Also passed is the data returned from the corresponding | ||
* callback event. | ||
* error - Callback called if there was an error. Arguments are the | ||
* Error object, and data. | ||
* done - Called when no other possible asynchronous callbacks could | ||
* be queued. | ||
* error - Callback called if there was an error. | ||
* | ||
@@ -83,2 +73,16 @@ * The returned key is an Object that serves as the unique key for the | ||
/** | ||
* process.createAsyncListener(callback[, object[, domain]]); | ||
* | ||
* Adding an async listener will immediately add it to the queue and | ||
* being listening for events. If you wish to create the listener in | ||
* advance, to say attach to the returned domain object, it's possible | ||
* to get the key and pass it to process.addAsyncListener() later. | ||
*/ | ||
var key = process.createAsyncListener(onAsync, callbackObject, domain); | ||
// Then activate like so: | ||
process.addAsyncListener(key); | ||
/** | ||
* process.removeAsyncListener(key); | ||
@@ -85,0 +89,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
90
31963
977