Dispose of attach addon event listeners
This commit is contained in:
@@ -5,9 +5,13 @@
|
|||||||
* Implements the attach method, that attaches the terminal to a WebSocket stream.
|
* Implements the attach method, that attaches the terminal to a WebSocket stream.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Terminal } from 'xterm';
|
import { Terminal, IDisposable } from 'xterm';
|
||||||
|
|
||||||
export interface IAttachAddonTerminal extends Terminal {
|
export interface IAttachAddonTerminal extends Terminal {
|
||||||
|
_core: {
|
||||||
|
register<T extends IDisposable>(d: T): void;
|
||||||
|
};
|
||||||
|
|
||||||
__socket?: WebSocket;
|
__socket?: WebSocket;
|
||||||
__attachSocketBuffer?: string;
|
__attachSocketBuffer?: string;
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
* Implements the attach method, that attaches the terminal to a WebSocket stream.
|
* Implements the attach method, that attaches the terminal to a WebSocket stream.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Terminal } from 'xterm';
|
import { Terminal, IDisposable } from 'xterm';
|
||||||
import { IAttachAddonTerminal } from './Interfaces';
|
import { IAttachAddonTerminal } from './Interfaces';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,14 +87,28 @@ export function attach(term: Terminal, socket: WebSocket, bidirectional: boolean
|
|||||||
socket.send(data);
|
socket.send(data);
|
||||||
};
|
};
|
||||||
|
|
||||||
socket.addEventListener('message', addonTerminal.__getMessage);
|
addonTerminal._core.register(addSocketListener(socket, 'message', addonTerminal.__getMessage));
|
||||||
|
|
||||||
if (bidirectional) {
|
if (bidirectional) {
|
||||||
addonTerminal.on('data', addonTerminal.__sendData);
|
addonTerminal.on('data', addonTerminal.__sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.addEventListener('close', () => detach(addonTerminal, socket));
|
addonTerminal._core.register(addSocketListener(socket, 'close', () => detach(addonTerminal, socket)));
|
||||||
socket.addEventListener('error', () => detach(addonTerminal, socket));
|
addonTerminal._core.register(addSocketListener(socket, 'error', () => detach(addonTerminal, socket)));
|
||||||
|
}
|
||||||
|
|
||||||
|
function addSocketListener(socket: WebSocket, type: string, handler: (this: WebSocket, ev: Event) => any): IDisposable {
|
||||||
|
socket.addEventListener(type, handler);
|
||||||
|
return {
|
||||||
|
dispose: () => {
|
||||||
|
if (!handler) {
|
||||||
|
// Already disposed
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
socket.removeEventListener(type, handler);
|
||||||
|
handler = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user