提交 0cbf1c75 编写于 作者: J Joao Moreno

use URI for UrlService

上级 bd810891
......@@ -310,21 +310,25 @@ export function getNextTickChannel<T extends IChannel>(channel: T): T {
return { call } as T;
}
export function eventToCall(event: Event<any>): TPromise<any> {
export type Serializer<T,R> = (obj: T) => R;
export type Deserializer<T,R> = (raw: R) => T;
export function eventToCall<T>(event: Event<T>, serializer: Serializer<T,any> = t => t): TPromise<void> {
let disposable: IDisposable;
return new Promise(
(c, e, p) => disposable = event(p),
(c, e, p) => disposable = event(t => p(serializer(t))),
() => disposable.dispose()
);
}
export function eventFromCall<T>(channel: IChannel, name: string, arg: any = null): Event<T> {
export function eventFromCall<T>(channel: IChannel, name: string, arg: any = null, deserializer: Deserializer<T,any> = t => t): Event<T> {
let promise: Promise;
const emitter = new Emitter<any>({
onFirstListenerAdd: () => {
promise = channel.call(name, arg).then(null, err => null, e => emitter.fire(e));
promise = channel.call(name, arg)
.then(null, err => null, e => emitter.fire(deserializer(e)));
},
onLastListenerRemove: () => {
promise.cancel();
......
......@@ -6,6 +6,7 @@
'use strict';
import Event from 'vs/base/common/event';
import URI from 'vs/base/common/uri';
import {createDecorator} from 'vs/platform/instantiation/common/instantiation';
export const ID = 'urlService';
......@@ -13,5 +14,5 @@ export const IURLService = createDecorator<IURLService>(ID);
export interface IURLService {
_serviceBrand: any;
onOpenURL: Event<string>;
onOpenURL: Event<URI>;
}
......@@ -6,10 +6,14 @@
'use strict';
import { TPromise } from 'vs/base/common/winjs.base';
import { IChannel, eventToCall, eventFromCall } from 'vs/base/parts/ipc/common/ipc';
import { IChannel, eventToCall, eventFromCall, Serializer, Deserializer } from 'vs/base/parts/ipc/common/ipc';
import { IURLService } from './url';
import Event, { filterEvent } from 'vs/base/common/event';
import { IWindowsService } from 'vs/code/electron-main/windows';
import URI from 'vs/base/common/uri';
const URISerializer: Serializer<URI,any> = uri => uri.toJSON();
const URIDeserializer: Deserializer<URI,any> = raw => URI.revive(raw);
export interface IURLChannel extends IChannel {
call(command: 'event:onOpenURL'): TPromise<void>;
......@@ -25,7 +29,7 @@ export class URLChannel implements IURLChannel {
call(command: string, arg: any): TPromise<any> {
switch (command) {
case 'event:onOpenURL': return eventToCall(filterEvent(this.service.onOpenURL, () => this.isWindowFocused(arg)));
case 'event:onOpenURL': return eventToCall(filterEvent(this.service.onOpenURL, () => this.isWindowFocused(arg)), URISerializer);
}
}
......@@ -49,6 +53,6 @@ export class URLChannelClient implements IURLService {
constructor(private channel: IChannel, private windowID: number) { }
private _onOpenURL = eventFromCall<string>(this.channel, 'event:onOpenURL', this.windowID);
get onOpenURL(): Event<string> { return this._onOpenURL; }
private _onOpenURL = eventFromCall<URI>(this.channel, 'event:onOpenURL', this.windowID, URIDeserializer);
get onOpenURL(): Event<URI> { return this._onOpenURL; }
}
\ No newline at end of file
......@@ -5,26 +5,34 @@
'use strict';
import Event, {mapEvent} from 'vs/base/common/event';
import Event, {mapEvent,filterEvent} from 'vs/base/common/event';
import {fromEventEmitter} from 'vs/base/node/event';
import {IURLService} from 'vs/platform/url/common/url';
import product from 'vs/platform/product';
import {app} from 'electron';
import URI from 'vs/base/common/uri';
export class URLService implements IURLService {
_serviceBrand: any;
onOpenURL: Event<string>;
onOpenURL: Event<URI>;
constructor() {
const rawOnOpenUrl = fromEventEmitter(app, 'open-url', (event: Electron.Event, url: string) => ({ event, url }));
this.onOpenURL = mapEvent(rawOnOpenUrl, ({ event, url }) => {
const uriEvent = mapEvent(rawOnOpenUrl, ({ event, url }) => {
event.preventDefault();
return url;
try {
return URI.parse(url);
} catch(e) {
return null;
}
});
this.onOpenURL = filterEvent(uriEvent, uri => !!uri);
app.setAsDefaultProtocolClient(product.urlProtocol);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册