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
1 changed files with 15 additions and 11 deletions

View File

@ -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 = {};