Skip to content

Commit

Permalink
dont cache deleted records
Browse files Browse the repository at this point in the history
  • Loading branch information
secondl1ght committed Jan 22, 2024
1 parent 11536a8 commit 9e28da4
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 83 deletions.
26 changes: 11 additions & 15 deletions src/lib/sync/areas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const limit = 500;

export const areasSync = async () => {
// clear tables if present
clearTables(['areas', 'areas_v2']);
clearTables(['areas', 'areas_v2', 'areas_v3']);

// get areas from local
await localforage
.getItem<Area[]>('areas_v3')
.getItem<Area[]>('areas_v4')
.then(async function (value) {
// get areas from API if initial sync
if (!value) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export const areasSync = async () => {

// set response to local
localforage
.setItem('areas_v3', areasData)
.setItem('areas_v4', areasFiltered)
.then(function () {
// set response to store
areas.set(areasFiltered);
Expand All @@ -61,9 +61,6 @@ export const areasSync = async () => {
});
}
} else {
// filter out deleted areas
const areasFiltered = value.filter((area) => !area['deleted_at']);

// start update sync from API
// sort to get most recent record
const cacheSorted = [...value];
Expand Down Expand Up @@ -99,17 +96,19 @@ export const areasSync = async () => {

// add new areas
newAreas.forEach((area) => {
areasData.push(area);
if (!area['deleted_at']) {
areasData.push(area);
}
});
} else {
// load areas from cache
areas.set(areasFiltered);
areas.set(value);
useCachedData = true;
break;
}
} catch (error) {
// load areas from cache
areas.set(areasFiltered);
areas.set(value);
useCachedData = true;

areaError.set('Could not update areas from API, please try again or contact BTC Map.');
Expand All @@ -119,19 +118,16 @@ export const areasSync = async () => {
} while (responseCount === limit);

if (!useCachedData) {
// filter out deleted areas
const newAreasFiltered = areasData.filter((area) => !area['deleted_at']);

// set updated areas locally
localforage
.setItem('areas_v3', areasData)
.setItem('areas_v4', areasData)
.then(function () {
// set updated areas to store
areas.set(newAreasFiltered);
areas.set(areasData);
})
.catch(function (err) {
// set updated areas to store
areas.set(newAreasFiltered);
areas.set(areasData);

areaError.set('Could not update areas locally, please try again or contact BTC Map.');
console.log(err);
Expand Down
24 changes: 16 additions & 8 deletions src/lib/sync/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ const limit = 5000;

export const elementsSync = async () => {
// clear tables if present
clearTables(['elements', 'elements_v2']);
clearTables(['elements', 'elements_v2', 'elements_v3']);

// get elements from local
await localforage
.getItem<Element[]>('elements_v3')
.getItem<Element[]>('elements_v4')
.then(async function (value) {
// get elements from API if initial sync
if (!value) {
Expand Down Expand Up @@ -51,15 +51,18 @@ export const elementsSync = async () => {
} while (responseCount === limit);

if (elementsData.length) {
// filter out deleted elements
const elementsFiltered = elementsData.filter((element) => !element['deleted_at']);

// set response to local
localforage
.setItem('elements_v3', elementsData)
.setItem('elements_v4', elementsFiltered)
.then(function () {
// set response to store
elements.set(elementsData);
elements.set(elementsFiltered);
})
.catch(function (err) {
elements.set(elementsData);
elements.set(elementsFiltered);
elementError.set(
'Could not store elements locally, please try again or contact BTC Map.'
);
Expand Down Expand Up @@ -106,7 +109,9 @@ export const elementsSync = async () => {

// add new elements
newElements.forEach((element) => {
elementsData.push(element);
if (!element['deleted_at']) {
elementsData.push(element);
}
});
} else {
// set cached elements to store
Expand All @@ -130,7 +135,7 @@ export const elementsSync = async () => {
if (!useCachedData) {
// set updated elements locally
localforage
.setItem('elements_v3', elementsData)
.setItem('elements_v4', elementsData)
.then(function () {
// set updated elements to store
elements.set(elementsData);
Expand Down Expand Up @@ -189,8 +194,11 @@ export const elementsSync = async () => {
} while (responseCount === limit);

if (elementsData.length) {
// filter out deleted elements
const elementsFiltered = elementsData.filter((element) => !element['deleted_at']);

// set response to store
elements.set(elementsData);
elements.set(elementsFiltered);
}
});
};
26 changes: 11 additions & 15 deletions src/lib/sync/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const limit = 50000;

export const eventsSync = async () => {
// clear tables if present
clearTables(['events', 'events_v2']);
clearTables(['events', 'events_v2', 'events_v3']);

// get events from local
await localforage
.getItem<Event[]>('events_v3')
.getItem<Event[]>('events_v4')
.then(async function (value) {
// get events from API if initial sync
if (!value) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export const eventsSync = async () => {

// set response to local
localforage
.setItem('events_v3', eventsData)
.setItem('events_v4', eventsFiltered)
.then(function () {
// set response to store
events.set(eventsFiltered);
Expand All @@ -63,9 +63,6 @@ export const eventsSync = async () => {
});
}
} else {
// filter out deleted events
const eventsFiltered = value.filter((event) => !event['deleted_at']);

// start update sync from API
// sort to get most recent record
const cacheSorted = [...value];
Expand Down Expand Up @@ -101,17 +98,19 @@ export const eventsSync = async () => {

// add new events
newEvents.forEach((event) => {
eventsData.push(event);
if (!event['deleted_at']) {
eventsData.push(event);
}
});
} else {
// load events from cache
events.set(eventsFiltered);
events.set(value);
useCachedData = true;
break;
}
} catch (error) {
// load events from cache
events.set(eventsFiltered);
events.set(value);
useCachedData = true;

eventError.set(
Expand All @@ -123,19 +122,16 @@ export const eventsSync = async () => {
} while (responseCount === limit);

if (!useCachedData) {
// filter out deleted events
const newEventsFiltered = eventsData.filter((event) => !event['deleted_at']);

// set updated events locally
localforage
.setItem('events_v3', eventsData)
.setItem('events_v4', eventsData)
.then(function () {
// set updated events to store
events.set(newEventsFiltered);
events.set(eventsData);
})
.catch(function (err) {
// set updated events to store
events.set(newEventsFiltered);
events.set(eventsData);

eventError.set(
'Could not update events locally, please try again or contact BTC Map.'
Expand Down
26 changes: 11 additions & 15 deletions src/lib/sync/reports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const limit = 20000;

export const reportsSync = async () => {
// clear tables if present
clearTables(['reports', 'reports_v2', 'reports_v3', 'reports_v4']);
clearTables(['reports', 'reports_v2', 'reports_v3', 'reports_v4', 'reports_v5']);

// get reports from local
await localforage
.getItem<Report[]>('reports_v5')
.getItem<Report[]>('reports_v6')
.then(async function (value) {
// get reports from API if initial sync
if (!value) {
Expand Down Expand Up @@ -51,7 +51,7 @@ export const reportsSync = async () => {

// set response to local
localforage
.setItem('reports_v5', reportsData)
.setItem('reports_v6', reportsFiltered)
.then(function () {
// set response to store
reports.set(reportsFiltered);
Expand All @@ -65,9 +65,6 @@ export const reportsSync = async () => {
});
}
} else {
// filter out deleted reports
const reportsFiltered = value.filter((report) => !report['deleted_at']);

// start update sync from API
// sort to get most recent record
const cacheSorted = [...value];
Expand Down Expand Up @@ -103,17 +100,19 @@ export const reportsSync = async () => {

// add new reports
newReports.forEach((report) => {
reportsData.push(report);
if (!report['deleted_at']) {
reportsData.push(report);
}
});
} else {
// load reports from cache
reports.set(reportsFiltered);
reports.set(value);
useCachedData = true;
break;
}
} catch (error) {
// load reports from cache
reports.set(reportsFiltered);
reports.set(value);
useCachedData = true;

reportError.set(
Expand All @@ -125,19 +124,16 @@ export const reportsSync = async () => {
} while (responseCount === limit);

if (!useCachedData) {
// filter out deleted reports
const newReportsFiltered = reportsData.filter((report) => !report['deleted_at']);

// set updated reports locally
localforage
.setItem('reports_v5', reportsData)
.setItem('reports_v6', reportsData)
.then(function () {
// set updated reports to store
reports.set(newReportsFiltered);
reports.set(reportsData);
})
.catch(function (err) {
// set updated reports to store
reports.set(newReportsFiltered);
reports.set(reportsData);

reportError.set(
'Could not update reports locally, please try again or contact BTC Map.'
Expand Down
26 changes: 11 additions & 15 deletions src/lib/sync/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const limit = 7500;

export const usersSync = async () => {
// clear tables if present
clearTables(['users', 'users_v2', 'users_v3']);
clearTables(['users', 'users_v2', 'users_v3', 'users_v4']);

// get users from local
await localforage
.getItem<User[]>('users_v4')
.getItem<User[]>('users_v5')
.then(async function (value) {
// get users from API if initial sync
if (!value) {
Expand Down Expand Up @@ -49,7 +49,7 @@ export const usersSync = async () => {

// set response to local
localforage
.setItem('users_v4', usersData)
.setItem('users_v5', usersFiltered)
.then(function () {
// set response to store
users.set(usersFiltered);
Expand All @@ -61,9 +61,6 @@ export const usersSync = async () => {
});
}
} else {
// filter out deleted users
const usersFiltered = value.filter((user) => !user['deleted_at']);

// start update sync from API
// sort to get most recent record
const cacheSorted = [...value];
Expand Down Expand Up @@ -99,17 +96,19 @@ export const usersSync = async () => {

// add new users
newUsers.forEach((user) => {
usersData.push(user);
if (!user['deleted_at']) {
usersData.push(user);
}
});
} else {
// load users from cache
users.set(usersFiltered);
users.set(value);
useCachedData = true;
break;
}
} catch (error) {
// load users from cache
users.set(usersFiltered);
users.set(value);
useCachedData = true;

userError.set('Could not update users from API, please try again or contact BTC Map.');
Expand All @@ -119,19 +118,16 @@ export const usersSync = async () => {
} while (responseCount === limit);

if (!useCachedData) {
// filter out deleted users
const newUsersFiltered = usersData.filter((user) => !user['deleted_at']);

// set updated users locally
localforage
.setItem('users_v4', usersData)
.setItem('users_v5', usersData)
.then(function () {
// set updated users to store
users.set(newUsersFiltered);
users.set(usersData);
})
.catch(function (err) {
// set updated users to store
users.set(newUsersFiltered);
users.set(usersData);

userError.set('Could not update users locally, please try again or contact BTC Map.');
console.log(err);
Expand Down
4 changes: 0 additions & 4 deletions src/routes/community/[area]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,6 @@
// add elements to map
filteredElements.forEach((element) => {
if (element['deleted_at']) {
return;
}
let icon = element.tags['icon:android'];
let payment = element.tags['payment:uri']
? { type: 'uri', url: element.tags['payment:uri'] }
Expand Down
Loading

0 comments on commit 9e28da4

Please sign in to comment.