Permalink
Browse files

Add back @async, using fibers now.

  • Loading branch information...
lowentropy committed Oct 7, 2012
1 parent c9613b1 commit beee20d9a5ea9dd43e95b5308f923d36353b1da8
Showing with 32 additions and 3 deletions.
  1. +13 −2 lib/worker.coffee
  2. +1 −1 lib/workspace.coffee
  3. +18 −0 test/async_test.coffee
View
@@ -235,10 +235,21 @@ class Worker
# console.log "LEAVE (#{@timestamp()}): #{@key}"
@_run()
+ # Perform the code block asynchronously, outside the
+ # main fiber. The code block should take a callback
+ # and, when the asynchronous operation is complete,
+ # call it with (error, result). The result is returned
+ # from @async, and the error, if any, is thrown.
+ async: (body) ->
+ body (args...) => @_run(args)
+ [err, result] = @yield()
+ throw err if err
+ result
+
# Resume the fiber, catching any errors
- _run: (args...) ->
+ _run: (arg) ->
try
- @fiber.run args... if @fiber
+ @fiber.run arg if @fiber
catch e
@error e
View
@@ -47,7 +47,7 @@ Workspace.mixin = (mixins) ->
extend_workspace mixins
core_methods = {}
-for method in ['emit', 'keys', 'worker', 'bless', 'all', 'each', 'atomic']
+for method in ['emit', 'keys', 'worker', 'bless', 'all', 'each', 'atomic', 'async']
core_methods[method] = Worker.prototype[method]
extend_workspace core_methods
View
@@ -0,0 +1,18 @@
+redeye_suite = require './support/redeye_suite'
+
+module.exports = redeye_suite
+
+ 'async test':
+
+ workers:
+ 'test': ->
+ @async (callback) ->
+ setTimeout (-> callback null, 216), 100
+
+ setup: ->
+ @request 'test'
+
+ expect: ->
+ @get 'test', (value) =>
+ @assert.eql value, 216
+ @finish()

0 comments on commit beee20d

Please sign in to comment.