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

This commit is contained in:
Joffrey JAFFEUX 2019-05-13 17:04:24 +02:00 committed by GitHub
parent 750c125707
commit 717aa764b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,15 @@
let _topicFooterButtons = []; let _topicFooterButtons = {};
export function registerTopicFooterButton(button) { 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 = { const defaultButton = {
// id of the button, required // id of the button, required
id: null, id: null,
@ -38,11 +47,6 @@ export function registerTopicFooterButton(button) {
const normalizedButton = Object.assign(defaultButton, button); const normalizedButton = Object.assign(defaultButton, button);
if (!normalizedButton.id) {
Ember.error(`Attempted to register a topic button: ${button} with no id.`);
return;
}
if ( if (
!normalizedButton.icon && !normalizedButton.icon &&
!normalizedButton.title && !normalizedButton.title &&
@ -56,14 +60,14 @@ export function registerTopicFooterButton(button) {
return; return;
} }
_topicFooterButtons.push(normalizedButton); _topicFooterButtons[normalizedButton.id] = normalizedButton;
_topicFooterButtons = _topicFooterButtons.uniqBy("id");
} }
export function getTopicFooterButtons() { export function getTopicFooterButtons() {
const dependentKeys = [].concat( 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({ const computedFunc = Ember.computed({
@ -81,7 +85,7 @@ export function getTopicFooterButtons() {
return field; return field;
}; };
return _topicFooterButtons return Object.values(_topicFooterButtons)
.filter(button => _compute(button, "displayed")) .filter(button => _compute(button, "displayed"))
.map(button => { .map(button => {
const computedButon = {}; const computedButon = {};