Change Widget#attach signature

Change Widget#attach signature

Take a fallback option instead of a list of names

diff --git a/app/assets/javascripts/discourse/widgets/user-notifications-large.js.es6 b/app/assets/javascripts/discourse/widgets/user-notifications-large.js.es6
index f94e174..f6d3acd 100644
--- a/app/assets/javascripts/discourse/widgets/user-notifications-large.js.es6
+++ b/app/assets/javascripts/discourse/widgets/user-notifications-large.js.es6
@@ -15,13 +15,13 @@ createWidget("large-notification-item", {
     const notificationName =
       this.site.notificationLookup[attrs.notification_type];
 
-    const widgetNames = [
-      `${notificationName.dasherize()}-notification-item`,
-      "default-notification-item"
-    ];
-
     return [
-      this.attach(widgetNames, attrs),
+      this.attach(
+        `${notificationName.dasherize()}-notification-item`,
+        attrs,
+        {},
+        { fallbackWidgetName: 'default-notification-item' },
+      ),
       h("span.time", dateNode(attrs.created_at))
     ];
   }
diff --git a/app/assets/javascripts/discourse/widgets/user-notifications.js.es6 b/app/assets/javascripts/discourse/widgets/user-notifications.js.es6
index 009fefe..579e18f 100644
--- a/app/assets/javascripts/discourse/widgets/user-notifications.js.es6
+++ b/app/assets/javascripts/discourse/widgets/user-notifications.js.es6
@@ -94,12 +94,14 @@ export default createWidget("user-notifications", {
           const notificationName =
             this.site.notificationLookup[notificationAttrs.notification_type];
 
-          const widgetNames = [
-            `${notificationName.dasherize()}-notification-item`,
-            "default-notification-item"
-          ];
-
-          return this.attach(widgetNames, notificationAttrs);
+          return (
+            this.attach(
+              `${notificationName.dasherize()}-notification-item`,
+              notificationAttrs,
+              {},
+              { fallbackWidgetName: 'default-notification-item' }
+            )
+          );
         });
 
       result.push(h("hr"));
diff --git a/app/assets/javascripts/discourse/widgets/widget.js.es6 b/app/assets/javascripts/discourse/widgets/widget.js.es6
index 27f3baf..dc011c7 100644
--- a/app/assets/javascripts/discourse/widgets/widget.js.es6
+++ b/app/assets/javascripts/discourse/widgets/widget.js.es6
@@ -196,27 +196,31 @@ export default class Widget {
     }
   }
 
-  attach(widgetNames, attrs, opts) {
-    widgetNames = [].concat(widgetNames);
-    let WidgetClass = null;
-
-    for (let widgetName of widgetNames) {
-      WidgetClass = _registry[widgetName];
-      if (WidgetClass) {
-        break;
-      }
+  lookupWidgetClass(widgetName) {
+    let WidgetClass = _registry[widgetName];
+    if (WidgetClass) {
+      return WidgetClass;
+    }
 
-      if (!this.register) {
-        // eslint-disable-next-line no-console
-        console.error("couldn't find register");
-        return;
-      }
+    if (!this.register) {
+      // eslint-disable-next-line no-console
+      console.error("couldn't find register");
+      return null;
+    }
 
-      WidgetClass = this.register.lookupFactory(`widget:${widgetName}`);
-      if (WidgetClass && WidgetClass.class) {
-        WidgetClass = WidgetClass.class;
-        break;
-      }
+    WidgetClass = this.register.lookupFactory(`widget:${widgetName}`);
+    if (WidgetClass && WidgetClass.class) {
+      return WidgetClass.class;
+    }
+
+    return null;
+  }
+
+  attach(widgetName, attrs, opts, otherOpts = {}) {
+    let WidgetClass = this.lookupWidgetClass(widgetName);
+
+    if (!WidgetClass && otherOpts.fallbackWidgetName) {
+      WidgetClass = this.lookupWidgetClass(otherOpts.fallbackWidgetName);
     }
 
     if (WidgetClass) {
@@ -225,7 +229,7 @@ export default class Widget {
       result.dirtyKeys = this.dirtyKeys;
       return result;
     } else {
-      throw new Error(`Couldn't find ${widgetNames} factory`);
+      throw new Error(`Couldn't find ${widgetName} or fallback ${otherOpts.fallbackWidgetName}`);
     }
   }

GitHub sha: 65a6e2c8