Bug 1234020: Part 2m - [webext] Return promises from the notifications API. r?rpl draft
authorKris Maglione <maglione.k@gmail.com>
Thu, 04 Feb 2016 13:34:11 -0800
changeset 328986 e13030016edf058b1cb02b7dcc031af91103b666
parent 328985 bb467ff2b3df47316edfafe39581d316ffad4d2e
child 513886 b5b65caf2d5502de58e4356df69caaaadb73a69b
push id10445
push usermaglione.k@gmail.com
push dateThu, 04 Feb 2016 21:38:16 +0000
reviewersrpl
bugs1234020
milestone47.0a1
Bug 1234020: Part 2m - [webext] Return promises from the notifications API. r?rpl
toolkit/components/extensions/ext-notifications.js
--- 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);
         };