Bug 1234020: Part 2m - [webext] Return promises from the notifications API. r?rpl
--- a/toolkit/components/extensions/ext-notifications.js
+++ b/toolkit/components/extensions/ext-notifications.js
@@ -1,17 +1,16 @@
"use strict";
var { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/ExtensionUtils.jsm");
var {
EventManager,
ignoreEvent,
- runSafe,
} = ExtensionUtils;
// WeakMap[Extension -> Set[Notification]]
var notificationsMap = new WeakMap();
// WeakMap[Extension -> Set[callback]]
var notificationCallbacksMap = new WeakMap();
@@ -51,17 +50,17 @@ Notification.prototype = {
notificationsMap.get(this.extension).delete(this);
},
observe(subject, topic, data) {
if (topic != "alertfinished") {
return;
}
- for (let callback in notificationCallbacksMap.get(this.extension)) {
+ for (let callback of notificationCallbacksMap.get(this.extension)) {
callback(this);
}
notificationsMap.get(this.extension).delete(this);
},
};
/* eslint-disable mozilla/balanced-listeners */
@@ -96,41 +95,37 @@ extensions.registerPrivilegedAPI("notifi
notificationId = nextId++;
}
// FIXME: Lots of options still aren't supported, especially
// buttons.
let notification = new Notification(extension, notificationId, options);
notificationsMap.get(extension).add(notification);
- if (callback) {
- runSafe(context, callback, notificationId);
- }
+ return context.wrapPromise(Promise.resolve(notificationId), callback);
},
clear: function(notificationId, callback) {
let notifications = notificationsMap.get(extension);
let cleared = false;
for (let notification of notifications) {
if (notification.id == notificationId) {
notification.clear();
cleared = true;
break;
}
}
- if (callback) {
- runSafe(context, callback, cleared);
- }
+ return context.wrapPromise(Promise.resolve(cleared), callback);
},
getAll: function(callback) {
let notifications = notificationsMap.get(extension);
notifications = Array.from(notifications, notification => notification.id);
- runSafe(context, callback, notifications);
+ return context.wrapPromise(Promise.resolve(notifications), callback);
},
onClosed: new EventManager(context, "notifications.onClosed", fire => {
let listener = notification => {
// FIXME: Support the byUser argument.
fire(notification.id, true);
};