Last active
August 29, 2015 14:14
-
-
Save viezel/ee7ccd939caae93424ae to your computer and use it in GitHub Desktop.
appcelerator - HTTP requsts queing system while being offline
This file contains hidden or 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
| // | |
| // Start the app while being offline | |
| // Then after these requests have been cache - go back online | |
| // | |
| var HTTPHelper = require("networkHelper"); | |
| // simulate some request while being offline | |
| for(var i = 0; i < 5; i++){ | |
| setTimeout(function(){ | |
| HTTPHelper.httpRequest({ | |
| url: "http://httpbin.org/post", | |
| settings: { | |
| timeout: 10000, | |
| method: "POST" | |
| }, | |
| success: function(){ | |
| Ti.API.info("SUCCESS"); | |
| }, | |
| error: function(e){ | |
| Ti.API.info(e); | |
| } | |
| }); | |
| }, 1500*i); | |
| } | |
| // at |
This file contains hidden or 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
| /** | |
| * Network helper | |
| * Dependent on underscore.js | |
| * @author Mads Møller | |
| * @version 1.2.0 | |
| * Copyright Napp ApS | |
| * www.napp.dk | |
| */ | |
| // get underscore | |
| var _ = require('alloy/underscore')._; | |
| /** | |
| * Simple HTTP Request | |
| * @param {Object} args | |
| */ | |
| exports.httpRequest = function(args) { | |
| // check if we are offline? | |
| if (!Ti.Network.online) { | |
| // fire error callback if enabled | |
| _.isFunction(args.error) && args.error({ | |
| code : 403, | |
| message : "offline" | |
| }); | |
| // save in request queue | |
| exports.saveRequestLocally(args); | |
| return; | |
| } | |
| // input settings | |
| var settings = args.settings || {}; | |
| // default settings | |
| var defaultSettings = { | |
| timeout : 10000, | |
| cache : true, | |
| method : "GET" | |
| }; | |
| // overrule the default settings with the inputs | |
| _.defaults(settings, defaultSettings); | |
| // create a http client | |
| var xhr = Ti.Network.createHTTPClient(settings); | |
| xhr.onload = function() { | |
| _.isFunction(args.success) && args.success(this.responseText); | |
| }; | |
| xhr.onerror = function(e) { | |
| Ti.API.error("[networkHelper] httpRequest onerror: "); | |
| Ti.API.error("code: " + e.code + " message: " + e.error); | |
| _.isFunction(args.error) && args.error(e); | |
| }; | |
| // headers | |
| for (var header in args.headers) { | |
| xhr.setRequestHeader(header, args.headers[header]); | |
| } | |
| // lets go | |
| xhr.open(settings.method, args.url); | |
| xhr.send(args.data); | |
| }; | |
| /** | |
| * Save the request data locally | |
| * @param {Object} args | |
| */ | |
| exports.saveRequestLocally = function(args) { | |
| // remove callbacks - because we dont want conflicting js contexts | |
| args.error = null; | |
| args.success = null; | |
| // get the current queue | |
| var storage = Ti.App.Properties.getObject("REQUEST_QUEUE", {}); | |
| var queue = storage.requests || []; | |
| // push the args onto the requests queue | |
| queue.push(args); | |
| // save it | |
| Ti.App.Properties.setObject("REQUEST_QUEUE", { | |
| requests : queue | |
| }); | |
| Ti.API.debug("[networkHelper] saveRequestLocally :: Queue requests in total: " + queue.length); | |
| }; | |
| /** | |
| * Run and reset the queued http requests | |
| * @param {Object} e | |
| */ | |
| var runQueuedRequests = function(e) { | |
| // we are back online | |
| if ((e && e.online) || Ti.Network.online) { | |
| // get the current queue | |
| var queue = Ti.App.Properties.getObject("REQUEST_QUEUE", {}); | |
| // reset the queue | |
| Ti.App.Properties.setObject("REQUEST_QUEUE", []); | |
| Ti.API.debug("[networkHelper] runQueuedRequests :: Queue requests in total: " + queue.requests.length); | |
| _.each(queue.requests, function(element) { | |
| Ti.API.debug("[networkHelper] runQueuedRequests :: Fire queued request " + element.url); | |
| exports.httpRequest(element); | |
| }); | |
| } | |
| }; | |
| // start listen to network changes | |
| Ti.Network.addEventListener('change', runQueuedRequests); | |
| // at app startup | |
| if (Ti.Network.online) { | |
| // check if we have any queued requests left | |
| runQueuedRequests(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment