synchronize
Advanced tools
Comparing version 0.5.3 to 0.5.4
{ | ||
"name" : "synchronize", | ||
"main" : "./sync", | ||
"version" : "0.5.3", | ||
"version" : "0.5.4", | ||
"homepage" : "http://alexeypetrushin.github.com/synchronize", | ||
@@ -6,0 +6,0 @@ "dependencies" : { |
@@ -7,7 +7,8 @@ synchronize.js allows You write asynchronous code as if it's synchronous. | ||
Contributors | ||
Contributors: | ||
- [Alexey Petrushin](https://github.com/alexeypetrushin) | ||
- [d3m3vilurr](https://github.com/d3m3vilurr) | ||
- [James Manning](https://github.com/jamesmanning) | ||
Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license. |
54
sync.js
@@ -25,2 +25,3 @@ var Fiber = require('fibers') | ||
// Sometimes `Fiber` needed outside of `sync`. | ||
sync.Fiber = Fiber | ||
@@ -156,1 +157,54 @@ | ||
} | ||
// Same as `sync` but with verbose logging for every method invocation. | ||
// Ignore this method, it shouldn't be used unless you want to track down | ||
// tricky and complex bugs and need full information abouth how and when all | ||
// this async stuff has been called. | ||
var fiberIdCounter = 1 | ||
sync.syncWithDebug = function(){ | ||
if(arguments.length > 1){ | ||
// Synchronizing functions of object. | ||
var obj = arguments[0] | ||
for(var i = 1; i < arguments.length; i++){ | ||
(function(fname){ | ||
var fn = obj[fname] | ||
if(!fn) throw new Error("object doesn't have '" + fname + "' function!") | ||
var syncedFn = sync(fn) | ||
obj[fname] = function(){ | ||
if(Fiber.current && Fiber.current._fiberId == undefined){ | ||
Fiber.current._fiberId = fiberIdCounter | ||
fiberIdCounter = fiberIdCounter + 1 | ||
Fiber.current._callbackLevel = 0 | ||
} | ||
var fiberId = '-' | ||
if(Fiber.current){ | ||
fiberId = Fiber.current._fiberId | ||
Fiber.current._callbackLevel = Fiber.current._callbackLevel + 1 | ||
} | ||
var indent = ' ' | ||
if(Fiber.current) | ||
for(var j = 0; j < Fiber.current._callbackLevel; j++) indent = indent + ' ' | ||
console.log(fiberId + indent + this.constructor.name + '.' | ||
+ fname + " called", JSON.stringify(arguments)) | ||
var result | ||
try{ | ||
result = syncedFn.apply(this, arguments) | ||
}finally{ | ||
console.log(fiberId + indent + this.constructor.name + '.' | ||
+ fname + " finished") | ||
if(Fiber.current) | ||
Fiber.current._callbackLevel = Fiber.current._callbackLevel - 1 | ||
} | ||
return result | ||
} | ||
})(arguments[i]) | ||
} | ||
}else{ | ||
return sync.syncFn(arguments[0]) | ||
} | ||
} |
@@ -127,2 +127,10 @@ var sync = require('../sync') | ||
}) | ||
beforeEach(function(){ | ||
this.someKey = 'some value' | ||
}) | ||
it('should provide asyncIt helper for tests', sync.asyncIt(function(){ | ||
expect(Fiber.current).to.exist | ||
expect(this.someKey).to.eql('some value') | ||
})) | ||
}) |
185495
5789
13