Change datastore to not need to deserialize LocalStorage for every read

This commit is contained in:
2025-07-06 15:29:51 +02:00
parent ab9f192f6f
commit ca35dbf92b
5 changed files with 93 additions and 74 deletions

View File

@@ -1,89 +1,109 @@
export class LocationsDataStore {
static rememberAll(location_list) {
let locations = LocationsDataStore.get();
for (let location of location_list) {
locations[location.id] = location;
}
LocationsDataStore.set(locations);
constructor() {
this.data = {};
this.read();
}
static rememberAllIfNotExist(location_list) {
let locations = LocationsDataStore.get();
read() {
this.data = DataStore.get("locations") || {};
}
write() {
DataStore.set("locations", this.data);
}
rememberAll(location_list) {
for (let location of location_list) {
if (!(location.id in locations)) {
locations[location.id] = location;
this.data[location.id] = location;
}
this.write();
}
rememberAllIfNotExist(location_list) {
for (let location of location_list) {
if (!(location.id in this.data)) {
this.data[location.id] = location;
}
}
LocationsDataStore.set(locations);
}
static get() {
return DataStore.get("locations") || {};
}
static set(value) {
DataStore.set("locations", value);
this.write();
}
}
export class RecentLocationsDataStore {
static remember(location_id) {
let recent_locations = RecentLocationsDataStore.get();
recent_locations = recent_locations.filter((item) => {
constructor() {
this.data = [];
this.read();
}
read() {
this.data = DataStore.get("recent-locations") || [];
}
write() {
DataStore.set("recent-locations", this.data);
}
remember(location_id) {
this.data = this.data.filter((item) => {
return item != location_id;
});
recent_locations.push(location_id);
RecentLocationsDataStore.set(recent_locations);
}
this.data.push(location_id);
static get() {
return DataStore.get("recent-locations") || [];
}
static set(value) {
DataStore.set("recent-locations", value);
this.write();
}
}
export class TrackedTripsDataStore {
static remember(trip_id, origin_id, destination_id) {
let tracked_trips = TrackedTripsDataStore.get();
tracked_trips.push({
constructor() {
this.data = [];
this.read();
}
read() {
this.data = DataStore.get("tracked-trips") || [];
}
write() {
DataStore.set("tracked-trips", this.data);
}
remember(trip_id, origin_id, destination_id) {
this.data.push({
trip: trip_id,
origin: origin_id,
destination: destination_id,
});
TrackedTripsDataStore.set(tracked_trips);
}
static get() {
return DataStore.get("tracked-trips") || [];
}
static set(value) {
DataStore.set("tracked-trips", value);
this.write();
}
}
export class TripsDataStore {
static remember(trip) {
TripsDataStore.rememberAll([trip]);
constructor() {
this.data = {};
this.read();
}
static rememberAll(trip_list) {
let tracked_trips = TripsDataStore.get();
read() {
this.data = DataStore.get("trips") || {};
}
write() {
DataStore.set("trips", this.data);
}
remember(trip) {
this.rememberAll([trip]);
}
rememberAll(trip_list) {
for (let trip of trip_list) {
tracked_trips[trip.id] = trip;
this.data[trip.id] = trip;
}
TripsDataStore.set(tracked_trips);
}
static get() {
return DataStore.get("trips") || {};
}
static set(value) {
DataStore.set("trips", value);
this.write();
}
}
@@ -96,8 +116,10 @@ export class DataStore {
window.localStorage.setItem(key, JSON.stringify(value));
}
static locations = LocationsDataStore;
static recent_locations = RecentLocationsDataStore;
static tracked_trips = TrackedTripsDataStore;
static trips = TripsDataStore;
constructor() {
this.locations = new LocationsDataStore();
this.recent_locations = new RecentLocationsDataStore();
this.tracked_trips = new TrackedTripsDataStore();
this.trips = new TripsDataStore();
}
}