Permalink
Browse files

fix emmitter interface

  • Loading branch information...
poelzi committed Sep 16, 2011
1 parent 7b97393 commit 6980b28e2424d53ef273e369f690dabbff385103
Showing with 26 additions and 12 deletions.
  1. +15 −8 src/flexcache.coffee
  2. +11 −4 test/test-flexcache.coffee
View
@@ -111,6 +111,7 @@ class Flexcache
# in case no callback was defined, push it back to the arguments list
if typeof(callback) != 'function'
wargs.push(callback)
callback = undefined
if @options.debug > 1
console.log("try cache call. args:", wargs)
@@ -120,12 +121,13 @@ class Flexcache
hash = hash_name + "_" + hasher(wargs...)
# create event emitter return value
if emitter
ee = new emitter(wargs...)
ee = new emitter wargs..., callback
opt = { serializer: loptions.serializer or @options.serializer}
@backend.get group, hash, opt, (err, cached) =>
# undecodeable means non cached
if err or not cached
# MISS
if @options.debug
console.log("cache MISS group:", group, " hash:", hash)
# call the masked function
@@ -164,24 +166,29 @@ class Flexcache
#console.log(wargs)
#console.log(results)
# call real callback function
if not ee
if callback
callback.apply(null, results)
else
# HIT
if @options.debug
console.log("cache HIT group:", group, " hash:", hash)
console.log("flexcache HIT group:", group, " hash:", hash)
if @options.debug >= 3
console.log("data:")
console.log(cached)
console.log("####")
#console.log(cached)
if not ee
callback.apply(null, cached)
if not emitter
if callback
callback.apply(null, cached)
else
# handle event emmitter
#console.log("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%cached", cached)
atest = () ->
cached.length
adata = (callback) ->
mydata = cached.splice(0,1)
console.log(mydata)
ee.emit 'data', mydata
#console.log(mydata)
ee.emit 'data', mydata[0]
setTimeout callback, 0
aend = () ->
ee.emit('end')
View
@@ -383,8 +383,8 @@ gentests "Stress", (test, backend) ->
#redis = require('redis')
#redis.debug_mode = true
module.exports.TestEmmiter = (test) ->
back = new RedisBackend()
gentests "EventEmitter", (test, backend) ->
back = new backend()
fc = new Flexcache back, debug:2
emitter_run = 0
@@ -396,7 +396,6 @@ module.exports.TestEmmiter = (test) ->
run: () =>
emitter_run++
console.trace()
async.waterfall [
(next) =>
@emit 'data', "emitter run: " + emitter_run
@@ -406,6 +405,14 @@ module.exports.TestEmmiter = (test) ->
@emit 'data', "\nsecond\n"
setTimeout(next, 0)
,
(next) =>
@emit 'data', new Buffer([01,00,02,0xff,00])
setTimeout(next, 0)
,
(next) =>
@emit 'data', 23
setTimeout(next, 0)
,
(next) =>
@emit 'end'
]
@@ -445,7 +452,7 @@ module.exports.TestEmmiter = (test) ->
(rv1, next) ->
rv2 = cached_emitter(1, "test2")
test.ok(rv2 instanceof TestEmitter, "not event emitter")
should_results = [ 'emitter run: 5', '\nsecond\n' ]
should_results = [ 'emitter run: 5', '\nsecond\n', new Buffer([01,00,02,0xff,00]), 23]
rv2.on 'data', (data) ->
test.equal(data, should_results.splice(0,1), "results differ")
console.log("got data", data)

0 comments on commit 6980b28

Please sign in to comment.