Skip to content

Commit

Permalink
Merge branch 'release/1.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
bchociej committed May 13, 2016
2 parents 8314412 + 08e2373 commit 2bd22a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lru-map",
"version": "1.4.0",
"version": "1.5.0",
"description": "An ES6-y Map with LRU and max-age eviction",
"main": "index.js",
"scripts": {
Expand Down
12 changes: 12 additions & 0 deletions src/lru-map.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,33 @@ module.exports = class LRUMap

opts.timeout ?= 10000
opts.invokeNewValueFunction ?= true
opts.onCacheHit ?= -> undefined
opts.onCacheMiss ?= -> undefined

unless typeof opts.timeout is 'number' and opts.timeout >= 1
throw new TypeError 'opts.timeout must be a positive number (possibly Infinity)'

unless typeof opts.invokeNewValueFunction is 'boolean'
throw new TypeError 'opts.invokeNewValueFunction must be boolean'

unless typeof opts.onCacheHit is 'function'
throw new TypeError 'opts.onCacheHit must be a function'

unless typeof opts.onCacheMiss is 'function'
throw new TypeError 'opts.onCacheMiss must be a function'

if _atomicInflights.has key
setTimeout -> opts.onCacheHit key
return _atomicInflights.get key

@reapStale()

if _map.has key
setTimeout -> opts.onCacheHit key
return Promise.resolve @get key

setTimeout -> opts.onCacheMiss key

if opts.invokeNewValueFunction and typeof newValue is 'function'
newValue = newValue()

Expand Down
31 changes: 31 additions & 0 deletions test/lru-map.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,18 @@ describe 'LRUMap', ->
lmap = new LRUMap
expect(-> lmap.setIfNull 'foo', {bar: true}, {timeout: 0.1}).to.throwError /positive/i

it 'errors if opts.invokeNewValueFunction is not boolean', ->
lmap = new LRUMap
expect(-> lmap.setIfNull 'foo', {bar: true}, {invokeNewValueFunction: 0.1}).to.throwError /boolean/i

it 'errors if opts.onCacheHit is not a function', ->
lmap = new LRUMap
expect(-> lmap.setIfNull 'foo', {bar: true}, {onCacheHit: 0.1}).to.throwError /function/i

it 'errors if opts.onCacheMiss is not a function', ->
lmap = new LRUMap
expect(-> lmap.setIfNull 'foo', {bar: true}, {onCacheMiss: 0.1}).to.throwError /function/i

it 'returns the inflight promise, if one exists', ->
lmap = new LRUMap
lmap.testInflights.set 'foo', Promise.resolve({hi: 'mom'})
Expand Down Expand Up @@ -673,6 +685,25 @@ describe 'LRUMap', ->
lmap.setIfNull('foo', (-> 'hi'), {invokeNewValueFunction: false})
.then (value) -> expect(typeof value).to.be 'function'

it 'calls opts.onCacheHit when a cache hit occurs', (done) ->
lmap = new LRUMap
lmap.set('foo', 'bar')
lmap.setIfNull('foo', 'bar', {
onCacheHit: (key) ->
expect(key).to.be 'foo'
done()
onCacheMiss: -> expect().fail('should have hit')
})

it 'calls opts.onCacheMiss when a cache miss occurs', (done) ->
lmap = new LRUMap
lmap.setIfNull('foo', 'bar', {
onCacheMiss: (key) ->
expect(key).to.be 'foo'
done()
onCacheHit: -> expect().fail('should have missed')
})

describe '#delete()', ->
it 'removes the specified key and its value', ->
lmap = new LRUMap
Expand Down

0 comments on commit 2bd22a2

Please sign in to comment.