diff --git a/src/addons/mod/book/services/book.ts b/src/addons/mod/book/services/book.ts index 6b6c18a8f..64d025dd6 100644 --- a/src/addons/mod/book/services/book.ts +++ b/src/addons/mod/book/services/book.ts @@ -69,6 +69,7 @@ export class AddonModBookProvider { () => CoreSites.getSiteTable(LAST_CHAPTER_VIEWED_TABLE, { siteId, config: { cachingStrategy: CoreDatabaseCachingStrategy.None }, + onDestroy: () => delete this.lastChapterViewedTables[siteId], }), ), ); diff --git a/src/core/classes/database/database-table.ts b/src/core/classes/database/database-table.ts index 5aae6cac5..43e0ac125 100644 --- a/src/core/classes/database/database-table.ts +++ b/src/core/classes/database/database-table.ts @@ -28,6 +28,7 @@ export class CoreDatabaseTable< protected database: SQLiteDB; protected tableName: string; protected primaryKeyColumns: PrimaryKeyColumn[]; + protected listeners: CoreDatabaseTableListener[] = []; constructor( config: Partial, @@ -86,7 +87,16 @@ export class CoreDatabaseTable< * Destroy. */ async destroy(): Promise { - // Nothing to destroy by default, override this method if necessary. + this.listeners.forEach(listener => listener.onDestroy?.()); + } + + /** + * Add listener. + * + * @param listener Listener. + */ + addListener(listener: CoreDatabaseTableListener): void { + this.listeners.push(listener); } /** @@ -368,6 +378,13 @@ export interface CoreDatabaseConfiguration { // This definition is augmented in subclasses. } +/** + * Database table listener. + */ +export interface CoreDatabaseTableListener { + onDestroy?(): void; +} + /** * CoreDatabaseTable constructor. */ diff --git a/src/core/features/course/services/course.ts b/src/core/features/course/services/course.ts index 5e1496358..f3f449c4e 100644 --- a/src/core/features/course/services/course.ts +++ b/src/core/features/course/services/course.ts @@ -153,6 +153,7 @@ export class CoreCourseProvider { () => CoreSites.getSiteTable(COURSE_STATUS_TABLE, { siteId, config: { cachingStrategy: CoreDatabaseCachingStrategy.Eager }, + onDestroy: () => delete this.statusTables[siteId], }), ), ); diff --git a/src/core/features/pushnotifications/services/pushnotifications.ts b/src/core/features/pushnotifications/services/pushnotifications.ts index be515b7c9..2477b1cfc 100644 --- a/src/core/features/pushnotifications/services/pushnotifications.ts +++ b/src/core/features/pushnotifications/services/pushnotifications.ts @@ -74,6 +74,7 @@ export class CorePushNotificationsProvider { siteId, config: { cachingStrategy: CoreDatabaseCachingStrategy.None }, primaryKeyColumns: ['appid', 'uuid'], + onDestroy: () => delete this.registeredDevicesTables[siteId], }, ), ), diff --git a/src/core/services/filepool.ts b/src/core/services/filepool.ts index 30d857010..f41ee3cd4 100644 --- a/src/core/services/filepool.ts +++ b/src/core/services/filepool.ts @@ -113,6 +113,7 @@ export class CoreFilepoolProvider { siteId, config: { cachingStrategy: CoreDatabaseCachingStrategy.Lazy }, primaryKeyColumns: ['fileId'], + onDestroy: () => delete this.filesTables[siteId], }), ), ); @@ -122,6 +123,7 @@ export class CoreFilepoolProvider { siteId, config: { cachingStrategy: CoreDatabaseCachingStrategy.Lazy }, primaryKeyColumns: ['fileId', 'component', 'componentId'], + onDestroy: () => delete this.linksTables[siteId], }), ), ); @@ -130,6 +132,7 @@ export class CoreFilepoolProvider { () => CoreSites.getSiteTable(PACKAGES_TABLE_NAME, { siteId, config: { cachingStrategy: CoreDatabaseCachingStrategy.Lazy }, + onDestroy: () => delete this.packagesTables[siteId], }), ), ); @@ -149,16 +152,6 @@ export class CoreFilepoolProvider { NgZone.run(() => this.checkQueueProcessing()); }); }); - - CoreEvents.on(CoreEvents.SITE_DELETED, async ({ siteId }) => { - if (!siteId || !(siteId in this.filesTables)) { - return; - } - - await this.filesTables[siteId].destroy(); - - delete this.filesTables[siteId]; - }); } /** diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index d18284bd7..c7cc2ba8e 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -158,6 +158,7 @@ export class CoreSitesProvider { config: Partial; database: SQLiteDB; primaryKeyColumns: PrimaryKeyColumn[]; + onDestroy(): void; }> = {}, ): Promise> { const siteId = options.siteId ?? this.getCurrentSiteId(); @@ -176,6 +177,8 @@ export class CoreSitesProvider { options.primaryKeyColumns, ); + options.onDestroy && table.addListener({ onDestroy: options.onDestroy }); + await table.initialize(); promisedTable.resolve(table as unknown as CoreDatabaseTable); @@ -1833,16 +1836,19 @@ export class CoreSitesProvider { * @returns Scehmas Table. */ protected getSiteSchemasTable(site: CoreSite): AsyncInstance> { - this.schemasTables[site.getId()] = this.schemasTables[site.getId()] ?? asyncInstance( + const siteId = site.getId(); + + this.schemasTables[siteId] = this.schemasTables[siteId] ?? asyncInstance( () => this.getSiteTable(SCHEMA_VERSIONS_TABLE_NAME, { - siteId: site.getId(), + siteId: siteId, database: site.getDb(), config: { cachingStrategy: CoreDatabaseCachingStrategy.Eager }, primaryKeyColumns: ['name'], + onDestroy: () => delete this.schemasTables[siteId], }), ); - return this.schemasTables[site.getId()]; + return this.schemasTables[siteId]; } }