diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-11-18 04:37:10 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-11-18 04:37:10 +0100 |
commit | eb9677c374418384f1a96eba95153dc69ebfccc4 (patch) | |
tree | 4ecd9d802403099b2f20ce9d779b40cabd4920ad /src/logging.ts | |
parent | 356ebd2137eb5ca31486ed49ab8223c8256b1554 (diff) |
sequence logging
Diffstat (limited to 'src/logging.ts')
-rw-r--r-- | src/logging.ts | 49 |
1 files changed, 32 insertions, 17 deletions
diff --git a/src/logging.ts b/src/logging.ts index 3f8757a07..16aa851bc 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -20,7 +20,7 @@ * @author Florian Dold */ -import {Store, QueryRoot} from "./query"; +import {Store, QueryRoot, openPromise} from "./query"; export type Level = "error" | "debug" | "info" | "warn"; @@ -126,31 +126,46 @@ export async function getLogs(): Promise<LogEntry[]> { return await new QueryRoot(db).iter(logsStore).toArray(); } +let barrier: any; + export async function record(level: Level, msg: string, source?: string, line?: number, col?: number): Promise<void> { if (typeof indexedDB === "undefined") { return; } - if (!db) { - db = await openLoggingDb(); + + let myBarrier: any; + + if (barrier) { + let p = barrier.promise; + myBarrier = barrier = openPromise(); + await p; + } else { + myBarrier = barrier = openPromise(); } - let count = await new QueryRoot(db).count(logsStore); + try { + if (!db) { + db = await openLoggingDb(); + } - console.log("count is", count); + let count = await new QueryRoot(db).count(logsStore); - if (count > 1000) { - await new QueryRoot(db).deleteIf(logsStore, (e, i) => (i < 200)); - } + if (count > 1000) { + await new QueryRoot(db).deleteIf(logsStore, (e, i) => (i < 200)); + } - let entry: LogEntry = { - timestamp: new Date().getTime(), - level, - msg, - source, - line, - col, - }; - await new QueryRoot(db).put(logsStore, entry); + let entry: LogEntry = { + timestamp: new Date().getTime(), + level, + msg, + source, + line, + col, + }; + await new QueryRoot(db).put(logsStore, entry); + } finally { + await Promise.resolve().then(() => myBarrier.resolve()); + } } const loggingDbVersion = 1; |