diff --git a/coffee/offline.coffee b/coffee/offline.coffee index df50587..70ba653 100644 --- a/coffee/offline.coffee +++ b/coffee/offline.coffee @@ -14,7 +14,6 @@ extendNative = (to, from) -> Offline = {} -Offline.options = if window.Offline then window.Offline.options or {} else {} defaultOptions = checks: xhr: @@ -240,7 +239,8 @@ Offline.onXHR = (cb) -> extendNative window.XDomainRequest, _XDomainRequest -init = -> +Offline.init = (options)-> + Offline.options = options or {} if Offline.getOption 'interceptRequests' Offline.onXHR ({xhr}) -> unless xhr.offline is false @@ -248,8 +248,9 @@ init = -> if Offline.getOption 'checkOnLoad' Offline.check() + if Offline.reconnect + Offline.reconnect.reset() # We call init in a setTimeout to give time for options to be set -setTimeout init, 0 -window.Offline = Offline +module.exports = Offline \ No newline at end of file diff --git a/coffee/reconnect.coffee b/coffee/reconnect.coffee index 28086e7..123174c 100644 --- a/coffee/reconnect.coffee +++ b/coffee/reconnect.coffee @@ -1,5 +1,5 @@ -unless window.Offline - throw new Error "Offline Reconnect brought in without offline.js" + +Offline = require('offline') rc = Offline.reconnect = {} @@ -39,7 +39,7 @@ down = -> reset() rc.state = 'waiting' - + Offline.trigger 'reconnect:started' retryIntv = setInterval tick, 1000 @@ -59,7 +59,7 @@ nope = -> rc.tryNow = tryNow -reset() +Offline.reconnect.reset = reset Offline.on 'down', down Offline.on 'confirmed-down', nope diff --git a/js/offline.js b/js/offline.js index 30bbea9..02163d9 100644 --- a/js/offline.js +++ b/js/offline.js @@ -1,5 +1,5 @@ (function() { - var Offline, checkXHR, defaultOptions, extendNative, grab, handlers, init; + var Offline, checkXHR, defaultOptions, extendNative, grab, handlers; extendNative = function(to, from) { var e, key, results, val; @@ -21,8 +21,6 @@ Offline = {}; - Offline.options = window.Offline ? window.Offline.options || {} : {}; - defaultOptions = { checks: { xhr: { @@ -287,7 +285,8 @@ } }; - init = function() { + Offline.init = function(options) { + Offline.options = options || {}; if (Offline.getOption('interceptRequests')) { Offline.onXHR(function(arg) { var xhr; @@ -298,12 +297,13 @@ }); } if (Offline.getOption('checkOnLoad')) { - return Offline.check(); + Offline.check(); + } + if (Offline.reconnect) { + return Offline.reconnect.reset(); } }; - setTimeout(init, 0); - - window.Offline = Offline; + module.exports = Offline; }).call(this); diff --git a/offline.js b/offline.js index 4c02d59..c37af43 100644 --- a/offline.js +++ b/offline.js @@ -1,6 +1,6 @@ /*! offline-js 0.7.19 */ (function() { - var Offline, checkXHR, defaultOptions, extendNative, grab, handlers, init; + var Offline, checkXHR, defaultOptions, extendNative, grab, handlers; extendNative = function(to, from) { var key, results, val; results = []; @@ -10,8 +10,7 @@ _error; } return results; - }, Offline = {}, Offline.options = window.Offline ? window.Offline.options || {} :{}, - defaultOptions = { + }, Offline = {}, defaultOptions = { checks:{ xhr:{ url:function() { @@ -132,16 +131,15 @@ var req; return req = new _XDomainRequest(), monitorXHR(req), req; }, extendNative(window.XDomainRequest, _XDomainRequest); - }, init = function() { - if (Offline.getOption("interceptRequests") && Offline.onXHR(function(arg) { + }, Offline.init = function(options) { + if (Offline.options = options || {}, Offline.getOption("interceptRequests") && Offline.onXHR(function(arg) { var xhr; if (xhr = arg.xhr, !1 !== xhr.offline) return checkXHR(xhr, Offline.markUp, Offline.confirmDown); - }), Offline.getOption("checkOnLoad")) return Offline.check(); - }, setTimeout(init, 0), window.Offline = Offline; + }), Offline.getOption("checkOnLoad") && Offline.check(), Offline.reconnect) return Offline.reconnect.reset(); + }, module.exports = Offline; }).call(this), function() { - var down, next, nope, rc, reset, retryIntv, tick, tryNow, up; - if (!window.Offline) throw new Error("Offline Reconnect brought in without offline.js"); - rc = Offline.reconnect = {}, retryIntv = null, reset = function() { + var Offline, down, next, nope, rc, reset, retryIntv, tick, tryNow, up; + Offline = require("offline"), rc = Offline.reconnect = {}, retryIntv = null, reset = function() { var ref; return null != rc.state && "inactive" !== rc.state && Offline.trigger("reconnect:stopped"), rc.state = "inactive", rc.remaining = rc.delay = null != (ref = Offline.getOption("reconnect.initialDelay")) ? ref :3; @@ -163,8 +161,8 @@ }, nope = function() { if (Offline.getOption("reconnect")) return "connecting" === rc.state ? (Offline.trigger("reconnect:failure"), rc.state = "waiting", next()) :void 0; - }, rc.tryNow = tryNow, reset(), Offline.on("down", down), Offline.on("confirmed-down", nope), - Offline.on("up", up); + }, rc.tryNow = tryNow, Offline.reconnect.reset = reset, Offline.on("down", down), + Offline.on("confirmed-down", nope), Offline.on("up", up); }.call(this), function() { var clear, flush, held, holdRequest, makeRequest, waitingOnConfirm; if (!window.Offline) throw new Error("Requests module brought in without offline.js"); diff --git a/offline.min.js b/offline.min.js index 469e4b7..3cd62f3 100644 --- a/offline.min.js +++ b/offline.min.js @@ -1,2 +1,2 @@ /*! offline-js 0.7.19 */ -(function(){var a,b,c,d,e,f,g;d=function(a,b){var c,d,e;d=[];for(c in b.prototype)try{e=b.prototype[c],null==a[c]&&"function"!=typeof e?d.push(a[c]=e):d.push(void 0)}catch(f){f}return d},a={},a.options=window.Offline?window.Offline.options||{}:{},c={checks:{xhr:{url:function(){return"/favicon.ico?_="+(new Date).getTime()},timeout:5e3,type:"HEAD"},image:{url:function(){return"/favicon.ico?_="+(new Date).getTime()}},active:"xhr"},checkOnLoad:!1,interceptRequests:!0,reconnect:!0,deDupBody:!1},e=function(a,b){var c,d,e,f,g,h;for(c=a,h=b.split("."),d=e=0,f=h.length;e1){for(j=[],h=0,i=g.length;h=12e3)&&f(),"function"==typeof b?b.apply(null,arguments):void 0})}),Offline.requests={flush:b,clear:a}},0)}.call(this),function(){var a,b,c,d,e,f;if(!Offline)throw new Error("Offline simulate brought in without offline.js");for(d=["up","down"],b=0,c=d.length;b=b)return e=Math.floor(a/b),[e,c];return["now",""]},l=function(){var c,h;return g=f(b),document.body.appendChild(g),null!=Offline.reconnect&&Offline.getOption("reconnect")&&(g.appendChild(f(a)),c=g.querySelector(".offline-ui-retry"),h=function(a){return a.preventDefault(),Offline.reconnect.tryNow()},null!=c.addEventListener?c.addEventListener("click",h,!1):c.attachEvent("click",h)),d("offline-ui-"+Offline.state),e=g.querySelector(".offline-ui-content")},j=function(){return l(),Offline.on("up",function(){return k("offline-ui-down"),d("offline-ui-up"),h("offline-ui-up-2s",2),h("offline-ui-up-5s",5)}),Offline.on("down",function(){return k("offline-ui-up"),d("offline-ui-down"),h("offline-ui-down-2s",2),h("offline-ui-down-5s",5)}),Offline.on("reconnect:connecting",function(){return d("offline-ui-connecting"),k("offline-ui-waiting")}),Offline.on("reconnect:tick",function(){var a,b,c;return d("offline-ui-waiting"),k("offline-ui-connecting"),a=m(Offline.reconnect.remaining),b=a[0],c=a[1],e.setAttribute("data-retry-in-value",b),e.setAttribute("data-retry-in-unit",c)}),Offline.on("reconnect:stopped",function(){return k("offline-ui-connecting offline-ui-waiting"),e.setAttribute("data-retry-in-value",null),e.setAttribute("data-retry-in-unit",null)}),Offline.on("reconnect:failure",function(){return h("offline-ui-reconnect-failed-2s",2),h("offline-ui-reconnect-failed-5s",5)}),Offline.on("reconnect:success",function(){return h("offline-ui-reconnect-succeeded-2s",2),h("offline-ui-reconnect-succeeded-5s",5)})},"complete"===document.readyState?j():null!=document.addEventListener?document.addEventListener("DOMContentLoaded",j,!1):(c=document.onreadystatechange,document.onreadystatechange=function(){return"complete"===document.readyState&&j(),"function"==typeof c?c.apply(null,arguments):void 0})}.call(this); \ No newline at end of file +(function(){var a,b,c,d,e,f;d=function(a,b){var c,d,e;d=[];for(c in b.prototype)try{e=b.prototype[c],null==a[c]&&"function"!=typeof e?d.push(a[c]=e):d.push(void 0)}catch(f){f}return d},a={},c={checks:{xhr:{url:function(){return"/favicon.ico?_="+(new Date).getTime()},timeout:5e3,type:"HEAD"},image:{url:function(){return"/favicon.ico?_="+(new Date).getTime()}},active:"xhr"},checkOnLoad:!1,interceptRequests:!0,reconnect:!0,deDupBody:!1},e=function(a,b){var c,d,e,f,g,h;for(c=a,h=b.split("."),d=e=0,f=h.length;e1){for(j=[],h=0,i=g.length;h=12e3)&&f(),"function"==typeof b?b.apply(null,arguments):void 0})}),Offline.requests={flush:b,clear:a}},0)}.call(this),function(){var a,b,c,d,e,f;if(!Offline)throw new Error("Offline simulate brought in without offline.js");for(d=["up","down"],b=0,c=d.length;b=b)return e=Math.floor(a/b),[e,c];return["now",""]},l=function(){var c,h;return g=f(b),document.body.appendChild(g),null!=Offline.reconnect&&Offline.getOption("reconnect")&&(g.appendChild(f(a)),c=g.querySelector(".offline-ui-retry"),h=function(a){return a.preventDefault(),Offline.reconnect.tryNow()},null!=c.addEventListener?c.addEventListener("click",h,!1):c.attachEvent("click",h)),d("offline-ui-"+Offline.state),e=g.querySelector(".offline-ui-content")},j=function(){return l(),Offline.on("up",function(){return k("offline-ui-down"),d("offline-ui-up"),h("offline-ui-up-2s",2),h("offline-ui-up-5s",5)}),Offline.on("down",function(){return k("offline-ui-up"),d("offline-ui-down"),h("offline-ui-down-2s",2),h("offline-ui-down-5s",5)}),Offline.on("reconnect:connecting",function(){return d("offline-ui-connecting"),k("offline-ui-waiting")}),Offline.on("reconnect:tick",function(){var a,b,c;return d("offline-ui-waiting"),k("offline-ui-connecting"),a=m(Offline.reconnect.remaining),b=a[0],c=a[1],e.setAttribute("data-retry-in-value",b),e.setAttribute("data-retry-in-unit",c)}),Offline.on("reconnect:stopped",function(){return k("offline-ui-connecting offline-ui-waiting"),e.setAttribute("data-retry-in-value",null),e.setAttribute("data-retry-in-unit",null)}),Offline.on("reconnect:failure",function(){return h("offline-ui-reconnect-failed-2s",2),h("offline-ui-reconnect-failed-5s",5)}),Offline.on("reconnect:success",function(){return h("offline-ui-reconnect-succeeded-2s",2),h("offline-ui-reconnect-succeeded-5s",5)})},"complete"===document.readyState?j():null!=document.addEventListener?document.addEventListener("DOMContentLoaded",j,!1):(c=document.onreadystatechange,document.onreadystatechange=function(){return"complete"===document.readyState&&j(),"function"==typeof c?c.apply(null,arguments):void 0})}.call(this); \ No newline at end of file