DEV: Use Ember.Service over manually registering a factory.

Follow up to 9e827eb420
This commit is contained in:
Guo Xiang Tan 2020-05-05 10:00:34 +08:00 committed by Alan Guo Xiang Tan
parent 4e1c2fbd18
commit b299f5f491
6 changed files with 83 additions and 129 deletions

View File

@ -28,7 +28,10 @@ export default {
const messageBus = window.MessageBus;
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();
app.register("current-user:main", currentUser, { instantiate: false });
@ -47,7 +50,7 @@ export default {
const siteSettings = app.SiteSettings;
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")
);
@ -77,7 +80,7 @@ export default {
);
if (currentUser) {
["component", "route", "controller"].forEach(t => {
["component", "route", "controller", "service"].forEach(t => {
app.inject(t, "currentUser", "current-user:main");
});
}

View File

@ -1,17 +1,12 @@
import Component from "@ember/component";
import { getOwner } from "@ember/application";
import { cancel } from "@ember/runloop";
import { equal, gt } from "@ember/object/computed";
import { inject as service } from "@ember/service";
import discourseComputed, {
observes,
on
} from "discourse-common/utils/decorators";
import {
REPLYING,
CLOSED,
EDITING,
COMPOSER_TYPE
} from "../lib/presence-manager";
import { REPLYING, CLOSED, EDITING, COMPOSER_TYPE } from "../lib/presence";
import { REPLY, EDIT } from "discourse/models/composer";
export default Component.extend({
@ -22,15 +17,7 @@ export default Component.extend({
reply: null,
title: null,
isWhispering: null,
presenceManager: null,
init() {
this._super(...arguments);
this.setProperties({
presenceManager: getOwner(this).lookup("presence-manager:main")
});
},
presenceManager: service(),
@discourseComputed("topic.id")
users(topicId) {

View File

@ -1,17 +1,12 @@
import Component from "@ember/component";
import { getOwner } from "@ember/application";
import { gt } from "@ember/object/computed";
import { inject as service } from "@ember/service";
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({
topic: null,
presenceManager: null,
init() {
this._super(...arguments);
this.set("presenceManager", getOwner(this).lookup("presence-manager:main"));
},
presenceManager: service(),
@discourseComputed("topic.id")
users(topicId) {

View File

@ -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 });
}
}
};

View File

@ -214,73 +214,4 @@ const Presence = EmberObject.extend({
}
});
const PresenceManager = EmberObject.extend({
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;
export default Presence;

View File

@ -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;