mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix: mobx issue with accessing empty observable array by index (removes warning), use common logger
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
de4226a24a
commit
1a3a215118
@ -8,8 +8,9 @@ import { autobind, EventEmitter } from "../utils";
|
|||||||
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api";
|
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api";
|
||||||
import { ensureObjectSelfLink, KubeApi } from "./kube-api";
|
import { ensureObjectSelfLink, KubeApi } from "./kube-api";
|
||||||
import { getHostedCluster } from "../../common/cluster-store";
|
import { getHostedCluster } from "../../common/cluster-store";
|
||||||
import { apiPrefix, isDevelopment } from "../../common/vars";
|
import { apiPrefix, isProduction } from "../../common/vars";
|
||||||
import { apiManager } from "./api-manager";
|
import { apiManager } from "./api-manager";
|
||||||
|
import logger from "../../main/logger";
|
||||||
|
|
||||||
export { IKubeWatchEvent, IKubeWatchEventStreamEnd }
|
export { IKubeWatchEvent, IKubeWatchEventStreamEnd }
|
||||||
|
|
||||||
@ -20,6 +21,11 @@ export interface IKubeWatchMessage<T extends KubeObject = any> {
|
|||||||
store?: KubeObjectStore<T>;
|
store?: KubeObjectStore<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IKubeWatchLog {
|
||||||
|
message: string | Error;
|
||||||
|
meta?: object | any;
|
||||||
|
}
|
||||||
|
|
||||||
@autobind()
|
@autobind()
|
||||||
export class KubeWatchApi {
|
export class KubeWatchApi {
|
||||||
protected stream: ReadableStream<Uint8Array>; // https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
|
protected stream: ReadableStream<Uint8Array>; // https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
|
||||||
@ -91,8 +97,9 @@ export class KubeWatchApi {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.writeLog({
|
this.log({
|
||||||
data: ["CONNECTING", payload.apis]
|
message: "connecting",
|
||||||
|
meta: payload,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -125,8 +132,9 @@ export class KubeWatchApi {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.writeLog({
|
this.log({
|
||||||
error: ["CONNECTION ERROR", error]
|
message: new Error("connection error"),
|
||||||
|
meta: { error }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -152,8 +160,9 @@ export class KubeWatchApi {
|
|||||||
const message = this.getMessage(JSON.parse(kubeEvent));
|
const message = this.getMessage(JSON.parse(kubeEvent));
|
||||||
this.onMessage.emit(message);
|
this.onMessage.emit(message);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.writeLog({
|
this.log({
|
||||||
error: ["failed to parse watch-api event", { error, jsonText }]
|
message: new Error("failed to parse watch-api event"),
|
||||||
|
meta: { error, jsonText },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -206,8 +215,9 @@ export class KubeWatchApi {
|
|||||||
await api.refreshResourceVersion({ namespace });
|
await api.refreshResourceVersion({ namespace });
|
||||||
this.connect();
|
this.connect();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.writeLog({
|
this.log({
|
||||||
error: ["failed to reconnect after stream ending", { event, error }]
|
message: new Error("failed to reconnect on stream end"),
|
||||||
|
meta: { error, event },
|
||||||
});
|
});
|
||||||
|
|
||||||
if (this.subscribers.size > 0) {
|
if (this.subscribers.size > 0) {
|
||||||
@ -219,10 +229,16 @@ export class KubeWatchApi {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected writeLog({ data, error }: { data?: any[], error?: any[] } = {}) {
|
protected log({ message, meta }: IKubeWatchLog) {
|
||||||
if (isDevelopment) {
|
if (isProduction) return;
|
||||||
const logStyle = `font-weight: bold; ${error ? "color: red;" : ""}`;
|
|
||||||
console.log("%cKUBE-WATCH-API:", logStyle, ...Array.from(data || error));
|
const logMessage = `[KUBE-WATCH-API]: ${String(message).toUpperCase()}`;
|
||||||
|
const isError = message instanceof Error;
|
||||||
|
|
||||||
|
if (isError) {
|
||||||
|
logger.error(logMessage, meta);
|
||||||
|
} else {
|
||||||
|
logger.log({ message: logMessage, level: "info" });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -190,7 +190,7 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
}
|
}
|
||||||
|
|
||||||
// collect items from watch-api events to avoid UI blowing up with huge streams of data
|
// collect items from watch-api events to avoid UI blowing up with huge streams of data
|
||||||
protected eventsBuffer = observable<IKubeWatchEvent<KubeJsonApiData>>([], { deep: false });
|
protected eventsBuffer = observable.array<IKubeWatchEvent<KubeJsonApiData>>([], { deep: false });
|
||||||
|
|
||||||
protected bindWatchEventsUpdater(delay = 1000) {
|
protected bindWatchEventsUpdater(delay = 1000) {
|
||||||
kubeWatchApi.onMessage.addListener(({ store, data }: IKubeWatchMessage<T>) => {
|
kubeWatchApi.onMessage.addListener(({ store, data }: IKubeWatchMessage<T>) => {
|
||||||
@ -198,7 +198,7 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
this.eventsBuffer.push(data);
|
this.eventsBuffer.push(data);
|
||||||
})
|
})
|
||||||
|
|
||||||
reaction(() => this.eventsBuffer[0], this.updateFromEventsBuffer, {
|
reaction(() => this.eventsBuffer.length > 0, this.updateFromEventsBuffer, {
|
||||||
delay
|
delay
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -209,10 +209,6 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
|||||||
|
|
||||||
@action
|
@action
|
||||||
protected updateFromEventsBuffer() {
|
protected updateFromEventsBuffer() {
|
||||||
if (!this.eventsBuffer.length) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// create latest non-observable copy of items to apply updates in one action (==single render)
|
|
||||||
const items = this.items.toJS();
|
const items = this.items.toJS();
|
||||||
|
|
||||||
for (const { type, object } of this.eventsBuffer.clear()) {
|
for (const { type, object } of this.eventsBuffer.clear()) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user