18
18
19
19
import { QueryResult } from '../api' ;
20
20
21
- /** Value is any FDC scalar value. */ // TODO: make this more accurate
21
+ /** Value is any FDC scalar value. */
22
+ // TODO: make this more accurate... what type should we use to represent any FDC scalar value?
22
23
type Value = string | number | boolean | null | undefined | object | Value [ ] ;
23
24
24
25
/**
25
26
* Defines the shape of query result data that represents a single entity.
26
27
* It must have __typename and __id for normalization.
27
- *
28
- * TODO: this is just a StubDataObject isn't it...?
29
28
*/
29
+ // TODO: this is just a StubDataObject isn't it...?
30
30
export interface QueryResultData {
31
31
[ key : string ] : Value ;
32
32
__typename ?: string ;
@@ -51,6 +51,18 @@ function isCacheableQueryResultData(value: unknown): value is QueryResultData {
51
51
/**
52
52
* Interface for a stub result tree, with fields which are stub data objects
53
53
*/
54
+ // TODO: need a better way to represent that a query may return a single entity or a list of entities
55
+ // ! ex: queryResult.data =
56
+ // ! {
57
+ // ! movies: [ <-- list
58
+ // ! {...movie1...},
59
+ // ! {...movie2...},
60
+ // ! {...movie3...}
61
+ // ! ],
62
+ // ! currentUser: { <-- singleton
63
+ // ! ...user...
64
+ // ! }
65
+ // ! }
54
66
interface StubResultTree {
55
67
[ key : string ] : StubDataObject | StubDataObjectList ;
56
68
}
@@ -125,7 +137,9 @@ export class BackingDataObject {
125
137
*/
126
138
updateLocal ( value : Value , key : string ) : void {
127
139
this . localValues . set ( key , value ) ;
128
- // notify listeners
140
+ for ( const listener of this . listeners ) {
141
+ listener [ key ] = value ;
142
+ }
129
143
}
130
144
}
131
145
@@ -192,7 +206,7 @@ export class Cache {
192
206
const existingBdo = this . bdoCache . get ( bdoCacheKey ) ;
193
207
194
208
// data is a single "movie" or "actor"
195
- // key is a field of the returned data
209
+ // key is a field of the returned data, such as "name"
196
210
for ( const key in data ) {
197
211
// eslint-disable-next-line no-prototype-builtins
198
212
if ( data . hasOwnProperty ( key ) ) {
@@ -223,7 +237,7 @@ export class Cache {
223
237
}
224
238
225
239
/**
226
- * Creates a new BackingDataObject and adds it to the cache.
240
+ * Creates a new BackingDataObject and adds it to the cache. This obejct
227
241
* @param bdoCacheKey The cache key for the new BDO.
228
242
* @param data The entity data from the server.
229
243
* @param stubDataObject The first stub to listen to this BDO.
@@ -233,7 +247,6 @@ export class Cache {
233
247
data : QueryResultData ,
234
248
stubDataObject : StubDataObject
235
249
) : void {
236
- // TODO: don't cache non-cacheable fields!
237
250
const serverValues = new Map < string , Value > ( Object . entries ( data ) ) ;
238
251
const newBdo = new BackingDataObject ( bdoCacheKey , serverValues ) ;
239
252
newBdo . listeners . add ( stubDataObject ) ;
@@ -252,6 +265,9 @@ export class Cache {
252
265
stubDataObject : StubDataObject
253
266
) : void {
254
267
// TODO: don't cache non-cacheable fields!
268
+ // ! how do we know that a field is not cacheable...?
269
+ // ! are we assuming entities themselves will be entirely not cacheable?
270
+ // ! ex: queryResult.data = {movies: {...cacheable...}, currentRating: {...not cacheable...}}
255
271
for ( const [ key , value ] of Object . entries ( data ) ) {
256
272
backingDataObject . updateFromServer ( value , key ) ;
257
273
}
0 commit comments