Skip to content

Commit b45a646

Browse files
committed
add some notes/todos
1 parent 7db8964 commit b45a646

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

packages/data-connect/src/core/Cache.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818

1919
import { QueryResult } from '../api';
2020

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?
2223
type Value = string | number | boolean | null | undefined | object | Value[];
2324

2425
/**
2526
* Defines the shape of query result data that represents a single entity.
2627
* It must have __typename and __id for normalization.
27-
*
28-
* TODO: this is just a StubDataObject isn't it...?
2928
*/
29+
// TODO: this is just a StubDataObject isn't it...?
3030
export interface QueryResultData {
3131
[key: string]: Value;
3232
__typename?: string;
@@ -51,6 +51,18 @@ function isCacheableQueryResultData(value: unknown): value is QueryResultData {
5151
/**
5252
* Interface for a stub result tree, with fields which are stub data objects
5353
*/
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+
// ! }
5466
interface StubResultTree {
5567
[key: string]: StubDataObject | StubDataObjectList;
5668
}
@@ -125,7 +137,9 @@ export class BackingDataObject {
125137
*/
126138
updateLocal(value: Value, key: string): void {
127139
this.localValues.set(key, value);
128-
// notify listeners
140+
for (const listener of this.listeners) {
141+
listener[key] = value;
142+
}
129143
}
130144
}
131145

@@ -192,7 +206,7 @@ export class Cache {
192206
const existingBdo = this.bdoCache.get(bdoCacheKey);
193207

194208
// 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"
196210
for (const key in data) {
197211
// eslint-disable-next-line no-prototype-builtins
198212
if (data.hasOwnProperty(key)) {
@@ -223,7 +237,7 @@ export class Cache {
223237
}
224238

225239
/**
226-
* Creates a new BackingDataObject and adds it to the cache.
240+
* Creates a new BackingDataObject and adds it to the cache. This obejct
227241
* @param bdoCacheKey The cache key for the new BDO.
228242
* @param data The entity data from the server.
229243
* @param stubDataObject The first stub to listen to this BDO.
@@ -233,7 +247,6 @@ export class Cache {
233247
data: QueryResultData,
234248
stubDataObject: StubDataObject
235249
): void {
236-
// TODO: don't cache non-cacheable fields!
237250
const serverValues = new Map<string, Value>(Object.entries(data));
238251
const newBdo = new BackingDataObject(bdoCacheKey, serverValues);
239252
newBdo.listeners.add(stubDataObject);
@@ -252,6 +265,9 @@ export class Cache {
252265
stubDataObject: StubDataObject
253266
): void {
254267
// 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...}}
255271
for (const [key, value] of Object.entries(data)) {
256272
backingDataObject.updateFromServer(value, key);
257273
}

0 commit comments

Comments
 (0)