mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-13 19:48:37 -05:00
Merge VS Code 1.23.1 (#1520)
This commit is contained in:
70
src/vs/platform/url/electron-main/electronUrlListener.ts
Normal file
70
src/vs/platform/url/electron-main/electronUrlListener.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { mapEvent, fromNodeEventEmitter, filterEvent, once } from 'vs/base/common/event';
|
||||
import { IURLService } from 'vs/platform/url/common/url';
|
||||
import product from 'vs/platform/node/product';
|
||||
import { app } from 'electron';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
|
||||
import { IWindowsMainService } from 'vs/platform/windows/electron-main/windows';
|
||||
import { ReadyState } from 'vs/platform/windows/common/windows';
|
||||
|
||||
function uriFromRawUrl(url: string): URI | null {
|
||||
try {
|
||||
return URI.parse(url);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export class ElectronURLListener {
|
||||
|
||||
private disposables: IDisposable[] = [];
|
||||
|
||||
constructor(
|
||||
initial: string | string[],
|
||||
@IURLService private urlService: IURLService,
|
||||
@IWindowsMainService windowsService: IWindowsMainService
|
||||
) {
|
||||
const globalBuffer = ((<any>global).getOpenUrls() || []) as string[];
|
||||
const rawBuffer = [
|
||||
...(typeof initial === 'string' ? [initial] : initial),
|
||||
...globalBuffer
|
||||
];
|
||||
|
||||
const buffer = rawBuffer.map(uriFromRawUrl).filter(uri => !!uri);
|
||||
const flush = () => buffer.forEach(uri => urlService.open(uri));
|
||||
|
||||
app.setAsDefaultProtocolClient(product.urlProtocol, process.execPath, ['--open-url', '--']);
|
||||
|
||||
const onOpenElectronUrl = mapEvent(
|
||||
fromNodeEventEmitter(app, 'open-url', (event: Electron.Event, url: string) => ({ event, url })),
|
||||
({ event, url }) => {
|
||||
// always prevent default and return the url as string
|
||||
event.preventDefault();
|
||||
return url;
|
||||
});
|
||||
|
||||
const onOpenUrl = filterEvent(mapEvent(onOpenElectronUrl, uriFromRawUrl), uri => !!uri);
|
||||
onOpenUrl(this.urlService.open, this.urlService, this.disposables);
|
||||
|
||||
const isWindowReady = windowsService.getWindows()
|
||||
.filter(w => w.readyState === ReadyState.READY)
|
||||
.length > 0;
|
||||
|
||||
if (isWindowReady) {
|
||||
flush();
|
||||
} else {
|
||||
once(windowsService.onWindowReady)(flush);
|
||||
}
|
||||
}
|
||||
|
||||
dispose(): void {
|
||||
this.disposables = dispose(this.disposables);
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the Source EULA. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import Event, { mapEvent, chain, echo, Emitter, anyEvent, fromNodeEventEmitter } from 'vs/base/common/event';
|
||||
import { IURLService } from 'vs/platform/url/common/url';
|
||||
import product from 'vs/platform/node/product';
|
||||
import { app } from 'electron';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { ILogService } from '../../log/common/log';
|
||||
|
||||
export class URLService implements IURLService {
|
||||
|
||||
_serviceBrand: any;
|
||||
|
||||
private openUrlEmitter: Emitter<string> = new Emitter<string>();
|
||||
onOpenURL: Event<URI>;
|
||||
|
||||
constructor(
|
||||
initial: string | string[],
|
||||
@ILogService private logService: ILogService
|
||||
) {
|
||||
const globalBuffer = (global.getOpenUrls() || []) as string[];
|
||||
const initialBuffer = [
|
||||
...(typeof initial === 'string' ? [initial] : initial),
|
||||
...globalBuffer
|
||||
];
|
||||
|
||||
app.setAsDefaultProtocolClient(product.urlProtocol, process.execPath, ['--open-url', '--']);
|
||||
|
||||
const rawOnOpenUrl = fromNodeEventEmitter(app, 'open-url', (event: Electron.Event, url: string) => ({ event, url }));
|
||||
|
||||
// always prevent default and return the url as string
|
||||
const preventedOnOpenUrl = mapEvent(rawOnOpenUrl, ({ event, url }) => {
|
||||
event.preventDefault();
|
||||
return url;
|
||||
});
|
||||
|
||||
// echo all `onOpenUrl` events to each listener
|
||||
const bufferedOnOpenUrl = echo(preventedOnOpenUrl, true, initialBuffer);
|
||||
|
||||
this.onOpenURL = chain(anyEvent(bufferedOnOpenUrl, this.openUrlEmitter.event))
|
||||
.map(url => {
|
||||
try {
|
||||
return URI.parse(url);
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(uri => !!uri)
|
||||
.event;
|
||||
}
|
||||
|
||||
open(url: string): void {
|
||||
this.logService.trace('urlService#open', url);
|
||||
this.openUrlEmitter.fire(url);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user