2020-02-02 20:07:11 +05:00
|
|
|
import Storage from "./storage";
|
|
|
|
|
|
|
|
|
|
export default class Indexer extends Storage {
|
|
|
|
|
constructor(context, type) {
|
|
|
|
|
super(context);
|
|
|
|
|
this.type = type;
|
|
|
|
|
this.indices = [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 12:03:07 +05:00
|
|
|
async init() {
|
2020-03-09 12:44:55 +05:00
|
|
|
this.indices = (await this.read(this.type, true)) || [];
|
2020-02-02 20:07:11 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-06 22:44:16 +05:00
|
|
|
exists(key) {
|
2020-02-03 23:53:58 +05:00
|
|
|
return this.indices.includes(key);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-02 20:07:11 +05:00
|
|
|
async index(key) {
|
|
|
|
|
this.indices[this.indices.length] = key;
|
|
|
|
|
await this.write(this.type, this.indices);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getIndices() {
|
|
|
|
|
return this.indices;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async deindex(key) {
|
|
|
|
|
this.indices.splice(this.indices.indexOf(key), 1);
|
|
|
|
|
await this.write(this.type, this.indices);
|
|
|
|
|
}
|
2020-09-19 11:33:31 +05:00
|
|
|
|
|
|
|
|
async clear() {
|
2020-11-06 22:44:16 +05:00
|
|
|
for (var i = 0; i < this.indices.length; ++i) {
|
|
|
|
|
let key = this.indices[i];
|
|
|
|
|
await this.remove(key);
|
|
|
|
|
}
|
2020-09-19 11:33:31 +05:00
|
|
|
this.indices = [];
|
|
|
|
|
}
|
2020-02-02 20:07:11 +05:00
|
|
|
}
|