From c01d14c88677fa88a04e1b9aeae97aa4446c8cc3 Mon Sep 17 00:00:00 2001 From: Chris Scribner Date: Thu, 4 Oct 2012 10:21:35 -0400 Subject: [PATCH] 2.1.6 Allow the current asyncblock's flow to be gotten from a function called from the current fiber. --- changelog | 28 ++++++++++++++++++++++++++++ lib/asyncblock.js | 12 +++++++++++- package.json | 2 +- tests/functionality.js | 19 +++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/changelog b/changelog index 2b69d0b..74bfa90 100644 --- a/changelog +++ b/changelog @@ -1,3 +1,31 @@ +2.1.6 + +* Allow the current asyncblock's flow to be gotten from a function called from the current fiber. + +2.1.5 + +* Allow asyncblock transform compilation to be kicked off independently. This allows a build process or otherwise to run the transformations beforehand. + +2.1.4 + +* Fix an instance of errors getting swallowed within nested asyncblocks. + +2.1.3 + +* Allow 2nd arg to be specified to an asyncblock, which acts as the return handler. + +2.1.2 + +* Fix an issue with deferred variables not getting handled correctly if assigned to an object property + +2.1.1 + +* Fix issue with asyncblock not working with Coffeescript + +2.1.0 + +* Update to fibers v0.6.8 for node v0.8 support. + 2.0.9 * x().sync() optimization - Reuse the fiber from the outer asyncblock in the inner asyncblock. Lower overhead and same behavior. diff --git a/lib/asyncblock.js b/lib/asyncblock.js index 02302f7..1aa2c6d 100644 --- a/lib/asyncblock.js +++ b/lib/asyncblock.js @@ -109,7 +109,7 @@ var asyncblock = function(fn, done, options) { }; module.exports = function(fn, done, options){ - //Capture stack trace by defaultrequire('./lib/transform.js'); + //Capture stack trace by default var err = new Error(); //Currently not capturing stack trace as it's about 60% slower than just making the error (and just takes 1 frame off stack trace) //Error.captureStackTrace(err, module.exports); @@ -147,3 +147,13 @@ module.exports.fullstack = module.exports; module.exports.nostack = function(fn, done){ asyncblock(fn, done); }; + +module.exports.getCurrentFlow = function(){ + var currFiber = Fiber.current; + if(currFiber){ + var currFlow = currFiber._asyncblock_flow; + currFiber = null; + + return currFlow; + } +}; diff --git a/package.json b/package.json index 904373d..7a0ca32 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asyncblock", - "version": "2.1.5", + "version": "2.1.6", "description": "A simple and powerful abstraction of node-fibers", "keywords": [ "fiber", "fibers", "coroutine", "stop", "go", "green", "red" ], diff --git a/tests/functionality.js b/tests/functionality.js index 9f3aa4a..c961158 100644 --- a/tests/functionality.js +++ b/tests/functionality.js @@ -1048,6 +1048,25 @@ suite.addBatch({ 'The results are as expected': function(err, result){ assert.equal(err.message, 'Error'); } + }, + + 'When using asyncblock.currentFlow': { + topic: function(){ + var testCurrent = function(){ + var flow = asyncblock.getCurrentFlow(); + + echo('test', flow.add()); + return flow.wait(); + }; + + asyncblock(function(){ + return testCurrent(); + }, this.callback); + }, + + 'Ok': function(result){ + assert.equal(result, 'test'); + } } });