@@ -2,42 +2,36 @@ import { KubernetesObject } from './types';
2
2
import { Watch } from './watch' ;
3
3
4
4
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 > ;
7
7
}
8
8
9
9
export type ListCallback < T extends KubernetesObject > = ( list : T [ ] ) => void ;
10
10
11
11
export class ListWatch < T extends KubernetesObject > implements ObjectCache < T > {
12
12
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 [ ] } = { } ;
17
14
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 ) {
19
18
this . watch = watch ;
20
19
this . listFn = listFn ;
21
- this . path = path ;
22
20
this . doneHandler ( null ) ;
23
21
}
24
22
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
+ } ) ;
34
28
}
35
29
36
- public list ( namespace ?: string | undefined ) : T [ ] {
30
+ public list ( namespace ?: string | undefined ) : ReadonlyArray < T > {
37
31
if ( ! namespace ) {
38
32
return this . objects ;
39
33
}
40
- return this . indexCache [ namespace ] as T [ ] ;
34
+ return this . indexCache [ namespace ] as ReadonlyArray < T > ;
41
35
}
42
36
43
37
private doneHandler ( err : any ) {
@@ -83,31 +77,28 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T> {
83
77
84
78
// Only public for testing.
85
79
export function addOrUpdateObject < T extends KubernetesObject > ( objects : T [ ] , obj : T ) {
86
- const ix = findObject ( objects , obj ) ;
80
+ const ix = findKubernetesObject ( objects , obj ) ;
87
81
if ( ix === - 1 ) {
88
82
objects . push ( obj ) ;
89
83
} else {
90
84
objects [ ix ] = obj ;
91
85
}
92
86
}
93
87
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
+ } ) ;
106
97
}
107
98
108
99
// Public for testing.
109
100
export function deleteObject < T extends KubernetesObject > ( objects : T [ ] , obj : T ) {
110
- const ix = findObject ( objects , obj ) ;
101
+ const ix = findKubernetesObject ( objects , obj ) ;
111
102
if ( ix !== - 1 ) {
112
103
objects . splice ( ix , 1 ) ;
113
104
}
0 commit comments