Skip to content

Instantly share code, notes, and snippets.

@rik
Created February 16, 2015 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rik/8c60533b9fc0a4da3d6c to your computer and use it in GitHub Desktop.
Save rik/8c60533b9fc0a4da3d6c to your computer and use it in GitHub Desktop.
ensure_endpoint: function() {
if (!navigator.push) {
// Do nothing when push notifications are not supported
console.log('no support for push notifications');
return Promise.resolve();
}
if (ensure_endpoint_in_progress) {
return Promise.resolve();
}
ensure_endpoint_in_progress = true;
function ensure_endpoint_done() {
ensure_endpoint_in_progress = false;
}
return asyncStorage.getItem(ENDPOINT_ID_KEY).then(function(endpoint) {
if (endpoint) {
return Promise.resolve();
}
var defer = Utils.defer();
var req = navigator.push.register();
req.onsuccess = function() {
defer.resolve(req.result);
};
req.onerror = function() {
defer.reject();
};
return defer.promise.then(function(endpoint_url) {
var set_promise = asyncStorage.setItem(ENDPOINT_ID_KEY, endpoint_url);
return Promise.all([endpoint_url, set_promise]);
}).then(function(endpoint_url) {
return SumoDB.register_push_endpoint(endpoint_url);
}).catch(function() {
return asyncStorage.removeItem(ENDPOINT_ID_KEY);
});
}).then(ensure_endpoint_done, ensure_endpoint_done);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment