Move all support for the legacy API into a plugin (#4614)

* Make legacy support optional
* Fix order of legacy plugin registration
* Added 'supportComposition' function
* Add composition policy to check that parent supports composition
* Fix memory leaks in timer
This commit is contained in:
Andrew Henry
2022-01-03 14:21:19 -08:00
committed by GitHub
parent 51e4c0c836
commit 3a65f75d21
23 changed files with 231 additions and 241 deletions

View File

@ -31,7 +31,7 @@ export default class LADTableViewProvider {
}
canView(domainObject) {
const supportsComposition = this.openmct.composition.get(domainObject) !== undefined;
const supportsComposition = this.openmct.composition.supportsComposition(domainObject);
const providesTelemetry = this.openmct.telemetry.isTelemetryObject(domainObject);
return domainObject.type === 'LadTable'

View File

@ -130,14 +130,6 @@ describe("the plugin", function () {
let mockComposition;
beforeEach(async () => {
const getFunc = openmct.$injector.get;
spyOn(openmct.$injector, "get")
.withArgs("exportImageService").and.returnValue({
exportPNG: () => {},
exportJPG: () => {}
})
.and.callFake(getFunc);
barGraphObject = {
identifier: {
namespace: "",

View File

@ -87,6 +87,7 @@ describe("Clock plugin:", () => {
spyOn(openmct.objects, 'get').and.returnValue(Promise.resolve(clockViewObject));
spyOn(openmct.objects, 'save').and.returnValue(Promise.resolve(true));
spyOn(openmct.objects, 'supportsMutation').and.returnValue(true);
const applicableViews = openmct.objectViews.get(clockViewObject, [clockViewObject]);
clockViewProvider = applicableViews.find(viewProvider => viewProvider.key === 'clock.view');

View File

@ -23,7 +23,6 @@ import ConditionSetViewProvider from './ConditionSetViewProvider.js';
import ConditionSetCompositionPolicy from "./ConditionSetCompositionPolicy";
import ConditionSetMetadataProvider from './ConditionSetMetadataProvider';
import ConditionSetTelemetryProvider from './ConditionSetTelemetryProvider';
import ConditionSetViewPolicy from './ConditionSetViewPolicy';
import uuid from "uuid";
export default function ConditionPlugin() {
@ -55,10 +54,6 @@ export default function ConditionPlugin() {
domainObject.telemetry = {};
}
});
openmct.legacyExtension('policies', {
category: 'view',
implementation: ConditionSetViewPolicy
});
openmct.composition.addPolicy(new ConditionSetCompositionPolicy(openmct).allow);
openmct.telemetry.addProvider(new ConditionSetMetadataProvider(openmct));
openmct.telemetry.addProvider(new ConditionSetTelemetryProvider(openmct));

View File

@ -0,0 +1,78 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, 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.
*****************************************************************************/
define(function () {
function BundleRegistry() {
this.bundles = {};
this.knownBundles = {};
}
BundleRegistry.prototype.register = function (path, definition) {
if (Object.prototype.hasOwnProperty.call(this.knownBundles, path)) {
throw new Error('Cannot register bundle with duplicate path', path);
}
this.knownBundles[path] = definition;
};
BundleRegistry.prototype.enable = function (path) {
if (!this.knownBundles[path]) {
throw new Error('Unknown bundle ' + path);
}
this.bundles[path] = this.knownBundles[path];
};
BundleRegistry.prototype.disable = function (path) {
if (!this.bundles[path]) {
throw new Error('Tried to disable inactive bundle ' + path);
}
delete this.bundles[path];
};
BundleRegistry.prototype.contains = function (path) {
return Boolean(this.bundles[path]);
};
BundleRegistry.prototype.get = function (path) {
return this.bundles[path];
};
BundleRegistry.prototype.list = function () {
return Object.keys(this.bundles);
};
BundleRegistry.prototype.remove = BundleRegistry.prototype.disable;
BundleRegistry.prototype.delete = function (path) {
if (!this.knownBundles[path]) {
throw new Error('Cannot remove Unknown Bundle ' + path);
}
delete this.bundles[path];
delete this.knownBundles[path];
};
return BundleRegistry;
});

View File

@ -0,0 +1,88 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, 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.
*****************************************************************************/
define(['./BundleRegistry'], function (BundleRegistry) {
describe("BundleRegistry", function () {
let testPath;
let bundleRegistry;
beforeEach(function () {
testPath = 'some/bundle';
bundleRegistry = new BundleRegistry();
});
it("initially lists no bundles", function () {
expect(bundleRegistry.list()).toEqual([]);
});
it("initially contains no bundles", function () {
expect(bundleRegistry.contains(testPath))
.toBe(false);
});
it("initially provides no bundles", function () {
expect(bundleRegistry.get(testPath))
.toBeUndefined();
});
describe("when a bundle has been registered", function () {
let testBundleDef;
beforeEach(function () {
testBundleDef = { someKey: "some value" };
bundleRegistry.register(testPath, testBundleDef);
bundleRegistry.enable(testPath);
});
it("lists registered bundles", function () {
expect(bundleRegistry.list()).toEqual([testPath]);
});
it("contains registered bundles", function () {
expect(bundleRegistry.contains(testPath))
.toBe(true);
});
it("provides registered bundles", function () {
expect(bundleRegistry.get(testPath))
.toBe(testBundleDef);
});
describe("and then removed", function () {
beforeEach(function () {
bundleRegistry.remove(testPath);
});
it("appears empty again", function () {
expect(bundleRegistry.list()).toEqual([]);
});
it("does not contain the removed bundle", function () {
expect(bundleRegistry.contains(testPath))
.toBe(false);
});
});
});
});
});

View File

@ -0,0 +1,101 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, 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.
*****************************************************************************/
const DEFAULTS = [
'src/adapter',
'platform/framework',
'platform/core',
'platform/representation',
'platform/commonUI/browse',
'platform/commonUI/edit',
'platform/commonUI/dialog',
'platform/commonUI/general',
'platform/commonUI/inspect',
'platform/commonUI/mobile',
'platform/commonUI/notification',
'platform/containment',
'platform/exporters',
'platform/telemetry',
'platform/identity',
'platform/persistence/aggregator',
'platform/policy',
'platform/entanglement',
'platform/status',
'platform/commonUI/regions'
];
define([
'../../adapter/bundle',
'../../../example/eventGenerator/bundle',
'../../../example/export/bundle',
'../../../example/forms/bundle',
'../../../example/identity/bundle',
'../../../example/mobile/bundle',
'../../../example/msl/bundle',
'../../../example/notifications/bundle',
'../../../example/persistence/bundle',
'../../../example/policy/bundle',
'../../../example/profiling/bundle',
'../../../example/scratchpad/bundle',
'../../../example/styleguide/bundle',
'../../../platform/commonUI/browse/bundle',
'../../../platform/commonUI/dialog/bundle',
'../../../platform/commonUI/edit/bundle',
'../../../platform/commonUI/general/bundle',
'../../../platform/commonUI/inspect/bundle',
'../../../platform/commonUI/mobile/bundle',
'../../../platform/commonUI/notification/bundle',
'../../../platform/commonUI/regions/bundle',
'../../../platform/containment/bundle',
'../../../platform/core/bundle',
'../../../platform/entanglement/bundle',
'../../../platform/exporters/bundle',
'../../../platform/features/static-markup/bundle',
'../../../platform/framework/bundle',
'../../../platform/framework/src/load/Bundle',
'../../../platform/identity/bundle',
'../../../platform/persistence/aggregator/bundle',
'../../../platform/persistence/elastic/bundle',
'../../../platform/persistence/queue/bundle',
'../../../platform/policy/bundle',
'../../../platform/representation/bundle',
'../../../platform/status/bundle',
'../../../platform/telemetry/bundle'
], function () {
const LEGACY_BUNDLES = Array.from(arguments);
return function installDefaultBundles(bundleRegistry) {
registerLegacyBundles(LEGACY_BUNDLES);
enableDefaultBundles();
function registerLegacyBundles(bundles) {
bundles.forEach((bundle, i) => {
bundleRegistry.register(bundle.name, bundle.definition);
});
}
function enableDefaultBundles() {
DEFAULTS.forEach(function (bundlePath) {
bundleRegistry.enable(bundlePath);
});
}
};
});

View File

@ -20,15 +20,6 @@
* at runtime from the About dialog for additional information.
*****************************************************************************/
function ConditionSetViewPolicy() {
}
ConditionSetViewPolicy.prototype.allow = function (view, domainObject) {
if (domainObject.getModel().type === 'conditionSet') {
return view.key === 'conditionSet.view';
}
return true;
};
export default ConditionSetViewPolicy;
define(['./BundleRegistry'], function (BundleRegistry) {
return new BundleRegistry();
});

View File

@ -0,0 +1,33 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, 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.
*****************************************************************************/
define([
'./legacyRegistry',
'./BundleRegistry'
], function (legacyRegistry, BundleRegistry) {
describe("legacyRegistry", function () {
it("is a BundleRegistry", function () {
expect(legacyRegistry instanceof BundleRegistry).toBe(true);
});
});
});

View File

@ -0,0 +1,126 @@
/*****************************************************************************
* Open MCT, Copyright (c) 2014-2021, 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 installDefaultBundles from './installDefaultBundles';
import BundleRegistry from './BundleRegistry';
import Main from '../../../platform/framework/src/Main';
import objectUtils from '../../api/objects/object-utils';
import DomainObjectImpl from '../../../platform/core/src/objects/DomainObjectImpl';
import ContextualDomainObject from '../../../platform/core/src/capabilities/ContextualDomainObject';
export default function LegacySupportPlugin() {
return function install(openmct) {
openmct.legacyBundle = {
extensions: {
services: [
{
key: "openmct",
implementation: function ($injector) {
openmct.$injector = $injector;
return openmct;
},
depends: ['$injector']
}
]
}
};
openmct.legacyExtension = function (category, extension) {
this.legacyBundle.extensions[category] =
this.legacyBundle.extensions[category] || [];
this.legacyBundle.extensions[category].push(extension);
}.bind(openmct);
/**
* Return a legacy object, for compatibility purposes only. This method
* will be deprecated and removed in the future.
* @private
*/
openmct.legacyObject = function (domainObject) {
let capabilityService = this.$injector.get('capabilityService');
function instantiate(model, keyString) {
const capabilities = capabilityService.getCapabilities(model, keyString);
model.id = keyString;
return new DomainObjectImpl(keyString, model, capabilities);
}
if (Array.isArray(domainObject)) {
// an array of domain objects. [object, ...ancestors] representing
// a single object with a given chain of ancestors. We instantiate
// as a single contextual domain object.
return domainObject
.map((o) => {
let keyString = objectUtils.makeKeyString(o.identifier);
let oldModel = objectUtils.toOldFormat(o);
return instantiate(oldModel, keyString);
})
.reverse()
.reduce((parent, child) => {
return new ContextualDomainObject(child, parent);
});
} else {
let keyString = objectUtils.makeKeyString(domainObject.identifier);
let oldModel = objectUtils.toOldFormat(domainObject);
return instantiate(oldModel, keyString);
}
}.bind(openmct);
openmct.legacyRegistry = new BundleRegistry();
installDefaultBundles(openmct.legacyRegistry);
const patchedStart = openmct.start.bind(openmct);
openmct.start = async () => {
openmct.legacyRegistry.register('adapter', openmct.legacyBundle);
openmct.legacyRegistry.enable('adapter');
openmct.legacyExtension('runs', {
depends: ['navigationService'],
implementation: function (navigationService) {
navigationService
.addListener(openmct.emit.bind(openmct, 'navigation'));
}
});
// TODO: remove with legacy types.
openmct.types.listKeys().forEach(function (typeKey) {
const type = openmct.types.get(typeKey);
const legacyDefinition = type.toLegacyDefinition();
legacyDefinition.key = typeKey;
openmct.legacyExtension('types', legacyDefinition);
});
const main = new Main();
const angularInstance = await main.run(openmct);
openmct.$angular = angularInstance;
openmct.$injector.get('objectService');
return patchedStart();
};
};
}

View File

@ -30,7 +30,6 @@ import {
describe('the plugin', () => {
let notificationIndicatorPlugin;
let openmct;
let indicatorObject;
let indicatorElement;
let parentElement;
let mockMessages = ['error', 'test', 'notifications'];
@ -43,9 +42,6 @@ describe('the plugin', () => {
parentElement = document.createElement('div');
indicatorObject = openmct.indicators.indicatorObjects.find(indicator => indicator.key === 'notifications-indicator');
indicatorElement = indicatorObject.element;
openmct.on('start', () => {
mockMessages.forEach(message => {
openmct.notifications.error(message);
@ -53,7 +49,7 @@ describe('the plugin', () => {
done();
});
openmct.startHeadless();
openmct.start();
});
afterEach(() => {
@ -68,7 +64,7 @@ describe('the plugin', () => {
});
it('notifies the user of the number of notifications', () => {
let notificationCountElement = parentElement.querySelector('.c-indicator__count');
let notificationCountElement = document.querySelector('.c-indicator__count');
expect(notificationCountElement.innerText).toEqual(mockMessages.length.toString());
});

View File

@ -533,13 +533,6 @@ describe("the plugin", function () {
let plotViewComponentObject;
beforeEach(() => {
const getFunc = openmct.$injector.get;
spyOn(openmct.$injector, "get")
.withArgs("exportImageService").and.returnValue({
exportPNG: () => {},
exportJPG: () => {}
})
.and.callFake(getFunc);
stackedPlotObject = {
identifier: {

View File

@ -74,7 +74,9 @@ define([
'./clock/plugin',
'./DeviceClassifier/plugin',
'./timer/plugin',
'./localStorage/plugin'
'./localStorage/plugin',
'./legacySupport/plugin.js',
'../adapter/indicators/legacy-indicators-plugin'
], function (
_,
UTCTimeSystem,
@ -129,7 +131,9 @@ define([
Clock,
DeviceClassifier,
Timer,
LocalStorage
LocalStorage,
LegacySupportPlugin,
LegacyIndicatorsPlugin
) {
const bundleMap = {
Elasticsearch: 'platform/persistence/elastic'
@ -237,6 +241,8 @@ define([
plugins.Timer = Timer.default;
plugins.DeviceClassifier = DeviceClassifier.default;
plugins.LocalStorage = LocalStorage.default;
plugins.LegacySupport = LegacySupportPlugin.default;
plugins.LegacyIndicators = LegacyIndicatorsPlugin;
return plugins;
});

View File

@ -191,7 +191,7 @@ export default {
});
});
},
destroyed() {
beforeDestroy() {
this.active = false;
if (this.unlisten) {
this.unlisten();

View File

@ -60,6 +60,8 @@ describe("Timer plugin:", () => {
timerDefinition = openmct.types.get('timer').definition;
timerDefinition.initialize(timerDomainObject);
spyOn(openmct.objects, 'supportsMutation').and.returnValue(true);
openmct.on('start', resolve);
openmct.start(appHolder);
});
@ -93,6 +95,8 @@ describe("Timer plugin:", () => {
const applicableViews = openmct.objectViews.get(timerViewObject, [timerViewObject]);
timerViewProvider = applicableViews.find(viewProvider => viewProvider.key === 'timer.view');
spyOn(openmct.objects, 'get').and.returnValue(Promise.resolve(timerViewObject));
mutableTimerObject = await openmct.objects.getMutable(timerViewObject.identifier);
timerObjectPath = [mutableTimerObject];
@ -102,6 +106,10 @@ describe("Timer plugin:", () => {
await Vue.nextTick();
});
afterEach(() => {
timerView.destroy();
});
it("should migrate old object properties to the configuration section", () => {
openmct.objects.applyGetInterceptors(timerViewObject.identifier, timerViewObject);
expect(timerViewObject.configuration.timerFormat).toBe('short');