-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eclipse-che/che#9063 implement first prototype of Command API
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
- Loading branch information
Showing
10 changed files
with
297 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2018 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
import { createProxyIdentifier, ProxyIdentifier } from './rpc-protocol'; | ||
import * as theia from 'theia'; | ||
/** | ||
* A command handler is an implementation of a command. | ||
* | ||
* A command can have multiple handlers | ||
* but they should be active in different contexts, | ||
* otherwise first active will be executed. | ||
*/ | ||
export interface CommandHandler { | ||
/** | ||
* Execute this handler. | ||
*/ | ||
execute(...args: any[]): any; | ||
/** | ||
* Test whether this handler is enabled (active). | ||
*/ | ||
isEnabled?(...args: any[]): boolean; | ||
/** | ||
* Test whether menu items for this handler should be visible. | ||
*/ | ||
isVisible?(...args: any[]): boolean; | ||
} | ||
|
||
export interface CommandRegistryMain { | ||
/** | ||
* Register the given command and handler if present. | ||
* | ||
* Throw if a command is already registered for the given command identifier. | ||
*/ | ||
registerCommand(command: theia.Command): void; | ||
|
||
unregisterCommand(id: string): void; | ||
executeCommand<T>(id: string, args: any[]): PromiseLike<T>; | ||
getCommands(): PromiseLike<string[]>; | ||
} | ||
|
||
export interface CommandRegistryExt { | ||
executeContributedCommand<T>(id: string): PromiseLike<T>; | ||
} | ||
|
||
export const EXTENSION_RPC_CONTEXT = { | ||
COMMAND_REGISTRY_MAIN: <ProxyIdentifier<CommandRegistryMain>>createProxyIdentifier<CommandRegistryMain>("CommandRegistryMain") | ||
}; | ||
|
||
export const MAIN_RPC_CONTEXT = { | ||
COMMAND_REGISTRY_EXT: createProxyIdentifier<CommandRegistryExt>("CommandRegistryExt") | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/isolated-extension-api/src/browser/command-registry-main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2015-2018 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
import { CommandRegistryExt, MAIN_RPC_CONTEXT, CommandRegistryMain } from '../api/extension-api'; | ||
import { injectable, inject } from "inversify"; | ||
import { CommandRegistry } from '@theia/core/lib/common/command'; | ||
import { ExtensionWorker } from './extension-worker'; | ||
import * as theia from 'theia'; | ||
import { Disposable } from '@theia/core/lib/common/disposable'; | ||
|
||
@injectable() | ||
export class CommandRegistryMainImpl implements CommandRegistryMain { | ||
private proxy: CommandRegistryExt; | ||
private disposables = new Map<string, Disposable>(); | ||
|
||
constructor( @inject(CommandRegistry) private readonly delegate: CommandRegistry, | ||
@inject(ExtensionWorker) worker: ExtensionWorker) { | ||
this.proxy = worker.rpc.getProxy(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT); | ||
} | ||
|
||
registerCommand(command: theia.Command): void { | ||
this.disposables.set( | ||
command.id, | ||
this.delegate.registerCommand(command, { | ||
execute: (...args: any[]) => { | ||
this.proxy.executeContributedCommand(command.id); | ||
}, | ||
isEnabled() { return true; }, | ||
isVisible() { return true; } | ||
})); | ||
} | ||
unregisterCommand(id: string): void { | ||
const dis = this.disposables.get(id); | ||
if (dis) { | ||
dis.dispose(); | ||
this.disposables.delete(id); | ||
} | ||
} | ||
executeCommand<T>(id: string, args: any[]): PromiseLike<T> { | ||
throw new Error("Method not implemented."); | ||
} | ||
getCommands(): PromiseLike<string[]> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/isolated-extension-api/src/browser/extension-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (C) 2015-2018 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
import { injectable } from "inversify"; | ||
import { RPCProtocolImpl, RPCProtocol } from '../api/rpc-protocol'; | ||
import { Emitter } from '@theia/core/lib/common/event'; | ||
|
||
@injectable() | ||
export class ExtensionWorker { | ||
|
||
private worker: Worker; | ||
public readonly rpc: RPCProtocol; | ||
constructor() { | ||
const emmitter = new Emitter(); | ||
this.worker = new (require('../worker/worker-main')); | ||
this.worker.onmessage = (message) => { | ||
emmitter.fire(message.data); | ||
}; | ||
|
||
this.rpc = new RPCProtocolImpl({ | ||
onMessage: emmitter.event, | ||
send: (m: {}) => this.worker.postMessage(m) | ||
}); | ||
|
||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
packages/isolated-extension-api/src/extension/comand-registry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2018 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
import { CommandRegistryExt, EXTENSION_RPC_CONTEXT as Ext, CommandRegistryMain } from '../api/extension-api'; | ||
import { Disposable } from '@theia/core/lib/common/disposable'; | ||
import { RPCProtocol } from '../api/rpc-protocol'; | ||
import * as theia from 'theia'; | ||
|
||
export type Handler = <T>(...args: any[]) => T | PromiseLike<T>; | ||
|
||
export class CommandRegistryImpl implements CommandRegistryExt { | ||
|
||
private proxy: CommandRegistryMain; | ||
private commands = new Map<string, Handler>(); | ||
|
||
constructor(rpc: RPCProtocol) { | ||
this.proxy = rpc.getProxy(Ext.COMMAND_REGISTRY_MAIN); | ||
} | ||
registerCommand(command: theia.Command, handler: Handler): Disposable { | ||
if (this.commands.has(command.id)) { | ||
throw new Error(`Command ${command.id} already exist`); | ||
} | ||
this.commands.set(command.id, handler); | ||
this.proxy.registerCommand(command); | ||
|
||
return Disposable.create(() => { | ||
this.proxy.unregisterCommand(command.id); | ||
}); | ||
|
||
} | ||
|
||
dispose(): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
executeContributedCommand<T>(id: string): PromiseLike<T> { | ||
const handler = this.commands.get(id); | ||
if (handler) { | ||
return Promise.resolve(handler()); | ||
} else { | ||
return Promise.reject(new Error(`Command ${id} doesn't exist`)); | ||
} | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
packages/isolated-extension-api/src/extension/extension-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (C) 2018 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
import { MAIN_RPC_CONTEXT } from '../api/extension-api'; | ||
import { Disposable } from '@theia/core/lib/common/disposable'; | ||
import { RPCProtocol } from '../api/rpc-protocol'; | ||
import * as theia from 'theia'; | ||
import { CommandRegistryImpl } from './comand-registry'; | ||
|
||
export function createAPI(rpc: RPCProtocol): typeof theia { | ||
const commandRegistryExt = rpc.set(MAIN_RPC_CONTEXT.COMMAND_REGISTRY_EXT, new CommandRegistryImpl(rpc)); | ||
|
||
const commands: typeof theia.commands = { | ||
registerCommand(command: theia.Command, callback: <T>(...args: any[]) => T | Thenable<T>): Disposable { | ||
return commandRegistryExt.registerCommand(command, callback); | ||
} | ||
}; | ||
return <typeof theia>{ | ||
commands | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright (C) 2018 Red Hat, Inc. | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
*/ | ||
//import { Disposable } from '@theia/core/lib/common/disposable'; | ||
|
||
declare module 'theia' { | ||
/** | ||
* A command is a unique identifier of a function | ||
* which can be executed by a user via a keyboard shortcut, | ||
* a menu action or directly. | ||
*/ | ||
export interface Command { | ||
/** | ||
* A unique identifier of this command. | ||
*/ | ||
id: string; | ||
/** | ||
* A label of this command. | ||
*/ | ||
label?: string; | ||
/** | ||
* An icon class of this command. | ||
*/ | ||
iconClass?: string; | ||
} | ||
export namespace commands { | ||
export function registerCommand(command: Command, callback: (...args: any[]) => any): Disposable | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters