@@ -37,14 +37,31 @@ export interface GetOptions {
3737export interface IItemList < T > {
3838 /** Cache time to live in seconds, defaults to 15 minutes */
3939 readonly timeToLive : number
40+ /** Cached items */
4041 items : T [ ]
42+ /** Expiration time */
4143 readonly expiresAt : number
44+ /** Creates an indexed collection from the cached items */
4245 index ( key ?: keyof T ) : ItemList < T >
46+ /** An indexed collection of items */
4347 indexedItems : Record < string , T >
48+ /** Determines if the cache has expired */
4449 expired ( ) : boolean
50+ /** Computes the expiration time based on timeToLive */
4551 setExpiration ( ) : void
52+ /** Ejects cache if expired */
4653 clearIfExpired ( ) : void
54+ /**
55+ * Searches the collection for an item with the specified key/value pair
56+ * @param key or keys to search
57+ * @param expression to match
58+ */
4759 find ( key : keyof T | Array < keyof T > , value : any ) : T | undefined
60+ /**
61+ * Gets the cache option value if present, otherwise defaults to true
62+ * @param options to check
63+ */
64+ getCacheDefault ( options ?: GetOptions ) : boolean
4865}
4966
5067export interface IEntityService < T > extends IItemList < T > {
@@ -58,11 +75,8 @@ export abstract class ItemList<T extends Record<string, any>>
5875 extends EntityService
5976 implements IItemList < T >
6077{
61- /** Cached items */
6278 items : T [ ] = [ ]
63- /** An indexed collection of items */
6479 indexedItems : Record < string , T > = { }
65- /** Time when cache expires */
6680 expiresAt = 0
6781 /** Key to index by */
6882 private keyField = 'id'
@@ -71,9 +85,6 @@ export abstract class ItemList<T extends Record<string, any>>
7185 super ( sdk , timeToLive )
7286 }
7387
74- /**
75- * Creates an indexed collection from the cached items
76- */
7788 index ( key : keyof T = this . keyField ) {
7889 this . keyField = key as string
7990 this . indexedItems = { }
@@ -85,33 +96,21 @@ export abstract class ItemList<T extends Record<string, any>>
8596 return this
8697 }
8798
88- /** Computes the expiration time based on timeToLive */
8999 setExpiration ( ) {
90100 this . expiresAt = Date . now ( ) + this . timeToLive * 1000
91101 }
92102
93- /**
94- * Determines if the cache has expired
95- */
96103 expired ( ) {
97104 return this . expiresAt <= Date . now ( )
98105 }
99106
100- /**
101- * Ejects cache if expired
102- */
103107 clearIfExpired ( ) {
104108 if ( this . expired ( ) ) {
105109 this . items = [ ]
106110 this . indexedItems = { }
107111 }
108112 }
109113
110- /**
111- * Searches the collection for an item with the specified key/value pair
112- * @param key or keys to search
113- * @param expression to match
114- */
115114 find ( key : keyof T | Array < keyof T > , expression : string ) : T | undefined {
116115 let result : T | undefined
117116 let keys : Array < keyof T >
@@ -122,9 +121,8 @@ export abstract class ItemList<T extends Record<string, any>>
122121 keys = key as Array < keyof T >
123122 }
124123
125- let rx : RegExp
126124 try {
127- rx = new RegExp ( expression , 'i' )
125+ const rx = new RegExp ( expression , 'i' )
128126
129127 for ( const item of this . items ) {
130128 for ( const k of keys ) {
@@ -141,12 +139,11 @@ export abstract class ItemList<T extends Record<string, any>>
141139 }
142140 }
143141
144- /**
145- * Gets the cache option value if present, otherwise defaults to true
146- * @param options to check
147- */
148142 getCacheDefault ( options ?: GetOptions ) {
149- const cache = options && 'useCache' in options ? options . useCache : true
143+ const cache =
144+ options && 'useCache' in options && options . useCache !== undefined
145+ ? options . useCache
146+ : true
150147 return cache
151148 }
152149}
0 commit comments