clean up composition effects

This commit is contained in:
Scott Bell 2024-09-05 13:44:08 +02:00
parent 9aeb454fb6
commit fb8730cf22
4 changed files with 51 additions and 19 deletions

View File

@ -11,6 +11,8 @@ export default class CompsManager extends EventEmitter {
#loaded = false;
#compositionLoaded = false;
#telemetryProcessors = {};
// make id random 4 digit number
#id = Math.floor(Math.random() * 9000) + 1000;
constructor(openmct, domainObject) {
super();
@ -70,7 +72,7 @@ export default class CompsManager extends EventEmitter {
testValue: 0,
timeMetaData
});
this.emit('parametersUpdated', keyString);
this.emit('parametersAdded', this.#domainObject);
}
getParameters() {
@ -129,20 +131,25 @@ export default class CompsManager extends EventEmitter {
}
}
startListeningToUnderlyingTelemetry() {
async startListeningToUnderlyingTelemetry() {
console.debug('🎉 CompsManager: startListeningToUnderlyingTelemetry');
this.#loaded = false;
Object.keys(this.#telemetryCollections).forEach((keyString) => {
if (!this.#telemetryCollections[keyString].loaded) {
const specificTelemetryProcessor = this.#getTelemetryProcessor(keyString);
this.#telemetryCollections[keyString].on('add', specificTelemetryProcessor);
this.#telemetryCollections[keyString].on('add', this.#getTelemetryProcessor(keyString));
this.#telemetryCollections[keyString].on('clear', this.clearData);
this.#telemetryCollections[keyString].load();
const telemetryLoadedPromise = this.#telemetryCollections[keyString].load();
this.#telemetryLoadedPromises.push(telemetryLoadedPromise);
}
});
await Promise.all(this.#telemetryLoadedPromises);
this.#telemetryLoadedPromises = [];
this.#loaded = true;
}
stopListeningToUnderlyingTelemetry() {
console.debug('🔇 CompsManager: stopListeningToUnderlyingTelemetry');
this.#loaded = false;
Object.keys(this.#telemetryCollections).forEach((keyString) => {
const specificTelemetryProcessor = this.#telemetryProcessors[keyString];
delete this.#telemetryProcessors[keyString];

View File

@ -74,7 +74,7 @@ function calculate(dataFrame, parameters, expression) {
otherParameters.forEach((parameter) => {
const otherDataFrame = dataFrame[parameter.keyString];
const otherTelemetry = otherDataFrame.get(referenceTime);
if (!otherTelemetry) {
if (otherTelemetry === undefined && otherTelemetry === null) {
missingData = true;
return;
}

View File

@ -29,6 +29,8 @@ export default class CompsTelemetryProvider {
#lastUniqueID = 1;
#requestPromises = {};
#subscriptionCallbacks = {};
// id is random 4 digit number
#id = Math.floor(Math.random() * 9000) + 1000;
constructor(openmct, compsManagerPool) {
this.#openmct = openmct;
@ -67,7 +69,11 @@ export default class CompsTelemetryProvider {
const callbackID = this.#getCallbackID();
const telemetryForComps = specificCompsManager.requestUnderlyingTelemetry();
const expression = specificCompsManager.getExpression();
const parameters = specificCompsManager.getParameters();
const parameters = JSON.parse(JSON.stringify(specificCompsManager.getParameters()));
if (!expression || !parameters) {
resolve([]);
return;
}
this.#requestPromises[callbackID] = { resolve, reject };
const payload = {
type: 'calculateRequest',
@ -85,13 +91,16 @@ export default class CompsTelemetryProvider {
});
}
#computeOnNewTelemetry(specificCompsManager, newTelemetry, callbackID) {
#computeOnNewTelemetry(specificCompsManager, callbackID, newTelemetry) {
if (!specificCompsManager.isReady()) {
return;
}
const expression = specificCompsManager.getExpression();
const telemetryForComps = specificCompsManager.getFullDataFrame(newTelemetry);
const parameters = JSON.parse(JSON.stringify(specificCompsManager.getParameters()));
if (!expression || !parameters) {
return;
}
const payload = {
type: 'calculateSubscription',
telemetryForComps,
@ -111,12 +120,19 @@ export default class CompsTelemetryProvider {
);
const callbackID = this.#getCallbackID();
this.#subscriptionCallbacks[callbackID] = callback;
specificCompsManager.on('underlyingTelemetryUpdated', (newTelemetry) => {
this.#computeOnNewTelemetry(specificCompsManager, newTelemetry, callbackID);
});
const boundComputeOnNewTelemetry = this.#computeOnNewTelemetry.bind(
this,
specificCompsManager,
callbackID
);
specificCompsManager.on('underlyingTelemetryUpdated', boundComputeOnNewTelemetry);
specificCompsManager.startListeningToUnderlyingTelemetry();
console.debug(
`🧮 Comps Telemetry Provider: subscribed to comps. Now have ${Object.keys(this.#subscriptionCallbacks).length} listener`,
this.#subscriptionCallbacks
);
return () => {
specificCompsManager.off('underlyingTelemetryUpdated', callback);
specificCompsManager.off('underlyingTelemetryUpdated', boundComputeOnNewTelemetry);
delete this.#subscriptionCallbacks[callbackID];
// if this is the last subscription, tell the comp manager to stop listening
specificCompsManager.stopListeningToUnderlyingTelemetry();

View File

@ -173,11 +173,11 @@ onBeforeMount(async () => {
outputTelemetryCollection = openmct.telemetry.requestCollection(domainObject);
outputTelemetryCollection.on('add', telemetryProcessor);
outputTelemetryCollection.on('clear', clearData);
compsManager.on('parametersAdded', reloadParameters);
await compsManager.load();
parameters.value = compsManager.getParameters();
expression.value = compsManager.getExpression();
outputFormat.value = compsManager.getOutputFormat();
compsManager.on('parametersUpdated', reloadParameters);
outputTelemetryCollection.load();
applyTestData();
});
@ -191,21 +191,27 @@ onBeforeUnmount(() => {
watch(
() => props.isEditing,
(editMode) => {
console.debug(`📢 Edit mode is: ${editMode}`);
if (!editMode) {
testDataApplied.value = false;
}
}
);
function reloadParameters() {
parameters.value = compsManager.getParameters();
domainObject.configuration.comps.parameters = parameters.value;
compsManager.setDomainObject(domainObject);
function reloadParameters(passedDomainObject) {
// Because this is triggered by a composition change, we have
// to defer mutation of our domain object, otherwise we might
// mutate an outdated version of the domain object.
setTimeout(function () {
console.debug('🚀 CompsView: parameter added', passedDomainObject);
domainObject.configuration.comps.parameters = passedDomainObject.configuration.comps.parameters;
parameters.value = domainObject.configuration.comps.parameters;
openmct.objects.mutate(domainObject, `configuration.comps.parameters`, parameters.value);
compsManager.setDomainObject(domainObject);
applyTestData();
});
}
function updateParameters() {
console.debug('🚀 CompsView: updateParameters', parameters.value);
openmct.objects.mutate(domainObject, `configuration.comps.parameters`, parameters.value);
compsManager.setDomainObject(domainObject);
applyTestData();
@ -233,6 +239,9 @@ function getValueFormatter() {
}
function applyTestData() {
if (!expression.value || !parameters.value) {
return;
}
const scope = parameters.value.reduce((acc, parameter) => {
acc[parameter.name] = parameter.testValue;
return acc;