fix: duplicate colors and indices

This commit is contained in:
thecodrr
2021-02-18 19:46:44 +05:00
parent 22d2950f24
commit 3e3b4220e7
3 changed files with 24 additions and 21 deletions

View File

@@ -16,7 +16,7 @@ export default class IndexedCollection {
async addItem(item) {
if (!item.id) throw new Error("The item must contain the id field.");
const exists = await this.exists(item.id);
const exists = this.exists(item.id);
if (!exists) item.dateCreated = item.dateCreated || Date.now();
await this.updateItem(item);
if (!exists) {

View File

@@ -11,20 +11,22 @@ export default class Indexer extends Storage {
this.indices = (await this.read(this.type, true)) || [];
}
async exists(key) {
exists(key) {
return this.indices.includes(key);
}
async index(key) {
this.indices[this.indices.length] = key;
if (this.exists(key)) return;
this.indices.push(key);
await this.write(this.type, this.indices);
}
async getIndices() {
getIndices() {
return this.indices;
}
async deindex(key) {
if (!this.exists(key)) return;
this.indices.splice(this.indices.indexOf(key), 1);
await this.write(this.type, this.indices);
}

View File

@@ -6,15 +6,17 @@ class Migrator {
await Promise.all(
collections.map(async (collection) => {
if (!collection.index || !collection.dbCollection) return;
await Promise.all(
collection.index.map(async (id) => {
for (var i = 0; i < collection.index.length; ++i) {
let id = collection.index[i];
let item = get(id);
if (!item) return;
if (item.deleted && !item.type)
return await collection.dbCollection?._collection?.addItem(item);
if (!item) {
continue;
}
if (item.deleted && !item.type) {
await collection.dbCollection?._collection?.addItem(item);
continue;
}
const migrate = migrations[version][item.type || collection.type];
if (migrate) item = migrate(item);
@@ -23,8 +25,7 @@ class Migrator {
} else {
await collection.dbCollection.add(item);
}
})
);
}
})
);
return true;