broadcastService.ts 1.5 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

J
Johannes Rieken 已提交
8 9
import { createDecorator } from 'vs/platform/instantiation/common/instantiation';
import Event, { Emitter } from 'vs/base/common/event';
E
Erich Gamma 已提交
10

11
import { ipcRenderer as ipc } from 'electron';
E
Erich Gamma 已提交
12

13
export const IBroadcastService = createDecorator<IBroadcastService>('broadcastService');
E
Erich Gamma 已提交
14 15 16 17 18 19

export interface IBroadcast {
	channel: string;
	payload: any;
}

20
export interface IBroadcastService {
21
	_serviceBrand: any;
E
Erich Gamma 已提交
22

23
	broadcast(b: IBroadcast, target?: string): void;
E
Erich Gamma 已提交
24

25
	onBroadcast: Event<IBroadcast>;
E
Erich Gamma 已提交
26 27
}

28
export class BroadcastService implements IBroadcastService {
29
	public _serviceBrand: any;
E
Erich Gamma 已提交
30

31
	private _onBroadcast: Emitter<IBroadcast>;
E
Erich Gamma 已提交
32

33
	constructor(private windowId: number) {
34
		this._onBroadcast = new Emitter<IBroadcast>();
E
Erich Gamma 已提交
35 36 37 38 39

		this.registerListeners();
	}

	private registerListeners(): void {
40
		ipc.on('vscode:broadcast', (event, b: IBroadcast) => {
E
Erich Gamma 已提交
41 42 43 44
			this._onBroadcast.fire(b);
		});
	}

45 46
	public get onBroadcast(): Event<IBroadcast> {
		return this._onBroadcast.event;
E
Erich Gamma 已提交
47 48
	}

49
	public broadcast(b: IBroadcast, target?: string): void {
50
		ipc.send('vscode:broadcast', this.windowId, target, {
E
Erich Gamma 已提交
51 52 53 54 55
			channel: b.channel,
			payload: b.payload
		});
	}
}