1414
1515import { Injectable } from '@angular/core' ;
1616import { CoreSites , CoreSiteSchema } from '@providers/sites' ;
17-
17+ import { SQLiteDB } from '@classes/sqlitedb' ;
1818import { makeSingleton } from '@singletons/core.singletons' ;
1919
2020/**
@@ -68,8 +68,10 @@ export class CoreXAPIOfflineProvider {
6868 ]
6969 } ;
7070
71+ protected dbReady : Promise < any > ; // Promise resolved when the DB schema is ready.
72+
7173 constructor ( ) {
72- CoreSites . instance . registerSiteSchema ( this . siteSchema ) ;
74+ this . dbReady = CoreSites . instance . registerSiteSchema ( this . siteSchema ) ;
7375 }
7476
7577 /**
@@ -93,9 +95,9 @@ export class CoreXAPIOfflineProvider {
9395 * @return Promise resolved if stored, rejected if failure.
9496 */
9597 async deleteStatements ( id : number , siteId ?: string ) : Promise < void > {
96- const site = await CoreSites . instance . getSite ( siteId ) ;
98+ const db = await this . getSiteDB ( siteId ) ;
9799
98- await site . getDb ( ) . deleteRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { id} ) ;
100+ await db . deleteRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { id} ) ;
99101 }
100102
101103 /**
@@ -106,9 +108,9 @@ export class CoreXAPIOfflineProvider {
106108 * @return Promise resolved if stored, rejected if failure.
107109 */
108110 async deleteStatementsForContext ( contextId : number , siteId ?: string ) : Promise < void > {
109- const site = await CoreSites . instance . getSite ( siteId ) ;
111+ const db = await this . getSiteDB ( siteId ) ;
110112
111- await site . getDb ( ) . deleteRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { contextid : contextId } ) ;
113+ await db . deleteRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { contextid : contextId } ) ;
112114 }
113115
114116 /**
@@ -118,9 +120,9 @@ export class CoreXAPIOfflineProvider {
118120 * @return Promise resolved with all the data.
119121 */
120122 async getAllStatements ( siteId ?: string ) : Promise < CoreXAPIOfflineStatementsDBData [ ] > {
121- const site = await CoreSites . instance . getSite ( siteId ) ;
123+ const db = await this . getSiteDB ( siteId ) ;
122124
123- return site . getDb ( ) . getRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , undefined , 'timecreated ASC' ) ;
125+ return db . getRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , undefined , 'timecreated ASC' ) ;
124126 }
125127
126128 /**
@@ -131,9 +133,9 @@ export class CoreXAPIOfflineProvider {
131133 * @return Promise resolved with the data.
132134 */
133135 async getContextStatements ( contextId : number , siteId ?: string ) : Promise < CoreXAPIOfflineStatementsDBData [ ] > {
134- const site = await CoreSites . instance . getSite ( siteId ) ;
136+ const db = await this . getSiteDB ( siteId ) ;
135137
136- return site . getDb ( ) . getRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { contextid : contextId } , 'timecreated ASC' ) ;
138+ return db . getRecords ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { contextid : contextId } , 'timecreated ASC' ) ;
137139 }
138140
139141 /**
@@ -144,9 +146,9 @@ export class CoreXAPIOfflineProvider {
144146 * @return Promise resolved with the data.
145147 */
146148 async getStatements ( id : number , siteId ?: string ) : Promise < CoreXAPIOfflineStatementsDBData > {
147- const site = await CoreSites . instance . getSite ( siteId ) ;
149+ const db = await this . getSiteDB ( siteId ) ;
148150
149- return site . getDb ( ) . getRecord ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { id} ) ;
151+ return db . getRecord ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , { id} ) ;
150152 }
151153
152154 /**
@@ -161,7 +163,7 @@ export class CoreXAPIOfflineProvider {
161163 async saveStatements ( contextId : number , component : string , statements : string , options ?: CoreXAPIOfflineSaveStatementsOptions )
162164 : Promise < void > {
163165
164- const site = await CoreSites . instance . getSite ( options . siteId ) ;
166+ const db = await this . getSiteDB ( options . siteId ) ;
165167
166168 const entry = {
167169 contextid : contextId ,
@@ -172,7 +174,19 @@ export class CoreXAPIOfflineProvider {
172174 extra : options . extra ,
173175 } ;
174176
175- await site . getDb ( ) . insertRecord ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , entry ) ;
177+ await db . insertRecord ( CoreXAPIOfflineProvider . STATEMENTS_TABLE , entry ) ;
178+ }
179+
180+ /**
181+ * Get Site database when ready.
182+ *
183+ * @param siteId Site id.
184+ * @return SQLiteDB Site database.
185+ */
186+ protected async getSiteDB ( siteId : string ) : Promise < SQLiteDB > {
187+ await this . dbReady ;
188+
189+ return CoreSites . instance . getSiteDb ( siteId ) ;
176190 }
177191}
178192
0 commit comments