Files
notesnook/packages/core/database/indexer.js

58 lines
1.4 KiB
JavaScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2022 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
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() {
this.indices = (await this.read(this.type, true)) || [];
2020-02-02 20:07:11 +05:00
}
2021-02-18 19:46:44 +05:00
exists(key) {
return this.indices.includes(key);
}
2020-02-02 20:07:11 +05:00
async index(key) {
2021-02-18 19:46:44 +05:00
if (this.exists(key)) return;
this.indices.push(key);
2020-02-02 20:07:11 +05:00
await this.write(this.type, this.indices);
}
2021-02-18 19:46:44 +05:00
getIndices() {
2020-02-02 20:07:11 +05:00
return this.indices;
}
async deindex(key) {
2021-02-18 19:46:44 +05:00
if (!this.exists(key)) return;
2020-02-02 20:07:11 +05:00
this.indices.splice(this.indices.indexOf(key), 1);
await this.write(this.type, this.indices);
}
async clear() {
this.indices = [];
await super.clear();
}
2020-02-02 20:07:11 +05:00
}