forked from spinnaker/deck
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Benjamin Malley
committed
Sep 12, 2014
1 parent
ae44463
commit a3291a6
Showing
7 changed files
with
151 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
'use strict'; | ||
|
||
var angular = require('angular'); | ||
angular.module('deckApp') | ||
.factory('scheduledCache', function($cacheFactory, scheduler) { | ||
// returns a cache that is cleared according to the scheduler | ||
return function(id, cycles) { | ||
function ScheduledCache(id, cycles) { | ||
var that = this; | ||
that.cycles = cycles || 0; | ||
|
||
that.cache = $cacheFactory(id); | ||
|
||
that.disposable = scheduler.get() | ||
.skip(that.cycles) | ||
.subscribe(function() { | ||
that.cache.removeAll(); | ||
}); | ||
|
||
that.info = that.cache.info; | ||
that.put = that.cache.put; | ||
that.get = that.cache.get; | ||
that.remove = that.cache.remove; | ||
that.removeAll = that.cache.removeAll; | ||
|
||
that.destroy = function() { | ||
that.disposable.dispose(); | ||
that.cache.destroy(); | ||
delete that.disposable; | ||
delete that.cache; | ||
}; | ||
|
||
return this; | ||
} | ||
|
||
return new ScheduledCache(id, cycles); | ||
}; | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
if (!Function.prototype.bind) { | ||
Function.prototype.bind = function (oThis) { | ||
if (typeof this !== "function") { | ||
// closest thing possible to the ECMAScript 5 | ||
// internal IsCallable function | ||
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); | ||
} | ||
|
||
var aArgs = Array.prototype.slice.call(arguments, 1), | ||
fToBind = this, | ||
fNOP = function () {}, | ||
fBound = function () { | ||
return fToBind.apply(this instanceof fNOP && oThis | ||
? this | ||
: oThis, | ||
aArgs.concat(Array.prototype.slice.call(arguments))); | ||
}; | ||
|
||
fNOP.prototype = this.prototype; | ||
fBound.prototype = new fNOP(); | ||
|
||
return fBound; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
'use strict'; | ||
|
||
describe('Service: scheduledCache', function() { | ||
beforeEach(function() { | ||
var subject = new Rx.Subject(); | ||
this.subject = subject; | ||
var scheduler = { | ||
get: angular.noop, | ||
}; | ||
this.scheduler = scheduler; | ||
spyOn(scheduler, 'get').andReturn(subject); | ||
|
||
module('deckApp'); | ||
module(function($provide) { | ||
$provide.value('scheduler', scheduler); | ||
}); | ||
|
||
inject(function(scheduledCache, $cacheFactory) { | ||
this.scheduledCache = scheduledCache; | ||
this.$cacheFactory = $cacheFactory; | ||
}); | ||
}); | ||
|
||
describe('initializing the cache', function() { | ||
|
||
it('creates an angular cache at the specified id', function() { | ||
var cache = this.scheduledCache('id'); | ||
expect(this.$cacheFactory.get('id').info().id).toEqual(cache.info().id); | ||
}); | ||
|
||
it('allows an optional parameter to specify the refresh interval', function() { | ||
var cache = this.scheduledCache('id', 2); | ||
expect(cache.cycles).toBe(2); | ||
}); | ||
|
||
it('defaults to refreshing every cycle', function() { | ||
var cache = this.scheduledCache('id'); | ||
expect(cache.cycles).toBe(0); | ||
}); | ||
|
||
it('gets the global scheduler on initialization', function() { | ||
this.scheduledCache('id'); | ||
expect(this.scheduler.get.callCount).toEqual(1); | ||
}); | ||
|
||
it('will throw if the same id is initialized twice', function() { | ||
this.scheduledCache('id'); | ||
expect(function() { | ||
this.scheduledCache('id'); | ||
}).toThrow(); | ||
}); | ||
|
||
it('shares the same id space as $cacheFactory', function() { | ||
this.scheduledCache('id'); | ||
expect(function() { | ||
this.$cacheFactory('id'); | ||
}.bind(this)).toThrow(); | ||
}); | ||
}); | ||
|
||
describe('the schedule', function() { | ||
it('clears the cache once every n cycles', function() { | ||
var cache = this.scheduledCache('id'); | ||
cache.put('foo', 'bar'); | ||
expect(cache.get('foo')).toEqual('bar'); | ||
this.subject.onNext(); | ||
expect(cache.get('foo')).toBeUndefined(); | ||
|
||
var cache = this.scheduledCache('id2', 1); | ||
cache.put('foo', 'bar'); | ||
expect(cache.get('foo')).toEqual('bar'); | ||
this.subject.onNext(); | ||
expect(cache.get('foo')).toEqual('bar'); | ||
this.subject.onNext(); | ||
expect(cache.get('foo')).toBeUndefined(); | ||
}); | ||
}); | ||
}); |