Created
May 17, 2016 07:07
-
-
Save itsmethemojo/51e06ded54c9035abe71412eaddcbf8e to your computer and use it in GitHub Desktop.
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
function _isEmpty = function(obj) { | |
if ('undefined' !== Object.keys) { | |
return (0 === Object.keys(obj).length); | |
} | |
for(var prop in obj) { | |
if(obj.hasOwnProperty(prop)) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
// called whenever a checkin is requested to be displayed | |
function checkCheckinStatus( checkinID ) { | |
var tS = Math.floor(now.getTime() / 1000), | |
removeCheckinFromApp = function(cid) { | |
// remove from data | |
delete app.data[cid]; | |
// update local cache | |
localforage.setItem('flightData',app.data); | |
// trigger event for updating UI elements | |
var event = new CustomEvent( | |
'updatedData', | |
{detail: {modified: cid}} | |
); | |
document.dispatchEvent(event); | |
}; | |
// no data or no checkin data? - return | |
if(_isEmpty(app.data) || !app.data[checkinID]) { | |
return false; | |
} | |
// remove only in case arrival time is min. 48 hours in past | |
if((tS - app.data[checkinID].ticket.arrivalTimestamp) < (60 * 60 * 48) ) { return false;} | |
if ('serviceWorker' in navigator) { | |
// delete cache of flight in service worker... | |
app.sendMessage( { | |
command: 'deleteCheckin', | |
keyID: checkinID } | |
).then(function(data) { | |
// remove the checkin from app data... | |
removeCheckinFromApp(checkinID); | |
}).catch(e) { | |
// could not remove checkin from service worker | |
}; | |
} else { | |
removeCheckinFromApp(checkinID); | |
} | |
} | |
// send data to the service worker | |
app.sendMessage = function(message) { | |
return new Promise(function(resolve, reject) { | |
var messageChannel = new MessageChannel(); | |
// the onmessage handler | |
messageChannel.port1.onmessage = function(event) { | |
if (event.data.error) { | |
reject(event.data.error); | |
} else { | |
resolve(event.data); | |
} | |
}; | |
if(!navigator.serviceWorker.controller){ | |
return; | |
} | |
// This sends the message data and port to the service worker. | |
// The service worker can use the port to reply via postMessage(), which | |
// will he onmessage handler on messageChannel.port1. | |
navigator.serviceWorker | |
.controller.postMessage(message,[messageChannel.port2]); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment