-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added backend communication template
fixed #64 Signed-off-by: Jonas Helming <[email protected]>
- Loading branch information
1 parent
a89ddea
commit eddb063
Showing
9 changed files
with
207 additions
and
4 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,26 @@ | ||
import { ConnectionHandler, JsonRpcConnectionHandler } from "@theia/core"; | ||
import { ContainerModule } from "inversify"; | ||
import { BackendClient, HelloBackendWithClientService, HelloBackendService, HELLO_BACKEND_PATH, HELLO_BACKEND_WITH_CLIENT_PATH } from "../common/protocol"; | ||
import { HelloBackendWithClientServiceImpl } from "./hello-backend-with-client-service"; | ||
import { HelloBackendServiceImpl } from "./hello-backend-service"; | ||
|
||
export default new ContainerModule(bind => { | ||
bind(HelloBackendService).to(HelloBackendServiceImpl).inSingletonScope() | ||
bind(ConnectionHandler).toDynamicValue(ctx => | ||
new JsonRpcConnectionHandler(HELLO_BACKEND_PATH, () => { | ||
return ctx.container.get<HelloBackendService>(HelloBackendService); | ||
}) | ||
).inSingletonScope(); | ||
|
||
bind(HelloBackendWithClientService).to(HelloBackendWithClientServiceImpl).inSingletonScope() | ||
bind(ConnectionHandler).toDynamicValue(ctx => | ||
new JsonRpcConnectionHandler<BackendClient>(HELLO_BACKEND_WITH_CLIENT_PATH, client => { | ||
const server = ctx.container.get<HelloBackendWithClientServiceImpl>(HelloBackendWithClientService); | ||
server.setClient(client); | ||
client.onDidCloseConnection(() => server.dispose()); | ||
return server; | ||
}) | ||
).inSingletonScope(); | ||
}); | ||
|
||
|
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,31 @@ | ||
import { Command, CommandContribution, CommandRegistry} from '@theia/core/lib/common'; | ||
import { inject, injectable } from 'inversify'; | ||
import { HelloBackendWithClientService, HelloBackendService } from '../common/protocol'; | ||
|
||
const SayHelloViaBackendCommandWithCallBack: Command = { | ||
id: 'sayHelloOnBackendWithCallBack.command', | ||
label: 'Say hello on the backend with a callback to the client', | ||
}; | ||
|
||
const SayHelloViaBackendCommand: Command = { | ||
id: 'sayHelloOnBackend.command', | ||
label: 'Say hello on the backend', | ||
}; | ||
|
||
@injectable() | ||
export class <%= params.extensionPrefix %>CommandContribution implements CommandContribution { | ||
|
||
constructor( | ||
@inject(HelloBackendWithClientService) private readonly helloBackendWithClientService: HelloBackendWithClientService, | ||
@inject(HelloBackendService) private readonly helloBackendService: HelloBackendService, | ||
) { } | ||
|
||
registerCommands(registry: CommandRegistry): void { | ||
registry.registerCommand(SayHelloViaBackendCommandWithCallBack, { | ||
execute: () => this.helloBackendWithClientService.greet().then(r => console.log(r)) | ||
}); | ||
registry.registerCommand(SayHelloViaBackendCommand, { | ||
execute: () => this.helloBackendService.sayHelloTo('World').then(r => console.log(r)) | ||
}); | ||
} | ||
} |
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 @@ | ||
import { CommandContribution} from '@theia/core'; | ||
import { WebSocketConnectionProvider } from "@theia/core/lib/browser"; | ||
import { ContainerModule, injectable } from "inversify"; | ||
import { BackendClient, HelloBackendWithClientService, HelloBackendService, HELLO_BACKEND_PATH, HELLO_BACKEND_WITH_CLIENT_PATH } from '../common/protocol'; | ||
import { <%= params.extensionPrefix %>CommandContribution} from './<%= params.extensionPath %>-contribution'; | ||
|
||
export default new ContainerModule(bind => { | ||
bind(CommandContribution).to(<%= params.extensionPrefix %>CommandContribution).inSingletonScope(); | ||
bind(BackendClient).to(BackendClientImpl).inSingletonScope(); | ||
|
||
bind(HelloBackendService).toDynamicValue(ctx => { | ||
const connection = ctx.container.get(WebSocketConnectionProvider); | ||
return connection.createProxy<HelloBackendService>(HELLO_BACKEND_PATH); | ||
}).inSingletonScope(); | ||
|
||
bind(HelloBackendWithClientService).toDynamicValue(ctx => { | ||
const connection = ctx.container.get(WebSocketConnectionProvider); | ||
const backendClient: BackendClient = ctx.container.get(BackendClient); | ||
return connection.createProxy<HelloBackendWithClientService>(HELLO_BACKEND_WITH_CLIENT_PATH, backendClient); | ||
}).inSingletonScope(); | ||
}); | ||
|
||
@injectable() | ||
class BackendClientImpl implements BackendClient { | ||
getName(): Promise<string> { | ||
return new Promise(resolve => resolve('Client')); | ||
} | ||
|
||
} |
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,9 @@ | ||
import { injectable } from "inversify"; | ||
import { HelloBackendService } from "../common/protocol"; | ||
|
||
@injectable() | ||
export class HelloBackendServiceImpl implements HelloBackendService { | ||
sayHelloTo(name: string): Promise<string> { | ||
return new Promise<string>(resolve => resolve('Hello ' + name)); | ||
} | ||
} |
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,20 @@ | ||
import { injectable } from "inversify"; | ||
import { BackendClient, HelloBackendWithClientService } from "../common/protocol"; | ||
|
||
@injectable() | ||
export class HelloBackendWithClientServiceImpl implements HelloBackendWithClientService { | ||
private client?: BackendClient; | ||
greet(): Promise<string> { | ||
return new Promise<string>((resolve, reject) => | ||
this.client ? this.client.getName().then(greet => resolve('Hello ' + greet)) | ||
: reject('No Client')); | ||
} | ||
dispose(): void { | ||
// do nothing | ||
} | ||
setClient(client: BackendClient): void { | ||
this.client = client; | ||
} | ||
|
||
|
||
} |
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,18 @@ | ||
import { JsonRpcServer } from '@theia/core/lib/common/messaging'; | ||
|
||
export const HelloBackendService = Symbol('HelloBackendService'); | ||
export const HELLO_BACKEND_PATH = '/services/helloBackend'; | ||
|
||
export interface HelloBackendService { | ||
sayHelloTo(name: string): Promise<string> | ||
} | ||
export const HelloBackendWithClientService = Symbol('BackendWithClient'); | ||
export const HELLO_BACKEND_WITH_CLIENT_PATH = '/services/withClient'; | ||
|
||
export interface HelloBackendWithClientService extends JsonRpcServer<BackendClient> { | ||
greet(): Promise<string> | ||
} | ||
export const BackendClient = Symbol('BackendClient'); | ||
export interface BackendClient { | ||
getName(): Promise<string>; | ||
} |
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