Skip to content

Commit

Permalink
Condition Widgets trigger hundreds of persistence calls. #5137
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilmandlik committed Apr 29, 2022
1 parent e75befa commit bfa604e
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/MCT.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ define([
this.actions = new api.ActionsAPI(this);

this.status = new api.StatusAPI(this);
this.styleManager = new api.StyleManagerAPI(this);

this.priority = api.PriorityAPI;

Expand Down
3 changes: 3 additions & 0 deletions src/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ define([
'./objects/ObjectAPI',
'./priority/PriorityAPI',
'./status/StatusAPI',
'./styles/StyleManagerAPI',
'./telemetry/TelemetryAPI',
'./time/TimeAPI',
'./types/TypeRegistry',
Expand All @@ -46,6 +47,7 @@ define([
ObjectAPI,
PriorityAPI,
StatusAPI,
StyleManagerAPI,
TelemetryAPI,
TimeAPI,
TypeRegistry,
Expand All @@ -62,6 +64,7 @@ define([
ObjectAPI: ObjectAPI,
PriorityAPI: PriorityAPI.default,
StatusAPI: StatusAPI.default,
StyleManagerAPI: StyleManagerAPI.default,
TelemetryAPI: TelemetryAPI,
TimeAPI: TimeAPI.default,
TypeRegistry: TypeRegistry,
Expand Down
67 changes: 67 additions & 0 deletions src/api/styles/StyleManagerAPI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2022, United States Government
* as represented by the Administrator of the National Aeronautics and Space
* Administration. All rights reserved.
*
* Open MCT is licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
* Open MCT includes source code licensed under additional open source
* licenses. See the Open Source Licenses file (LICENSES.md) included with
* this source code distribution or the Licensing information page available
* at runtime from the About dialog for additional information.
*****************************************************************************/

import EventEmitter from 'EventEmitter';

export default class StyleManagerAPI extends EventEmitter {
constructor(openmct) {
super();

this._openmct = openmct;
this._styleCache = {};

this.get = this.get.bind(this);
this.set = this.set.bind(this);
this.observe = this.observe.bind(this);
}

get(identifier) {
let keyString = this._openmct.objects.makeKeyString(identifier);

return this._styleCache[keyString];
}

set(identifier, value) {
let keyString = this._openmct.objects.makeKeyString(identifier);

this._styleCache[keyString] = value;
this.emit(keyString, value);
}

delete(identifier) {
let keyString = this._openmct.objects.makeKeyString(identifier);

this._styleCache[keyString] = undefined;
this.emit(keyString, undefined);
delete this._styleCache[keyString];
}

observe(identifier, callback) {
let key = this._openmct.objects.makeKeyString(identifier);

this.on(key, callback);

return () => {
this.off(key, callback);
};
}
}
2 changes: 2 additions & 0 deletions src/plugins/condition/StyleRuleManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import EventEmitter from 'EventEmitter';
export default class StyleRuleManager extends EventEmitter {
constructor(styleConfiguration, openmct, callback, suppressSubscriptionOnEdit) {
super();

this.openmct = openmct;
this.callback = callback;
this.refreshData = this.refreshData.bind(this);
Expand Down Expand Up @@ -152,6 +153,7 @@ export default class StyleRuleManager extends EventEmitter {

updateDomainObjectStyle() {
if (this.callback) {
this.emit('updateStyles', this.currentStyle);
this.callback(Object.assign({}, this.currentStyle));
}
}
Expand Down
28 changes: 26 additions & 2 deletions src/plugins/conditionWidget/components/ConditionWidget.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
:href="url"
>
<div class="c-condition-widget__label">
{{ internalDomainObject.conditionalLabel || internalDomainObject.label }}
{{ label }}
</div>
</component>
</template>
Expand All @@ -39,10 +39,16 @@ export default {
inject: ['openmct', 'domainObject'],
data: function () {
return {
internalDomainObject: this.domainObject
internalDomainObject: this.domainObject,
conditionalLabel: ''
};
},
computed: {
label() {
return this.conditionalLabel.length
? this.conditionalLabel
: this.internalDomainObject.label;
},
urlDefined() {
return this.internalDomainObject.url && this.internalDomainObject.url.length > 0;
},
Expand All @@ -52,13 +58,31 @@ export default {
},
mounted() {
this.unlisten = this.openmct.objects.observe(this.internalDomainObject, '*', this.updateInternalDomainObject);
this.unobserve = this.openmct.styleManager.observe(this.internalDomainObject.identifier, this.observeStyleManagerChanges.bind(this));
},
beforeDestroy() {
if (this.unlisten) {
this.unlisten();
}
if (this.unobserve) {
this.openmct.styleManager.delete(this.internalDomainObject.identifier);
this.unobserve();
}
},
methods: {
observeStyleManagerChanges(styleManager) {
if (styleManager) {
this.styleManager = styleManager;
this.styleManager.on('updateStyles', this.updateConditionLabel);
} else {
this.styleManager.off('updateStyles', this.updateConditionLabel);
}
},
updateConditionLabel(styleObj = {}) {
this.conditionalLabel = styleObj.output || '';
},
updateInternalDomainObject(domainObject) {
this.internalDomainObject = domainObject;
}
Expand Down
6 changes: 0 additions & 6 deletions src/plugins/plot/stackedPlot/mixins/objectStyles-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,6 @@ export default {
}
}
});

if (this.object && this.object.type === 'conditionWidget' && keys.includes('output')) {
this.openmct.objects.mutate(this.object, 'conditionalLabel', styleObj.output);
} else {
this.openmct.objects.mutate(this.object, 'conditionalLabel', '');
}
}
}
};
12 changes: 6 additions & 6 deletions src/ui/components/ObjectView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ export default {
},
methods: {
clear() {
if (this.domainObject) {
this.openmct.styleManager.delete(this.domainObject.identifier);
}
if (this.currentView) {
this.currentView.destroy();
if (this.$refs.objectViewWrapper) {
Expand Down Expand Up @@ -213,12 +217,6 @@ export default {
}
}
});
if (this.domainObject && this.domainObject.type === 'conditionWidget' && keys.includes('output')) {
this.openmct.objects.mutate(this.domainObject, 'conditionalLabel', styleObj.output);
} else {
this.openmct.objects.mutate(this.domainObject, 'conditionalLabel', '');
}
},
updateView(immediatelySelect) {
this.clear();
Expand Down Expand Up @@ -310,8 +308,10 @@ export default {
this.initObjectStyles();
},
initObjectStyles() {
this.styleRuleManager = this.openmct.styleManager.get(this.domainObject.identifier);
if (!this.styleRuleManager) {
this.styleRuleManager = new StyleRuleManager((this.domainObject.configuration && this.domainObject.configuration.objectStyles), this.openmct, this.updateStyle.bind(this), true);
this.openmct.styleManager.set(this.domainObject.identifier, this.styleRuleManager);
} else {
this.styleRuleManager.updateObjectStyleConfig(this.domainObject.configuration && this.domainObject.configuration.objectStyles);
}
Expand Down

0 comments on commit bfa604e

Please # to comment.