Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbilcke committed Jun 10, 2012
1 parent 209f167 commit 42d3b74
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 39 deletions.
55 changes: 51 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

This library is still in development so expect heavy refactoring and sparse documentation until I have more time to settle everything.

### TODO / Wishlist

* insertion of event at arbitrary timesteps (eg. when working with incomplete time-series)
* reverse playback!

### License

BSD
Expand Down Expand Up @@ -45,10 +50,52 @@

## Documentation

### How it works
### Example

``` coffeescript
# myapp.coffee
Recorder = require 'recorder'

record = new Recorder()
record.on 'event', (event) -> log "#{event.timestamp}: #{event.data}"

# coffee-style timeouts
delay = (t,f) -> setTimeout f, t

# record some dummy events
log "recording events.."
delay 100, -> record.rec companyhelpdesk: "hi how can I help you"
delay 500, -> record.rec facebook: "wow! this was a big earthquake"
delay 1000, -> record.rec twitter: "just saw my dead neighbor walking in my street. It's weird. wait I'm gonna check it out"
delay 1500, -> record.rec twitter: "ZOMBIE APOCALYPSE!!1!!"

delay 2000, ->
log "playing events back.."
record.play()

delay 5000, ->
log "playing events back. and faster."
record.play 5.0 # 2.0x

```

which should output something like:

```
10 Jun 14:57:49 - recording events..
10 Jun 14:57:51 - playing events back..
10 Jun 14:57:51 - 1339333069383: { companyhelpdesk: 'hi how can I help you' }
10 Jun 14:57:51 - 1339333069783: { facebook: 'wow! this was a big earthquake' }
10 Jun 14:57:52 - 1339333070284: { twitter: 'just saw my dead neighbor walking in my street. It\'s weird. wait I\'m gonna check it out' }
10 Jun 14:57:52 - 1339333070784: { twitter: 'ZOMBIE APOCALYPSE!!1!!' }
10 Jun 14:57:54 - playing events back. and faster.
10 Jun 14:57:54 - 1339333069383: { companyhelpdesk: 'hi how can I help you' }
10 Jun 14:57:54 - 1339333069783: { facebook: 'wow! this was a big earthquake' }
10 Jun 14:57:54 - 1339333070284: { twitter: 'just saw my dead neighbor walking in my street. It\'s weird. wait I\'m gonna check it out' }
10 Jun 14:57:54 - 1339333070784: { twitter: 'ZOMBIE APOCALYPSE!!1!!' }
Add some documentation here
```

alert (from node.js docs):
You can see it here but the second batch is two times faster

It is important to note that your callback will probably not be called with the exact temporal sequence - Node.js makes no guarantees about the exact timing of when the callback will fire, nor of the ordering things will fire in. The callback will be called as close as possible to the time specified.
74 changes: 60 additions & 14 deletions lib/recorder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 46 additions & 16 deletions src/recorder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,35 @@
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

{log,error,inspect} = require 'util'
events = require 'events'
BinaryTree = require './btree'
_ = require 'underscore'
moment = require 'moment'

delay = (t,f) -> setTimeout f, t

contains = (item, text) -> (text.indexOf(item) isnt -1)

class InMemory
constructor: () ->
@events = []
@first = no
@last = no

push: (event) ->
push: (event) =>
# for the moment, we can only manage insertion at the end
# TODO later: use https://github.com/vadimg/js_bintrees
first = _.first @events
first = event unless first
first.previous = event
event.last = first
event.next = first
@first = first

last = _.last @events
last = event unless last
last.next = event
event.previous = last
@last = last

@events.push event

Expand All @@ -55,49 +64,70 @@ class PlaybackModule
@cursor = 0
@running = false
@rate = 1.0
@looped = no


fire = (event) =>
#BROKEN
#next = main.database.nextr event.timestamp
#delta = event.timestamp - next.timestamp
#setTimeout delta, ->
#log "fire! #{inspect event}"
main.emit 'event', timestamp: event.timestamp, data: event.data
#log "checking next event.."
next = event.next
if next is main.database.first
#log "reched the end. checking if we should loop.."
return unless @looped
#log "looping.."
delta = (next.timestamp - event.timestamp) / @rate
now = moment()
next.expected = now + delta
#log "event.timestamp: #{event.timestamp} next.timestamp: #{next.timestamp} now: #{now} delta: #{delta} next.expected: #{next.expected}"

delay delta, ->
#log "setTimeout triggered. calling fire next"
fire next
# @fire nextEvent

# give playback capabilities to the main class
main.play = (rate=1.0) =>
@rate = rate
main.play = (rate=no) =>
@rate = rate if rate
#log "PlaybackModule: playing at rate #{@rate}"
first = main.database.first
if first
log "firing first event"
#log "firing first event"
fire first
else
log "no event to fire"
#log "no event to fire"
1

class RecordModule

constructor: (main) ->

# give record capabilities to the main class
main.record = (data) ->
main.rec = (data) ->
#log "RecordModule: pushing into database the event"
timestamp = moment()
main.database.push
timestamp: moment()
timestamp: timestamp
data: data
#log "RecordModule: db is #{inspect main.database.events}"
timestamp

main.recordAt = (timestamp, data) ->
main.overwrite = (timestamp, data) ->
throw "Not Implemented"
return

class module.exports
class module.exports extends events.EventEmitter
constructor: (url="") ->
@database = new InMemory()

# in case user want to use another backend
if contains "file://", url
@database = new SimpleFile url
else
log "using default database (in-memory)"
#log "using default database (in-memory)"
1

# now initialize the sub-controllers
new RecordModule @
new PlaybackModule @

32 changes: 27 additions & 5 deletions test/main.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
recorder = require '../lib/recorder'
Recorder = require '../lib/recorder'
{log,error,inspect} = require 'util'
delay = (t,f) -> setTimeout f, t

# these are not real tests, it's more like a demo
# more complete tests should check for returned values!!!

# our tests
describe 'Recorder', ->
describe '#new Recorder()', ->
it 'should work', (done) ->
recorder = new Recorder()
recorder.insert foo: 'bar'
#if (err) throw err
done()

record = new Recorder()
record.on 'event', (event) -> log "#{event.timestamp}: #{inspect event.data}"

# record some dummy events
log "recording events.."
delay 100, -> record.rec companyhelpdesk: "hi how can I help you"
delay 500, -> record.rec facebook: "wow! this was a big earthquake"
delay 1000, -> record.rec twitter: "just saw my dead neighbor walking in my street. It's weird. wait I'm gonna check it out"
delay 1500, -> record.rec twitter: "ZOMBIE APOCALYPSE!!1!!"

delay 1600, ->
log "playing events back.."
record.play()

delay 100, ->
log "playing events back. and faster."
record.play 5.0 # 2.0x
done()

#if (err) throw err

0 comments on commit 42d3b74

Please sign in to comment.