envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
129 lines (120 loc) • 3.19 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Utils from "./Utils.res.mjs";
import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
let Call = {};
let Group = {};
function make() {
return {
groups: {},
isCollecting: false
};
}
async function schedule(loadManager) {
loadManager.isCollecting = true;
await Promise.resolve();
loadManager.isCollecting = false;
let groups = loadManager.groups;
Object.keys(groups).forEach(async key => {
let group = groups[key];
let calls = group.calls;
let inputsToLoad = [];
let currentInputKeys = [];
Utils.Dict.forEachWithKey(calls, (call, inputKey) => {
if (!call.isLoading) {
call.isLoading = true;
currentInputKeys.push(inputKey);
if (!group.hasInMemory(inputKey)) {
inputsToLoad.push(call.input);
return;
} else {
return;
}
}
});
if (!Utils.$$Array.isEmpty(inputsToLoad)) {
try {
await group.load(inputsToLoad, (inputKey, exn) => {
let call = calls[inputKey];
call.exn = exn;
});
} catch (raw_exn) {
let exn = Primitive_exceptions.internalToException(raw_exn);
let exn$1 = Utils.prettifyExn(exn);
currentInputKeys.forEach(inputKey => {
let call = calls[inputKey];
call.exn = exn$1;
});
}
}
if (Utils.$$Array.isEmpty(currentInputKeys)) {
return;
}
currentInputKeys.forEach(inputKey => {
let call = calls[inputKey];
Utils.Dict.deleteInPlace(calls, inputKey);
let exn = call.exn;
if (exn !== undefined) {
return call.reject(Utils.prettifyExn(exn));
} else {
return call.resolve(group.getUnsafeInMemory(inputKey));
}
});
let latestGroup = groups[key];
if (Utils.Dict.isEmpty(latestGroup.calls)) {
return Utils.Dict.deleteInPlace(groups, key);
}
});
}
function noopHasher(input) {
return input;
}
function call(loadManager, input, key, load, hasher, shouldGroup, hasInMemory, getUnsafeInMemory) {
let inputKey = hasher === noopHasher ? input : hasher(input);
if (!shouldGroup && hasInMemory(inputKey)) {
return Promise.resolve(getUnsafeInMemory(inputKey));
}
let group = loadManager.groups[key];
let group$1;
if (group !== undefined) {
group$1 = group;
} else {
let g_calls = {};
let g = {
calls: g_calls,
load: load,
getUnsafeInMemory: getUnsafeInMemory,
hasInMemory: hasInMemory
};
loadManager.groups[key] = g;
group$1 = g;
}
let c = group$1.calls[inputKey];
if (c !== undefined) {
return c.promise;
}
let promise = new Promise((resolve, reject) => {
let call = {
input: input,
resolve: resolve,
reject: reject,
exn: undefined,
promise: null,
isLoading: false
};
group$1.calls[inputKey] = call;
});
group$1.calls[inputKey].promise = promise;
if (!loadManager.isCollecting) {
schedule(loadManager);
}
return promise;
}
export {
Call,
Group,
make,
schedule,
noopHasher,
call,
}
/* Utils Not a pure module */