第一篇: vscode源码分析【一】从源码运行vscode
第二篇:vscode源码分析【二】程序的启动逻辑,第一个窗口是如何建立的
第三篇:vscode源码分析【三】程序的启动逻辑,性能问题的追踪
第四篇:vscode源码分析【四】程序启动的逻辑,最初建立的服务
第五篇:vscode源码分析【五】事件分发机制
第六篇:vscode源码分析【六】服务实例化和单例的实现
在mian.ts中的doStartup方法里,建立了一个命名管道服务
(src\vs\code\electron-main\main.ts)html
server = await serve(environmentService.mainIPCHandle); once(lifecycleService.onWillShutdown)(() => server.dispose());
传入的environmentService.mainIPCHandle是命名管道的识别路径,
一个有规则的字符串,规则以下:node
function getWin32IPCHandle(userDataPath: string, type: string): string { const scope = crypto.createHash('md5').update(userDataPath).digest('hex'); return `\\\\.\\pipe\\${scope}-${pkg.version}-${type}-sock`; }
注意:每次启动程序,取这个字符串的时候,都会得到一样的值(并且这个值是会被缓存起来的);
之后监听消息、发送消息,都根据这个字符串来;
建立服务的代码(serve):es6
export function serve(hook: any): Promise<Server> { return new Promise<Server>((c, e) => { const server = createServer(); server.on('error', e); server.listen(hook, () => { server.removeListener('error', e); c(new Server(server)); }); }); }
这个方法返回了一个Promise的对象,
c和e是Promise的参数,c表明成功时的回调,e表明失败时的回调(有点相似es6的Promise)
匿名函数内createServer就是nodejs里的原生接口,
Server类绑定了链接和断开的事件,暂时不细说;
回头看看main.ts startup方法里有这么一句:windows
instantiationService.createInstance(CodeApplication, mainIpcServer, instanceEnvironment).startup();
这句显然是建立了CodeApplication的实例,而后执行了实例的startup方法
注意:建立这个实例的时候,把咱们前面建立的mainIpcServer传递进去了;
CodeApplication(src\vs\code\electron-main\app.ts)的startup方法,还启动了Electron的IPCServer缓存
const electronIpcServer = new ElectronIPCServer();
vscode把electron默认的通讯机制也接入到了本身的事件体系内,有消息过来,会触发事件;
具体先不细说,后面再讲.
接着就跳转到同类型里的openFirstWindow方法(是否是很熟悉,咱们在第一篇文章中讲到过这里)
在这里,给这两个服务(mainIpcServer和electronIpcServer ),建立了一堆信道:app
const launchService = accessor.get(ILaunchService); const launchChannel = new LaunchChannel(launchService); this.mainIpcServer.registerChannel('launch', launchChannel); const updateService = accessor.get(IUpdateService); const updateChannel = new UpdateChannel(updateService); electronIpcServer.registerChannel('update', updateChannel); const issueService = accessor.get(IIssueService); const issueChannel = new IssueChannel(issueService); electronIpcServer.registerChannel('issue', issueChannel); const workspacesService = accessor.get(IWorkspacesMainService); const workspacesChannel = new WorkspacesChannel(workspacesService); electronIpcServer.registerChannel('workspaces', workspacesChannel); const windowsService = accessor.get(IWindowsService); const windowsChannel = new WindowsChannel(windowsService); electronIpcServer.registerChannel('windows', windowsChannel); sharedProcessClient.then(client => client.registerChannel('windows', windowsChannel)); const menubarService = accessor.get(IMenubarService); const menubarChannel = new MenubarChannel(menubarService); electronIpcServer.registerChannel('menubar', menubarChannel); const urlService = accessor.get(IURLService); const urlChannel = new URLServiceChannel(urlService); electronIpcServer.registerChannel('url', urlChannel); const storageMainService = accessor.get(IStorageMainService); const storageChannel = this._register(new GlobalStorageDatabaseChannel(this.logService, storageMainService as StorageMainService)); electronIpcServer.registerChannel('storage', storageChannel); const logLevelChannel = new LogLevelSetterChannel(accessor.get(ILogService)); electronIpcServer.registerChannel('loglevel', logLevelChannel);
有存储、日志、菜单栏、工做台、升级.....等等
主要的通讯仍是用electronIpcServer 来干的,mainIpcServer只有一个launch信道;
下面咱们看看消息是怎么传递的
咱们随便打开一个信道的类型(src\vs\platform\windows\node\windowsIpc.ts)
它有两个主要的函数,listen和call,electron
listen(_: unknown, event: string): Event<any> { switch (event) { case 'onWindowOpen': return this.onWindowOpen; case 'onWindowFocus': return this.onWindowFocus; case 'onWindowBlur': return this.onWindowBlur; case 'onWindowMaximize': return this.onWindowMaximize; case 'onWindowUnmaximize': return this.onWindowUnmaximize; case 'onRecentlyOpenedChange': return this.onRecentlyOpenedChange; } throw new Error(`Event not found: ${event}`); }
call(_: unknown, command: string, arg?: any): Promise<any> { switch (command) { case 'pickFileFolderAndOpen': return this.service.pickFileFolderAndOpen(arg); case 'pickFileAndOpen': return this.service.pickFileAndOpen(arg); case 'pickFolderAndOpen': return this.service.pickFolderAndOpen(arg); case 'pickWorkspaceAndOpen': return this.service.pickWorkspaceAndOpen(arg); case 'showMessageBox': return this.service.showMessageBox(arg[0], arg[1]); case 'showSaveDialog': return this.service.showSaveDialog(arg[0], arg[1]); case 'showOpenDialog': return this.service.showOpenDialog(arg[0], arg[1]); //......
消息来了,进入listen函数,发送消息,进入call函数;
注意,消息来了,触发的也不是他本身的方法,咱们看看它的构造函数:函数
constructor(private service: IWindowsService) { this.onWindowOpen = Event.buffer(service.onWindowOpen, true); this.onWindowFocus = Event.buffer(service.onWindowFocus, true); this.onWindowBlur = Event.buffer(service.onWindowBlur, true); this.onWindowMaximize = Event.buffer(service.onWindowMaximize, true); this.onWindowUnmaximize = Event.buffer(service.onWindowUnmaximize, true); this.onRecentlyOpenedChange = Event.buffer(service.onRecentlyOpenedChange, true); }
看到没,触发的实际上是一个事件,事件是关联到service实例的;
这个实例是这样建立的:源码分析
const windowsService = accessor.get(IWindowsService);
具体的代码在:src\vs\platform\windows\electron-browser\windowsService.ts
post