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:
parent
ee501b4965
commit
8f812c59f5
@ -11,7 +11,7 @@ import { KubeJsonApi, KubeJsonApiData, KubeJsonApiDataList } from "./kube-json-a
|
||||
import { IKubeObjectConstructor, KubeObject } from "./kube-object";
|
||||
import byline from "byline";
|
||||
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> {
|
||||
/**
|
||||
@ -399,10 +399,12 @@ export class KubeApi<T extends KubeObject = any> {
|
||||
|
||||
stream.on("data", (line) => {
|
||||
try {
|
||||
const data: IKubeWatchEvent = JSON.parse(line);
|
||||
const event: IKubeWatchEvent = JSON.parse(line);
|
||||
|
||||
this.modifyWatchEvent(event);
|
||||
|
||||
if (callback) {
|
||||
callback(data);
|
||||
callback(event);
|
||||
}
|
||||
} catch (ignore) {
|
||||
// ignore parse errors
|
||||
@ -429,32 +431,23 @@ export class KubeApi<T extends KubeObject = any> {
|
||||
return disposer;
|
||||
}
|
||||
|
||||
protected generateMessage(event: IKubeWatchEvent): IKubeWatchMessage {
|
||||
const message: IKubeWatchMessage = {};
|
||||
protected modifyWatchEvent(event: IKubeWatchEvent) {
|
||||
|
||||
switch (event.type) {
|
||||
case "ADDED":
|
||||
case "DELETED":
|
||||
|
||||
case "MODIFIED": {
|
||||
const data = event as IKubeWatchEvent<KubeJsonApiData>;
|
||||
ensureObjectSelfLink(this, event.object);
|
||||
|
||||
message.data = data;
|
||||
|
||||
ensureObjectSelfLink(this, data.object);
|
||||
|
||||
const { namespace, resourceVersion } = data.object.metadata;
|
||||
const { namespace, resourceVersion } = event.object.metadata;
|
||||
|
||||
this.setResourceVersion(namespace, resourceVersion);
|
||||
this.setResourceVersion("", resourceVersion);
|
||||
|
||||
message.api = this;
|
||||
message.namespace = namespace;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
// Kubernetes watch-api client
|
||||
// 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 { ClusterContext } from "../components/context";
|
||||
|
||||
@ -9,7 +8,7 @@ import plimit from "p-limit";
|
||||
import { comparer, IReactionDisposer, observable, reaction, when } from "mobx";
|
||||
import { autobind, noop } from "../utils";
|
||||
import { KubeApi } from "./kube-api";
|
||||
import { KubeJsonApiData, KubeJsonApiError } from "./kube-json-api";
|
||||
import { KubeJsonApiData } from "./kube-json-api";
|
||||
import { isDebugging, isProduction } from "../../common/vars";
|
||||
|
||||
export interface IKubeWatchEvent<T = KubeJsonApiData> {
|
||||
@ -17,14 +16,6 @@ export interface IKubeWatchEvent<T = KubeJsonApiData> {
|
||||
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 {
|
||||
namespaces?: string[]; // default: all accessible namespaces
|
||||
preload?: boolean; // preload store items, default: true
|
||||
|
||||
Loading…
Reference in New Issue
Block a user