sequence logging

This commit is contained in:
Florian Dold 2016-11-18 04:37:10 +01:00
parent 356ebd2137
commit eb9677c374
2 changed files with 33 additions and 18 deletions

View File

@ -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);
} finally {
await Promise.resolve().then(() => myBarrier.resolve());
}
let entry: LogEntry = {
timestamp: new Date().getTime(),
level,
msg,
source,
line,
col,
};
await new QueryRoot(db).put(logsStore, entry);
}
const loggingDbVersion = 1;

View File

@ -86,7 +86,7 @@ export let AbortTransaction = Symbol("abort_transaction");
* Get an unresolved promise together with its extracted resolve / reject
* function.
*/
function openPromise<T>() {
export function openPromise<T>() {
let resolve: ((value?: T | PromiseLike<T>) => void) | null = null;
let reject: ((reason?: any) => void) | null = null;
const promise = new Promise<T>((res, rej) => {