Path: blob/main/src/vs/workbench/services/extensions/common/proxyIdentifier.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 { VSBuffer } from '../../../../base/common/buffer.js';6import type { CancellationToken } from '../../../../base/common/cancellation.js';78export interface IRPCProtocol {9/**10* Returns a proxy to an object addressable/named in the extension host process or in the renderer process.11*/12getProxy<T>(identifier: ProxyIdentifier<T>): Proxied<T>;1314/**15* Register manually created instance.16*/17set<T, R extends T>(identifier: ProxyIdentifier<T>, instance: R): R;1819/**20* Assert these identifiers are already registered via `.set`.21*/22assertRegistered(identifiers: ProxyIdentifier<any>[]): void;2324/**25* Wait for the write buffer (if applicable) to become empty.26*/27drain(): Promise<void>;2829dispose(): void;30}3132export class ProxyIdentifier<T> {33public static count = 0;34_proxyIdentifierBrand: void = undefined;3536public readonly sid: string;37public readonly nid: number;3839constructor(sid: string) {40this.sid = sid;41this.nid = (++ProxyIdentifier.count);42}43}4445const identifiers: ProxyIdentifier<any>[] = [];4647export function createProxyIdentifier<T>(identifier: string): ProxyIdentifier<T> {48const result = new ProxyIdentifier<T>(identifier);49identifiers[result.nid] = result;50return result;51}5253/**54* Mapped-type that replaces all JSONable-types with their toJSON-result type55*/56export type Dto<T> = T extends { toJSON(): infer U }57? U58: T extends VSBuffer // VSBuffer is understood by rpc-logic59? T60: T extends CancellationToken // CancellationToken is understood by rpc-logic61? T62: T extends Function // functions are dropped during JSON-stringify63? never64: T extends object // recurse65? { [k in keyof T]: Dto<T[k]>; }66: T;6768export type Proxied<T> = { [K in keyof T]: T[K] extends (...args: infer A) => infer R69? (...args: { [K in keyof A]: Dto<A[K]> }) => Promise<Dto<Awaited<R>>>70: never71};7273export function getStringIdentifierForProxy(nid: number): string {74return identifiers[nid].sid;75}7677/**78* Marks the object as containing buffers that should be serialized more efficiently.79*/80export class SerializableObjectWithBuffers<T> {81constructor(82public readonly value: T83) { }84}858687