Handle disconnects (#363)
* Make proxies decide how to handle disconnects * Connect to a new terminal instance on disconnect * Use our retry for the watcher * Specify method when proxy doesn't exist * Don't error when closing/killing disconnected proxy * Specify proxy ID when a method doesn't exist * Use our retry for the searcher Also dispose some things for the watcher because it doesn't seem that was done properly. The searcher also now starts immediately so there won't be lag when you perform your first search. * Use our retry for the extension host * Emit error in parent proxy class Reduces duplicate code. Not all items are "supposed" to have an error event according to the original implementation we are filling, but there is no reason why we can't emit our own events (and are already doing so for the "disconnected" event anyway). * Reconnect spdlog * Add error message when shared process disconnects * Pass method resolve to parse * Don't pass method to getProxy It doesn't tell you anything that trace logging wouldn't and has no relation to what the function actually does. * Fix infinite recursion when disposing protocol client in tests
This commit is contained in:
@@ -29,21 +29,48 @@ const unpromisify = <T extends ServerProxy>(proxyPromise: Promise<T>): T => {
|
||||
* need a bunch of `then` calls everywhere.
|
||||
*/
|
||||
export abstract class ClientProxy<T extends ServerProxy> extends EventEmitter {
|
||||
protected readonly proxy: T;
|
||||
private _proxy: T | undefined;
|
||||
|
||||
/**
|
||||
* You can specify not to bind events in order to avoid emitting twice for
|
||||
* duplex streams.
|
||||
*/
|
||||
public constructor(proxyPromise: Promise<T> | T, bindEvents: boolean = true) {
|
||||
public constructor(
|
||||
proxyPromise: Promise<T> | T,
|
||||
private readonly bindEvents: boolean = true,
|
||||
) {
|
||||
super();
|
||||
this.proxy = isPromise(proxyPromise) ? unpromisify(proxyPromise) : proxyPromise;
|
||||
if (bindEvents) {
|
||||
this.initialize(proxyPromise);
|
||||
if (this.bindEvents) {
|
||||
this.on("disconnected", (error) => {
|
||||
try {
|
||||
this.emit("error", error);
|
||||
} catch (error) {
|
||||
// If nothing is listening, EventEmitter will throw an error.
|
||||
}
|
||||
this.handleDisconnect();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected get proxy(): T {
|
||||
if (!this._proxy) {
|
||||
throw new Error("not initialized");
|
||||
}
|
||||
|
||||
return this._proxy;
|
||||
}
|
||||
|
||||
protected initialize(proxyPromise: Promise<T> | T): void {
|
||||
this._proxy = isPromise(proxyPromise) ? unpromisify(proxyPromise) : proxyPromise;
|
||||
if (this.bindEvents) {
|
||||
this.proxy.onEvent((event, ...args): void => {
|
||||
this.emit(event, ...args);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract handleDisconnect(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user