Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Further async work.

  • Loading branch information...
commit 0d827acedb52f545e32fc0195d46bfb015b3b84d 1 parent bbeecff
@d0ugal d0ugal authored
View
12 locache.js
@@ -97,7 +97,7 @@
// Internal utility functions
// --------------------
- var defer = (function(){
+ var defer = LocacheCache.prototype._defer = (function(){
var timeouts = []
var messageName = "function-defer-message"
@@ -119,9 +119,9 @@
}
Deferred.prototype.defer = function(){
- var result = this.resultValue = this.fn()
+ this.resultValue = this.fn()
if (this.hasOwnProperty('finishedFunction')){
- this.finishedFunction(result)
+ this.finishedFunction(this.resultValue)
}
}
@@ -131,6 +131,10 @@
Deferred.prototype.finished = function(fn){
this.finishedFunction = fn
+ if (this.hasFinished()){
+ this.finishedFunction(this.resultValue)
+ }
+ return this
}
function defer(fn) {
@@ -140,10 +144,8 @@
return d
}
-
return defer
-
})()
// Two cache prefixes. When storing values, all keys are prefixed
View
7 tests/index.html
@@ -14,11 +14,12 @@
<!-- include spec files here... -->
<script type="text/javascript" src="specs/async.js"></script>
- <script type="text/javascript" src="specs/nostorage.js"></script>
+ <script type="text/javascript" src="specs/custom-storage.js"></script>
+ <script type="text/javascript" src="specs/defer.js"></script>
+ <script type="text/javascript" src="specs/expire.js"></script>
<script type="text/javascript" src="specs/local.js"></script>
+ <script type="text/javascript" src="specs/nostorage.js"></script>
<script type="text/javascript" src="specs/session.js"></script>
- <script type="text/javascript" src="specs/expire.js"></script>
- <script type="text/javascript" src="specs/custom-storage.js"></script>
<script type="text/javascript">
(function() {
View
19 tests/specs/async.js
@@ -11,29 +11,38 @@ describe("asynclocal:", function(){
this.cache = locache.createCache({storage: locache.backends.asynclocal})
})
- it("should return a length of 3", function(){
+ it("should test setting a key async", function(){
+ var asynDone = false
var cache = this.cache
cache.set("key", "value").finished(function(event){
-
- console.log("a" + cache.length())
-
+ expect(cache.length()).toBe(1)
+ asynDone = true
});
- console.log("b" + cache.length())
+ expect(cache.length()).toBe(0)
+
+ waitsFor(function(){return asynDone})
})
it("should set and get a string and verify the data type", function(){
+
var cache = this.cache
+ var asynDone = false
+
cache.set("my_string", "my_value").finished(function(){
+ console.log("set")
cache.get("my_string").finished(function(result){
+ console.log("get")
expect(result).toBe("my_value")
expect(typeof result).toBe("string")
+ asynDone = true
})
})
+ waitsFor(function(){return asynDone})
})
View
36 tests/specs/defer.js
@@ -0,0 +1,36 @@
+/*jshint asi:true */
+
+describe("defer:", function(){
+
+ "use strict";
+
+ beforeEach(function(){
+ this.defer = locache._defer
+ })
+
+ it("should test defer", function(){
+
+ var c = 0
+
+ this.defer(function(){
+ c++
+ expect(c).toBe(1)
+ })
+ expect(c).toBe(0)
+ })
+
+ it("should test finished", function(){
+
+ var c = 0, done = false
+
+ this.defer(function(){
+ c++
+ }).finished(function(){
+ expect(c).toBe(1)
+ done = true
+ })
+
+ waitsFor(function(){return done})
+ })
+
+})
Please sign in to comment.
Something went wrong with that request. Please try again.