Created
February 16, 2015 21:30
-
-
Save rik/8c60533b9fc0a4da3d6c to your computer and use it in GitHub Desktop.
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
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