diff --git a/breathe.js b/breathe.js index 7cdef74..86e15d1 100644 --- a/breathe.js +++ b/breathe.js @@ -199,7 +199,7 @@ **********************/ var breathe = { - version: '0.2.0' + version: '0.2.1' }; /********************** @@ -361,6 +361,26 @@ } }; + var customHandlers = {}; + breathe.on = function (type, fn) { + customHandlers[type] = customHandlers[type] || []; + customHandlers[type].push(fn); + }; + var trigger = function (type, data) { + var i; + var handlers = customHandlers[type]; + if (!handlers) { + return; + } + for (i=0; i < handlers.length; i++) { + if (data) { + handlers[i].apply(this, data); + } else { + handlers[i](); + } + } + }; + /********************** * Main loop **********************/ @@ -373,10 +393,10 @@ var doSomeWork = function () { var start = timer.now(); var ind; - var item; var id; var throttleCount = {}; _inMainLoop = true; + trigger('batchBegin'); if (_workTimeouter.prework) { _workTimeoutRef = _workTimeouter.prework(doSomeWork); } @@ -425,6 +445,7 @@ _workTimeoutRef = _workTimeouter.postwork(doSomeWork); } _currWorkId = GENERAL_WORK_ID; + trigger('batchEnd'); _inMainLoop = false; }; // Start the main loop, even though there are no items in any of the queues @@ -582,9 +603,6 @@ var id = _currWorkId; var currVal; var stateHandler = new StateHandler(); - var callGate; - var pauser; - var pauseGate; var atEnd = function() { return endPromise.state === promiseStates.resolved @@ -659,9 +677,6 @@ var condition = config.condition; var currVal = config.initVal; var stateHandler = new StateHandler(); - var callGate; - var pauseGate; - var pauser; var retLoop = breatheChain(function (resolve, reject) { var workBit; var preworkBit; diff --git a/index.js b/index.js new file mode 100644 index 0000000..300d7ae --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require('./breathe.js'); \ No newline at end of file diff --git a/package.json b/package.json index c1f4a04..10c87dd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "breathejs", - "version": "0.2.0", + "version": "0.2.1", "description": "a library to write nonblocking, asynchronous JavaScript", "license": "BSD-3-Clause", "homepage": "https://breathejs.org", diff --git a/test/basic.js b/test/basic.js index 7403b06..53c2a2c 100644 --- a/test/basic.js +++ b/test/basic.js @@ -37,9 +37,11 @@ var blockingRecursiveChain = function (remaining, fn) { var blockingChain = function (time) { return breathe.chain(function (resolve, reject) { + var i=0; var start = getMilliseconds(); while (getMilliseconds() - start < time) { // wait + i++; } resolve(); }); @@ -218,7 +220,7 @@ describe('`breathe`', function () { it('should not immediately run', function () { - var i, c = initValue; + var i = 0, c = initValue; var loop = breathe.loop(function () { return i < testIterations; }, function () { @@ -258,7 +260,7 @@ describe('`breathe`', function () { var v = initValue; var i = 0; var ret = breathe.loop(function () { - return i < 100; + return i < 200; }, function () { i++; return blockingChain(1); @@ -276,7 +278,7 @@ describe('`breathe`', function () { var v = initValue; var i = 0; var ret = breathe.loop(function () { - return i < 100; + return i < 200; }, function () { i++; return blockingChain(1);