2020-11-04 10:42:19 +05:00
|
|
|
import IndexedCollection from "./indexed-collection";
|
2021-06-07 23:44:48 +05:00
|
|
|
import MapStub from "../utils/map";
|
2020-02-02 20:07:11 +05:00
|
|
|
|
2020-11-04 10:42:19 +05:00
|
|
|
export default class CachedCollection extends IndexedCollection {
|
2022-03-30 15:52:48 +05:00
|
|
|
constructor(context, type, eventManager) {
|
|
|
|
|
super(context, type, eventManager);
|
2021-06-07 23:44:48 +05:00
|
|
|
this.type = type;
|
2021-06-08 12:00:34 +05:00
|
|
|
this.map = new Map();
|
2022-06-16 16:01:08 +05:00
|
|
|
this.items = undefined;
|
2022-03-30 15:52:48 +05:00
|
|
|
// this.eventManager = eventManager;
|
|
|
|
|
// this.encryptionKeyFactory = encryptionKeyFactory;
|
2020-11-16 15:00:52 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-03 12:03:07 +05:00
|
|
|
async init() {
|
2020-11-16 15:00:52 +05:00
|
|
|
await super.init();
|
2022-02-08 13:16:41 +05:00
|
|
|
let data = await this.indexer.readMulti(this.indexer.indices);
|
2021-06-07 23:44:48 +05:00
|
|
|
if (this.map && this.map.dispose) this.map.dispose();
|
2022-02-08 13:16:41 +05:00
|
|
|
|
2022-03-28 10:22:26 +05:00
|
|
|
// const encryptionKey =
|
|
|
|
|
// this.encryptionKeyFactory && (await this.encryptionKeyFactory());
|
|
|
|
|
// if (encryptionKey) {
|
|
|
|
|
// for (let item of data) {
|
|
|
|
|
// const [_key, value] = item;
|
|
|
|
|
// const decryptedValue = JSON.parse(
|
|
|
|
|
// await this.indexer.decrypt(encryptionKey, value)
|
|
|
|
|
// );
|
|
|
|
|
// item[1] = decryptedValue;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2022-02-08 13:16:41 +05:00
|
|
|
|
2021-06-07 23:44:48 +05:00
|
|
|
this.map = new MapStub.Map(data, this.type);
|
2020-11-16 15:00:52 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clear() {
|
|
|
|
|
await super.clear();
|
|
|
|
|
this.map.clear();
|
2022-06-16 16:01:08 +05:00
|
|
|
this.invalidateCache();
|
2020-11-16 15:00:52 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 15:01:16 +05:00
|
|
|
async updateItem(item) {
|
|
|
|
|
await super.updateItem(item);
|
2020-11-16 15:00:52 +05:00
|
|
|
this.map.set(item.id, item);
|
2022-06-16 16:01:08 +05:00
|
|
|
this.invalidateCache();
|
2020-02-02 20:07:11 +05:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 16:46:23 +05:00
|
|
|
exists(id) {
|
2020-11-16 15:00:52 +05:00
|
|
|
return this.map.has(id) && !this.map.get(id).deleted;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-16 16:56:06 +05:00
|
|
|
has(id) {
|
|
|
|
|
return this.map.has(id);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-16 15:00:52 +05:00
|
|
|
getItem(id) {
|
|
|
|
|
return this.map.get(id);
|
2020-02-02 20:07:11 +05:00
|
|
|
}
|
|
|
|
|
|
2021-01-13 13:02:35 +05:00
|
|
|
async deleteItem(id) {
|
|
|
|
|
this.map.delete(id);
|
|
|
|
|
await super.deleteItem(id);
|
2022-06-16 16:01:08 +05:00
|
|
|
this.invalidateCache();
|
2021-01-13 13:02:35 +05:00
|
|
|
}
|
|
|
|
|
|
2020-03-23 15:06:12 +05:00
|
|
|
getRaw() {
|
2020-11-16 15:00:52 +05:00
|
|
|
return Array.from(this.map.values());
|
2020-03-23 15:06:12 +05:00
|
|
|
}
|
|
|
|
|
|
2022-06-16 16:01:08 +05:00
|
|
|
getItems(map = undefined) {
|
|
|
|
|
if (this.items) return this.items;
|
|
|
|
|
|
|
|
|
|
this.items = [];
|
2020-11-16 15:00:52 +05:00
|
|
|
this.map.forEach((value) => {
|
2021-01-10 11:12:54 +05:00
|
|
|
if (!value || value.deleted || !value.id) return;
|
2022-06-16 16:01:08 +05:00
|
|
|
value = map ? map(value) : value;
|
|
|
|
|
this.items.push(value);
|
2020-03-01 11:37:59 +05:00
|
|
|
});
|
2022-06-16 16:01:08 +05:00
|
|
|
this.items.sort((a, b) => b.dateCreated - a.dateCreated);
|
|
|
|
|
return this.items;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
invalidateCache() {
|
|
|
|
|
this.items = undefined;
|
2020-02-02 20:07:11 +05:00
|
|
|
}
|
|
|
|
|
}
|