Last active
December 16, 2015 13:19
-
-
Save dglittle/5440598 to your computer and use it in GitHub Desktop.
a function for fiberizing and defiberizing stuff that I plan to use later, maybe (untested)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.fiberize = function (x) { | |
if (typeof(x) == 'object') { | |
for (var k in x) | |
if (typeof(x[k]) == 'function') | |
x[k] = _.fiberize(x[k]) | |
return x | |
} else if (typeof(x) == 'function') { | |
return function () { | |
var args = _.toArray(arguments) | |
args.push(_.p()) | |
x.apply(this, args) | |
return _.p() | |
} | |
} | |
} | |
_.defib = _.defiberize = function (x) { | |
if (typeof(x) == 'object') { | |
for (var k in x) | |
if (typeof(x[k]) == 'function') | |
x[k] = _.defiberize(x[k]) | |
return x | |
} else if (typeof(x) == 'function') { | |
return function () { | |
if (Fiber.current) { | |
return x.apply(this, arguments) | |
} else { | |
var args = _.toArray(arguments) | |
var cb = args.pop() | |
var me = this | |
_.run(function () { | |
try { | |
cb(null, x.apply(me, args)) | |
} catch (e) { | |
cb(e) | |
} | |
}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment