Skip to content

Instantly share code, notes, and snippets.

@mohayonao
Forked from hitode909/worker.coffee
Last active December 16, 2015 06:39
Show Gist options
  • Save mohayonao/5393453 to your computer and use it in GitHub Desktop.
Save mohayonao/5393453 to your computer and use it in GitHub Desktop.
流れるようなインターフェイス, 引数つき
class Worker
constructor: (@context=@)->
@jobs = []
register: (job) ->
@jobs.push job
work: ->
job = do @jobs.shift
args = [=> @work.apply @, arguments].concat [].slice.call arguments
job?.apply @context, args
worker = new Worker
worker.register (done, arg)->
console.log "1 do, arg:#{arg}"
setTimeout ->
console.log "1 done"
done arg + 1
, 1000
worker.register (done, arg)->
console.log "2 do, arg:#{arg}"
setTimeout ->
console.log "2 done"
done arg + 1
, 1000
worker.register (done, arg)->
console.log "3 do, arg:#{arg}"
setTimeout ->
console.log "3 done"
done arg + 1
, 1000
worker.work 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment