|
|
@@ -9,6 +9,14 @@ import NetInfo from '@react-native-community/netinfo';
|
|
|
let db1: SQLite.SQLiteDatabase | null = null;
|
|
|
let db2: SQLite.SQLiteDatabase | null = null;
|
|
|
let db3: SQLite.SQLiteDatabase | null = null;
|
|
|
+
|
|
|
+type DbKey = 'nm' | 'darePlaces' | 'countries';
|
|
|
+
|
|
|
+const refreshState: Record<DbKey, { isRefreshing: boolean; promise: Promise<void> | null }> = {
|
|
|
+ nm: { isRefreshing: false, promise: null },
|
|
|
+ darePlaces: { isRefreshing: false, promise: null },
|
|
|
+ countries: { isRefreshing: false, promise: null }
|
|
|
+};
|
|
|
const nmRegionsDBname = 'nmRegions.db';
|
|
|
const darePlacesDBname = 'darePlaces.db';
|
|
|
const countriesDBname = 'nmCountries.db';
|
|
|
@@ -16,6 +24,24 @@ const sqliteDirectory = 'SQLite';
|
|
|
const sqliteFullPath = FileSystem.documentDirectory + sqliteDirectory;
|
|
|
const DS = '/';
|
|
|
|
|
|
+export function isNmDbRefreshing(): boolean {
|
|
|
+ return refreshState.nm.isRefreshing;
|
|
|
+}
|
|
|
+
|
|
|
+function runRefresh(key: DbKey, fn: () => Promise<void>): Promise<void> {
|
|
|
+ const state = refreshState[key];
|
|
|
+ if (state.isRefreshing && state.promise) {
|
|
|
+ console.log(`[DB] "${key}" already refreshing, reusing promise`);
|
|
|
+ return state.promise;
|
|
|
+ }
|
|
|
+ state.isRefreshing = true;
|
|
|
+ state.promise = fn().finally(() => {
|
|
|
+ state.isRefreshing = false;
|
|
|
+ state.promise = null;
|
|
|
+ });
|
|
|
+ return state.promise;
|
|
|
+}
|
|
|
+
|
|
|
async function copyDatabaseFile(dbName: string, dbAsset: Asset) {
|
|
|
const state = await NetInfo.fetch();
|
|
|
|
|
|
@@ -166,66 +192,67 @@ const openDatabase = (dbName: string) =>
|
|
|
useNewConnection: true
|
|
|
});
|
|
|
|
|
|
-async function refreshNmDatabase() {
|
|
|
- try {
|
|
|
- await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, { idempotent: true });
|
|
|
- const nmUrl = `${API_HOST}/static/app/${nmRegionsDBname}`;
|
|
|
- let nmFileUri = sqliteFullPath + DS + nmRegionsDBname;
|
|
|
+export function refreshNmDatabase(): Promise<void> {
|
|
|
+ return runRefresh('nm', async () => {
|
|
|
+ try {
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + nmRegionsDBname, { idempotent: true });
|
|
|
+ const nmUrl = `${API_HOST}/static/app/${nmRegionsDBname}`;
|
|
|
+ const nmFileUri = sqliteFullPath + DS + nmRegionsDBname;
|
|
|
+ const nmResponse = await FileSystem.downloadAsync(nmUrl, nmFileUri);
|
|
|
|
|
|
- const nmResponse = await FileSystem.downloadAsync(nmUrl, nmFileUri);
|
|
|
+ if (nmResponse.status !== 200) {
|
|
|
+ console.error(`Failed to download nmDb: Status ${nmResponse.status}`);
|
|
|
+ }
|
|
|
|
|
|
- if (nmResponse.status !== 200) {
|
|
|
- console.error(`Failed to download the nmDb file: Status code ${nmResponse.status}`);
|
|
|
+ db1 = null;
|
|
|
+ db1 = openDatabase(nmRegionsDBname);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('refreshNmDatabase - Error:');
|
|
|
+ console.error(JSON.stringify(error, null, 2));
|
|
|
}
|
|
|
-
|
|
|
- db1 = null;
|
|
|
- db1 = openDatabase(nmRegionsDBname);
|
|
|
- } catch (error) {
|
|
|
- console.error('refreshDatabase nmRegions - Error:');
|
|
|
- console.error(JSON.stringify(error, null, 2));
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-async function refreshDarePlacesDatabase() {
|
|
|
- try {
|
|
|
- await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname, { idempotent: true });
|
|
|
- const dareUrl = `${API_HOST}/static/app/${darePlacesDBname}`;
|
|
|
- let dareFileUri = sqliteFullPath + DS + darePlacesDBname;
|
|
|
+export function refreshDarePlacesDatabase(): Promise<void> {
|
|
|
+ return runRefresh('darePlaces', async () => {
|
|
|
+ try {
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + darePlacesDBname, { idempotent: true });
|
|
|
+ const dareUrl = `${API_HOST}/static/app/${darePlacesDBname}`;
|
|
|
+ const dareFileUri = sqliteFullPath + DS + darePlacesDBname;
|
|
|
+ const dareResponse = await FileSystem.downloadAsync(dareUrl, dareFileUri);
|
|
|
|
|
|
- const dareResponse = await FileSystem.downloadAsync(dareUrl, dareFileUri);
|
|
|
+ if (dareResponse.status !== 200) {
|
|
|
+ console.error(`Failed to download dareDb: Status ${dareResponse.status}`);
|
|
|
+ }
|
|
|
|
|
|
- if (dareResponse.status !== 200) {
|
|
|
- console.error(`Failed to download the dareDb file: Status code ${dareResponse.status}`);
|
|
|
+ db2 = null;
|
|
|
+ db2 = openDatabase(darePlacesDBname);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('refreshDarePlacesDatabase - Error:');
|
|
|
+ console.error(JSON.stringify(error, null, 2));
|
|
|
}
|
|
|
-
|
|
|
- db2 = null;
|
|
|
- db2 = openDatabase(darePlacesDBname);
|
|
|
- } catch (error) {
|
|
|
- console.error('refreshDatabase darePlaces - Error:');
|
|
|
- console.error(JSON.stringify(error, null, 2));
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
-async function refreshCountriesDatabase() {
|
|
|
- try {
|
|
|
- await FileSystem.deleteAsync(sqliteFullPath + DS + countriesDBname, { idempotent: true });
|
|
|
- const countriesUrl = `${API_HOST}/static/app/${countriesDBname}`;
|
|
|
- let countriesFileUri = sqliteFullPath + DS + countriesDBname;
|
|
|
+export function refreshCountriesDatabase(): Promise<void> {
|
|
|
+ return runRefresh('countries', async () => {
|
|
|
+ try {
|
|
|
+ await FileSystem.deleteAsync(sqliteFullPath + DS + countriesDBname, { idempotent: true });
|
|
|
+ const countriesUrl = `${API_HOST}/static/app/${countriesDBname}`;
|
|
|
+ const countriesFileUri = sqliteFullPath + DS + countriesDBname;
|
|
|
+ const countriesResponse = await FileSystem.downloadAsync(countriesUrl, countriesFileUri);
|
|
|
|
|
|
- const countriesResponse = await FileSystem.downloadAsync(countriesUrl, countriesFileUri);
|
|
|
+ if (countriesResponse.status !== 200) {
|
|
|
+ console.error(`Failed to download countriesDb: Status ${countriesResponse.status}`);
|
|
|
+ }
|
|
|
|
|
|
- if (countriesResponse.status !== 200) {
|
|
|
- console.error(
|
|
|
- `Failed to download the countriesDb file: Status code ${countriesResponse.status}`
|
|
|
- );
|
|
|
+ db3 = null;
|
|
|
+ db3 = openDatabase(countriesDBname);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('refreshCountriesDatabase - Error:');
|
|
|
+ console.error(JSON.stringify(error, null, 2));
|
|
|
}
|
|
|
-
|
|
|
- db3 = null;
|
|
|
- db3 = openDatabase(countriesDBname);
|
|
|
- } catch (error) {
|
|
|
- console.error('refreshDatabase nmCountries - Error:');
|
|
|
- console.error(JSON.stringify(error, null, 2));
|
|
|
- }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
export async function updateNmRegionsDb(localLastDate: string) {
|