Path: blob/main/src/vs/workbench/api/common/extHostDataChannels.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import type * as vscode from 'vscode';6import { Emitter, Event } from '../../../base/common/event.js';7import { Disposable } from '../../../base/common/lifecycle.js';8import { ExtHostDataChannelsShape } from './extHost.protocol.js';9import { checkProposedApiEnabled } from '../../services/extensions/common/extensions.js';10import { IExtensionDescription } from '../../../platform/extensions/common/extensions.js';11import { createDecorator } from '../../../platform/instantiation/common/instantiation.js';1213export interface IExtHostDataChannels extends ExtHostDataChannelsShape {14readonly _serviceBrand: undefined;15createDataChannel<T>(extension: IExtensionDescription, channelId: string): vscode.DataChannel<T>;16}1718export const IExtHostDataChannels = createDecorator<IExtHostDataChannels>('IExtHostDataChannels');1920export class ExtHostDataChannels implements IExtHostDataChannels {21declare readonly _serviceBrand: undefined;2223private readonly _channels = new Map<string, DataChannelImpl<any>>();2425constructor() {26}2728createDataChannel<T>(extension: IExtensionDescription, channelId: string): vscode.DataChannel<T> {29checkProposedApiEnabled(extension, 'dataChannels');3031let channel = this._channels.get(channelId);32if (!channel) {33channel = new DataChannelImpl<T>(channelId);34this._channels.set(channelId, channel);35}36return channel;37}3839$onDidReceiveData(channelId: string, data: any): void {40const channel = this._channels.get(channelId);41if (channel) {42channel._fireDidReceiveData(data);43}44}45}4647class DataChannelImpl<T> extends Disposable implements vscode.DataChannel<T> {48private readonly _onDidReceiveData = new Emitter<vscode.DataChannelEvent<T>>();49public readonly onDidReceiveData: Event<vscode.DataChannelEvent<T>> = this._onDidReceiveData.event;5051constructor(private readonly channelId: string) {52super();53this._register(this._onDidReceiveData);54}5556_fireDidReceiveData(data: T): void {57this._onDidReceiveData.fire({ data });58}5960override toString(): string {61return `DataChannel(${this.channelId})`;62}63}646566