UNPKG

@beenotung/tslib

Version:
62 lines 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.showNotification = exports.requireNotification = exports.isSupportNotification = void 0; const tslib_1 = require("tslib"); const defer_1 = require("./async/defer"); function isSupportNotification() { return 'Notification' in window; } exports.isSupportNotification = isSupportNotification; function requireNotification() { return tslib_1.__awaiter(this, void 0, void 0, function* () { if (!isSupportNotification()) { return false; } const defer = defer_1.createDefer(); Notification.requestPermission(res => { if (res === 'granted') { defer.resolve(true); } else if (res === 'denied') { defer.resolve(false); } else { defer.reject(new Error('unexpected result:' + res)); } }); return defer.promise; }); } exports.requireNotification = requireNotification; /** * alert can be used as fallback, otherwise will reject the promise * */ function showNotification(msg, options, useAlert = true) { return tslib_1.__awaiter(this, void 0, void 0, function* () { function fallback() { if (useAlert) { return alert(msg); } else { throw new Error('Notification is not supported'); } } if (!(yield requireNotification()) || !('ServiceWorkerRegistration' in window)) { return fallback(); } try { return ServiceWorkerRegistration.prototype.showNotification(msg, options); } catch (e) { try { return new Notification(msg, options); } catch (e) { return fallback(); } } }); } exports.showNotification = showNotification; //# sourceMappingURL=notification.js.map