116 lines
3.6 KiB
JavaScript
116 lines
3.6 KiB
JavaScript
// Metro module 1917 (f1918).
|
|
import { Logger } from "./log";
|
|
import { KV_RAW } from "./Module1909";
|
|
let c1604_2 = 1000000;
|
|
let c1604_3 = globalThis.process.env.KV_STORAGE_LOGGING === "1";
|
|
let c1604_4 = new Logger("Runtime");
|
|
export class Runtime {
|
|
static counter = 0;
|
|
static pending = new Map();
|
|
static initialized = false;
|
|
static dbStateCallbacks = [];
|
|
static completionCallbacks = [];
|
|
constructor() {
|
|
return {
|
|
__proto__: new.target.prototype
|
|
};
|
|
}
|
|
static nextId() {
|
|
const counter = this.counter + 1;
|
|
this.counter = counter;
|
|
return counter;
|
|
}
|
|
static executeAsync(tag, c13323_0) {
|
|
this.initialize();
|
|
return new Promise((a0, a1) => {
|
|
const id = this.nextId();
|
|
c13323_0(id);
|
|
this.pending.set(id, {
|
|
id,
|
|
tag,
|
|
started: performance.now(),
|
|
resolve: a0,
|
|
reject: a1
|
|
});
|
|
});
|
|
}
|
|
static addCompletionCallback(a0) {
|
|
this.completionCallbacks.push(a0);
|
|
return a0;
|
|
}
|
|
static addDatabaseStateCallback(a0) {
|
|
this.dbStateCallbacks.push(a0);
|
|
return a0;
|
|
}
|
|
static removeCompletionCallback(c13324_0) {
|
|
this.completionCallbacks = this.completionCallbacks.filter((completionCallback) => completionCallback !== c13324_0);
|
|
}
|
|
static removeDatabaseStateCallback(c13325_0) {
|
|
this.dbStateCallbacks = this.dbStateCallbacks.filter((dbStateCallback) => dbStateCallback !== c13325_0);
|
|
}
|
|
static onResponse(a0, a1) {
|
|
const r6 = performance.now();
|
|
const r13 = this.pending.get(a0.id);
|
|
if (r13 != null) {
|
|
let materializationTimeNanoseconds = a1;
|
|
this.pending.delete(a0.id);
|
|
const timings = a0.timings;
|
|
if (a1 == null) {
|
|
materializationTimeNanoseconds = 0;
|
|
}
|
|
timings.materializationTimeNanoseconds = materializationTimeNanoseconds;
|
|
this.completeOperation(r13, a0, r6);
|
|
this.resolveOperation(r13, a0);
|
|
}
|
|
}
|
|
static onStatus(a0) {
|
|
for (const dbStateCallback of this.dbStateCallbacks) {
|
|
dbStateCallback(a0.handle, a0.state);
|
|
}
|
|
}
|
|
static resolveOperation(a0, a1) {
|
|
if (a1.ok) {
|
|
a0.resolve(a1.data);
|
|
} else {
|
|
a0.reject.call(a0, typeof a1.data === "string" ? new Error(a1.data) : a1.data);
|
|
}
|
|
}
|
|
static completeOperation(a0, a1, a2) {
|
|
if (this.completionCallbacks.length > 0) {
|
|
const r3 = {
|
|
id: a0.id,
|
|
tag: a0.tag,
|
|
ok: a1.ok,
|
|
value: a1.data,
|
|
timings: {
|
|
queue: a1.timings.queueTimeNanoseconds / c1604_2,
|
|
execution: a1.timings.executionTimeNanoseconds / c1604_2,
|
|
materialization: a1.timings.materializationTimeNanoseconds / c1604_2,
|
|
ccTotal: a1.timings.totalTimeNanoseconds / c1604_2,
|
|
jsTotal: a2 - a0.started
|
|
}
|
|
};
|
|
for (const completionCallback of this.completionCallbacks) {
|
|
completionCallback(r3);
|
|
}
|
|
}
|
|
}
|
|
static initialize() {
|
|
if (!this.initialized) {
|
|
KV_RAW.setCallbacks({
|
|
status: (a0) => this.onStatus(a0),
|
|
response: (a0, a1) => this.onResponse(a0, a1)
|
|
});
|
|
if (c1604_3) {
|
|
this.addCompletionCallback((a0) => {
|
|
const r15 = a0.ok ? "completed" : "failed";
|
|
const r17 = [`${a0.timings.execution.toFixed(3)}ms execution`, `${a0.timings.materialization.toFixed(3)}ms js materialization`, `${a0.timings.ccTotal.toFixed(3)}ms cc completion`, `${a0.timings.jsTotal.toFixed(3)}ms js reception`].join(", ");
|
|
c1604_4.info(`${a0.tag} (#${a0.id}) ${r15} in ${a0.timings.ccTotal.toFixed(3)}ms (${r17}).`);
|
|
});
|
|
this.addDatabaseStateCallback((a0, a1) => c1604_4.info(`${a0} (state: ${a1})`));
|
|
}
|
|
this.initialized = true;
|
|
}
|
|
}
|
|
}
|