Skip to content

Commit 0cbf1c7

Browse files
committed
use URI for UrlService
1 parent bd81089 commit 0cbf1c7

4 files changed

Lines changed: 30 additions & 13 deletions

File tree

src/vs/base/parts/ipc/common/ipc.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,21 +310,25 @@ export function getNextTickChannel<T extends IChannel>(channel: T): T {
310310
return { call } as T;
311311
}
312312

313-
export function eventToCall(event: Event<any>): TPromise<any> {
313+
export type Serializer<T,R> = (obj: T) => R;
314+
export type Deserializer<T,R> = (raw: R) => T;
315+
316+
export function eventToCall<T>(event: Event<T>, serializer: Serializer<T,any> = t => t): TPromise<void> {
314317
let disposable: IDisposable;
315318

316319
return new Promise(
317-
(c, e, p) => disposable = event(p),
320+
(c, e, p) => disposable = event(t => p(serializer(t))),
318321
() => disposable.dispose()
319322
);
320323
}
321324

322-
export function eventFromCall<T>(channel: IChannel, name: string, arg: any = null): Event<T> {
325+
export function eventFromCall<T>(channel: IChannel, name: string, arg: any = null, deserializer: Deserializer<T,any> = t => t): Event<T> {
323326
let promise: Promise;
324327

325328
const emitter = new Emitter<any>({
326329
onFirstListenerAdd: () => {
327-
promise = channel.call(name, arg).then(null, err => null, e => emitter.fire(e));
330+
promise = channel.call(name, arg)
331+
.then(null, err => null, e => emitter.fire(deserializer(e)));
328332
},
329333
onLastListenerRemove: () => {
330334
promise.cancel();

src/vs/platform/url/common/url.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
'use strict';
77

88
import Event from 'vs/base/common/event';
9+
import URI from 'vs/base/common/uri';
910
import {createDecorator} from 'vs/platform/instantiation/common/instantiation';
1011

1112
export const ID = 'urlService';
1213
export const IURLService = createDecorator<IURLService>(ID);
1314

1415
export interface IURLService {
1516
_serviceBrand: any;
16-
onOpenURL: Event<string>;
17+
onOpenURL: Event<URI>;
1718
}

src/vs/platform/url/common/urlIpc.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
'use strict';
77

88
import { TPromise } from 'vs/base/common/winjs.base';
9-
import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc';
9+
import { IChannel, eventToCall, eventFromCall, Serializer, Deserializer } from 'vs/base/parts/ipc/common/ipc';
1010
import { IURLService } from './url';
1111
import Event, { filterEvent } from 'vs/base/common/event';
1212
import { IWindowsService } from 'vs/code/electron-main/windows';
13+
import URI from 'vs/base/common/uri';
14+
15+
const URISerializer: Serializer<URI,any> = uri => uri.toJSON();
16+
const URIDeserializer: Deserializer<URI,any> = raw => URI.revive(raw);
1317

1418
export interface IURLChannel extends IChannel {
1519
call(command: 'event:onOpenURL'): TPromise<void>;
@@ -25,7 +29,7 @@ export class URLChannel implements IURLChannel {
2529

2630
call(command: string, arg: any): TPromise<any> {
2731
switch (command) {
28-
case 'event:onOpenURL': return eventToCall(filterEvent(this.service.onOpenURL, () => this.isWindowFocused(arg)));
32+
case 'event:onOpenURL': return eventToCall(filterEvent(this.service.onOpenURL, () => this.isWindowFocused(arg)), URISerializer);
2933
}
3034
}
3135

@@ -49,6 +53,6 @@ export class URLChannelClient implements IURLService {
4953

5054
constructor(private channel: IChannel, private windowID: number) { }
5155

52-
private _onOpenURL = eventFromCall<string>(this.channel, 'event:onOpenURL', this.windowID);
53-
get onOpenURL(): Event<string> { return this._onOpenURL; }
56+
private _onOpenURL = eventFromCall<URI>(this.channel, 'event:onOpenURL', this.windowID, URIDeserializer);
57+
get onOpenURL(): Event<URI> { return this._onOpenURL; }
5458
}

src/vs/platform/url/electron-main/urlService.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,34 @@
55

66
'use strict';
77

8-
import Event, {mapEvent} from 'vs/base/common/event';
8+
import Event, {mapEvent,filterEvent} from 'vs/base/common/event';
99
import {fromEventEmitter} from 'vs/base/node/event';
1010
import {IURLService} from 'vs/platform/url/common/url';
1111
import product from 'vs/platform/product';
1212
import {app} from 'electron';
13+
import URI from 'vs/base/common/uri';
1314

1415
export class URLService implements IURLService {
1516

1617
_serviceBrand: any;
1718

18-
onOpenURL: Event<string>;
19+
onOpenURL: Event<URI>;
1920

2021
constructor() {
2122
const rawOnOpenUrl = fromEventEmitter(app, 'open-url', (event: Electron.Event, url: string) => ({ event, url }));
2223

23-
this.onOpenURL = mapEvent(rawOnOpenUrl, ({ event, url }) => {
24+
const uriEvent = mapEvent(rawOnOpenUrl, ({ event, url }) => {
2425
event.preventDefault();
25-
return url;
26+
27+
try {
28+
return URI.parse(url);
29+
} catch(e) {
30+
return null;
31+
}
2632
});
2733

34+
this.onOpenURL = filterEvent(uriEvent, uri => !!uri);
35+
2836
app.setAsDefaultProtocolClient(product.urlProtocol);
2937
}
3038
}

0 commit comments

Comments
 (0)