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

modify watch event before calling callback

Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
This commit is contained in:
Jari Kolehmainen 2021-02-09 09:20:33 +02:00
parent ee501b4965
commit 8f812c59f5
2 changed files with 9 additions and 25 deletions

View File

@ -11,7 +11,7 @@ import { KubeJsonApi, KubeJsonApiData, KubeJsonApiDataList } from "./kube-json-a
import { IKubeObjectConstructor, KubeObject } from "./kube-object"; import { IKubeObjectConstructor, KubeObject } from "./kube-object";
import byline from "byline"; import byline from "byline";
import { ReadableWebToNodeStream } from "readable-web-to-node-stream"; import { ReadableWebToNodeStream } from "readable-web-to-node-stream";
import { IKubeWatchEvent, IKubeWatchMessage } from "./kube-watch-api"; import { IKubeWatchEvent } from "./kube-watch-api";
export interface IKubeApiOptions<T extends KubeObject> { export interface IKubeApiOptions<T extends KubeObject> {
/** /**
@ -399,10 +399,12 @@ export class KubeApi<T extends KubeObject = any> {
stream.on("data", (line) => { stream.on("data", (line) => {
try { try {
const data: IKubeWatchEvent = JSON.parse(line); const event: IKubeWatchEvent = JSON.parse(line);
this.modifyWatchEvent(event);
if (callback) { if (callback) {
callback(data); callback(event);
} }
} catch (ignore) { } catch (ignore) {
// ignore parse errors // ignore parse errors
@ -429,32 +431,23 @@ export class KubeApi<T extends KubeObject = any> {
return disposer; return disposer;
} }
protected generateMessage(event: IKubeWatchEvent): IKubeWatchMessage { protected modifyWatchEvent(event: IKubeWatchEvent) {
const message: IKubeWatchMessage = {};
switch (event.type) { switch (event.type) {
case "ADDED": case "ADDED":
case "DELETED": case "DELETED":
case "MODIFIED": { case "MODIFIED": {
const data = event as IKubeWatchEvent<KubeJsonApiData>; ensureObjectSelfLink(this, event.object);
message.data = data; const { namespace, resourceVersion } = event.object.metadata;
ensureObjectSelfLink(this, data.object);
const { namespace, resourceVersion } = data.object.metadata;
this.setResourceVersion(namespace, resourceVersion); this.setResourceVersion(namespace, resourceVersion);
this.setResourceVersion("", resourceVersion); this.setResourceVersion("", resourceVersion);
message.api = this;
message.namespace = namespace;
break; break;
} }
} }
return message;
} }
} }

View File

@ -1,7 +1,6 @@
// Kubernetes watch-api client // Kubernetes watch-api client
// API: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams // API: https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams
import type { KubeObject } from "./kube-object";
import type { KubeObjectStore } from "../kube-object.store"; import type { KubeObjectStore } from "../kube-object.store";
import type { ClusterContext } from "../components/context"; import type { ClusterContext } from "../components/context";
@ -9,7 +8,7 @@ import plimit from "p-limit";
import { comparer, IReactionDisposer, observable, reaction, when } from "mobx"; import { comparer, IReactionDisposer, observable, reaction, when } from "mobx";
import { autobind, noop } from "../utils"; import { autobind, noop } from "../utils";
import { KubeApi } from "./kube-api"; import { KubeApi } from "./kube-api";
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api"; import { KubeJsonApiData } from "./kube-json-api";
import { isDebugging, isProduction } from "../../common/vars"; import { isDebugging, isProduction } from "../../common/vars";
export interface IKubeWatchEvent<T = KubeJsonApiData> { export interface IKubeWatchEvent<T = KubeJsonApiData> {
@ -17,14 +16,6 @@ export interface IKubeWatchEvent<T = KubeJsonApiData> {
object?: T; object?: T;
} }
export interface IKubeWatchMessage<T extends KubeObject = any> {
namespace?: string;
data?: IKubeWatchEvent<KubeJsonApiData>
error?: IKubeWatchEvent<KubeJsonApiError>;
api?: KubeApi<T>;
store?: KubeObjectStore<T>;
}
export interface IKubeWatchSubscribeStoreOptions { export interface IKubeWatchSubscribeStoreOptions {
namespaces?: string[]; // default: all accessible namespaces namespaces?: string[]; // default: all accessible namespaces
preload?: boolean; // preload store items, default: true preload?: boolean; // preload store items, default: true