diff --git a/packages/core/api/index.js b/packages/core/api/index.js index 1d8b62745..2678501a1 100644 --- a/packages/core/api/index.js +++ b/packages/core/api/index.js @@ -70,6 +70,15 @@ class Database { this.syncer.stopAutoSync(); this.disconnectSSE(); }); + EV.subscribe(EVENTS.databaseCollectionInitiated, async (collectionName) => { + switch (collectionName) { + case "notes": { + await this.monographs.init(); + await this.trash.init(); + break; + } + } + }); this.session = new Session(this.storage); await this._validate(); @@ -103,7 +112,6 @@ class Database { this.trash = new Trash(this); - await this.trash.cleanup(); await this.settings.init(); await this.outbox.init(); await this.user.init(); diff --git a/packages/core/collections/collection.js b/packages/core/collections/collection.js index cd2a3100d..89bfcedb9 100644 --- a/packages/core/collections/collection.js +++ b/packages/core/collections/collection.js @@ -6,7 +6,7 @@ class Collection { static async new(db, name, cached = true, deferred = false) { const collection = new this(db, name, cached); - if (!deferred && collection.init) await collection.init(); + if (!deferred) await collection.init(); else await collection._collection.indexer.init(); if (collection._collection.clear) @@ -21,6 +21,7 @@ class Collection { async init() { if (this.initialized) return; await this._collection.init(); + EV.publish(EVENTS.databaseCollectionInitiated, this.collectionName); this.initialized = true; } @@ -30,6 +31,7 @@ class Collection { */ constructor(db, name, cached) { this._db = db; + this.collectionName = name; if (cached) this._collection = new CachedCollection(this._db.storage, name); else this._collection = new IndexedCollection(this._db.storage, name); } diff --git a/packages/core/common.js b/packages/core/common.js index 46fb33023..9db44a836 100644 --- a/packages/core/common.js +++ b/packages/core/common.js @@ -43,6 +43,7 @@ export const EVENTS = { databaseSyncRequested: "db:syncRequested", databaseMigrated: "db:migrated", databaseUpdated: "db:updated", + databaseCollectionInitiated: "db:collectionInitiated", appRefreshRequested: "app:refreshRequested", noteRemoved: "note:removed", tokenRefreshed: "token:refreshed",