1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Remove listener before starts a new one

Signed-off-by: Hung-Han (Henry) Chen <chenhungh@gmail.com>
This commit is contained in:
Hung-Han (Henry) Chen 2021-11-23 13:30:26 +02:00
parent ecc87933b2
commit 068a92365e
No known key found for this signature in database
GPG Key ID: 54B44603D251B788

View File

@ -545,28 +545,7 @@ export class KubeApi<T extends KubeObject> {
return retry(() => dns.lookup(url), retryOptions); return retry(() => dns.lookup(url), retryOptions);
} }
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void { private suspendListener(opts: KubeApiWatchOptions, abort: AbortController["abort"], watchId: string, watchUrl: string) {
let errorReceived = false;
let timedRetry: NodeJS.Timeout;
const { abortController: { abort, signal } = new AbortController(), namespace, callback = noop, retry, timeout } = opts;
const { watchId = `${this.kind.toLowerCase()}-${this.watchId++}` } = opts;
signal.addEventListener("abort", () => {
logger.info(`[KUBE-API] watch (${watchId}) aborted ${watchUrl}`);
clearTimeout(timedRetry);
});
const requestParams = timeout ? { query: { timeoutSeconds: timeout }}: {};
const watchUrl = this.getWatchUrl(namespace);
const responsePromise = this.request.getResponse(watchUrl, requestParams, { signal, timeout: 600_000 });
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
if (!this.watchingSystemStatus) {
this.watchSystemStatus();
}
electron.powerMonitor.once("suspend", () => {
logger.info(`[KUBE-API] system suspended, abort watching of ${watchUrl}...`); logger.info(`[KUBE-API] system suspended, abort watching of ${watchUrl}...`);
try { try {
@ -591,13 +570,39 @@ export class KubeApi<T extends KubeObject> {
} }
logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`); logger.info(`[KUBE-API] system resumed, resume watching of ${watchUrl}...`);
this.watch({ ...opts, abortController, namespace, callback, watchId, retry: true }); this.watch({ ...opts, abortController });
}).catch((error) => { }).catch((error) => {
logger.warn(`[KUBE-API] error resolving domain name ${url}`, error); logger.warn(`[KUBE-API] error resolving domain name ${url}`, error);
}); });
}); });
}
watch(opts: KubeApiWatchOptions = { namespace: "", retry: false }): () => void {
let errorReceived = false;
let timedRetry: NodeJS.Timeout;
const { abortController: { abort, signal } = new AbortController(), namespace, callback = noop, retry, timeout } = opts;
const { watchId = `${this.kind.toLowerCase()}-${this.watchId++}` } = opts;
signal.addEventListener("abort", () => {
logger.info(`[KUBE-API] watch (${watchId}) aborted ${watchUrl}`);
clearTimeout(timedRetry);
}); });
const requestParams = timeout ? { query: { timeoutSeconds: timeout }}: {};
const watchUrl = this.getWatchUrl(namespace);
const responsePromise = this.request.getResponse(watchUrl, requestParams, { signal, timeout: 600_000 });
logger.info(`[KUBE-API] watch (${watchId}) ${retry === true ? "retried" : "started"} ${watchUrl}`);
if (!this.watchingSystemStatus) {
this.watchSystemStatus();
}
electron.powerMonitor.removeListener("suspend", this.suspendListener);
electron.powerMonitor.addListener("suspend", this.suspendListener.bind(
{ ...opts, namespace, callback, watchId, retry: true }, abort, watchId, watchUrl),
);
responsePromise responsePromise
.then(response => { .then(response => {
if (!response.ok) { if (!response.ok) {