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) { async addItem(item) {
if (!item.id) throw new Error("The item must contain the id field."); 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(); if (!exists) item.dateCreated = item.dateCreated || Date.now();
await this.updateItem(item); await this.updateItem(item);
if (!exists) { if (!exists) {

View File

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

View File

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