2020-03-01 11:37:59 +05:00
|
|
|
import sort from "fast-sort";
|
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-02-08 13:16:41 +05:00
|
|
|
constructor(context, type, encryptionKeyFactory) {
|
|
|
|
|
super(context, type, encryptionKeyFactory);
|
2021-06-07 23:44:48 +05:00
|
|
|
this.type = type;
|
2021-06-08 12:00:34 +05:00
|
|
|
this.map = new Map();
|
2022-02-08 13:16:41 +05:00
|
|
|
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
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-11-18 11:01:12 +05:00
|
|
|
getItems(sortFn = (u) => u.dateCreated, manipulate = (item) => item) {
|
2020-02-02 20:07:11 +05:00
|
|
|
let 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;
|
2021-11-18 11:01:12 +05:00
|
|
|
value = manipulate ? manipulate(value) : value;
|
|
|
|
|
items.push(value);
|
2020-03-01 11:37:59 +05:00
|
|
|
});
|
2020-03-01 11:42:30 +05:00
|
|
|
return sort(items).desc(sortFn);
|
2020-02-02 20:07:11 +05:00
|
|
|
}
|
|
|
|
|
}
|