feat(multimonitor): Add simple multi-monitor support to open another study(#4178)
This commit is contained in:
1 parent
a4a6de07f4
commit
07c628e689
49 files changed
+1476
-367
No files matched your search
@@ -1,6 +1,8 @@
|
||||
import log from '../log.js';
|
||||
import { Command, Commands, ComplexCommand } from '../types/Command';
|
||||
|
||||
export type RunInput = Command | Commands | Command[] | string | undefined;
|
||||
|
||||
/**
|
||||
* The definition of a command
|
||||
*
|
||||
@@ -157,6 +159,44 @@ export class CommandsManager {
|
||||
}
|
||||
}
|
||||
|
||||
public static convertCommands(toRun: Command | Commands | Command[] | string) {
|
||||
if (typeof toRun === 'string') {
|
||||
return [{ commandName: toRun }];
|
||||
}
|
||||
if ('commandName' in toRun) {
|
||||
return [toRun as ComplexCommand];
|
||||
}
|
||||
if ('commands' in toRun) {
|
||||
const commandsInput = (toRun as Commands).commands;
|
||||
return this.convertCommands(commandsInput);
|
||||
}
|
||||
if (Array.isArray(toRun)) {
|
||||
return toRun.map(command => CommandsManager.convertCommands(command)[0]);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
private validate(input: RunInput, options: Record<string, unknown> = {}): ComplexCommand[] {
|
||||
if (!input) {
|
||||
console.debug('No command to run');
|
||||
return [];
|
||||
}
|
||||
|
||||
// convert commands
|
||||
const converted: ComplexCommand[] = CommandsManager.convertCommands(input);
|
||||
if (!converted.length) {
|
||||
console.debug('Command is not runnable', input);
|
||||
return [];
|
||||
}
|
||||
|
||||
return converted.map(command => ({
|
||||
commandName: command.commandName,
|
||||
commandOptions: { ...options, ...command.commandOptions },
|
||||
context: command.context,
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Run one or more commands with specified extra options.
|
||||
* Returns the result of the last command run.
|
||||
@@ -178,57 +218,34 @@ export class CommandsManager {
|
||||
* @param options - to include in the commands run beyond
|
||||
* the commandOptions specified in the base.
|
||||
*/
|
||||
public run(
|
||||
toRun: Command | Commands | (Command | string)[] | string | undefined,
|
||||
options?: Record<string, unknown>
|
||||
): unknown {
|
||||
if (!toRun) {
|
||||
return;
|
||||
}
|
||||
public run(input: RunInput, options: Record<string, unknown> = {}): unknown[] {
|
||||
const commands = this.validate(input, options);
|
||||
|
||||
// Normalize `toRun` to an array of `ComplexCommand`
|
||||
let commands: ComplexCommand[] = [];
|
||||
if (typeof toRun === 'string') {
|
||||
commands = [{ commandName: toRun }];
|
||||
} else if ('commandName' in toRun) {
|
||||
commands = [toRun as ComplexCommand];
|
||||
} else if ('commands' in toRun) {
|
||||
const commandsInput = (toRun as Commands).commands;
|
||||
commands = Array.isArray(commandsInput)
|
||||
? commandsInput.map(cmd => (typeof cmd === 'string' ? { commandName: cmd } : cmd))
|
||||
: [{ commandName: commandsInput }];
|
||||
} else if (Array.isArray(toRun)) {
|
||||
commands = toRun.map(cmd => (typeof cmd === 'string' ? { commandName: cmd } : cmd));
|
||||
}
|
||||
|
||||
if (commands.length === 0) {
|
||||
console.log("Command isn't runnable", toRun);
|
||||
return;
|
||||
}
|
||||
|
||||
// Execute each command in the array
|
||||
let result: unknown;
|
||||
commands.forEach(command => {
|
||||
const results: unknown[] = [];
|
||||
for (let i = 0; i < commands.length; i++) {
|
||||
const command = commands[i];
|
||||
const { commandName, commandOptions, context } = command;
|
||||
if (commandName) {
|
||||
result = this.runCommand(
|
||||
commandName,
|
||||
{
|
||||
...commandOptions,
|
||||
...options,
|
||||
},
|
||||
context
|
||||
);
|
||||
} else {
|
||||
if (typeof command === 'function') {
|
||||
result = command();
|
||||
} else {
|
||||
console.warn('No command name supplied in', toRun);
|
||||
}
|
||||
}
|
||||
});
|
||||
results.push(this.runCommand(commandName, commandOptions, context));
|
||||
}
|
||||
|
||||
return result;
|
||||
return results;
|
||||
}
|
||||
|
||||
/** Like run, but await each command before continuing */
|
||||
public async runAsync(
|
||||
input: RunInput,
|
||||
options: Record<string, unknown> = {}
|
||||
): Promise<unknown[]> {
|
||||
const commands = this.validate(input, options);
|
||||
|
||||
const results: unknown[] = [];
|
||||
for (let i = 0; i < commands.length; i++) {
|
||||
const command = commands[i];
|
||||
const { commandName, commandOptions, context } = command;
|
||||
results.push(await this.runCommand(commandName, commandOptions, context));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user