Skip to content

Instantly share code, notes, and snippets.

@torkale
Created March 28, 2012 21:11
Show Gist options
  • Select an option

  • Save torkale/2230574 to your computer and use it in GitHub Desktop.

Select an option

Save torkale/2230574 to your computer and use it in GitHub Desktop.
buffer that gets a method and callback and executes one method at time
window.Buffer =
buffer: []
executing: false
pushToBuffer: (method, callback) ->
Buffer.buffer.push ->
Buffer.executing = true
method (result) ->
callback(result)
Buffer.executeFromBuffer()
Buffer.executeFromBuffer() unless Buffer.executing
executeFromBuffer: ->
if Buffer.buffer.length > 0
executable = Buffer.buffer.shift()
executable()
else
Buffer.executing = false
# Usage:
#
# sayHello = (callback) ->
# callback("Hello World")
#
# print = (result) ->
# console.log result
#
# Buffer.pushToBuffer sayHello, print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment