DEV: Use Ember.Service over manually registering a factory.
Follow up to 9e827eb420
This commit is contained in:
parent
4e1c2fbd18
commit
b299f5f491
|
@ -28,7 +28,10 @@ export default {
|
||||||
|
|
||||||
const messageBus = window.MessageBus;
|
const messageBus = window.MessageBus;
|
||||||
app.register("message-bus:main", messageBus, { instantiate: false });
|
app.register("message-bus:main", messageBus, { instantiate: false });
|
||||||
ALL_TARGETS.forEach(t => app.inject(t, "messageBus", "message-bus:main"));
|
|
||||||
|
ALL_TARGETS.concat("service").forEach(t =>
|
||||||
|
app.inject(t, "messageBus", "message-bus:main")
|
||||||
|
);
|
||||||
|
|
||||||
const currentUser = User.current();
|
const currentUser = User.current();
|
||||||
app.register("current-user:main", currentUser, { instantiate: false });
|
app.register("current-user:main", currentUser, { instantiate: false });
|
||||||
|
@ -47,7 +50,7 @@ export default {
|
||||||
|
|
||||||
const siteSettings = app.SiteSettings;
|
const siteSettings = app.SiteSettings;
|
||||||
app.register("site-settings:main", siteSettings, { instantiate: false });
|
app.register("site-settings:main", siteSettings, { instantiate: false });
|
||||||
ALL_TARGETS.forEach(t =>
|
ALL_TARGETS.concat("service").forEach(t =>
|
||||||
app.inject(t, "siteSettings", "site-settings:main")
|
app.inject(t, "siteSettings", "site-settings:main")
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -77,7 +80,7 @@ export default {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (currentUser) {
|
if (currentUser) {
|
||||||
["component", "route", "controller"].forEach(t => {
|
["component", "route", "controller", "service"].forEach(t => {
|
||||||
app.inject(t, "currentUser", "current-user:main");
|
app.inject(t, "currentUser", "current-user:main");
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,12 @@
|
||||||
import Component from "@ember/component";
|
import Component from "@ember/component";
|
||||||
import { getOwner } from "@ember/application";
|
|
||||||
import { cancel } from "@ember/runloop";
|
import { cancel } from "@ember/runloop";
|
||||||
import { equal, gt } from "@ember/object/computed";
|
import { equal, gt } from "@ember/object/computed";
|
||||||
|
import { inject as service } from "@ember/service";
|
||||||
import discourseComputed, {
|
import discourseComputed, {
|
||||||
observes,
|
observes,
|
||||||
on
|
on
|
||||||
} from "discourse-common/utils/decorators";
|
} from "discourse-common/utils/decorators";
|
||||||
import {
|
import { REPLYING, CLOSED, EDITING, COMPOSER_TYPE } from "../lib/presence";
|
||||||
REPLYING,
|
|
||||||
CLOSED,
|
|
||||||
EDITING,
|
|
||||||
COMPOSER_TYPE
|
|
||||||
} from "../lib/presence-manager";
|
|
||||||
import { REPLY, EDIT } from "discourse/models/composer";
|
import { REPLY, EDIT } from "discourse/models/composer";
|
||||||
|
|
||||||
export default Component.extend({
|
export default Component.extend({
|
||||||
|
@ -22,15 +17,7 @@ export default Component.extend({
|
||||||
reply: null,
|
reply: null,
|
||||||
title: null,
|
title: null,
|
||||||
isWhispering: null,
|
isWhispering: null,
|
||||||
presenceManager: null,
|
presenceManager: service(),
|
||||||
|
|
||||||
init() {
|
|
||||||
this._super(...arguments);
|
|
||||||
|
|
||||||
this.setProperties({
|
|
||||||
presenceManager: getOwner(this).lookup("presence-manager:main")
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
@discourseComputed("topic.id")
|
@discourseComputed("topic.id")
|
||||||
users(topicId) {
|
users(topicId) {
|
||||||
|
|
|
@ -1,17 +1,12 @@
|
||||||
import Component from "@ember/component";
|
import Component from "@ember/component";
|
||||||
import { getOwner } from "@ember/application";
|
|
||||||
import { gt } from "@ember/object/computed";
|
import { gt } from "@ember/object/computed";
|
||||||
|
import { inject as service } from "@ember/service";
|
||||||
import discourseComputed, { on } from "discourse-common/utils/decorators";
|
import discourseComputed, { on } from "discourse-common/utils/decorators";
|
||||||
import { TOPIC_TYPE } from "../lib/presence-manager";
|
import { TOPIC_TYPE } from "../lib/presence";
|
||||||
|
|
||||||
export default Component.extend({
|
export default Component.extend({
|
||||||
topic: null,
|
topic: null,
|
||||||
presenceManager: null,
|
presenceManager: service(),
|
||||||
|
|
||||||
init() {
|
|
||||||
this._super(...arguments);
|
|
||||||
this.set("presenceManager", getOwner(this).lookup("presence-manager:main"));
|
|
||||||
},
|
|
||||||
|
|
||||||
@discourseComputed("topic.id")
|
@discourseComputed("topic.id")
|
||||||
users(topicId) {
|
users(topicId) {
|
||||||
|
|
|
@ -1,32 +0,0 @@
|
||||||
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
||||||
import PresenceManager from "../lib/presence-manager";
|
|
||||||
import ENV from "discourse-common/config/environment";
|
|
||||||
|
|
||||||
function initializeDiscoursePresence(api, { app }) {
|
|
||||||
const currentUser = api.getCurrentUser();
|
|
||||||
|
|
||||||
if (currentUser) {
|
|
||||||
app.register(
|
|
||||||
"presence-manager:main",
|
|
||||||
PresenceManager.create({
|
|
||||||
currentUser,
|
|
||||||
messageBus: api.container.lookup("message-bus:main"),
|
|
||||||
siteSettings: api.container.lookup("site-settings:main")
|
|
||||||
}),
|
|
||||||
{ instantiate: false }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
|
||||||
name: "discourse-presence",
|
|
||||||
after: "message-bus",
|
|
||||||
|
|
||||||
initialize(container, app) {
|
|
||||||
const siteSettings = container.lookup("site-settings:main");
|
|
||||||
|
|
||||||
if (siteSettings.presence_enabled && ENV.environment !== "test") {
|
|
||||||
withPluginApi("0.8.40", initializeDiscoursePresence, { app });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -214,73 +214,4 @@ const Presence = EmberObject.extend({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const PresenceManager = EmberObject.extend({
|
export default Presence;
|
||||||
presences: null,
|
|
||||||
currentUser: null,
|
|
||||||
messageBus: null,
|
|
||||||
siteSettings: null,
|
|
||||||
|
|
||||||
init() {
|
|
||||||
this._super(...arguments);
|
|
||||||
|
|
||||||
this.setProperties({
|
|
||||||
presences: {}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
subscribe(topicId, type) {
|
|
||||||
if (!topicId) return;
|
|
||||||
this._getPresence(topicId).subscribe(type);
|
|
||||||
},
|
|
||||||
|
|
||||||
unsubscribe(topicId, type) {
|
|
||||||
if (!topicId) return;
|
|
||||||
const presence = this._getPresence(topicId);
|
|
||||||
|
|
||||||
if (presence.unsubscribe(type)) {
|
|
||||||
delete this.presences[topicId];
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
users(topicId) {
|
|
||||||
if (!topicId) return [];
|
|
||||||
return this._getPresence(topicId).users;
|
|
||||||
},
|
|
||||||
|
|
||||||
editingUsers(topicId) {
|
|
||||||
if (!topicId) return [];
|
|
||||||
return this._getPresence(topicId).editingUsers;
|
|
||||||
},
|
|
||||||
|
|
||||||
throttlePublish(topicId, state, whisper, postId) {
|
|
||||||
if (!topicId) return;
|
|
||||||
return this._getPresence(topicId).throttlePublish(state, whisper, postId);
|
|
||||||
},
|
|
||||||
|
|
||||||
publish(topicId, state, whisper, postId) {
|
|
||||||
if (!topicId) return;
|
|
||||||
return this._getPresence(topicId).publish(state, whisper, postId);
|
|
||||||
},
|
|
||||||
|
|
||||||
cleanUpPresence(type) {
|
|
||||||
Object.keys(this.presences).forEach(key => {
|
|
||||||
this.publish(key, CLOSED);
|
|
||||||
this.unsubscribe(key, type);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
_getPresence(topicId) {
|
|
||||||
if (!this.presences[topicId]) {
|
|
||||||
this.presences[topicId] = Presence.create({
|
|
||||||
messageBus: this.messageBus,
|
|
||||||
siteSettings: this.siteSettings,
|
|
||||||
currentUser: this.currentUser,
|
|
||||||
topicId
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.presences[topicId];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export default PresenceManager;
|
|
|
@ -0,0 +1,70 @@
|
||||||
|
import Service from "@ember/service";
|
||||||
|
import Presence, { CLOSED } from "../lib/presence";
|
||||||
|
|
||||||
|
const PresenceManager = Service.extend({
|
||||||
|
presences: null,
|
||||||
|
|
||||||
|
init() {
|
||||||
|
this._super(...arguments);
|
||||||
|
|
||||||
|
this.setProperties({
|
||||||
|
presences: {}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
subscribe(topicId, type) {
|
||||||
|
if (!topicId) return;
|
||||||
|
this._getPresence(topicId).subscribe(type);
|
||||||
|
},
|
||||||
|
|
||||||
|
unsubscribe(topicId, type) {
|
||||||
|
if (!topicId) return;
|
||||||
|
const presence = this._getPresence(topicId);
|
||||||
|
|
||||||
|
if (presence.unsubscribe(type)) {
|
||||||
|
delete this.presences[topicId];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
users(topicId) {
|
||||||
|
if (!topicId) return [];
|
||||||
|
return this._getPresence(topicId).users;
|
||||||
|
},
|
||||||
|
|
||||||
|
editingUsers(topicId) {
|
||||||
|
if (!topicId) return [];
|
||||||
|
return this._getPresence(topicId).editingUsers;
|
||||||
|
},
|
||||||
|
|
||||||
|
throttlePublish(topicId, state, whisper, postId) {
|
||||||
|
if (!topicId) return;
|
||||||
|
return this._getPresence(topicId).throttlePublish(state, whisper, postId);
|
||||||
|
},
|
||||||
|
|
||||||
|
publish(topicId, state, whisper, postId) {
|
||||||
|
if (!topicId) return;
|
||||||
|
return this._getPresence(topicId).publish(state, whisper, postId);
|
||||||
|
},
|
||||||
|
|
||||||
|
cleanUpPresence(type) {
|
||||||
|
Object.keys(this.presences).forEach(key => {
|
||||||
|
this.publish(key, CLOSED);
|
||||||
|
this.unsubscribe(key, type);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
_getPresence(topicId) {
|
||||||
|
if (!this.presences[topicId]) {
|
||||||
|
this.presences[topicId] = Presence.create({
|
||||||
|
messageBus: this.messageBus,
|
||||||
|
siteSettings: this.siteSettings,
|
||||||
|
currentUser: this.currentUser,
|
||||||
|
topicId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.presences[topicId];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default PresenceManager;
|
Loading…
Reference in New Issue