/*--------------------------------------------------------------------------------------------- * 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 { TPromise } from 'vs/base/common/winjs.base'; import { IChannel } from 'vs/base/parts/ipc/common/ipc'; import { Event, Emitter } from 'vs/base/common/event'; import { onUnexpectedError } from 'vs/base/common/errors'; import { IUpdateService, State } from './update'; export interface IUpdateChannel extends IChannel { listen(event: 'onStateChange'): Event; listen(command: string, arg?: any): Event; call(command: 'checkForUpdates', arg: any): TPromise; call(command: 'downloadUpdate'): TPromise; call(command: 'applyUpdate'): TPromise; call(command: 'quitAndInstall'): TPromise; call(command: '_getInitialState'): TPromise; call(command: 'isLatestVersion'): TPromise; call(command: string, arg?: any): TPromise; } export class UpdateChannel implements IUpdateChannel { constructor(private service: IUpdateService) { } listen(event: string, arg?: any): Event { switch (event) { case 'onStateChange': return this.service.onStateChange; } throw new Error('No event found'); } call(command: string, arg?: any): TPromise { switch (command) { case 'checkForUpdates': return this.service.checkForUpdates(arg); case 'downloadUpdate': return this.service.downloadUpdate(); case 'applyUpdate': return this.service.applyUpdate(); case 'quitAndInstall': return this.service.quitAndInstall(); case '_getInitialState': return TPromise.as(this.service.state); case 'isLatestVersion': return this.service.isLatestVersion(); } return undefined; } } export class UpdateChannelClient implements IUpdateService { _serviceBrand: any; private _onStateChange = new Emitter(); get onStateChange(): Event { return this._onStateChange.event; } private _state: State = State.Uninitialized; get state(): State { return this._state; } constructor(private channel: IUpdateChannel) { // always set this._state as the state changes this.onStateChange(state => this._state = state); channel.call('_getInitialState').done(state => { // fire initial state this._onStateChange.fire(state); // fire subsequent states as they come in from remote this.channel.listen('onStateChange')(state => this._onStateChange.fire(state)); }, onUnexpectedError); } checkForUpdates(context: any): TPromise { return this.channel.call('checkForUpdates', context); } downloadUpdate(): TPromise { return this.channel.call('downloadUpdate'); } applyUpdate(): TPromise { return this.channel.call('applyUpdate'); } quitAndInstall(): TPromise { return this.channel.call('quitAndInstall'); } isLatestVersion(): TPromise { return this.channel.call('isLatestVersion'); } }