UNPKG

@sussudio/platform

Version:

Internal APIs for VS Code's service injection the base services.

42 lines (41 loc) 1.81 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ import { createCancelablePromise } from '@sussudio/base/common/async.mjs'; import { Event } from '@sussudio/base/common/event.mjs'; import { Disposable, DisposableStore } from '@sussudio/base/common/lifecycle.mjs'; export class ActiveWindowManager extends Disposable { disposables = this._register(new DisposableStore()); firstActiveWindowIdPromise; activeWindowId; constructor({ onDidOpenWindow, onDidFocusWindow, getActiveWindowId }) { super(); // remember last active window id upon events const onActiveWindowChange = Event.latch(Event.any(onDidOpenWindow, onDidFocusWindow)); onActiveWindowChange(this.setActiveWindow, this, this.disposables); // resolve current active window this.firstActiveWindowIdPromise = createCancelablePromise(() => getActiveWindowId()); (async () => { try { const windowId = await this.firstActiveWindowIdPromise; this.activeWindowId = typeof this.activeWindowId === 'number' ? this.activeWindowId : windowId; } catch (error) { // ignore } finally { this.firstActiveWindowIdPromise = undefined; } })(); } setActiveWindow(windowId) { if (this.firstActiveWindowIdPromise) { this.firstActiveWindowIdPromise.cancel(); this.firstActiveWindowIdPromise = undefined; } this.activeWindowId = windowId; } async getActiveClientId() { const id = this.firstActiveWindowIdPromise ? await this.firstActiveWindowIdPromise : this.activeWindowId; return `window:${id}`; } }