|
| 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 * as vscode from 'vscode'; |
| 7 | +import { UriComponents, URI } from 'vs/base/common/uri'; |
| 8 | +import { createDecorator } from 'vs/platform/instantiation/common/instantiation'; |
| 9 | +import { ExtHostTimelineShape, MainThreadTimelineShape, IMainContext, MainContext } from 'vs/workbench/api/common/extHost.protocol'; |
| 10 | +import { TimelineItem, TimelineProvider, toKey } from 'vs/workbench/contrib/timeline/common/timeline'; |
| 11 | +import { IDisposable, toDisposable } from 'vs/base/common/lifecycle'; |
| 12 | +import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions'; |
| 13 | + |
| 14 | +export interface IExtHostTimeline extends ExtHostTimelineShape { |
| 15 | + readonly _serviceBrand: undefined; |
| 16 | + $getTimeline(key: string, uri: UriComponents, since: number, token: vscode.CancellationToken): Promise<TimelineItem[]>; |
| 17 | +} |
| 18 | + |
| 19 | +export const IExtHostTimeline = createDecorator<IExtHostTimeline>('IExtHostTimeline'); |
| 20 | + |
| 21 | +export class ExtHostTimeline implements IExtHostTimeline { |
| 22 | + _serviceBrand: undefined; |
| 23 | + |
| 24 | + private _proxy: MainThreadTimelineShape; |
| 25 | + |
| 26 | + private _providers = new Map<string, TimelineProvider>(); |
| 27 | + |
| 28 | + constructor( |
| 29 | + mainContext: IMainContext, |
| 30 | + |
| 31 | + ) { |
| 32 | + this._proxy = mainContext.getProxy(MainContext.MainThreadTimeline); |
| 33 | + |
| 34 | + this.registerTimelineProvider('bar', { |
| 35 | + id: 'baz', |
| 36 | + async provideTimeline(uri: URI, since: number, token: vscode.CancellationToken) { |
| 37 | + return [ |
| 38 | + { |
| 39 | + id: '1', |
| 40 | + label: 'Bar Timeline1', |
| 41 | + description: uri.toString(true), |
| 42 | + detail: new Date().toString(), |
| 43 | + date: Date.now(), |
| 44 | + source: 'log' |
| 45 | + }, |
| 46 | + { |
| 47 | + id: '2', |
| 48 | + label: 'Bar Timeline2', |
| 49 | + description: uri.toString(true), |
| 50 | + detail: new Date(Date.now() - 100).toString(), |
| 51 | + date: Date.now() - 100, |
| 52 | + source: 'log' |
| 53 | + } |
| 54 | + ]; |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + async $getTimeline(key: string, uri: UriComponents, since: number, token: vscode.CancellationToken): Promise<TimelineItem[]> { |
| 60 | + const provider = this._providers.get(key); |
| 61 | + return provider?.provideTimeline(URI.revive(uri), since, token) ?? []; |
| 62 | + } |
| 63 | + |
| 64 | + registerTimelineProvider(extension: ExtensionIdentifier | string, provider: TimelineProvider): IDisposable { |
| 65 | + console.log(`ExtHostTimeline#registerTimelineProvider: extension=${extension.toString()}, provider=${provider.id}`); |
| 66 | + |
| 67 | + const key = toKey(extension, provider.id); |
| 68 | + if (this._providers.has(key)) { |
| 69 | + throw new Error(`Timeline Provider ${key} already exists.`); |
| 70 | + } |
| 71 | + |
| 72 | + this._proxy.$registerTimelineProvider(key, provider.id); |
| 73 | + this._providers.set(key, provider); |
| 74 | + |
| 75 | + return toDisposable(() => { |
| 76 | + this._providers.delete(key); |
| 77 | + this._proxy.$unregisterTimelineProvider(key); |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
0 commit comments