Skip to content

Commit b6e83ad

Browse files
committed
Use liveQuery for updates
1 parent 945f1fe commit b6e83ad

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/main.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import './style.css'
2-
import Dexie, { type EntityTable } from "dexie";
2+
import Dexie, { type EntityTable, liveQuery } from "dexie";
33

44
interface Note {
55
id: number;
@@ -18,27 +18,30 @@ db.version(1).stores({
1818
notes: "++id, time, text, lat, lon",
1919
});
2020

21-
const form = document.querySelector("form")!;
22-
const posts = document.querySelector("ol")!;
21+
const query = liveQuery(() => db.notes.orderBy("time").reverse().toArray());
2322

24-
async function update() {
25-
const notes = await db.notes.orderBy("time").reverse().toArray();
23+
query.subscribe({
24+
next(notes) {
25+
console.log("Subs", notes);
2626

27-
console.log(notes);
27+
posts.innerHTML = "";
2828

29-
posts.innerHTML = "";
29+
for (const note of notes) {
30+
const li = document.createElement("li");
3031

31-
for (const note of notes) {
32-
const li = document.createElement("li");
32+
const timestamp = new Date(note.time);
33+
li.innerText = `${note.text} (${timestamp.toLocaleString()})`;
3334

34-
const timestamp = new Date(note.time);
35-
li.innerText = `${note.text} (${timestamp.toLocaleString()})`;
36-
37-
posts.appendChild(li);
38-
}
39-
}
35+
posts.appendChild(li);
36+
}
37+
},
38+
error(err) {
39+
console.error(err);
40+
},
41+
});
4042

41-
update();
43+
const form = document.querySelector("form")!;
44+
const posts = document.querySelector("ol")!;
4245

4346
form?.addEventListener("submit", async (e) => {
4447
e.preventDefault();
@@ -68,8 +71,6 @@ form?.addEventListener("submit", async (e) => {
6871

6972
console.log(noteId);
7073

71-
update();
72-
7374
form.reset();
7475
});
7576

0 commit comments

Comments
 (0)