Deprecates global use of `Discourse.MessageBus`
We can use DI for this, which makes it easier for plugins to subscribe to the message bus.
This commit is contained in:
parent
9395bbbbea
commit
69851bc6cf
|
@ -1,11 +1,11 @@
|
||||||
import showModal from 'discourse/lib/show-modal';
|
import showModal from 'discourse/lib/show-modal';
|
||||||
|
|
||||||
|
const LOG_CHANNEL = "/admin/backups/logs";
|
||||||
|
|
||||||
export default Discourse.Route.extend({
|
export default Discourse.Route.extend({
|
||||||
|
|
||||||
LOG_CHANNEL: "/admin/backups/logs",
|
|
||||||
|
|
||||||
activate() {
|
activate() {
|
||||||
Discourse.MessageBus.subscribe(this.LOG_CHANNEL, this._processLogMessage.bind(this));
|
this.messageBus.subscribe(LOG_CHANNEL, this._processLogMessage.bind(this));
|
||||||
},
|
},
|
||||||
|
|
||||||
_processLogMessage(log) {
|
_processLogMessage(log) {
|
||||||
|
@ -40,7 +40,7 @@ export default Discourse.Route.extend({
|
||||||
},
|
},
|
||||||
|
|
||||||
deactivate() {
|
deactivate() {
|
||||||
Discourse.MessageBus.unsubscribe(this.LOG_CHANNEL);
|
this.messageBus.unsubscribe(LOG_CHANNEL);
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
|
|
|
@ -563,7 +563,7 @@ export default ObjectController.extend(Discourse.SelectedPostsCount, BufferedCon
|
||||||
this.unsubscribe();
|
this.unsubscribe();
|
||||||
|
|
||||||
const self = this;
|
const self = this;
|
||||||
Discourse.MessageBus.subscribe("/topic/" + this.get('id'), function(data) {
|
this.messageBus.subscribe("/topic/" + this.get('id'), function(data) {
|
||||||
const topic = self.get('model');
|
const topic = self.get('model');
|
||||||
|
|
||||||
if (data.notification_level_change) {
|
if (data.notification_level_change) {
|
||||||
|
@ -605,7 +605,7 @@ export default ObjectController.extend(Discourse.SelectedPostsCount, BufferedCon
|
||||||
if (!topicId) return;
|
if (!topicId) return;
|
||||||
|
|
||||||
// there is a condition where the view never calls unsubscribe, navigate to a topic from a topic
|
// there is a condition where the view never calls unsubscribe, navigate to a topic from a topic
|
||||||
Discourse.MessageBus.unsubscribe('/topic/*');
|
this.messageBus.unsubscribe('/topic/*');
|
||||||
},
|
},
|
||||||
|
|
||||||
// Topic related
|
// Topic related
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
/**
|
// Subscribe to "asset-version" change events via the Message Bus
|
||||||
Subscribe to "asset-version" change events via the Message Bus
|
|
||||||
**/
|
|
||||||
export default {
|
export default {
|
||||||
name: "asset-version",
|
name: "asset-version",
|
||||||
after: "message-bus",
|
after: "message-bus",
|
||||||
|
|
||||||
initialize: function () {
|
initialize(container) {
|
||||||
var timeoutIsSet = false;
|
let timeoutIsSet = false;
|
||||||
if (!Discourse.MessageBus) { return; }
|
const messageBus = container.lookup('message-bus:main');
|
||||||
|
if (!messageBus) { return; }
|
||||||
|
|
||||||
Discourse.MessageBus.subscribe("/global/asset-version", function (version) {
|
messageBus.subscribe("/global/asset-version", function (version) {
|
||||||
Discourse.set("assetVersion", version);
|
Discourse.set("assetVersion", version);
|
||||||
|
|
||||||
if (!timeoutIsSet && Discourse.get("requiresRefresh")) {
|
if (!timeoutIsSet && Discourse.get("requiresRefresh")) {
|
||||||
|
|
|
@ -2,15 +2,16 @@ export default {
|
||||||
name: "banner",
|
name: "banner",
|
||||||
after: "message-bus",
|
after: "message-bus",
|
||||||
|
|
||||||
initialize: function (container) {
|
initialize(container) {
|
||||||
var banner = Em.Object.create(PreloadStore.get("banner")),
|
const banner = Em.Object.create(PreloadStore.get("banner")),
|
||||||
site = container.lookup('site:main');
|
site = container.lookup('site:main');
|
||||||
|
|
||||||
site.set("banner", banner);
|
site.set("banner", banner);
|
||||||
|
|
||||||
if (!Discourse.MessageBus) { return; }
|
const messageBus = container.lookup('message-bus:main');
|
||||||
|
if (!messageBus) { return; }
|
||||||
|
|
||||||
Discourse.MessageBus.subscribe("/site/banner", function (banner) {
|
messageBus.subscribe("/site/banner", function (banner) {
|
||||||
site.set("banner", Em.Object.create(banner));
|
site.set("banner", Em.Object.create(banner));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,53 +2,49 @@ import Session from 'discourse/models/session';
|
||||||
import AppEvents from 'discourse/lib/app-events';
|
import AppEvents from 'discourse/lib/app-events';
|
||||||
import Store from 'discourse/models/store';
|
import Store from 'discourse/models/store';
|
||||||
|
|
||||||
|
function inject() {
|
||||||
|
const app = arguments[0],
|
||||||
|
name = arguments[1],
|
||||||
|
singletonName = Ember.String.underscore(name).replace(/_/, '-') + ':main';
|
||||||
|
|
||||||
|
Array.prototype.slice.call(arguments, 2).forEach(function(dest) {
|
||||||
|
app.inject(dest, name, singletonName);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function injectAll(app, name) {
|
||||||
|
inject(app, name, 'controller', 'component', 'route', 'view', 'model');
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "inject-objects",
|
name: "inject-objects",
|
||||||
initialize(container, app) {
|
initialize(container, app) {
|
||||||
|
|
||||||
// Inject appEvents everywhere
|
|
||||||
const appEvents = AppEvents.create();
|
const appEvents = AppEvents.create();
|
||||||
app.register('app-events:main', appEvents, { instantiate: false });
|
app.register('app-events:main', appEvents, { instantiate: false });
|
||||||
|
injectAll(app, 'appEvents');
|
||||||
app.inject('controller', 'appEvents', 'app-events:main');
|
|
||||||
app.inject('component', 'appEvents', 'app-events:main');
|
|
||||||
app.inject('route', 'appEvents', 'app-events:main');
|
|
||||||
app.inject('view', 'appEvents', 'app-events:main');
|
|
||||||
app.inject('model', 'appEvents', 'app-events:main');
|
|
||||||
Discourse.URL.appEvents = appEvents;
|
Discourse.URL.appEvents = appEvents;
|
||||||
|
|
||||||
// Inject Discourse.Site to avoid using Discourse.Site.current()
|
// Inject Discourse.Site to avoid using Discourse.Site.current()
|
||||||
const site = Discourse.Site.current();
|
const site = Discourse.Site.current();
|
||||||
app.register('site:main', site, { instantiate: false });
|
app.register('site:main', site, { instantiate: false });
|
||||||
app.inject('controller', 'site', 'site:main');
|
injectAll(app, 'site');
|
||||||
app.inject('component', 'site', 'site:main');
|
|
||||||
app.inject('route', 'site', 'site:main');
|
|
||||||
app.inject('view', 'site', 'site:main');
|
|
||||||
app.inject('model', 'site', 'site:main');
|
|
||||||
|
|
||||||
// Inject Discourse.SiteSettings to avoid using Discourse.SiteSettings globals
|
// Inject Discourse.SiteSettings to avoid using Discourse.SiteSettings globals
|
||||||
app.register('site-settings:main', Discourse.SiteSettings, { instantiate: false });
|
app.register('site-settings:main', Discourse.SiteSettings, { instantiate: false });
|
||||||
app.inject('controller', 'siteSettings', 'site-settings:main');
|
injectAll(app, 'siteSettings');
|
||||||
app.inject('component', 'siteSettings', 'site-settings:main');
|
|
||||||
app.inject('route', 'siteSettings', 'site-settings:main');
|
|
||||||
app.inject('view', 'siteSettings', 'site-settings:main');
|
|
||||||
app.inject('model', 'siteSettings', 'site-settings:main');
|
|
||||||
|
|
||||||
// Inject Session for transient data
|
// Inject Session for transient data
|
||||||
app.register('session:main', Session.current(), { instantiate: false });
|
app.register('session:main', Session.current(), { instantiate: false });
|
||||||
app.inject('controller', 'session', 'session:main');
|
injectAll(app, 'session');
|
||||||
app.inject('component', 'session', 'session:main');
|
|
||||||
app.inject('route', 'session', 'session:main');
|
|
||||||
app.inject('view', 'session', 'session:main');
|
|
||||||
app.inject('model', 'session', 'session:main');
|
|
||||||
|
|
||||||
app.register('current-user:main', Discourse.User.current(), { instantiate: false });
|
app.register('current-user:main', Discourse.User.current(), { instantiate: false });
|
||||||
app.inject('component', 'currentUser', 'current-user:main');
|
inject(app, 'currentUser', 'component', 'route', 'controller');
|
||||||
app.inject('route', 'currentUser', 'current-user:main');
|
|
||||||
app.inject('controller', 'currentUser', 'current-user:main');
|
app.register('message-bus:main', window.MessageBus, { instantiate: false });
|
||||||
|
inject(app, 'messageBus', 'route', 'controller');
|
||||||
|
|
||||||
app.register('store:main', Store);
|
app.register('store:main', Store);
|
||||||
app.inject('route', 'store', 'store:main');
|
inject(app, 'store', 'route', 'controller');
|
||||||
app.inject('controller', 'store', 'store:main');
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,7 +3,8 @@ import loadScript from 'discourse/lib/load-script';
|
||||||
// Use the message bus for live reloading of components for faster development.
|
// Use the message bus for live reloading of components for faster development.
|
||||||
export default {
|
export default {
|
||||||
name: "live-development",
|
name: "live-development",
|
||||||
initialize() {
|
initialize(container) {
|
||||||
|
const messageBus = container.lookup('message-bus:main');
|
||||||
|
|
||||||
// subscribe to any site customizations that are loaded
|
// subscribe to any site customizations that are loaded
|
||||||
$('link.custom-css').each(function() {
|
$('link.custom-css').each(function() {
|
||||||
|
@ -11,7 +12,7 @@ export default {
|
||||||
id = split[split.length - 1].split(".css")[0],
|
id = split[split.length - 1].split(".css")[0],
|
||||||
self = this;
|
self = this;
|
||||||
|
|
||||||
return Discourse.MessageBus.subscribe("/file-change/" + id, function(data) {
|
return messageBus.subscribe("/file-change/" + id, function(data) {
|
||||||
if (!$(self).data('orig')) {
|
if (!$(self).data('orig')) {
|
||||||
$(self).data('orig', self.href);
|
$(self).data('orig', self.href);
|
||||||
}
|
}
|
||||||
|
@ -24,13 +25,13 @@ export default {
|
||||||
// Custom header changes
|
// Custom header changes
|
||||||
$('header.custom').each(function() {
|
$('header.custom').each(function() {
|
||||||
const header = $(this);
|
const header = $(this);
|
||||||
return Discourse.MessageBus.subscribe("/header-change/" + $(this).data('key'), function(data) {
|
return messageBus.subscribe("/header-change/" + $(this).data('key'), function(data) {
|
||||||
return header.html(data);
|
return header.html(data);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Observe file changes
|
// Observe file changes
|
||||||
Discourse.MessageBus.subscribe("/file-change", function(data) {
|
messageBus.subscribe("/file-change", function(data) {
|
||||||
Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
|
Ember.TEMPLATES.empty = Handlebars.compile("<div></div>");
|
||||||
_.each(data,function(me) {
|
_.each(data,function(me) {
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,17 @@
|
||||||
/**
|
// Subscribe to "logout" change events via the Message Bus
|
||||||
Subscribe to "logout" change events via the Message Bus
|
|
||||||
**/
|
|
||||||
export default {
|
export default {
|
||||||
name: "logout",
|
name: "logout",
|
||||||
after: "message-bus",
|
after: "message-bus",
|
||||||
|
|
||||||
initialize: function () {
|
initialize: function (container) {
|
||||||
if (!Discourse.MessageBus) { return; }
|
const messageBus = container.lookup('message-bus:main'),
|
||||||
|
siteSettings = container.lookup('site-settings:main');
|
||||||
|
|
||||||
Discourse.MessageBus.subscribe("/logout", function () {
|
if (!messageBus) { return; }
|
||||||
|
|
||||||
|
messageBus.subscribe("/logout", function () {
|
||||||
var refresher = function() {
|
var refresher = function() {
|
||||||
var redirect = Discourse.SiteSettings.logout_redirect;
|
var redirect = siteSettings.logout_redirect;
|
||||||
if(redirect.length === 0){
|
if(redirect.length === 0){
|
||||||
window.location.pathname = Discourse.getURL('/');
|
window.location.pathname = Discourse.getURL('/');
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,17 +1,24 @@
|
||||||
/**
|
// Initialize the message bus to receive messages.
|
||||||
Initialize the message bus to receive messages.
|
|
||||||
**/
|
|
||||||
export default {
|
export default {
|
||||||
name: "message-bus",
|
name: "message-bus",
|
||||||
after: 'inject-objects',
|
after: 'inject-objects',
|
||||||
|
|
||||||
initialize: function() {
|
initialize(container) {
|
||||||
|
|
||||||
// We don't use the message bus in testing
|
// We don't use the message bus in testing
|
||||||
if (Discourse.testing) { return; }
|
if (Discourse.testing) { return; }
|
||||||
|
|
||||||
Discourse.MessageBus.alwaysLongPoll = Discourse.Environment === "development";
|
const messageBus = container.lookup('message-bus:main');
|
||||||
Discourse.MessageBus.start();
|
|
||||||
Discourse.KeyValueStore.init("discourse_", Discourse.MessageBus);
|
const deprecatedBus = {};
|
||||||
|
deprecatedBus.prototype = messageBus;
|
||||||
|
deprecatedBus.subscribe = function() {
|
||||||
|
Ember.warn("Discourse.MessageBus is deprecated. Use `this.messageBus` instead");
|
||||||
|
messageBus.subscribe.apply(messageBus, Array.prototype.slice(arguments));
|
||||||
|
};
|
||||||
|
Discourse.MessageBus = deprecatedBus;
|
||||||
|
|
||||||
|
messageBus.alwaysLongPoll = Discourse.Environment === "development";
|
||||||
|
messageBus.start();
|
||||||
|
Discourse.KeyValueStore.init("discourse_", messageBus);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,15 +1,14 @@
|
||||||
/**
|
// Subscribe to "read-only" status change events via the Message Bus
|
||||||
Subscribe to "read-only" status change events via the Message Bus
|
|
||||||
**/
|
|
||||||
export default {
|
export default {
|
||||||
name: "read-only",
|
name: "read-only",
|
||||||
after: "message-bus",
|
after: "message-bus",
|
||||||
|
|
||||||
initialize: function (container) {
|
initialize(container) {
|
||||||
if (!Discourse.MessageBus) { return; }
|
const messageBus = container.lookup('message-bus:main');
|
||||||
|
if (!messageBus) { return; }
|
||||||
|
|
||||||
var site = container.lookup('site:main');
|
const site = container.lookup('site:main');
|
||||||
Discourse.MessageBus.subscribe("/site/read-only", function (enabled) {
|
messageBus.subscribe("/site/read-only", function (enabled) {
|
||||||
site.set('isReadOnly', enabled);
|
site.set('isReadOnly', enabled);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
/**
|
// Subscribes to user events on the message bus
|
||||||
Subscribes to user events on the message bus
|
|
||||||
**/
|
|
||||||
export default {
|
export default {
|
||||||
name: 'subscribe-user-notifications',
|
name: 'subscribe-user-notifications',
|
||||||
after: 'message-bus',
|
after: 'message-bus',
|
||||||
initialize: function(container) {
|
initialize(container) {
|
||||||
var user = Discourse.User.current();
|
const user = container.lookup('current-user:main'),
|
||||||
|
site = container.lookup('site:main'),
|
||||||
|
siteSettings = container.lookup('site-settings:main'),
|
||||||
|
bus = container.lookup('message-bus:main');
|
||||||
|
|
||||||
var site = container.lookup('site:main'),
|
|
||||||
siteSettings = container.lookup('site-settings:main');
|
|
||||||
|
|
||||||
var bus = Discourse.MessageBus;
|
|
||||||
bus.callbackInterval = siteSettings.anon_polling_interval;
|
bus.callbackInterval = siteSettings.anon_polling_interval;
|
||||||
bus.backgroundCallbackInterval = siteSettings.background_polling_interval;
|
bus.backgroundCallbackInterval = siteSettings.background_polling_interval;
|
||||||
bus.baseUrl = siteSettings.long_polling_base_url;
|
bus.baseUrl = siteSettings.long_polling_base_url;
|
||||||
|
@ -36,8 +33,8 @@ export default {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
bus.subscribe("/notification/" + user.get('id'), (function(data) {
|
bus.subscribe("/notification/" + user.get('id'), (function(data) {
|
||||||
var oldUnread = user.get('unread_notifications');
|
const oldUnread = user.get('unread_notifications');
|
||||||
var oldPM = user.get('unread_private_messages');
|
const oldPM = user.get('unread_private_messages');
|
||||||
|
|
||||||
user.set('unread_notifications', data.unread_notifications);
|
user.set('unread_notifications', data.unread_notifications);
|
||||||
user.set('unread_private_messages', data.unread_private_messages);
|
user.set('unread_private_messages', data.unread_private_messages);
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
Discourse.MessageBus = window.MessageBus;
|
|
|
@ -52,11 +52,10 @@ const TopicTrackingState = Discourse.Model.extend({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Discourse.MessageBus.subscribe("/new", process);
|
this.messageBus.subscribe("/new", process);
|
||||||
Discourse.MessageBus.subscribe("/latest", process);
|
this.messageBus.subscribe("/latest", process);
|
||||||
const currentUser = Discourse.User.current();
|
if (this.currentUser) {
|
||||||
if(currentUser) {
|
this.messageBus.subscribe("/unread/" + this.currentUser.get('id'), process);
|
||||||
Discourse.MessageBus.subscribe("/unread/" + currentUser.id, process);
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -293,8 +292,14 @@ const TopicTrackingState = Discourse.Model.extend({
|
||||||
|
|
||||||
|
|
||||||
TopicTrackingState.reopenClass({
|
TopicTrackingState.reopenClass({
|
||||||
createFromStates(data){
|
createFromStates(data) {
|
||||||
const instance = Discourse.TopicTrackingState.create();
|
|
||||||
|
// TODO: This should be a model that does injection automatically
|
||||||
|
const container = Discourse.__container__,
|
||||||
|
messageBus = container.lookup('message-bus:main'),
|
||||||
|
currentUser = container.lookup('current-user:main'),
|
||||||
|
instance = Discourse.TopicTrackingState.create({ messageBus, currentUser });
|
||||||
|
|
||||||
instance.loadStates(data);
|
instance.loadStates(data);
|
||||||
instance.establishChannels();
|
instance.establishChannels();
|
||||||
return instance;
|
return instance;
|
||||||
|
|
|
@ -69,14 +69,14 @@ export default Discourse.Route.extend({
|
||||||
activate: function() {
|
activate: function() {
|
||||||
this._super();
|
this._super();
|
||||||
var user = this.modelFor('user');
|
var user = this.modelFor('user');
|
||||||
Discourse.MessageBus.subscribe("/users/" + user.get('username_lower'), function(data) {
|
this.messageBus.subscribe("/users/" + user.get('username_lower'), function(data) {
|
||||||
user.loadUserAction(data);
|
user.loadUserAction(data);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
deactivate: function() {
|
deactivate: function() {
|
||||||
this._super();
|
this._super();
|
||||||
Discourse.MessageBus.unsubscribe("/users/" + this.modelFor('user').get('username_lower'));
|
this.messageBus.unsubscribe("/users/" + this.modelFor('user').get('username_lower'));
|
||||||
|
|
||||||
// Remove the search context
|
// Remove the search context
|
||||||
this.controllerFor('search').set('searchContext', null);
|
this.controllerFor('search').set('searchContext', null);
|
||||||
|
|
|
@ -62,7 +62,7 @@ sinon.config = {
|
||||||
window.assetPath = function() { return null; };
|
window.assetPath = function() { return null; };
|
||||||
|
|
||||||
// Stop the message bus so we don't get ajax calls
|
// Stop the message bus so we don't get ajax calls
|
||||||
Discourse.MessageBus.stop();
|
window.MessageBus.stop();
|
||||||
|
|
||||||
// Trick JSHint into allow document.write
|
// Trick JSHint into allow document.write
|
||||||
var d = document;
|
var d = document;
|
||||||
|
|
Loading…
Reference in New Issue