Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/api/common/extHostDataChannels.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import type * as vscode from 'vscode';
7
import { Emitter, Event } from '../../../base/common/event.js';
8
import { Disposable } from '../../../base/common/lifecycle.js';
9
import { ExtHostDataChannelsShape } from './extHost.protocol.js';
10
import { checkProposedApiEnabled } from '../../services/extensions/common/extensions.js';
11
import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';
12
import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';
13
14
export interface IExtHostDataChannels extends ExtHostDataChannelsShape {
15
readonly _serviceBrand: undefined;
16
createDataChannel<T>(extension: IExtensionDescription, channelId: string): vscode.DataChannel<T>;
17
}
18
19
export const IExtHostDataChannels = createDecorator<IExtHostDataChannels>('IExtHostDataChannels');
20
21
export class ExtHostDataChannels implements IExtHostDataChannels {
22
declare readonly _serviceBrand: undefined;
23
24
private readonly _channels = new Map<string, DataChannelImpl<any>>();
25
26
constructor() {
27
}
28
29
createDataChannel<T>(extension: IExtensionDescription, channelId: string): vscode.DataChannel<T> {
30
checkProposedApiEnabled(extension, 'dataChannels');
31
32
let channel = this._channels.get(channelId);
33
if (!channel) {
34
channel = new DataChannelImpl<T>(channelId);
35
this._channels.set(channelId, channel);
36
}
37
return channel;
38
}
39
40
$onDidReceiveData(channelId: string, data: any): void {
41
const channel = this._channels.get(channelId);
42
if (channel) {
43
channel._fireDidReceiveData(data);
44
}
45
}
46
}
47
48
class DataChannelImpl<T> extends Disposable implements vscode.DataChannel<T> {
49
private readonly _onDidReceiveData = new Emitter<vscode.DataChannelEvent<T>>();
50
public readonly onDidReceiveData: Event<vscode.DataChannelEvent<T>> = this._onDidReceiveData.event;
51
52
constructor(private readonly channelId: string) {
53
super();
54
this._register(this._onDidReceiveData);
55
}
56
57
_fireDidReceiveData(data: T): void {
58
this._onDidReceiveData.fire({ data });
59
}
60
61
override toString(): string {
62
return `DataChannel(${this.channelId})`;
63
}
64
}
65
66