Created
September 27, 2017 16:56
-
-
Save remy/2f5dbe7032004a5dd09bd57733d30ab6 to your computer and use it in GitHub Desktop.
Fitbit Ionic fetch abstraction (only supports JSON in this state)
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
// FILENAME: app/polyfills/fetch.js | |
import Promise from './promise'; | |
import * as messaging from "messaging"; | |
const OPEN = messaging.peerSocket.OPEN; | |
// Helpful to check whether we are connected or not. | |
// setInterval(() => { | |
// const readyState = messaging.peerSocket.readyState; | |
// console.log(`App running - Connectivity status=${readyState} | |
// Connected? ${readyState === OPEN ? 'YES' : 'no'}`); | |
// }, 3000); | |
// Listen for the onerror event | |
messaging.peerSocket.onerror = function(err) { | |
// Handle any errors | |
console.log("Connection error: " + err.code + " - " + err.message); | |
} | |
let guid = 0; | |
const promises = {}; | |
const queue = []; | |
function send(data) { | |
if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) { | |
messaging.peerSocket.send(data); | |
} else { | |
queue.push(data); | |
} | |
} | |
messaging.peerSocket.onopen = () => { | |
// give it a moment | |
setTimeout(() => { | |
for (let i = 0; i < queue.length; i++) { | |
send(queue[i]); | |
} | |
queue.length = 0; // ditch memory | |
}, 100); | |
} | |
export const fetch = url => { | |
return new Promise((resolve, reject) => { | |
guid++; | |
promises[guid] = { | |
resolve, reject, url | |
}; | |
send({ url, guid }); | |
}); | |
} | |
// Listen for the onmessage event | |
messaging.peerSocket.onmessage = event => { | |
// Output the message to the console | |
const { guid, url, json } = event.data; | |
promises[guid].resolve(json); | |
} |
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
// FILENAME: app/polyfills/promise.js | |
const global = {}; | |
!function(e){function n(){}function t(e,n){return function(){e.apply(n,arguments)}}function o(e){if("object"!=typeof this)throw new TypeError("Promises must be constructed via new");if("function"!=typeof e)throw new TypeError("not a function");this._state=0,this._handled=!1,this._value=void 0,this._deferreds=[],s(e,this)}function i(e,n){for(;3===e._state;)e=e._value;return 0===e._state?void e._deferreds.push(n):(e._handled=!0,void o._immediateFn(function(){var t=1===e._state?n.onFulfilled:n.onRejected;if(null===t)return void(1===e._state?r:u)(n.promise,e._value);var o;try{o=t(e._value)}catch(i){return void u(n.promise,i)}r(n.promise,o)}))}function r(e,n){try{if(n===e)throw new TypeError("A promise cannot be resolved with itself.");if(n&&("object"==typeof n||"function"==typeof n)){var i=n.then;if(n instanceof o)return e._state=3,e._value=n,void f(e);if("function"==typeof i)return void s(t(i,n),e)}e._state=1,e._value=n,f(e)}catch(r){u(e,r)}}function u(e,n){e._state=2,e._value=n,f(e)}function f(e){2===e._state&&0===e._deferreds.length&&o._immediateFn(function(){e._handled||o._unhandledRejectionFn(e._value)});for(var n=0,t=e._deferreds.length;n<t;n++)i(e,e._deferreds[n]);e._deferreds=null}function c(e,n,t){this.onFulfilled="function"==typeof e?e:null,this.onRejected="function"==typeof n?n:null,this.promise=t}function s(e,n){var t=!1;try{e(function(e){t||(t=!0,r(n,e))},function(e){t||(t=!0,u(n,e))})}catch(o){if(t)return;t=!0,u(n,o)}}var a=setTimeout;o.prototype["catch"]=function(e){return this.then(null,e)},o.prototype.then=function(e,t){var o=new this.constructor(n);return i(this,new c(e,t,o)),o},o.all=function(e){var n=Array.prototype.slice.call(e);return new o(function(e,t){function o(r,u){try{if(u&&("object"==typeof u||"function"==typeof u)){var f=u.then;if("function"==typeof f)return void f.call(u,function(e){o(r,e)},t)}n[r]=u,0===--i&&e(n)}catch(c){t(c)}}if(0===n.length)return e([]);for(var i=n.length,r=0;r<n.length;r++)o(r,n[r])})},o.resolve=function(e){return e&&"object"==typeof e&&e.constructor===o?e:new o(function(n){n(e)})},o.reject=function(e){return new o(function(n,t){t(e)})},o.race=function(e){return new o(function(n,t){for(var o=0,i=e.length;o<i;o++)e[o].then(n,t)})},o._immediateFn="function"==typeof setImmediate&&function(e){setImmediate(e)}||function(e){a(e,0)},o._unhandledRejectionFn=function(e){"undefined"!=typeof console&&console&&console.warn("Possible Unhandled Promise Rejection:",e)},o._setImmediateFn=function(e){o._immediateFn=e},o._setUnhandledRejectionFn=function(e){o._unhandledRejectionFn=e},"undefined"!=typeof module&&module.exports?module.exports=o:e.Promise||(e.Promise=o)}(global); | |
export default global.Promise; |
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
// FILENAME: companion/index.js | |
import * as messaging from "messaging"; | |
messaging.peerSocket.onmessage = event => { | |
const { url, guid } = event.data; | |
fetch(url).then(res => res.json()).then(json => { | |
messaging.peerSocket.send({ url, guid, json }); | |
}).catch(e => console.log(e)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment