Skip to content
This repository has been archived by the owner on Jun 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #19 from sipmann/master
Browse files Browse the repository at this point in the history
Replaced the 'Defer' function with the new Promise
  • Loading branch information
d0ugal committed Jun 30, 2015
2 parents 0029c81 + c2cc4f4 commit 5cde108
Show file tree
Hide file tree
Showing 15 changed files with 2,556 additions and 981 deletions.
567 changes: 567 additions & 0 deletions build/locache.0.4.3.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions build/locache.0.4.3.min.js

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

117 changes: 8 additions & 109 deletions build/locache.js
@@ -1,6 +1,6 @@
/*jshint forin:true, noarg:true, noempty:true, eqeqeq:true, bitwise:true, strict:true, undef:true, unused:true, curly:true, browser:true, jquery:true, indent:4, maxerr:200 */

// locache 0.4.2
// locache 0.4.3
//
// (c) 2012 Dougal Matthews
// locache may be freely distributed under the MIT license.
Expand All @@ -19,6 +19,10 @@
// Save a reference to the global object, in most cases this is `window`.
var root = this;

//Check if Promises is available
if (!window['Promise'])
console.log("Promise isn't available at this browser");

// Object context bnding shim to support older versions of IE.
var bind = function (func, thisValue) {
return function () {
Expand Down Expand Up @@ -49,7 +53,7 @@
}

// Current version of locache, this is inserted in the build process.
LocacheCache.prototype.VERSION = "0.4.2";
LocacheCache.prototype.VERSION = "0.4.3";

// Boolean value that determines if they browser supports localStorage or
// not. This is based on the Modernizr implementation that can be found
Expand Down Expand Up @@ -104,106 +108,6 @@
// Internal utility functions
// --------------------

// A defer implementation to avoid IO access blocking the current
// thread. This is exposed on the LocacheCache prototype, simply so it
// can be accessed from within the unit tests. It's not intended for
// public use.
var defer = LocacheCache.prototype._defer = (function () {

// Store of the pending functions
var timeouts = [];
// Message name used to identify posts related to the defer
var messageName = "function-defer-message";

// Add a message event listener. If its not from this window or doesn't
// have the message name defined above, don't do anything and allow it
// to propagate to other handlers. Otherwise, its meant for us so stop
// the event.
var addEventListener;
if (root.addEventListener) {
addEventListener = root.addEventListener;
} else if (root.attachEvent) {
addEventListener = root.attachEvent;
}

addEventListener("message", function (event) {

if (event.source !== root || event.data !== messageName) {
return;
}
event.stopPropagation();

// Make sure we have some pending functions, otherwise return.
if (timeouts.length === 0) {
return;
}

// take the oldest from the 'queue' and call that functions.
var fn = timeouts.shift();
fn();

// Return false, to make sure the event isn't propagated
return false;

}, true);

// Constructor for the Defer, takes a function object and stores it
// on itself.
function Deferred(fn) {
this.fn = fn;
}

// The defer method runs the function and stores the result. Upon
// finishing, it looks for a finishedFunction, that if exists, is
// called and passed the result.
Deferred.prototype.defer = function () {
this.resultValue = this.fn();
if (this.hasOwnProperty('finishedFunction')) {
this.finishedFunction(this.resultValue);
}
};

// Return if the defer has finished. THis is determined by the
// existence of the resultValue on the object.
Deferred.prototype.hasFinished = function () {
return this.hasOwnProperty('resultValue');
};

// The finished function takes another function and assigns that to
// be called when the deferred function has finished.
Deferred.prototype.finished = function (fn) {
this.finishedFunction = fn;
// Check to see if the deferred function has finished, this can
// happen if its very quick or the finished function is assigned
// late. If it is, call it straight away.
if (this.hasFinished()) {
this.finishedFunction(this.resultValue);
}
// Make this object chainable.
return this;
};

// Wrapper utility function that provides access to the Deferred
// implementation and makes it very simple to use.
function defer(fn) {
// Create the defer instance that wraps the function
var d = new Deferred(fn);
// Add the defer method on the Deferred object, with the instance
// bound to the queue.
timeouts.push(bind(d.defer, d));
// post a message to the window that can be received by the
// message handler.
root.postMessage(messageName, "*");
// Finally, return the deferred object instance.
return d;
}

// Only return the defer function, this is all we want to be publicly
// accessible.
return defer;

})();

// Two cache prefixes. When storing values, all keys are prefixed
// to avoid collisions with other usage of the storage backend.
// If the stored value is given an expire time then a second key
Expand Down Expand Up @@ -390,16 +294,11 @@
LocacheCache.prototype.async = {

set: function (key, value, seconds) {
return defer(bind(function () {
return this.set(key, value, seconds);
}, this));
return Promise.resolve(this.set(key, value, seconds));
},

get: function (key) {
return defer(bind(function () {
return this.get(key);
}, this));

return Promise.resolve(this.get(key));
}

};
Expand Down
19 changes: 9 additions & 10 deletions build/locache.min.js

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

0 comments on commit 5cde108

Please sign in to comment.