Skip to content

Commit 00a4d9f

Browse files
committed
Address comments.
1 parent 684e6a4 commit 00a4d9f

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

src/cache.ts

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,36 @@ import { KubernetesObject } from './types';
22
import { Watch } from './watch';
33

44
export interface ObjectCache<T> {
5-
get(name: string, namespace?: string): T | null;
6-
list(namespace?: string): T[];
5+
get(name: string, namespace?: string): T | undefined;
6+
list(namespace?: string): ReadonlyArray<T>;
77
}
88

99
export type ListCallback<T extends KubernetesObject> = (list: T[]) => void;
1010

1111
export class ListWatch<T extends KubernetesObject> implements ObjectCache<T> {
1212
private objects: T[] = [];
13-
private indexCache: any = {};
14-
private path: string;
15-
private watch: Watch;
16-
private listFn: (callback: ListCallback<T>) => void;
13+
private readonly indexCache: { [key: string]: T[] } = {};
1714

18-
public constructor(path: string, watch: Watch, listFn: (callback: ListCallback<T>) => void) {
15+
public constructor(private readonly path: string,
16+
private readonly watch: Watch,
17+
private readonly listFn: (callback: ListCallback<T>) => void) {
1918
this.watch = watch;
2019
this.listFn = listFn;
21-
this.path = path;
2220
this.doneHandler(null);
2321
}
2422

25-
public get(name: string, namespace?: string): T | null {
26-
let result: T | null = null;
27-
for (const element of this.objects) {
28-
if (element.metadata.name === name &&
29-
(!namespace || element.metadata.namespace === namespace)) {
30-
result = element;
31-
}
32-
}
33-
return result;
23+
public get(name: string, namespace?: string): T | undefined {
24+
return this.objects.find((obj: T): boolean => {
25+
return (obj.metadata.name === name &&
26+
(!namespace || obj.metadata.namespace === namespace));
27+
});
3428
}
3529

36-
public list(namespace?: string | undefined): T[] {
30+
public list(namespace?: string | undefined): ReadonlyArray<T> {
3731
if (!namespace) {
3832
return this.objects;
3933
}
40-
return this.indexCache[namespace] as T[];
34+
return this.indexCache[namespace] as ReadonlyArray<T>;
4135
}
4236

4337
private doneHandler(err: any) {
@@ -83,31 +77,28 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T> {
8377

8478
// Only public for testing.
8579
export function addOrUpdateObject<T extends KubernetesObject>(objects: T[], obj: T) {
86-
const ix = findObject(objects, obj);
80+
const ix = findKubernetesObject(objects, obj);
8781
if (ix === -1) {
8882
objects.push(obj);
8983
} else {
9084
objects[ix] = obj;
9185
}
9286
}
9387

94-
// Public for testing.
95-
export function findObject<T extends KubernetesObject>(objects: T[], obj: T): number {
96-
for (let ix = 0; ix < objects.length; ix++) {
97-
const elt = objects[ix];
98-
if (obj.metadata.name !== elt.metadata.name) {
99-
continue;
100-
}
101-
if (obj.metadata.namespace === elt.metadata.namespace) {
102-
return ix;
103-
}
104-
}
105-
return -1;
88+
function isSameObject<T extends KubernetesObject>(o1: T, o2: T): boolean {
89+
return o1.metadata.name === o2.metadata.name &&
90+
o1.metadata.namespace === o2.metadata.namespace;
91+
}
92+
93+
function findKubernetesObject<T extends KubernetesObject>(objects: T[], obj: T): number {
94+
return objects.findIndex((elt: T) => {
95+
return isSameObject(elt, obj);
96+
});
10697
}
10798

10899
// Public for testing.
109100
export function deleteObject<T extends KubernetesObject>(objects: T[], obj: T) {
110-
const ix = findObject(objects, obj);
101+
const ix = findKubernetesObject(objects, obj);
111102
if (ix !== -1) {
112103
objects.splice(ix, 1);
113104
}

src/cache_test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('ListWatchCache', () => {
6060
} as V1ObjectMeta,
6161
} as V1Namespace);
6262
expect(cache.list().length).to.equal(2);
63-
expect(cache.get('name2')).to.equal(null);
63+
expect(cache.get('name2')).to.equal(undefined);
6464
});
6565

6666
it('should perform namespace caching', () => {
@@ -138,7 +138,7 @@ describe('ListWatchCache', () => {
138138
} as V1Pod);
139139
expect(cache.list().length).to.equal(2);
140140
expect(cache.list('ns2').length).to.equal(0);
141-
expect(cache.get('name2', 'ns2')).to.equal(null);
141+
expect(cache.get('name2', 'ns2')).to.equal(undefined);
142142
});
143143
it('should delete an object correctly', () => {
144144
const list: V1Pod[] = [

0 commit comments

Comments
 (0)