Skip to content

Commit 0b88e61

Browse files
committed
Logging couldn't work, this fixes.
When I changed undo I failed to take into account logging.
1 parent 8323efb commit 0b88e61

File tree

3 files changed

+75
-49
lines changed

3 files changed

+75
-49
lines changed

src/components/logger.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class Logger extends TreeBase {
8383
second: "numeric",
8484
});
8585
record = { DateTime, ...record };
86-
db.write("log", record);
86+
db.writeLog(record);
8787
}
8888

8989
init() {
@@ -140,8 +140,8 @@ export class Logger extends TreeBase {
140140
}
141141
TreeBase.register(Logger, "Logger");
142142

143-
export async function SaveLogs() {
144-
let toSave = await db.readAll("log");
143+
export async function SaveLog() {
144+
let toSave = await db.readLog();
145145
if (toSave.length > 0) {
146146
await saveContent("log", toSave, "xlsx");
147147
} else {
@@ -150,6 +150,6 @@ export async function SaveLogs() {
150150
}
151151
}
152152

153-
export async function ClearLogs() {
154-
await db.clear("log");
153+
export async function ClearLog() {
154+
await db.clearLog();
155155
}

src/components/toolbar.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { DB } from "app/db";
1616
import { Designer } from "./designer";
1717
import { readSheetFromBlob, saveContent } from "./content";
1818
import { Data } from "app/data";
19-
import { SaveLogs, ClearLogs } from "./logger";
19+
import { SaveLog, ClearLog } from "./logger";
2020
import { friendlyName, wikiName } from "./names";
2121

2222
import { workerUpdateButton } from "components/serviceWorker";
@@ -341,14 +341,14 @@ function getFileMenuItems(bar) {
341341
title: "Save any logs as spreadsheets",
342342
divider: "Logs",
343343
callback: async () => {
344-
SaveLogs();
344+
SaveLog();
345345
},
346346
}),
347347
new MenuItem({
348348
label: "Clear logs",
349349
title: "Clear any stored logs",
350350
callback: async () => {
351-
ClearLogs();
351+
ClearLog();
352352
},
353353
}),
354354
new MenuItem({

src/db.js

Lines changed: 67 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,43 @@ import Globals from "./globals";
55

66
export class DB {
77
constructor() {
8-
this.dbPromise = openDB("os-dpi", 5, {
8+
this.dbPromise = openDB("os-dpi", 6, {
99
async upgrade(db, oldVersion, _newVersion, transaction) {
10-
let store5 = db.createObjectStore("store5", {
11-
keyPath: ["name", "type"],
12-
});
13-
store5.createIndex("by-name", "name");
14-
if (oldVersion == 4) {
15-
// copy data from old store to new
16-
const store4 = transaction.objectStore("store");
17-
for await (const cursor of store4) {
18-
const record4 = cursor.value;
19-
store5.put(record4);
20-
}
21-
db.deleteObjectStore("store");
22-
// add an etag index to url store
23-
transaction.objectStore("url").createIndex("by-etag", "etag");
24-
} else if (oldVersion < 4) {
25-
db.createObjectStore("media");
26-
let savedStore = db.createObjectStore("saved", {
27-
keyPath: "name",
10+
if (oldVersion < 6) {
11+
let logStore = db.createObjectStore("logstore", {
12+
keyPath: "id",
13+
autoIncrement: true,
2814
});
29-
savedStore.createIndex("by-etag", "etag");
30-
// track etags for urls
31-
const urlStore = db.createObjectStore("url", {
32-
keyPath: "url",
15+
logStore.createIndex("by-name", "name");
16+
}
17+
if (oldVersion < 5) {
18+
let store5 = db.createObjectStore("store5", {
19+
keyPath: ["name", "type"],
3320
});
34-
// add an etag index to the url store
35-
urlStore.createIndex("by-etag", "etag");
21+
store5.createIndex("by-name", "name");
22+
if (oldVersion == 4) {
23+
// copy data from old store to new
24+
const store4 = transaction.objectStore("store");
25+
for await (const cursor of store4) {
26+
const record4 = cursor.value;
27+
store5.put(record4);
28+
}
29+
db.deleteObjectStore("store");
30+
// add an etag index to url store
31+
transaction.objectStore("url").createIndex("by-etag", "etag");
32+
} else if (oldVersion < 4) {
33+
db.createObjectStore("media");
34+
let savedStore = db.createObjectStore("saved", {
35+
keyPath: "name",
36+
});
37+
savedStore.createIndex("by-etag", "etag");
38+
// track etags for urls
39+
const urlStore = db.createObjectStore("url", {
40+
keyPath: "url",
41+
});
42+
// add an etag index to the url store
43+
urlStore.createIndex("by-etag", "etag");
44+
}
3645
}
3746
},
3847
blocked(currentVersion, blockedVersion, event) {
@@ -162,16 +171,23 @@ export class DB {
162171
}
163172

164173
/**
165-
* Read all records of the given type
174+
* Read log records
166175
*
167-
* @param {string} type
168176
* @returns {Promise<Object[]>}
169177
*/
170-
async readAll(type) {
171-
return [this.read(type)];
178+
async readLog() {
179+
const db = await this.dbPromise;
180+
const index = db.transaction("logstore", "readonly").store.index("by-name");
181+
const key = this.designName;
182+
const result = [];
183+
for await (const cursor of index.iterate(key)) {
184+
const data = cursor.value.data;
185+
result.push(data);
186+
}
187+
return result;
172188
}
173189

174-
/** Add a new record
190+
/** Write a design record
175191
* @param {string} type
176192
* @param {Object} data
177193
*/
@@ -189,6 +205,16 @@ export class DB {
189205
this.notify({ action: "update", name: this.designName });
190206
}
191207

208+
/** Write a log record
209+
* @param {Object} data
210+
*/
211+
async writeLog(data) {
212+
const db = await this.dbPromise;
213+
const tx = db.transaction(["logstore"], "readwrite");
214+
tx.objectStore("logstore").put({ name: this.designName, data });
215+
await tx.done;
216+
}
217+
192218
/**
193219
* delete records of this type
194220
*
@@ -200,19 +226,19 @@ export class DB {
200226
return db.delete("store5", [this.designName, type]);
201227
}
202228

203-
/** Undo by deleting the most recent record
204-
* @param {string} type
229+
/**
230+
* delete log records
231+
*
232+
* @returns {Promise<void>}
205233
*/
206-
async undo(type) {
207-
if (type == "content") return;
234+
async clearLog() {
208235
const db = await this.dbPromise;
209-
const index = db
210-
.transaction("store5", "readwrite")
211-
.store.index("by-name-type");
212-
const cursor = await index.openCursor([this.designName, type], "prev");
213-
if (cursor) await cursor.delete();
214-
await db.delete("saved", this.designName);
215-
this.notify({ action: "update", name: this.designName });
236+
const tx = db.transaction("logstore", "readwrite");
237+
const index = tx.store.index("by-name");
238+
for await (const cursor of index.iterate(this.designName)) {
239+
cursor.delete();
240+
}
241+
await tx.done;
216242
}
217243

218244
/** Read a design from a local file

0 commit comments

Comments
 (0)