Skip to content

Commit e0a1861

Browse files
committed
First pass
1 parent ccbf7ba commit e0a1861

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class BackingDataObject {
2+
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { BackingDataObject } from "./BackingDataObject";
2+
3+
export class CacheProvider {
4+
createGlobalId(): string {
5+
throw new Error("Method not implemented.");
6+
}
7+
getBdo(globalId: string): BackingDataObject {
8+
throw new Error("Method not implemented.");
9+
}
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class ResultTree {
2+
// TTL in MS
3+
ttlInMs: number;
4+
// Tree data
5+
data: string;
6+
// Last cached at timestamp
7+
cachedAt: Date;
8+
// Last accessed
9+
lastAccessed: Date
10+
11+
rootStub: StubDataObject;
12+
13+
isStale(): boolean {
14+
return Date.now() - this.cachedAt.getTime() > this.ttlInMs;
15+
}
16+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)