discourse/plugins/discourse-narrative-bot/assets/javascripts/initializers/new-user-narrative.js
Jarek Radosz 19214aff18
DEV: Clean up all message bus subscriptions (#19268)
1. "What Goes Up Must Come Down" – if you subscribe to message bus, make sure you also unsubscribe
2. When you unsubscribe - remove only your subscription, not **all** subscriptions on given channel

Attempt #2. The first attempt tried to extend a core `@bound` method in new-user-narrative plugin which did not work. I reworked that plugin in the meantime. This new PR also cleans up message bus subscriptions in now core-merged chat plugin.
2022-12-12 16:32:25 +01:00

62 lines
1.4 KiB
JavaScript

import { withPluginApi } from "discourse/lib/plugin-api";
import { bind } from "discourse-common/utils/decorators";
export default {
name: "new-user-narrative",
initialize(container) {
const siteSettings = container.lookup("service:site-settings");
if (!siteSettings.discourse_narrative_bot_enabled) {
return;
}
this.messageBus = container.lookup("service:message-bus");
this.appEvents = container.lookup("service:app-events");
withPluginApi("0.8.7", (api) => {
const currentUser = api.getCurrentUser();
if (!currentUser) {
return;
}
api.dispatchWidgetAppEvent(
"site-header",
"header",
"header:search-context-trigger"
);
api.attachWidgetAction(
"header",
"headerSearchContextTrigger",
function () {
if (this.site.mobileView) {
this.state.skipSearchContext = false;
} else {
this.state.contextEnabled = true;
this.state.searchContextType = "topic";
}
}
);
this.messageBus.subscribe(
"/new_user_narrative/tutorial_search",
this.onMessage
);
});
},
teardown() {
this.messageBus?.unsubscribe(
"/new_user_narrative/tutorial_search",
this.onMessage
);
},
@bind
onMessage() {
this.appEvents.trigger("header:search-context-trigger");
},
};