@@ -688,81 +688,81 @@ a diff reading, useful for benchmarks and measuring intervals:
688688The ` AsyncListener ` API is the JavaScript interface for the ` AsyncWrap `
689689class which allows developers to be notified about key events in the
690690lifetime of an asynchronous event. Node performs a lot of asynchronous
691- events internally, and significant use of this API will have a ** dramatic
692- performance impact** on your application.
691+ events internally, and significant use of this API may have a
692+ ** significant performance impact** on your application.
693693
694694
695- ## process.createAsyncListener(asyncListener [ , callbacksObj[ , storageValue ] ] )
695+ ## process.createAsyncListener(callbacksObj[ , userData ] )
696696
697- * ` asyncListener ` {Function} callback fired when an asynchronous event is
698- instantiated.
699- * ` callbacksObj ` {Object} optional callbacks that will fire at specific
700- times in the lifetime of the asynchronous event.
701- * ` storageValue ` {Value} a value that will be passed as the first argument
702- when the ` asyncListener ` callback is run, and to all subsequent callback.
697+ * ` callbacksObj ` {Object} Contains optional callbacks that will fire at
698+ specific times in the life cycle of the asynchronous event.
699+ * ` userData ` {Value} a value that will be passed to all callbacks.
703700
704701Returns a constructed ` AsyncListener ` object.
705702
706- To begin capturing asynchronous events pass the object to
707- [ ` process.addAsyncListener() ` ] [ ] . The same ` AsyncListener ` instance can
708- only be added once to the active queue, and subsequent attempts to add the
709- instance will be ignored.
703+ To begin capturing asynchronous events pass either the ` callbacksObj ` or
704+ and existing ` AsyncListener ` instance to [ ` process.addAsyncListener() ` ] [ ] .
705+ The same ` AsyncListener ` instance can only be added once to the active
706+ queue, and subsequent attempts to add the instance will be ignored.
710707
711- To stop capturing pass the object to [ ` process.removeAsyncListener() ` ] [ ] .
712- This does _ not_ mean the ` AsyncListener ` previously added will stop
713- triggering callbacks. Once attached to an asynchronous event it will
714- persist with the lifetime of the asynchronous call stack.
708+ To stop capturing pass the ` AsyncListener ` instance to
709+ [ ` process.removeAsyncListener() ` ] [ ] . This does _ not_ mean the
710+ ` AsyncListener ` previously added will stop triggering callbacks. Once
711+ attached to an asynchronous event it will persist with the lifetime of the
712+ asynchronous call stack.
715713
716714Explanation of function parameters:
717715
718- ` asyncListener(storageValue) ` : A ` Function ` called when an asynchronous
716+
717+ ` callbacksObj ` : An ` Object ` which may contain three optional fields:
718+
719+ * ` create(userData) ` : A ` Function ` called when an asynchronous
719720event is instantiated. If a ` Value ` is returned then it will be attached
720721to the event and overwrite any value that had been passed to
721- ` process.createAsyncListener() ` 's ` storageValue ` argument. If an initial
722- ` storageValue ` was passed when created, then ` asyncListener ()` will
722+ ` process.createAsyncListener() ` 's ` userData ` argument. If an initial
723+ ` userData ` was passed when created, then ` create ()` will
723724receive that as a function argument.
724725
725- ` callbacksObj ` : An ` Object ` which may contain three optional fields:
726-
727- * ` before(context, storageValue) ` : A ` Function ` that is called immediately
726+ * ` before(context, userData) ` : A ` Function ` that is called immediately
728727before the asynchronous callback is about to run. It will be passed both
729- the ` context ` (i.e. ` this ` ) of the calling function and the ` storageValue `
730- either returned from ` asyncListener ` or passed during construction (if
728+ the ` context ` (i.e. ` this ` ) of the calling function and the ` userData `
729+ either returned from ` create() ` or passed during construction (if
731730either occurred).
732731
733- * ` after(context, storageValue ) ` : A ` Function ` called immediately after
732+ * ` after(context, userData ) ` : A ` Function ` called immediately after
734733the asynchronous event's callback has run. Note this will not be called
735734if the callback throws and the error is not handled.
736735
737- * ` error(storageValue , error) ` : A ` Function ` called if the event's
738- callback threw. If ` error ` returns ` true ` then Node will assume the error
739- has been properly handled and resume execution normally. When multiple
740- ` error() ` callbacks have been registered, only ** one** of those callbacks
741- needs to return ` true ` for ` AsyncListener ` to accept that the error has
742- been handled.
736+ * ` error(userData , error) ` : A ` Function ` called if the event's
737+ callback threw. If this registered callback returns ` true ` then Node will
738+ assume the error has been properly handled and resume execution normally.
739+ When multiple ` error() ` callbacks have been registered only ** one** of
740+ those callbacks needs to return ` true ` for ` AsyncListener ` to accept that
741+ the error has been handled, but all ` error() ` callbacks will always be run .
743742
744- ` storageValue ` : A ` Value ` (i.e. anything) that will be, by default,
743+ ` userData ` : A ` Value ` (i.e. anything) that will be, by default,
745744attached to all new event instances. This will be overwritten if a ` Value `
746- is returned by ` asyncListener ()` .
745+ is returned by ` create ()` .
747746
748- Here is an example of overwriting the ` storageValue ` :
747+ Here is an example of overwriting the ` userData ` :
749748
750- process.createAsyncListener(function listener(value) {
751- // value === true
752- return false;
749+ process.createAsyncListener({
750+ create: function listener(value) {
751+ // value === true
752+ return false;
753753 }, {
754754 before: function before(context, value) {
755755 // value === false
756756 }
757757 }, true);
758758
759759** Note:** The [ EventEmitter] [ ] , while used to emit status of an asynchronous
760- event, is not itself asynchronous. So ` asyncListener ()` will not fire when
760+ event, is not itself asynchronous. So ` create ()` will not fire when
761761an event is added, and ` before ` /` after ` will not fire when emitted
762762callbacks are called.
763763
764764
765- ## process.addAsyncListener(asyncListener [ , callbacksObj[ , storageValue ] ] )
765+ ## process.addAsyncListener(callbacksObj[ , userData ] )
766766## process.addAsyncListener(asyncListener)
767767
768768Returns a constructed ` AsyncListener ` object and immediately adds it to
@@ -774,36 +774,32 @@ object.
774774
775775Example usage for capturing errors:
776776
777+ var fs = require('fs');
778+
777779 var cntr = 0;
778- var key = process.addAsyncListener(function() {
779- return { uid: cntr++ };
780- }, {
780+ var key = process.addAsyncListener({
781+ create: function onCreate() {
782+ return { uid: cntr++ };
783+ },
781784 before: function onBefore(context, storage) {
782- // Need to remove the listener while logging or will end up
783- // with an infinite call loop.
784- process.removeAsyncListener(key);
785- console.log('uid: %s is about to run', storage.uid);
786- process.addAsyncListener(key);
785+ // Write directly to stdout or we'll enter a recursive loop
786+ fs.writeSync(1, 'uid: ' + storage.uid + ' is about to run\n');
787787 },
788788 after: function onAfter(context, storage) {
789- process.removeAsyncListener(key);
790- console.log('uid: %s is about to run', storage.uid);
791- process.addAsyncListener(key);
789+ fs.writeSync(1, 'uid: ' + storage.uid + ' is about to run\n');
792790 },
793791 error: function onError(storage, err) {
794792 // Handle known errors
795- if (err.message === 'really, it\'s ok') {
796- process.removeAsyncListener(key);
797- console.log('handled error just threw:');
798- console.log(err.stack);
799- process.addAsyncListener(key);
793+ if (err.message === 'everything is fine') {
794+ fs.writeSync(1, 'handled error just threw:\n');
795+ fs.writeSync(1, err.stack + '\n');
800796 return true;
801797 }
802798 }
803799 });
804800
805801 process.nextTick(function() {
806- throw new Error('really, it\'s ok ');
802+ throw new Error('everything is fine ');
807803 });
808804
809805 // Output:
@@ -820,16 +816,19 @@ Example usage for capturing errors:
820816
821817Removes the ` AsyncListener ` from the listening queue.
822818
823- Removing the ` AsyncListener ` from the queue does _ not_ mean asynchronous
824- events called during its execution scope will stop firing callbacks. Once
825- attached to an event it will persist for the entire asynchronous call
826- stack. For example:
819+ Removing the ` AsyncListener ` from the active queue does _ not_ mean the
820+ ` asyncListener ` callbacks will cease to fire on the events they've been
821+ registered. Subsequently, any asynchronous events fired during the
822+ execution of a callback will also have the same ` asyncListener ` callbacks
823+ attached for future execution. For example:
827824
828- var key = process.createAsyncListener(function asyncListener() {
829- // To log we must stop listening or we'll enter infinite recursion.
830- process.removeAsyncListener(key);
831- console.log('You summoned me?');
832- process.addAsyncListener(key);
825+ var fs = require('fs');
826+
827+ var key = process.createAsyncListener({
828+ create: function asyncListener() {
829+ // Write directly to stdout or we'll enter a recursive loop
830+ fs.writeSync(1, 'You summoned me?\n');
831+ }
833832 });
834833
835834 // We want to begin capturing async events some time in the future.
@@ -861,11 +860,13 @@ To stop capturing from a specific asynchronous event stack
861860` process.removeAsyncListener() ` must be called from within the call
862861stack itself. For example:
863862
864- var key = process.createAsyncListener(function asyncListener() {
865- // To log we must stop listening or we'll enter infinite recursion.
866- process.removeAsyncListener(key);
867- console.log('You summoned me?');
868- process.addAsyncListener(key);
863+ var fs = require('fs');
864+
865+ var key = process.createAsyncListener({
866+ create: function asyncListener() {
867+ // Write directly to stdout or we'll enter a recursive loop
868+ fs.writeSync(1, 'You summoned me?\n');
869+ }
869870 });
870871
871872 // We want to begin capturing async events some time in the future.
0 commit comments