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 { ensureObjectSelfLink, KubeApi } from "./kube-api";
|
||||
import { getHostedCluster } from "../../common/cluster-store";
|
||||
import { apiPrefix, isDevelopment } from "../../common/vars";
|
||||
import { apiPrefix, isProduction } from "../../common/vars";
|
||||
import { apiManager } from "./api-manager";
|
||||
import logger from "../../main/logger";
|
||||
|
||||
export { IKubeWatchEvent, IKubeWatchEventStreamEnd }
|
||||
|
||||
@ -20,6 +21,11 @@ export interface IKubeWatchMessage<T extends KubeObject = any> {
|
||||
store?: KubeObjectStore<T>;
|
||||
}
|
||||
|
||||
export interface IKubeWatchLog {
|
||||
message: string | Error;
|
||||
meta?: object | any;
|
||||
}
|
||||
|
||||
@autobind()
|
||||
export class KubeWatchApi {
|
||||
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;
|
||||
}
|
||||
|
||||
this.writeLog({
|
||||
data: ["CONNECTING", payload.apis]
|
||||
this.log({
|
||||
message: "connecting",
|
||||
meta: payload,
|
||||
});
|
||||
|
||||
try {
|
||||
@ -125,8 +132,9 @@ export class KubeWatchApi {
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
this.writeLog({
|
||||
error: ["CONNECTION ERROR", error]
|
||||
this.log({
|
||||
message: new Error("connection error"),
|
||||
meta: { error }
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -152,8 +160,9 @@ export class KubeWatchApi {
|
||||
const message = this.getMessage(JSON.parse(kubeEvent));
|
||||
this.onMessage.emit(message);
|
||||
} catch (error) {
|
||||
this.writeLog({
|
||||
error: ["failed to parse watch-api event", { error, jsonText }]
|
||||
this.log({
|
||||
message: new Error("failed to parse watch-api event"),
|
||||
meta: { error, jsonText },
|
||||
});
|
||||
}
|
||||
})
|
||||
@ -206,8 +215,9 @@ export class KubeWatchApi {
|
||||
await api.refreshResourceVersion({ namespace });
|
||||
this.connect();
|
||||
} catch (error) {
|
||||
this.writeLog({
|
||||
error: ["failed to reconnect after stream ending", { event, error }]
|
||||
this.log({
|
||||
message: new Error("failed to reconnect on stream end"),
|
||||
meta: { error, event },
|
||||
});
|
||||
|
||||
if (this.subscribers.size > 0) {
|
||||
@ -219,10 +229,16 @@ export class KubeWatchApi {
|
||||
}
|
||||
}
|
||||
|
||||
protected writeLog({ data, error }: { data?: any[], error?: any[] } = {}) {
|
||||
if (isDevelopment) {
|
||||
const logStyle = `font-weight: bold; ${error ? "color: red;" : ""}`;
|
||||
console.log("%cKUBE-WATCH-API:", logStyle, ...Array.from(data || error));
|
||||
protected log({ message, meta }: IKubeWatchLog) {
|
||||
if (isProduction) return;
|
||||
|
||||
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
|
||||
protected eventsBuffer = observable<IKubeWatchEvent<KubeJsonApiData>>([], { deep: false });
|
||||
protected eventsBuffer = observable.array<IKubeWatchEvent<KubeJsonApiData>>([], { deep: false });
|
||||
|
||||
protected bindWatchEventsUpdater(delay = 1000) {
|
||||
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);
|
||||
})
|
||||
|
||||
reaction(() => this.eventsBuffer[0], this.updateFromEventsBuffer, {
|
||||
reaction(() => this.eventsBuffer.length > 0, this.updateFromEventsBuffer, {
|
||||
delay
|
||||
});
|
||||
}
|
||||
@ -209,10 +209,6 @@ export abstract class KubeObjectStore<T extends KubeObject = any> extends ItemSt
|
||||
|
||||
@action
|
||||
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();
|
||||
|
||||
for (const { type, object } of this.eventsBuffer.clear()) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user