Skip to content

Commit

Permalink
Add back @async, using fibers now.
Browse files Browse the repository at this point in the history
  • Loading branch information
lowentropy committed Oct 7, 2012
1 parent c9613b1 commit beee20d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/worker.coffee
Expand Up @@ -235,10 +235,21 @@ class Worker
# console.log "LEAVE (#{@timestamp()}): #{@key}" # console.log "LEAVE (#{@timestamp()}): #{@key}"
@_run() @_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 # Resume the fiber, catching any errors
_run: (args...) -> _run: (arg) ->
try try
@fiber.run args... if @fiber @fiber.run arg if @fiber
catch e catch e
@error e @error e


Expand Down
2 changes: 1 addition & 1 deletion lib/workspace.coffee
Expand Up @@ -47,7 +47,7 @@ Workspace.mixin = (mixins) ->
extend_workspace mixins extend_workspace mixins


core_methods = {} 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] core_methods[method] = Worker.prototype[method]
extend_workspace core_methods extend_workspace core_methods


Expand Down
18 changes: 18 additions & 0 deletions 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()

0 comments on commit beee20d

Please sign in to comment.