Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // browser/fusb-service-contribution.ts
- import { interfaces } from "inversify";
- import { UsbService, workspacePath } from "../../common/usb-service-protocol";
- import { WebSocketConnectionProvider } from "@theia/core/lib/browser";
- export function bindUsbService(bind: interfaces.Bind): void {
- bind(UsbService).toDynamicValue(ctx => {
- const provider = ctx.container.get(WebSocketConnectionProvider);
- return provider.createProxy<UsbService>(workspacePath);
- }).inSingletonScope();
- };
- // common/usb-service-protocol.ts
- import { Event } from "@theia/core";
- import { Device } from "usb";
- export const workspacePath = '/services/ves/usb';
- export const UsbService = Symbol('UsbService');
- export interface UsbService {
- onDeviceConnected: Event<Device>;
- }
- // node/usb-service-module.ts
- import { ContainerModule } from 'inversify';
- import { ConnectionHandler, JsonRpcConnectionHandler } from '@theia/core';
- import { DefaultUsbService } from './default-usb-service';
- import { UsbService, workspacePath } from '../common/usb-service-protocol';
- export default new ContainerModule(bind => {
- bind(DefaultUsbService).toSelf().inSingletonScope();
- bind(UsbService).toService(DefaultUsbService);
- bind(ConnectionHandler).toDynamicValue(ctx =>
- new JsonRpcConnectionHandler(workspacePath, () => {
- return ctx.container.get(UsbService);
- })
- ).inSingletonScope()
- });
- // node/default-usb-service.ts
- import { injectable, postConstruct } from "inversify";
- import { UsbService } from "../common/usb-service-protocol";
- import { Device, on } from "usb";
- import { Emitter } from "@theia/core";
- @injectable()
- export class DefaultUsbService implements UsbService {
- protected readonly onDeviceConnectedEmitter = new Emitter<Device>();
- readonly onDeviceConnected = this.onDeviceConnectedEmitter.event;
- @postConstruct()
- protected init(): void {
- const self = this;
- on('attach', async function (device) {
- console.log('attach');
- self.onDeviceConnectedEmitter.fire(device);
- });
- }
- }
- // somewhere in frontend
- this.usbService.onDeviceConnected(() => console.log("connected!"));
- // error
- root INFO attach
- root ERROR TypeError: Cannot read property 'apply' of undefined
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement