|
| 1 | +/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 2 | +/** |
| 3 | + * @license |
| 4 | + * Copyright 2024 Google LLC |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +type Value = string | number | boolean | null | undefined | object | Value[]; // Value is any FDC scalar value // ! this is just for debugging/testing!!! |
| 20 | + |
| 21 | +/** |
| 22 | + * map of [query + variables] --> stubs returned from that query |
| 23 | + */ |
| 24 | +const stubCache = new Map<string, StubDataObject>(); |
| 25 | + |
| 26 | +/** |
| 27 | + * map of [entity primary key] --> data for that entity |
| 28 | + */ |
| 29 | +const bdoCache = new Map<string, BackingDataObject>(); |
| 30 | + |
| 31 | +// operation stub result tree |
| 32 | +export interface StubResultTree { |
| 33 | + [key: string]: StubDataObject | StubDataObjectList; |
| 34 | +} |
| 35 | + |
| 36 | +// StubDataObject contains a reference to its BackingDataObject |
| 37 | +// Generated Data implements this interface |
| 38 | +export interface StubDataObject { |
| 39 | + readonly typedKey: string; |
| 40 | + [key: string]: Value | StubDataObject; |
| 41 | +} |
| 42 | + |
| 43 | +// Custom class for holding lists of objects. |
| 44 | +// We will need this for supporting operations like append, delete, paginate |
| 45 | +// not strictly necessary for caching but might be good to get it ready. |
| 46 | +class StubDataObjectList extends Array<StubDataObject> {} |
| 47 | + |
| 48 | +// Class used to hold entity values across all queries |
| 49 | +// public because its referenced from Gen SDK |
| 50 | +export class BackingDataObject { |
| 51 | + // Stable unique key identifying the entity across types. |
| 52 | + // TypeName + CompositePrimaryKey |
| 53 | + private typedKey: string; |
| 54 | + |
| 55 | + // Represent values received from server |
| 56 | + private serverValues: Map<string, Value> = new Map(); // Value is any FDC scalar value |
| 57 | + |
| 58 | + private listeners: StubDataObject[] = []; |
| 59 | + |
| 60 | + // Updates value for named property |
| 61 | + updateFromServer(value: Value, key: string): void { |
| 62 | + this.serverValues.set(key, value); |
| 63 | + // update listeners |
| 64 | + for (const listener of this.listeners) { |
| 65 | + listener[key] = value; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected value(key: string): Value | undefined { |
| 70 | + return this.serverValues.get(key); |
| 71 | + |
| 72 | + // FUTURE - for latency comp |
| 73 | + // return this.localValues.get(key) ?? this.serverValues.get(key) |
| 74 | + } |
| 75 | + |
| 76 | + // ----- FUTURE - for latency comp |
| 77 | + |
| 78 | + // Values modified locally |
| 79 | + private localValues: Map<string, Value> = new Map(); // Value is any FDC scalar value |
| 80 | + |
| 81 | + updateLocal(value: Value, key: string): void { |
| 82 | + this.localValues.set(key, value); |
| 83 | + // notify listeners |
| 84 | + } |
| 85 | +} |
0 commit comments