Skip to content

Commit

Permalink
fix: hmr update in messenger store
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Trost <[email protected]>
  • Loading branch information
galexrt committed Oct 17, 2024
1 parent f984f7d commit 8d2c4ad
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
25 changes: 23 additions & 2 deletions app/store/calendar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { format } from 'date-fns';
import Dexie, { type Table } from 'dexie';
import 'dexie-observable';
import 'dexie-syncable';
import { defineStore } from 'pinia';
import '~/utils/dexie.ts';
import type { Calendar, CalendarEntry } from '~~/gen/ts/resources/calendar/calendar';
import { RsvpResponses } from '~~/gen/ts/resources/calendar/calendar';
import { NotificationCategory, NotificationType } from '~~/gen/ts/resources/notifications/notifications';
Expand Down Expand Up @@ -39,8 +43,6 @@ export interface CalendarState {
eventReminders: Map<string, number>;
}

// TODO use dexie to persist some state locally

export const useCalendarStore = defineStore('calendar', {
state: () =>
({
Expand Down Expand Up @@ -69,6 +71,9 @@ export const useCalendarStore = defineStore('calendar', {

const now = new Date();
response.entries.forEach((entry) => {
calendarDB.entries.add(entry);
calendarDB.entries.delete(entry);

const startTime = toDate(entry.startTime);
const time = startTime.getTime() - now.getTime();

Expand Down Expand Up @@ -357,6 +362,22 @@ export const useCalendarStore = defineStore('calendar', {
},
});

class CalendarDexie extends Dexie {
calendars!: Table<Calendar>;
entries!: Table<CalendarEntry>;

constructor() {
super('calendar');
this.version(1).stores({
calendars: 'id',
entries: 'id, calendarId',
rsvps: 'entry_id, user_id',
});
}
}

export const calendarDB = new CalendarDexie();

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useCalendarStore, import.meta.hot));
}
8 changes: 4 additions & 4 deletions app/store/messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,6 @@ export const useMessengerStore = defineStore('messenger', {
},
});

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useMessengerStore, import.meta.hot));
}

class MessengerDexie extends Dexie {
threads!: Table<Thread>;
messages!: Table<Message>;
Expand All @@ -240,3 +236,7 @@ class MessengerDexie extends Dexie {
}

export const messengerDB = new MessengerDexie();

if (import.meta.hot) {
import.meta.hot.accept(acceptHMRUpdate(useMessengerStore, import.meta.hot));
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"consola": "3.2.3",
"date-fns": "4.1.0",
"dexie": "4.0.8",
"dexie-observable": "4.0.1-beta.13",
"dexie-syncable": "4.0.1-beta.13",
"emoji-blast": "0.10.1",
"google-protobuf": "3.21.4",
"grpc-web": "1.5.0",
Expand Down
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8d2c4ad

Please sign in to comment.