@finos/legend-application
Version:
Legend application core
54 lines • 2.1 kB
JavaScript
/**
* Copyright (c) 2020-present, Goldman Sachs
*
* 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.
*/
import { LogEvent } from '@finos/legend-shared';
import { action, makeObservable, observable } from 'mobx';
import { APPLICATION_EVENT } from '../__lib__/LegendApplicationEvent.js';
export const collectKeyedCommandConfigEntriesFromConfig = (rawEntries) => Object.entries(rawEntries).map((entry) => ({
key: entry[0],
content: entry[1],
}));
export class CommandService {
applicationStore;
commandRegistry = new Map();
constructor(applicationStore) {
makeObservable(this, {
commandRegistry: observable,
registerCommand: action,
deregisterCommand: action,
});
this.applicationStore = applicationStore;
}
registerCommand(command) {
const commandKey = command.key;
if (this.commandRegistry.has(commandKey)) {
this.applicationStore.logService.warn(LogEvent.create(APPLICATION_EVENT.COMMAND_CENTER_REGISTRATION__FAILURE), `Can't register command: command '${commandKey}' is already registered`);
return;
}
this.commandRegistry.set(commandKey, command);
}
deregisterCommand(commandKey) {
this.commandRegistry.delete(commandKey);
}
runCommand(commandKey, args) {
const command = this.commandRegistry.get(commandKey);
if (command && (!command.trigger || command.trigger())) {
command.action?.(args);
return true;
}
return false;
}
}
//# sourceMappingURL=CommandService.js.map