Skip to content

Instantly share code, notes, and snippets.

@dglittle
Last active December 16, 2015 13:19
Show Gist options
  • Save dglittle/5440598 to your computer and use it in GitHub Desktop.
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)
_.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