forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainThreadNotebook.ts
More file actions
156 lines (135 loc) · 6.7 KB
/
mainThreadNotebook.ts
File metadata and controls
156 lines (135 loc) · 6.7 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { VSBuffer } from 'vs/base/common/buffer';
import { CancellationToken } from 'vs/base/common/cancellation';
import { Emitter } from 'vs/base/common/event';
import { DisposableStore, dispose, IDisposable } from 'vs/base/common/lifecycle';
import { StopWatch } from 'vs/base/common/stopwatch';
import { assertType } from 'vs/base/common/types';
import { URI } from 'vs/base/common/uri';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { ILogService } from 'vs/platform/log/common/log';
import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
import { INotebookCellStatusBarService } from 'vs/workbench/contrib/notebook/common/notebookCellStatusBarService';
import { INotebookCellStatusBarItemProvider, INotebookContributionData, NotebookData, NotebookExtensionDescription, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { INotebookService, SimpleNotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookService';
import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
import { SerializableObjectWithBuffers } from 'vs/workbench/services/extensions/common/proxyIdentifier';
import { ExtHostContext, ExtHostNotebookShape, MainContext, MainThreadNotebookShape } from '../common/extHost.protocol';
@extHostNamedCustomer(MainContext.MainThreadNotebook)
export class MainThreadNotebooks implements MainThreadNotebookShape {
private readonly _disposables = new DisposableStore();
private readonly _proxy: ExtHostNotebookShape;
private readonly _notebookSerializer = new Map<number, IDisposable>();
private readonly _notebookCellStatusBarRegistrations = new Map<number, IDisposable>();
constructor(
extHostContext: IExtHostContext,
@INotebookService private readonly _notebookService: INotebookService,
@INotebookCellStatusBarService private readonly _cellStatusBarService: INotebookCellStatusBarService,
@ILogService private readonly _logService: ILogService,
) {
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostNotebook);
}
dispose(): void {
this._disposables.dispose();
dispose(this._notebookSerializer.values());
}
$registerNotebookSerializer(handle: number, extension: NotebookExtensionDescription, viewType: string, options: TransientOptions, data: INotebookContributionData | undefined): void {
const registration = this._notebookService.registerNotebookSerializer(viewType, extension, {
options,
dataToNotebook: async (data: VSBuffer): Promise<NotebookData> => {
const sw = new StopWatch(true);
const dto = await this._proxy.$dataToNotebook(handle, data, CancellationToken.None);
const result = NotebookDto.fromNotebookDataDto(dto.value);
this._logService.trace('[NotebookSerializer] dataToNotebook DONE', extension.id, sw.elapsed());
return result;
},
notebookToData: (data: NotebookData): Promise<VSBuffer> => {
const sw = new StopWatch(true);
const result = this._proxy.$notebookToData(handle, new SerializableObjectWithBuffers(NotebookDto.toNotebookDataDto(data)), CancellationToken.None);
this._logService.trace('[NotebookSerializer] notebookToData DONE', extension.id, sw.elapsed());
return result;
}
});
const disposables = new DisposableStore();
disposables.add(registration);
if (data) {
disposables.add(this._notebookService.registerContributedNotebookType(viewType, data));
}
this._notebookSerializer.set(handle, disposables);
}
$unregisterNotebookSerializer(handle: number): void {
this._notebookSerializer.get(handle)?.dispose();
this._notebookSerializer.delete(handle);
}
$emitCellStatusBarEvent(eventHandle: number): void {
const emitter = this._notebookCellStatusBarRegistrations.get(eventHandle);
if (emitter instanceof Emitter) {
emitter.fire(undefined);
}
}
async $registerNotebookCellStatusBarItemProvider(handle: number, eventHandle: number | undefined, viewType: string): Promise<void> {
const that = this;
const provider: INotebookCellStatusBarItemProvider = {
async provideCellStatusBarItems(uri: URI, index: number, token: CancellationToken) {
const result = await that._proxy.$provideNotebookCellStatusBarItems(handle, uri, index, token);
return {
items: result?.items ?? [],
dispose() {
if (result) {
that._proxy.$releaseNotebookCellStatusBarItems(result.cacheId);
}
}
};
},
viewType
};
if (typeof eventHandle === 'number') {
const emitter = new Emitter<void>();
this._notebookCellStatusBarRegistrations.set(eventHandle, emitter);
provider.onDidChangeStatusBarItems = emitter.event;
}
const disposable = this._cellStatusBarService.registerCellStatusBarItemProvider(provider);
this._notebookCellStatusBarRegistrations.set(handle, disposable);
}
async $unregisterNotebookCellStatusBarItemProvider(handle: number, eventHandle: number | undefined): Promise<void> {
const unregisterThing = (handle: number) => {
const entry = this._notebookCellStatusBarRegistrations.get(handle);
if (entry) {
this._notebookCellStatusBarRegistrations.get(handle)?.dispose();
this._notebookCellStatusBarRegistrations.delete(handle);
}
};
unregisterThing(handle);
if (typeof eventHandle === 'number') {
unregisterThing(eventHandle);
}
}
}
CommandsRegistry.registerCommand('_executeDataToNotebook', async (accessor, ...args) => {
const [notebookType, bytes] = args;
assertType(typeof notebookType === 'string', 'string');
assertType(bytes instanceof VSBuffer, 'VSBuffer');
const notebookService = accessor.get(INotebookService);
const info = await notebookService.withNotebookDataProvider(notebookType);
if (!(info instanceof SimpleNotebookProviderInfo)) {
return;
}
const dto = await info.serializer.dataToNotebook(bytes);
return new SerializableObjectWithBuffers(NotebookDto.toNotebookDataDto(dto));
});
CommandsRegistry.registerCommand('_executeNotebookToData', async (accessor, ...args) => {
const [notebookType, dto] = args;
assertType(typeof notebookType === 'string', 'string');
assertType(typeof dto === 'object');
const notebookService = accessor.get(INotebookService);
const info = await notebookService.withNotebookDataProvider(notebookType);
if (!(info instanceof SimpleNotebookProviderInfo)) {
return;
}
const data = NotebookDto.fromNotebookDataDto(dto.value);
const bytes = await info.serializer.notebookToData(data);
return bytes;
});