-
Notifications
You must be signed in to change notification settings - Fork 2
/
objhydratetest.js
173 lines (144 loc) · 4.22 KB
/
objhydratetest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
let TypeMessage = {
name: '',
topic: '',
threadid: '',
body: '',
};
let TypeStoreMessage = {
cache: {
id: '',
updated: '',
},
src: TypeMessage,
}
class TestRecordDatabase {
constructor(records, opts={}) {
this.records = records;
this.opts = opts;
}
getRecord(id) {
let records = this.getRecords([id]);
return records[0];
}
getRecords(ids=[]) {
let recordCollection = {
promises: [],
records: [],
};
recordCollection.records = ids.map(id => {
let record = {
loaded: false,
promise: null,
src: {
id,
name: '',
topic: '',
threadid: '',
body: '',
},
}
record.promise = new Promise((resolve, reject) => {
if (this.records[id]) {
setTimeout(() => {
// TODO: get the record from local caches here
record.src = this.records[id]
record.loaded = true;
resolve(this.records[id]);
}, this.opts.delay ? 5000 : 0);
} else {
reject();
}
});
recordCollection.promises.push(record.promise);
return record;
});
return recordCollection;
}
};
// Create a promise with the resolve/reject functions tacked on
function makePromise() {
let resolve = null;
let reject = null;
let p = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
p.resolve = resolve;
p.reject = reject;
return p;
}
function getMessageIds(ids, sources=[]) {
let promises = [];
let collectionMap = Object.create(null); // id:<record in collection> for faster lookup
let collection = [];
// Build our record collection
ids.forEach(id => {
let record = collectionMap[id] = {
id,
promise: makePromise(),
loading: true,
state: 'loading', // loading/loaded/failed
src: {},
};
promises.push(record.promise);
collection.push(record);
});
// To get our collection back to the caller as soon as possible, run the actual data
// loading in the next tick
setTimeout(async () => {
let outstandingRecordIds = [...ids];
for (let source of sources) {
let recordsFromSource = source.getRecords(outstandingRecordIds);
await Promise.allSettled(recordsFromSource.promises);
outstandingRecordIds = [];
recordsFromSource.records.forEach(r => {
if (r.loaded) {
collectionMap[r.src.id].src = r.src;
collectionMap[r.src.id].state = 'loaded';
collectionMap[r.src.id].promise.resolve();
} else {
outstandingRecordIds.push(r.src.id);
}
});
}
// Any remaining unloaded records have failed
collection.forEach(r => {
if (r.state === 'loading') {
collectionMap[r.id].state = 'failed';
collectionMap[r.id].promise.reject();
}
});
}, 0)
return {
collection,
promises,
};
}
(async () => {
let internalDb = new TestRecordDatabase({
msg1: {
id: 'msg1',
name: 'Darren',
topic: 'bla bla',
threadid: 'aaaa-bbbb',
body: 'hi! go sum update?',
},
});
let externalDb = new TestRecordDatabase({
msg2: {
id: 'msg2',
name: 'Phil',
topic: 'foo',
threadid: 'aaaa-cccc',
body: 'no update',
},
}, {delay:true});
let records = getMessageIds(['msg1', 'msg2'], [internalDb, externalDb]);
await records.collection[0].promise;
console.log('before', records.collection);
await Promise.allSettled(records.promises);
console.log('after', records.collection)
})();
setTimeout(() => {
console.log(1)
}, 60000)