-
Notifications
You must be signed in to change notification settings - Fork 393
/
api.ts
114 lines (98 loc) · 3.23 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { ipcMain } from 'electron';
import { Injectable, Autowired, INJECTOR_TOKEN, Injector } from '@opensumi/di';
import { IDisposable, Disposable, getDebugLogger } from '@opensumi/ide-core-common';
import { IElectronURLService, IURLHandler } from '@opensumi/ide-core-common/lib/electron';
import {
ElectronMainApiRegistry,
ElectronURLHandlerRegistry,
IElectronMainApiProvider,
IElectronMainApp,
} from './types';
@Injectable()
export class ElectronMainApiRegistryImpl implements ElectronMainApiRegistry {
private apis: Map<string, ElectronMainApiProxy> = new Map();
@Autowired(INJECTOR_TOKEN)
injector: Injector;
constructor() {}
registerMainApi(name: string, api: IElectronMainApiProvider): IDisposable {
if (this.apis.has(name)) {
this.apis.get(name)!.dispose();
}
const proxy = this.injector.get(ElectronMainApiProxy, [name, api]);
getDebugLogger().log('注册Electron Main Api: ' + name);
this.apis.set(name, proxy);
return {
dispose: () => {
if (this.apis.get(name) === proxy) {
this.apis.delete(name);
}
proxy.dispose();
},
};
}
}
@Injectable()
export class ElectronURLHandlerRegistryImpl implements ElectronURLHandlerRegistry {
@Autowired(INJECTOR_TOKEN)
injector: Injector;
registerURLDefaultHandler(handler: IURLHandler): IDisposable {
const urlService: IElectronURLService = this.injector.get(IElectronURLService);
urlService.registerDefaultHandler(handler);
return {
dispose: () => {},
};
}
registerURLHandler(handler: IURLHandler): IDisposable {
const urlService: IElectronURLService = this.injector.get(IElectronURLService);
urlService.registerHandler(handler);
return {
dispose: () => {
urlService.deregisterHandler(handler);
},
};
}
}
@Injectable({ multiple: true })
export class ElectronMainApiProxy extends Disposable {
@Autowired(IElectronMainApp)
app: IElectronMainApp;
constructor(name: string, target: IElectronMainApiProvider) {
super();
const requestHandler = async (event, method: string, requestId: number, ...args: any[]) => {
try {
if (!target[method] || typeof target[method] !== 'function') {
throw new Error(`No Request Handler for ${name}.${method}`);
}
const result = await target[method](...args);
if (!event.sender.isDestroyed()) {
event.sender.send('response:' + name, requestId, undefined, result);
}
} catch (e) {
getDebugLogger().error(e);
const err = {
message: e.message || e.toString(),
stack: e.stack,
};
if (!event.sender.isDestroyed()) {
event.sender.send('response:' + name, requestId, err);
}
}
};
ipcMain.on('request:' + name, requestHandler);
target.eventEmitter = {
fire: (event: string, ...args: any[]) => {
this.app.getCodeWindows().forEach((window) => {
const browser = window.getBrowserWindow();
if (!browser.isDestroyed()) {
browser.webContents.send('event:' + name, event, ...args);
}
});
},
};
this.addDispose({
dispose: () => {
ipcMain.removeAllListeners('request:' + name);
},
});
}
}