Skip to content

Commit

Permalink
move clearing tables into function
Browse files Browse the repository at this point in the history
  • Loading branch information
secondl1ght committed Dec 19, 2023
1 parent 1ba53b3 commit b1feefc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 257 deletions.
44 changes: 3 additions & 41 deletions src/lib/sync/areas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { areaError, areas } from '$lib/store';
import { clearTables } from '$lib/sync/clearTables';
import type { Area } from '$lib/types';
import axios from 'axios';
import axiosRetry from 'axios-retry';
Expand All @@ -9,47 +10,8 @@ axiosRetry(axios, { retries: 3 });
const limit = 500;

export const areasSync = async () => {
// clear v1 table if present
await localforage
.getItem('areas')
.then(function (value) {
if (value) {
localforage
.removeItem('areas')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
areaError.set('Could not clear areas locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
areaError.set('Could not check areas locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v2 table if present
await localforage
.getItem('areas_v2')
.then(function (value) {
if (value) {
localforage
.removeItem('areas_v2')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
areaError.set('Could not clear areas locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
areaError.set('Could not check areas locally, please try again or contact BTC Map.');
console.log(err);
});
// clear tables if present
clearTables(['areas', 'areas_v2']);

// get areas from local
await localforage
Expand Down
23 changes: 23 additions & 0 deletions src/lib/sync/clearTables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import localforage from 'localforage';

export const clearTables = (tables: string[]) => {
for (const table of tables) {
localforage
.getItem(table)
.then(function (value) {
if (value) {
localforage
.removeItem(table)
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
console.log(err);
});
}
})
.catch(function (err) {
console.log(err);
});
}
};
48 changes: 3 additions & 45 deletions src/lib/sync/elements.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { elementError, elements, elementsSyncCount, mapUpdates } from '$lib/store';
import { clearTables } from '$lib/sync/clearTables';
import type { Element } from '$lib/types';
import axios from 'axios';
import axiosRetry from 'axios-retry';
Expand All @@ -10,51 +11,8 @@ axiosRetry(axios, { retries: 3 });
const limit = 5000;

export const elementsSync = async () => {
// clear v1 table if present
await localforage
.getItem('elements')
.then(function (value) {
if (value) {
localforage
.removeItem('elements')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
elementError.set(
'Could not clear elements locally, please try again or contact BTC Map.'
);
console.log(err);
});
}
})
.catch(function (err) {
elementError.set('Could not check elements locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v2 table if present
await localforage
.getItem('elements_v2')
.then(function (value) {
if (value) {
localforage
.removeItem('elements_v2')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
elementError.set(
'Could not clear elements locally, please try again or contact BTC Map.'
);
console.log(err);
});
}
})
.catch(function (err) {
elementError.set('Could not check elements locally, please try again or contact BTC Map.');
console.log(err);
});
// clear tables if present
clearTables(['elements', 'elements_v2']);

// get elements from local
await localforage
Expand Down
44 changes: 3 additions & 41 deletions src/lib/sync/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { eventError, events } from '$lib/store';
import { clearTables } from '$lib/sync/clearTables';
import type { Event } from '$lib/types';
import axios from 'axios';
import axiosRetry from 'axios-retry';
Expand All @@ -9,47 +10,8 @@ axiosRetry(axios, { retries: 3 });
const limit = 50000;

export const eventsSync = async () => {
// clear v1 table if present
await localforage
.getItem('events')
.then(function (value) {
if (value) {
localforage
.removeItem('events')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
eventError.set('Could not clear events locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
eventError.set('Could not check events locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v2 table if present
await localforage
.getItem('events_v2')
.then(function (value) {
if (value) {
localforage
.removeItem('events_v2')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
eventError.set('Could not clear events locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
eventError.set('Could not check events locally, please try again or contact BTC Map.');
console.log(err);
});
// clear tables if present
clearTables(['events', 'events_v2']);

// get events from local
await localforage
Expand Down
71 changes: 3 additions & 68 deletions src/lib/sync/reports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { reportError, reports } from '$lib/store';
import { clearTables } from '$lib/sync/clearTables';
import type { Report } from '$lib/types';
import axios from 'axios';
import axiosRetry from 'axios-retry';
Expand All @@ -9,74 +10,8 @@ axiosRetry(axios, { retries: 3 });
const limit = 20000;

export const reportsSync = async () => {
// clear v1 table if present
await localforage
.getItem('reports')
.then(function (value) {
if (value) {
localforage
.removeItem('reports')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
reportError.set(
'Could not clear reports locally, please try again or contact BTC Map.'
);
console.log(err);
});
}
})
.catch(function (err) {
reportError.set('Could not check reports locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v2 table if present
await localforage
.getItem('reports_v2')
.then(function (value) {
if (value) {
localforage
.removeItem('reports_v2')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
reportError.set(
'Could not clear reports locally, please try again or contact BTC Map.'
);
console.log(err);
});
}
})
.catch(function (err) {
reportError.set('Could not check reports locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v3 table if present
await localforage
.getItem('reports_v3')
.then(function (value) {
if (value) {
localforage
.removeItem('reports_v3')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
reportError.set(
'Could not clear reports locally, please try again or contact BTC Map.'
);
console.log(err);
});
}
})
.catch(function (err) {
reportError.set('Could not check reports locally, please try again or contact BTC Map.');
console.log(err);
});
// clear tables if present
clearTables(['reports', 'reports_v2', 'reports_v3']);

// get reports from local
await localforage
Expand Down
65 changes: 3 additions & 62 deletions src/lib/sync/users.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { userError, users } from '$lib/store';
import { clearTables } from '$lib/sync/clearTables';
import type { User } from '$lib/types';
import axios from 'axios';
import axiosRetry from 'axios-retry';
Expand All @@ -9,68 +10,8 @@ axiosRetry(axios, { retries: 3 });
const limit = 7500;

export const usersSync = async () => {
// clear potentially broken users v1 sync due to top level ID changing from string to int
await localforage
.getItem('users')
.then(function (value) {
if (value) {
localforage
.removeItem('users')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
userError.set('Could not clear users locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
userError.set('Could not check users locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v2 table if present
await localforage
.getItem('users_v2')
.then(function (value) {
if (value) {
localforage
.removeItem('users_v2')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
userError.set('Could not clear users locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
userError.set('Could not check users locally, please try again or contact BTC Map.');
console.log(err);
});

// clear v3 table if present
await localforage
.getItem('users_v3')
.then(function (value) {
if (value) {
localforage
.removeItem('users_v3')
.then(function () {
console.log('Key is cleared!');
})
.catch(function (err) {
userError.set('Could not clear users locally, please try again or contact BTC Map.');
console.log(err);
});
}
})
.catch(function (err) {
userError.set('Could not check users locally, please try again or contact BTC Map.');
console.log(err);
});
// clear tables if present
clearTables(['users', 'users_v2', 'users_v3']);

// get users from local
await localforage
Expand Down

0 comments on commit b1feefc

Please sign in to comment.