|
| 1 | +import { DataConnectError } from '../api'; |
| 2 | +import { BackingDataObject } from './BackingDataObject'; |
| 3 | +import { CacheProvider } from './CacheProvider'; |
| 4 | + |
| 5 | +type ScalarValue = number | string | null | object | ScalarValue[]; |
| 6 | +export const GLOBAL_ID_KEY = 'cacheId'; |
| 7 | +export class StubDataObject { |
| 8 | + backingData?: BackingDataObject; |
| 9 | + scalars: { [key: string]: ScalarValue } = {}; |
| 10 | + references: { [key: string]: StubDataObject } = {}; |
| 11 | + objectLists: { |
| 12 | + [key: string]: StubDataObject[]; |
| 13 | + } = {}; |
| 14 | + globalId?: string; |
| 15 | + |
| 16 | + constructor(values: ScalarValue, cacheProvider: CacheProvider) { |
| 17 | + if (typeof values !== 'object' || Array.isArray(values)) { |
| 18 | + throw new DataConnectError( |
| 19 | + 'invalid-argument', |
| 20 | + 'StubDataObject initialized with non-object value' |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + this.globalId = values[GLOBAL_ID_KEY]; |
| 25 | + if (values.hasOwnProperty(GLOBAL_ID_KEY)) { |
| 26 | + this.backingData = cacheProvider.getBdo(this.globalId); |
| 27 | + } |
| 28 | + for (const key in values) { |
| 29 | + if (values.hasOwnProperty(key)) { |
| 30 | + if (key === GLOBAL_ID_KEY) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + if (typeof values[key] === 'object') { |
| 34 | + if (Array.isArray(values[key])) { |
| 35 | + if(!this.objectLists[key]) { |
| 36 | + this.objectLists[key] = []; |
| 37 | + } |
| 38 | + const objArray = []; |
| 39 | + const scalarArray = []; |
| 40 | + for(const value of values[key]) { |
| 41 | + if(typeof value === 'object') { |
| 42 | + if(Array.isArray(value)) { |
| 43 | + // TODO: What if it's an array of arrays? |
| 44 | + } else { |
| 45 | + objArray.push( |
| 46 | + new StubDataObject(value, cacheProvider)); |
| 47 | + } |
| 48 | + } else { |
| 49 | + scalarArray.push(value); |
| 50 | + } |
| 51 | + } |
| 52 | + if(scalarArray.length > 0 && objArray.length > 0) { |
| 53 | + throw new DataConnectError('invalid-argument', 'Sparse array detected.'); |
| 54 | + } |
| 55 | + if(scalarArray.length > 0) { |
| 56 | + if(this.backingData) { |
| 57 | + this.backingData.updateServerValue(key, scalarArray); |
| 58 | + } else { |
| 59 | + this.scalars[key] = scalarArray; |
| 60 | + } |
| 61 | + } else if(objArray.length > 0) { |
| 62 | + this.objectLists[key] = objArray; |
| 63 | + } else { |
| 64 | + this.scalars[key] = []; |
| 65 | + } |
| 66 | + } else { |
| 67 | + const stubDataObject = new StubDataObject( |
| 68 | + values[key], |
| 69 | + cacheProvider |
| 70 | + ); |
| 71 | + this.references[key] = stubDataObject; |
| 72 | + } |
| 73 | + } else { |
| 74 | + if(this.backingData) { |
| 75 | + this.backingData.updateServerValue(key, values[key]); |
| 76 | + } else { |
| 77 | + this.scalars[key] = values[key]; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + if(this.backingData) { |
| 83 | + cacheProvider.updateBackingData(this.backingData); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments