-
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
Ruben
committed
Feb 14, 2011
1 parent
57d6496
commit cb39f5b
Showing
44 changed files
with
584 additions
and
2 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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,205 @@ | ||
/** | ||
* General Purpose Offline Syncing Library | ||
* | ||
* @event losechanges Fires before the offline state is removed. | ||
* cancelable: Prevents the application from losing it's recorded offline state. | ||
* @event beforeoffline Fires before bringing the application offline. | ||
* cancelable: Prevents the application from going offline | ||
* @event afteroffline Firest after the application is brought offline. | ||
* @event beforeonline Fires before bringing the application online. | ||
* cancelable: Prevents the application from going online | ||
* @event afteronline Fires after the application is brought online. | ||
* @event beforeload Fires before loading the offline state into this application. | ||
* cancelable: Prevents the application from reloading it's offline state. | ||
* @event sync Fires at each sync item's completion. | ||
* object: | ||
* {Number} position the number of the item in the list that is currently processed. | ||
* {Number} length the total number of items in the list. | ||
* | ||
* @property {Number} progress the progress of the sync. A number between 0 and 1. | ||
* @property {Number} position the progress of the sync. | ||
* @property {Number} length the total length of items to sync. | ||
* @property {Boolean} syncing whether the application is syncing while coming online. | ||
* @property {Boolean} onLine whether the application is online. This property is false during sync. | ||
* | ||
* @copyright 2010, Ajax.org B.V. | ||
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt> | ||
*/ | ||
require.def("ext/offline/lib-offline", | ||
function() { | ||
|
||
var Offline = function(namespace, detectUrl){ | ||
/** | ||
* whether the application is online. | ||
* @type {Boolean} | ||
*/ | ||
this.onLine = -1; | ||
this.detectUrl = detectUrl; | ||
this.interval = 5000; | ||
this.namespace = namespace; | ||
|
||
//navigator.onLine | ||
var cache = window.applicationCache; | ||
|
||
//@todo this is non-ie for now | ||
|
||
cache.addEventListener("offline", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("online", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("checking", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("downloading", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("progress", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("cached", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("noupdate", function(e){ | ||
console.log(e.type); | ||
}); | ||
|
||
cache.addEventListener("updateready", function(e){ | ||
console.log(e.type); | ||
cache.swapCache(); | ||
}); | ||
|
||
cache.addEventListener("error", function(e){ | ||
console.log(e.type); | ||
}); | ||
}; | ||
|
||
(function(){ | ||
|
||
this.start = function(){ | ||
this.offlineTime = parseInt(localStorage[this.namespace + ".offlinetime"]); | ||
|
||
//If we were offline lets stay offline | ||
if (this.offlineTime) | ||
this.goOffline(); | ||
else //Else we try to go online | ||
this.goOnline(); | ||
|
||
this.startDetect(); | ||
} | ||
|
||
/**** Offline Detection ****/ | ||
|
||
this.isSiteAvailable = function(callback){ | ||
var _self = this; | ||
|
||
this.http.get(apf.getNoCacheUrl(this.detectUrl), { | ||
callback: function(data, state, extra){ | ||
if(state != apf.SUCCESS || !window.navigator.onLine){ | ||
_self.goOffline(callback); //retry here?? | ||
} | ||
else{ | ||
_self.goOnline(callback); | ||
} | ||
}, | ||
ignoreOffline : true, | ||
hideLogMessage : true | ||
}); | ||
}; | ||
|
||
this.startDetect = function(){ | ||
if (this.detectErrorHandler) //Detection already started | ||
return; | ||
|
||
var _self = this; | ||
|
||
apf.addEventListener("error", this.detectErrorHandler = function(e){ | ||
//Timeout detected.. Network is probably gone | ||
if (e.state == apf.TIMEOUT) { | ||
//Let's try to go offline and return false to cancel the error | ||
return !_self.goOffline();//callback //@todo callback??? | ||
} | ||
}); | ||
|
||
this.http = new apf.http(); | ||
this.http.timeout = this.interval; | ||
|
||
//Check if we have connection right now | ||
this.isSiteAvailable(); | ||
|
||
//#ifdef __DEBUG | ||
apf.console.info("Started automatic detection of network state"); | ||
//#endif | ||
|
||
this.detectTimer = setInterval(function(){ | ||
_self.isSiteAvailable(); | ||
}, this.interval); | ||
} | ||
|
||
this.stopDetect = function(){ | ||
clearInterval(this.detectTimer); | ||
apf.removeEventListener("error", this.detectErrorHandler); | ||
|
||
//#ifdef __DEBUG | ||
apf.console.info("Stopped automatic detection of network state"); | ||
//#endif | ||
} | ||
|
||
/**** Offline State Management ****/ | ||
|
||
/** | ||
* Brings the application offline. | ||
*/ | ||
this.goOffline = function(){ | ||
if (this.onLine === false) | ||
return false; | ||
|
||
if (this.dispatchEvent("beforeoffline") === false) | ||
return false; | ||
|
||
//We're offline, let's dim the light | ||
this.onLine = false; | ||
|
||
if (!this.offlineTime) { | ||
this.offlineTime = new Date().getTime(); | ||
localStorage[this.namespace + ".offlinetime"] = this.offlineTime; | ||
} | ||
|
||
this.dispatchEvent("afteroffline"); | ||
|
||
return true;//success | ||
} | ||
|
||
/** | ||
* Brings the application online. | ||
*/ | ||
this.goOnline = function(){ | ||
if (this.onLine === true) | ||
return false; | ||
|
||
if (this.dispatchEvent("beforeonline") === false) | ||
return false; | ||
|
||
//We're online, let's show the beacon | ||
this.onlineTime = new Date().getTime(); | ||
this.onLine = true; //@todo Think about doing this in the callback, because of processes that will now intersect | ||
this.offlineTime = null; | ||
|
||
delete localStorage[this.namespace + ".offlinetime"]; | ||
|
||
this.dispatchEvent("afteronline"); | ||
|
||
return true;//success | ||
} | ||
}).call(Offline.prototype = new apf.Class().$init()); | ||
|
||
return Offline; | ||
|
||
}); |
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,98 @@ | ||
require.def("ext/offline/lib-sync", | ||
function() { | ||
|
||
var Sync = function(namespace){ | ||
this.namespace = namespace; | ||
|
||
this.items = localStorage[this.namespace + ".syncitems"] | ||
? apf.unserialize(localStorage[this.namespace + ".syncitems"]) | ||
: {length: 100}; | ||
|
||
}; | ||
|
||
(function(){ | ||
this.getLength = function(){ | ||
return this.items.length; | ||
} | ||
|
||
/** | ||
* Clears all offline data. | ||
*/ | ||
this.clear = function(){ | ||
if (!this.enabled) | ||
return false; | ||
|
||
//#ifdef __DEBUG | ||
apf.console.info("Clearing all offline and state cache"); | ||
//#endif | ||
|
||
this.items = {length: 0}; | ||
localStorage[this.namespace + ".syncitems"] = apf.serialize(this.items); | ||
} | ||
|
||
this.add = function(id, syncItem){ | ||
this.items[id] = syncItem; | ||
this.items.length++; | ||
console.log("SYNC"); | ||
localStorage[this.namespace + ".syncitems"] = apf.serialize(this.items); | ||
|
||
//@todo error handling | ||
} | ||
|
||
/** | ||
* Does cleanup after we've come online | ||
* @private | ||
*/ | ||
this.start = function(handler){ | ||
if (this.syncing) | ||
return; | ||
|
||
var syncItems = apf.extend({}, this.items), | ||
syncLength = this.items.length, | ||
len, i; | ||
|
||
var _self = this; | ||
var next = function(){ | ||
if (!_self.syncing) | ||
return false; | ||
|
||
syncItems.length--; | ||
localStorage[_self.namespace + ".syncitems"] = apf.serialize(syncItems); //Save state up to now | ||
|
||
if (syncItems.length < 0) { | ||
_self.items = {length: 0}; | ||
localStorage[_self.namespace + ".syncitems"] = apf.serialize(_self.items); | ||
|
||
return -1; | ||
} | ||
|
||
for (var id in syncItems) { | ||
if (id == "length") continue; | ||
var item = syncItems[id]; | ||
delete syncItems[id]; | ||
break; | ||
} | ||
|
||
handler({ | ||
item : item, | ||
progress : parseInt((syncLength - syncItems.length)/syncLength*100), | ||
position : (syncLength - syncItems.length), | ||
length : syncLength | ||
}, next); | ||
|
||
return 1; | ||
} | ||
|
||
this.syncing = true; | ||
next(); | ||
}; | ||
|
||
this.stop = function(){ | ||
if (this.syncing) | ||
this.syncing = false; | ||
}; | ||
}).call(Sync.prototype); | ||
|
||
return Sync; | ||
|
||
}); |
Oops, something went wrong.