Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add back @async, using fibers now.

  • Loading branch information...
commit beee20d9a5ea9dd43e95b5308f923d36353b1da8 1 parent c9613b1
@lowentropy lowentropy authored
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
15 lib/worker.coffee
@@ -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
2  lib/workspace.coffee
@@ -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
18 test/async_test.coffee
@@ -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()
Please sign in to comment.
Something went wrong with that request. Please try again.