synchronize
Advanced tools
Comparing version 0.3.0 to 0.5.0
@@ -38,7 +38,7 @@ /* =================================================== | ||
if ( $.browser.webkit ) { | ||
transitionEnd = "webkitTransitionEnd" | ||
transitionEnd = "webkitTransitionEnd" | ||
} else if ( $.browser.mozilla ) { | ||
transitionEnd = "transitionend" | ||
transitionEnd = "transitionend" | ||
} else if ( $.browser.opera ) { | ||
transitionEnd = "oTransitionEnd" | ||
transitionEnd = "oTransitionEnd" | ||
} | ||
@@ -428,3 +428,3 @@ return transitionEnd | ||
var Collapse = function ( element, options ) { | ||
this.$element = $(element) | ||
this.$element = $(element) | ||
this.options = $.extend({}, $.fn.collapse.defaults, options) | ||
@@ -500,7 +500,7 @@ | ||
complete() | ||
} | ||
} | ||
, toggle: function () { | ||
this[this.$element.hasClass('in') ? 'hide' : 'show']() | ||
} | ||
} | ||
@@ -755,3 +755,3 @@ } | ||
function backdrop( callback ) { | ||
function backdrop( cb ) { | ||
var that = this | ||
@@ -775,4 +775,4 @@ , animate = this.$element.hasClass('fade') ? 'fade' : '' | ||
doAnimate ? | ||
this.$backdrop.one($.support.transition.end, callback) : | ||
callback() | ||
this.$backdrop.one($.support.transition.end, cb) : | ||
cb() | ||
@@ -786,4 +786,4 @@ } else if (!this.isShown && this.$backdrop) { | ||
} else if (callback) { | ||
callback() | ||
} else if (cb) { | ||
cb() | ||
} | ||
@@ -1400,5 +1400,5 @@ } | ||
, activate: function ( element, container, callback) { | ||
, activate: function ( element, container, cb) { | ||
var $active = container.find('> .active') | ||
, transition = callback | ||
, transition = cb | ||
&& $.support.transition | ||
@@ -1426,3 +1426,3 @@ && $active.hasClass('fade') | ||
callback && callback() | ||
cb && cb() | ||
} | ||
@@ -1429,0 +1429,0 @@ |
@@ -6,3 +6,3 @@ var q=null;window.PR_SHOULD_USE_CONTINUATION=!0; | ||
(j=+j.substring(1))&&j<=i&&(f[c]="\\"+d[i]);for(i=c=0;c<b;++c)"^"===f[c]&&"^"!==f[c+1]&&(f[c]="");if(a.ignoreCase&&s)for(c=0;c<b;++c)j=f[c],a=j.charAt(0),j.length>=2&&a==="["?f[c]=h(j):a!=="\\"&&(f[c]=j.replace(/[A-Za-z]/g,function(a){a=a.charCodeAt(0);return"["+String.fromCharCode(a&-33,a|32)+"]"}));return f.join("")}for(var t=0,s=!1,l=!1,p=0,d=a.length;p<d;++p){var g=a[p];if(g.ignoreCase)l=!0;else if(/[a-z]/i.test(g.source.replace(/\\u[\da-f]{4}|\\x[\da-f]{2}|\\[^UXux]/gi,""))){s=!0;l=!1;break}}for(var r= | ||
{b:8,t:9,n:10,v:11,f:12,r:13},n=[],p=0,d=a.length;p<d;++p){g=a[p];if(g.global||g.multiline)throw Error(""+g);n.push("(?:"+y(g)+")")}return RegExp(n.join("|"),l?"gi":"g")}function M(a){function m(a){switch(a.nodeType){case 1:if(e.test(a.className))break;for(var g=a.firstChild;g;g=g.nextSibling)m(g);g=a.nodeName;if("BR"===g||"LI"===g)h[s]="\n",t[s<<1]=y++,t[s++<<1|1]=a;break;case 3:case 4:g=a.nodeValue,g.length&&(g=p?g.replace(/\r\n?/g,"\n"):g.replace(/[\t\n\r ]+/g," "),h[s]=g,t[s<<1]=y,y+=g.length, | ||
{b:8,t:9,n:10,v:11,f:12,r:13},n=[],p=0,d=a.length;p<d;++p){g=a[p];if(g.global||g.multiline)throw Error(""+g);n.push("(?:"+y(g)+")")}return RegExp(n.join("|"),l?"gi":"g")}function M(a){function m(a){switch(a.nodeType){case 1:if(e.test(a.className))break;for(var g=a.firstChild;g;g=g.nextSibling)m(g);g=a.nodeName;if("BR"===g||"LI"===g)h[s]="\n",t[s<<1]=y++,t[s++<<1|1]=a;break;case 3:case 4:g=a.nodeValue,g.length&&(g=p?g.replace(/\r\n?/g,"\n"):g.replace(/[ \n\r ]+/g," "),h[s]=g,t[s<<1]=y,y+=g.length, | ||
t[s++<<1|1]=a)}}var e=/(?:^|\s)nocode(?:\s|$)/,h=[],y=0,t=[],s=0,l;a.currentStyle?l=a.currentStyle.whiteSpace:window.getComputedStyle&&(l=document.defaultView.getComputedStyle(a,q).getPropertyValue("white-space"));var p=l&&"pre"===l.substring(0,3);m(a);return{a:h.join("").replace(/\n$/,""),c:t}}function B(a,m,e,h){m&&(a={a:m,d:a},e(a),h.push.apply(h,a.e))}function x(a,m){function e(a){for(var l=a.d,p=[l,"pln"],d=0,g=a.a.match(y)||[],r={},n=0,z=g.length;n<z;++n){var f=g[n],b=r[f],o=void 0,c;if(typeof b=== | ||
@@ -13,3 +13,3 @@ "string")c=!1;else{var i=h[f.charAt(0)];if(i)o=f.match(i[1]),b=i[0];else{for(c=0;c<t;++c)if(i=m[c],o=f.match(i[1])){b=i[0];break}o||(b="pln")}if((c=b.length>=5&&"lang-"===b.substring(0,5))&&!(o&&typeof o[1]==="string"))c=!1,b="src";c||(r[f]=b)}i=d;d+=f.length;if(c){c=o[1];var j=f.indexOf(c),k=j+c.length;o[2]&&(k=f.length-o[2].length,j=k-c.length);b=b.substring(5);B(l+i,f.substring(0,j),e,p);B(l+i+j,c,C(b,c),p);B(l+i+k,f.substring(k),e,p)}else p.push(l+i,b)}a.e=p}var h={},y;(function(){for(var e=a.concat(m), | ||
q,"#"]));a.cStyleComments&&(e.push(["com",/^\/\/[^\n\r]*/,q]),e.push(["com",/^\/\*[\S\s]*?(?:\*\/|$)/,q]));a.regexLiterals&&e.push(["lang-regex",/^(?:^^\.?|[!+-]|!=|!==|#|%|%=|&|&&|&&=|&=|\(|\*|\*=|\+=|,|-=|->|\/|\/=|:|::|;|<|<<|<<=|<=|=|==|===|>|>=|>>|>>=|>>>|>>>=|[?@[^]|\^=|\^\^|\^\^=|{|\||\|=|\|\||\|\|=|~|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\s*(\/(?=[^*/])(?:[^/[\\]|\\[\S\s]|\[(?:[^\\\]]|\\[\S\s])*(?:]|$))+\/)/]);(h=a.types)&&e.push(["typ",h]);a=(""+a.keywords).replace(/^ | $/g, | ||
"");a.length&&e.push(["kwd",RegExp("^(?:"+a.replace(/[\s,]+/g,"|")+")\\b"),q]);m.push(["pln",/^\s+/,q," \r\n\t\xa0"]);e.push(["lit",/^@[$_a-z][\w$@]*/i,q],["typ",/^(?:[@_]?[A-Z]+[a-z][\w$@]*|\w+_t\b)/,q],["pln",/^[$_a-z][\w$@]*/i,q],["lit",/^(?:0x[\da-f]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+-]?\d+)?)[a-z]*/i,q,"0123456789"],["pln",/^\\[\S\s]?/,q],["pun",/^.[^\s\w"-$'./@\\`]*/,q]);return x(m,e)}function D(a,m){function e(a){switch(a.nodeType){case 1:if(k.test(a.className))break;if("BR"===a.nodeName)h(a), | ||
"");a.length&&e.push(["kwd",RegExp("^(?:"+a.replace(/[\s,]+/g,"|")+")\\b"),q]);m.push(["pln",/^\s+/,q," \r\n \xa0"]);e.push(["lit",/^@[$_a-z][\w$@]*/i,q],["typ",/^(?:[@_]?[A-Z]+[a-z][\w$@]*|\w+_t\b)/,q],["pln",/^[$_a-z][\w$@]*/i,q],["lit",/^(?:0x[\da-f]+|(?:\d(?:_\d+)*\d*(?:\.\d*)?|\.\d\+)(?:e[+-]?\d+)?)[a-z]*/i,q,"0123456789"],["pln",/^\\[\S\s]?/,q],["pun",/^.[^\s\w"-$'./@\\`]*/,q]);return x(m,e)}function D(a,m){function e(a){switch(a.nodeType){case 1:if(k.test(a.className))break;if("BR"===a.nodeName)h(a), | ||
a.parentNode&&a.parentNode.removeChild(a);else for(a=a.firstChild;a;a=a.nextSibling)e(a);break;case 3:case 4:if(p){var b=a.nodeValue,d=b.match(t);if(d){var c=b.substring(0,d.index);a.nodeValue=c;(b=b.substring(d.index+d[0].length))&&a.parentNode.insertBefore(s.createTextNode(b),a.nextSibling);h(a);c||a.parentNode.removeChild(a)}}}}function h(a){function b(a,d){var e=d?a.cloneNode(!1):a,f=a.parentNode;if(f){var f=b(f,1),g=a.nextSibling;f.appendChild(e);for(var h=g;h;h=g)g=h.nextSibling,f.appendChild(h)}return e} | ||
@@ -24,3 +24,3 @@ for(;!a.nextSibling;)if(a=a.parentNode,!a)return;for(var a=b(a.nextSibling,0),e;(e=a.parentNode)&&e.nodeType===1;)a=e;d.push(a)}var k=/(?:^|\s)nocode(?:\s|$)/,t=/\r\n?|\n/,s=a.ownerDocument,l;a.currentStyle?l=a.currentStyle.whiteSpace:window.getComputedStyle&&(l=s.defaultView.getComputedStyle(a,q).getPropertyValue("white-space"));var p=l&&"pre"===l.substring(0,3);for(l=s.createElement("LI");a.firstChild;)l.appendChild(a.firstChild);for(var d=[l],g=0;g<d.length;++g)e(d[g]);m===(m|0)&&d[0].setAttribute("value", | ||
I,J,v],hashComments:!0,cStyleComments:!0,multiLineStrings:!0,regexLiterals:!0}),A={};k(O,["default-code"]);k(x([],[["pln",/^[^<?]+/],["dec",/^<!\w[^>]*(?:>|$)/],["com",/^<\!--[\S\s]*?(?:--\>|$)/],["lang-",/^<\?([\S\s]+?)(?:\?>|$)/],["lang-",/^<%([\S\s]+?)(?:%>|$)/],["pun",/^(?:<[%?]|[%?]>)/],["lang-",/^<xmp\b[^>]*>([\S\s]+?)<\/xmp\b[^>]*>/i],["lang-js",/^<script\b[^>]*>([\S\s]*?)(<\/script\b[^>]*>)/i],["lang-css",/^<style\b[^>]*>([\S\s]*?)(<\/style\b[^>]*>)/i],["lang-in.tag",/^(<\/?[a-z][^<>]*>)/i]]), | ||
["default-markup","htm","html","mxml","xhtml","xml","xsl"]);k(x([["pln",/^\s+/,q," \t\r\n"],["atv",/^(?:"[^"]*"?|'[^']*'?)/,q,"\"'"]],[["tag",/^^<\/?[a-z](?:[\w-.:]*\w)?|\/?>$/i],["atn",/^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],["lang-uq.val",/^=\s*([^\s"'>]*(?:[^\s"'/>]|\/(?=\s)))/],["pun",/^[/<->]+/],["lang-js",/^on\w+\s*=\s*"([^"]+)"/i],["lang-js",/^on\w+\s*=\s*'([^']+)'/i],["lang-js",/^on\w+\s*=\s*([^\s"'>]+)/i],["lang-css",/^style\s*=\s*"([^"]+)"/i],["lang-css",/^style\s*=\s*'([^']+)'/i],["lang-css", | ||
["default-markup","htm","html","mxml","xhtml","xml","xsl"]);k(x([["pln",/^\s+/,q," \r\n"],["atv",/^(?:"[^"]*"?|'[^']*'?)/,q,"\"'"]],[["tag",/^^<\/?[a-z](?:[\w-.:]*\w)?|\/?>$/i],["atn",/^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],["lang-uq.val",/^=\s*([^\s"'>]*(?:[^\s"'/>]|\/(?=\s)))/],["pun",/^[/<->]+/],["lang-js",/^on\w+\s*=\s*"([^"]+)"/i],["lang-js",/^on\w+\s*=\s*'([^']+)'/i],["lang-js",/^on\w+\s*=\s*([^\s"'>]+)/i],["lang-css",/^style\s*=\s*"([^"]+)"/i],["lang-css",/^style\s*=\s*'([^']+)'/i],["lang-css", | ||
/^style\s*=\s*([^\s"'>]+)/i]]),["in.tag"]);k(x([],[["atv",/^[\S\s]+/]]),["uq.val"]);k(u({keywords:F,hashComments:!0,cStyleComments:!0,types:K}),["c","cc","cpp","cxx","cyc","m"]);k(u({keywords:"null,true,false"}),["json"]);k(u({keywords:H,hashComments:!0,cStyleComments:!0,verbatimStrings:!0,types:K}),["cs"]);k(u({keywords:G,cStyleComments:!0}),["java"]);k(u({keywords:v,hashComments:!0,multiLineStrings:!0}),["bsh","csh","sh"]);k(u({keywords:I,hashComments:!0,multiLineStrings:!0,tripleQuotedStrings:!0}), | ||
@@ -27,0 +27,0 @@ ["cv","py"]);k(u({keywords:"caller,delete,die,do,dump,elsif,eval,exit,foreach,for,goto,if,import,last,local,my,next,no,our,print,package,redo,require,sub,undef,unless,until,use,wantarray,while,BEGIN,END",hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["perl","pl","pm"]);k(u({keywords:J,hashComments:!0,multiLineStrings:!0,regexLiterals:!0}),["rb"]);k(u({keywords:w,cStyleComments:!0,regexLiterals:!0}),["js"]);k(u({keywords:"all,and,by,catch,class,else,extends,false,finally,for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then,true,try,unless,until,when,while,yes", |
@@ -1,16 +0,12 @@ | ||
var sync = require('synchronize') | ||
var fs = require('fs') | ||
var sync = require('synchronize') | ||
var fs = require('fs') | ||
sync(fs, 'readFile') | ||
sync.it = function(desc, callback){ | ||
it(desc, function(done){ | ||
sync.fiber(callback.bind(this), done) | ||
}) | ||
} | ||
var async = sync.asyncIt | ||
describe('File System', function(){ | ||
sync.it('should read file', function(){ | ||
describe('File System', async(function(){ | ||
it('should read file', function(){ | ||
var data = fs.readFile(__filename, 'utf8') | ||
}) | ||
}) | ||
})) |
{ | ||
"name" : "synchronize", | ||
"main" : "./sync", | ||
"version" : "0.3.0", | ||
"version" : "0.5.0", | ||
"homepage" : "http://alexeypetrushin.github.com/synchronize", | ||
@@ -10,4 +10,4 @@ "dependencies" : { | ||
"devDependencies" : { | ||
"mocha" : "1.0.x", | ||
"expect.js" : "0.1.x" | ||
"mocha" : "1.0.x", | ||
"chai" : "1.3.x" | ||
}, | ||
@@ -14,0 +14,0 @@ "description" : "Turns asynchronous function into synchronous", |
@@ -7,4 +7,2 @@ synchronize.js allows You write asynchronous code as if it's synchronous. | ||
TODO: sequence, parallel, first, join, http://taskjs.org | ||
Copyright (c) Alexey Petrushin, http://petrush.in, released under the MIT license. |
82
sync.js
@@ -54,18 +54,26 @@ require('fibers') | ||
// Creates special, fiber-aware asynchronous callback resuming current fiber when it will be finished. | ||
// Creates fiber-aware asynchronous callback resuming current fiber when it will be finished. | ||
sync.defer = function(){ | ||
if(!Fiber.current) throw new Error("no current Fiber, defer can'b be used without Fiber!") | ||
if(Fiber.current._syncParallel) | ||
return sync.deferParallel() | ||
else | ||
return sync.deferSerial() | ||
} | ||
// | ||
sync.deferSerial = function(){ | ||
var fiber = Fiber.current | ||
if(!fiber) throw "no current Fiber, defer can'b be used without Fiber!" | ||
if(!fiber) throw new Error("no current Fiber, defer can'b be used without Fiber!") | ||
return function(){ | ||
var thatArguments = arguments | ||
// Returning asynchronous callback. | ||
return function(err, result){ | ||
// Wrapping in nextTick as a safe measure against not asynchronous usage. | ||
process.nextTick(function(){ | ||
if(thatArguments[0]){ | ||
if(err){ | ||
// Resuming fiber and throwing error. | ||
fiber.throwInto(thatArguments[0]) | ||
fiber.throwInto(err) | ||
}else{ | ||
// Resuming fiber and returning result. | ||
fiber.run(thatArguments[1]) | ||
fiber.run(result) | ||
} | ||
@@ -76,6 +84,45 @@ }) | ||
// Executes `callback` within `Fiber`, when it finish it will call `done` callback. | ||
sync.deferParallel = function(){ | ||
var fiber = Fiber.current | ||
if(!fiber) throw new Error("no current Fiber, defer can'b be used without Fiber!") | ||
if(!fiber._syncParallel) throw new Error("invalid usage, should be called in parallel mode!") | ||
var data = fiber._syncParallel | ||
// Counting amount of `defer` calls. | ||
data.called += 1 | ||
var resultIndex = data.called - 1 | ||
// Returning asynchronous callback. | ||
return function(err, result){ | ||
// Wrapping in nextTick as a safe measure against not asynchronous usage. | ||
process.nextTick(function(){ | ||
if(err){ | ||
// Resuming fiber and throwing error. | ||
fiber.throwInto(err) | ||
}else{ | ||
data.returned += 1 | ||
data.results[resultIndex] = result | ||
// Resuming fiber and returning result when all callbacks finished. | ||
if(data.returned == data.called) fiber.run(data.results) | ||
} | ||
}) | ||
} | ||
} | ||
// Support for parallel calls, all `defer` calls within callback will be | ||
// performed in parallel. | ||
sync.parallel = function(cb){ | ||
var fiber = Fiber.current | ||
if(!fiber) throw new Error("no current Fiber, defer can'b be used without Fiber!") | ||
// Enabling `defer` calls to be performed in parallel. | ||
fiber._syncParallel = {called: 0, returned: 0, results: []} | ||
cb() | ||
delete fiber._syncParallel | ||
} | ||
// Executes `cb` within `Fiber`, when it finish it will call `done` callback. | ||
// If error will be thrown during execution, this error will be catched and passed to `done`, | ||
// if `done` not provided it will be just rethrown. | ||
sync.fiber = function(callback, done){ | ||
sync.fiber = function(cb, done){ | ||
var that = this | ||
@@ -85,3 +132,3 @@ Fiber(function(){ | ||
try { | ||
callback.call(that) | ||
cb.call(that) | ||
done() | ||
@@ -93,5 +140,16 @@ } catch (error){ | ||
// Don't catch errors if done not provided! | ||
callback.call(that) | ||
cb.call(that) | ||
} | ||
}).run() | ||
} | ||
// Asynchronous wrapper for mocha.js tests. | ||
// | ||
// async = sync.asyncIt | ||
// it('should pass', async(function(){ | ||
// ... | ||
// })) | ||
// | ||
sync.asyncIt = function(cb){ | ||
return function(done){sync.fiber(cb, done)} | ||
} |
var sync = require('../sync') | ||
var expect = require('expect.js') | ||
var expect = require('chai').expect | ||
describe('Control Flow', function(){ | ||
var fn = function(arg, callback){ | ||
expect(arg).to.be('something') | ||
var fn = function(arg, cb){ | ||
expect(arg).to.eql('something') | ||
process.nextTick(function(){ | ||
callback(null, 'ok') | ||
cb(null, 'ok') | ||
}) | ||
@@ -15,5 +15,5 @@ } | ||
var result = sync.await(fn('something', sync.defer())) | ||
expect(result).to.be('ok') | ||
expect(result).to.eql('ok') | ||
}, done) | ||
}), | ||
}) | ||
@@ -23,3 +23,3 @@ it('should synchronize function', function(done){ | ||
sync.fiber(function(){ | ||
expect(fn('something')).to.be('ok') | ||
expect(fn('something')).to.eql('ok') | ||
}, done) | ||
@@ -31,5 +31,5 @@ }) | ||
sync.fiber(function(){ | ||
expect(fn('something')).to.be('ok') | ||
expect(fn('something')).to.eql('ok') | ||
}, done) | ||
}), | ||
}) | ||
@@ -39,11 +39,11 @@ it('should allow call synchronized function explicitly', function(done){ | ||
fn('something', function(err, result){ | ||
expect(result).to.be('ok') | ||
expect(result).to.eql('ok') | ||
done(err) | ||
}) | ||
}), | ||
}) | ||
it("should catch asynchronous errors", function(done){ | ||
var fn = function(callback){ | ||
var fn = function(cb){ | ||
process.nextTick(function(){ | ||
callback(new Error('an error')) | ||
cb(new Error('an error')) | ||
}) | ||
@@ -59,19 +59,19 @@ } | ||
} | ||
expect(err.message).to.be('an error') | ||
expect(err.message).to.eql('an error') | ||
}, done) | ||
}), | ||
}) | ||
it("should be compatible with not asynchronous callbacks", function(done){ | ||
fn = function(callback){ | ||
callback(null, 'ok') | ||
it("should be compatible with not asynchronous cbs", function(done){ | ||
fn = function(cb){ | ||
cb(null, 'ok') | ||
} | ||
fn = sync(fn) | ||
sync.fiber(function(){ | ||
expect(fn()).to.be('ok') | ||
expect(fn()).to.eql('ok') | ||
}, done) | ||
}), | ||
}) | ||
it("should catch non asynchronous errors", function(done){ | ||
fn = function(callback){ | ||
callback(new Error('an error')) | ||
fn = function(cb){ | ||
cb(new Error('an error')) | ||
} | ||
@@ -86,5 +86,48 @@ fn = sync(fn) | ||
} | ||
expect(err.message).to.be('an error') | ||
expect(err.message).to.eql('an error') | ||
}, done) | ||
}) | ||
describe("Special cases", function(){ | ||
it('should be able to emulate sleep', function(done){ | ||
var sleep = function(ms){ | ||
sync.await(setTimeout(sync.defer(), ms)) | ||
} | ||
sync.fiber(function(){ | ||
var start = new Date().getTime() | ||
sleep(50) | ||
expect(new Date().getTime()).to.be.greaterThan(start) | ||
}, done) | ||
}) | ||
}) | ||
it('should support parallel calls', function(done){ | ||
sync.fiber(function(){ | ||
var calls = [] | ||
var readA = function(cb){ | ||
calls.push('readA') | ||
process.nextTick(function(){ | ||
calls.push('nextTick') | ||
cb(null, 'dataA') | ||
}) | ||
} | ||
var readB = function(cb){ | ||
calls.push('readB') | ||
process.nextTick(function(){ | ||
calls.push('nextTick') | ||
cb(null, 'dataB') | ||
}) | ||
} | ||
sync.parallel(function(){ | ||
readA(sync.defer()) | ||
readB(sync.defer()) | ||
}) | ||
var results = sync.await() | ||
expect(results).to.eql(['dataA', 'dataB']) | ||
expect(calls).to.eql(['readA', 'readB', 'nextTick', 'nextTick']) | ||
}, done) | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
183139
5732
7