@@ -2,42 +2,36 @@ import { KubernetesObject } from './types';
22import { Watch } from './watch' ;
33
44export 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
99export type ListCallback < T extends KubernetesObject > = ( list : T [ ] ) => void ;
1010
1111export 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.
8579export 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.
109100export 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 }
0 commit comments