Skip to content

A platform for community discussion. Free, open, simple.

FIX: more efficient topic-footer-button-api (#7535)

Joffrey JAFFEUX717aa764b6dc
diff --git a/app/assets/javascripts/discourse/lib/register-topic-footer-button.js.es6 b/app/assets/javascripts/discourse/lib/register-topic-footer-button.js.es6
index d74e71695e8..789122618dd 100644
--- a/app/assets/javascripts/discourse/lib/register-topic-footer-button.js.es6
+++ b/app/assets/javascripts/discourse/lib/register-topic-footer-button.js.es6
@@ -1,6 +1,15 @@
-let _topicFooterButtons = [];
+let _topicFooterButtons = {};
export function registerTopicFooterButton(button) {
+ if (!button.id) {
+ Ember.error(`Attempted to register a topic button: ${button} with no id.`);
+ return;
+ }
+
+ if (_topicFooterButtons[button.id]) {
+ return;
+ }
+
const defaultButton = {
// id of the button, required
id: null,
@@ -38,11 +47,6 @@ export function registerTopicFooterButton(button) {
const normalizedButton = Object.assign(defaultButton, button);
- if (!normalizedButton.id) {
- Ember.error(`Attempted to register a topic button: ${button} with no id.`);
- return;
- }
-
if (
!normalizedButton.icon &&
!normalizedButton.title &&
@@ -56,14 +60,14 @@ export function registerTopicFooterButton(button) {
return;
}
- _topicFooterButtons.push(normalizedButton);
-
- _topicFooterButtons = _topicFooterButtons.uniqBy("id");
+ _topicFooterButtons[normalizedButton.id] = normalizedButton;
}
export function getTopicFooterButtons() {
const dependentKeys = [].concat(
- ..._topicFooterButtons.map(tfb => tfb.dependentKeys).filter(x => x)
+ ...Object.values(_topicFooterButtons)
+ .map(tfb => tfb.dependentKeys)
+ .filter(x => x)
);
const computedFunc = Ember.computed({
@@ -81,7 +85,7 @@ export function getTopicFooterButtons() {
return field;
};
- return _topicFooterButtons
+ return Object.values(_topicFooterButtons)
.filter(button => _compute(button, "displayed"))
.map(button => {
const computedButon = {};