mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
Previously devtools used a nested workspace for its bazel configurations. This meant framework dependencies were consumed via npm. Now devtools is part of the root bazel directory that all other files in this codebase fall under. This allows us to build devtools using local angular packages, removing the need to consume these dependencies with npn. This is useful because we no longer have to update these dependencies with an automated tool like renovate, and our CI tests will always run against the most up to date framework packages.
84 lines
2.3 KiB
TypeScript
84 lines
2.3 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright Google LLC All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
|
|
/// <reference types="chrome"/>
|
|
|
|
import {Events, MessageBus, Parameters} from 'protocol';
|
|
|
|
interface ChromeMessage<T, K extends keyof T> {
|
|
topic: K;
|
|
args: Parameters<T[K]>;
|
|
}
|
|
|
|
type AnyEventCallback<Ev> = <E extends keyof Ev>(topic: E, args: Parameters<Ev[E]>) => void;
|
|
|
|
export class ChromeMessageBus extends MessageBus<Events> {
|
|
private _disconnected = false;
|
|
private _listeners: any[] = [];
|
|
|
|
constructor(private _port: chrome.runtime.Port) {
|
|
super();
|
|
|
|
_port.onDisconnect.addListener(() => {
|
|
// console.log('Disconnected the port');
|
|
this._disconnected = true;
|
|
});
|
|
}
|
|
|
|
onAny(cb: AnyEventCallback<Events>): () => void {
|
|
const listener = (msg: ChromeMessage<Events, keyof Events>): void => {
|
|
cb(msg.topic, msg.args);
|
|
};
|
|
this._port.onMessage.addListener(listener);
|
|
this._listeners.push(listener);
|
|
return () => {
|
|
this._listeners.splice(this._listeners.indexOf(listener), 1);
|
|
this._port.onMessage.removeListener(listener);
|
|
};
|
|
}
|
|
|
|
on<E extends keyof Events>(topic: E, cb: Events[E]): () => void {
|
|
const listener = (msg: ChromeMessage<Events, keyof Events>): void => {
|
|
if (msg.topic === topic) {
|
|
cb.apply(null, msg.args);
|
|
}
|
|
};
|
|
this._port.onMessage.addListener(listener);
|
|
this._listeners.push(listener);
|
|
return () => {
|
|
this._listeners.splice(this._listeners.indexOf(listener), 1);
|
|
this._port.onMessage.removeListener(listener);
|
|
};
|
|
}
|
|
|
|
once<E extends keyof Events>(topic: E, cb: Events[E]): void {
|
|
const listener = (msg: ChromeMessage<Events, keyof Events>) => {
|
|
if (msg.topic === topic) {
|
|
cb.apply(null, msg.args);
|
|
this._port.onMessage.removeListener(listener);
|
|
}
|
|
};
|
|
this._port.onMessage.addListener(listener);
|
|
}
|
|
|
|
emit<E extends keyof Events>(topic: E, args?: Parameters<Events[E]>): boolean {
|
|
if (this._disconnected) {
|
|
return false;
|
|
}
|
|
this._port.postMessage({
|
|
topic,
|
|
args,
|
|
});
|
|
return true;
|
|
}
|
|
|
|
destroy(): void {
|
|
this._listeners.forEach((l) => window.removeEventListener('message', l));
|
|
this._listeners = [];
|
|
}
|
|
}
|