envio
Version:
A latency and sync speed optimized, developer friendly blockchain data indexer.
122 lines (115 loc) • 4.75 kB
JavaScript
// Generated by ReScript, PLEASE EDIT WITH CARE
import * as Table from "./db/Table.res.mjs";
import * as Stdlib_Dict from "@rescript/runtime/lib/es6/Stdlib_Dict.js";
import * as EntityFilter from "./db/EntityFilter.res.mjs";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
function make(parentPort, initialState) {
let proxy = {
parentPort: parentPort,
initialState: initialState,
pendingRequests: {},
requestCounter: 0
};
parentPort.on("message", msg => {
let idStr = msg.id.toString();
let pending = proxy.pendingRequests[idStr];
let match = pending !== undefined ? pending : Stdlib_JsError.throwWithMessage(`TestIndexer: No pending request found for id ` + idStr);
Stdlib_Dict.$$delete(proxy.pendingRequests, idStr);
let match$1 = msg.payload;
if (match$1.type === "response") {
return match.resolve(match$1.data);
} else {
return match.reject(new Error(match$1.message));
}
});
return proxy;
}
function nextRequestId(proxy) {
proxy.requestCounter = proxy.requestCounter + 1 | 0;
return proxy.requestCounter;
}
function sendRequest(proxy, payload) {
return new Promise((resolve, reject) => {
let id = nextRequestId(proxy);
proxy.pendingRequests[id.toString()] = {
resolve: resolve,
reject: reject
};
proxy.parentPort.postMessage({
id: id,
payload: payload
});
});
}
function makeStorage(proxy) {
return {
name: "test-proxy",
isInitialized: async () => true,
initialize: async (param, param$1, param$2, param$3) => Stdlib_JsError.throwWithMessage("TestIndexer: initialize should not be called. Use resumeInitialState instead."),
resumeInitialState: async () => proxy.initialState,
loadOrThrow: async (filter, table) => {
let serializeLeafOrThrow = (fieldName, fieldValue, isArray) => {
let queryField = Table.queryFields(table)[fieldName];
let queryField$1 = queryField !== undefined ? queryField : Stdlib_JsError.throwWithMessage(`TestIndexer: The table "` + table.tableName + `" doesn't have the field "` + fieldName + `"`);
return S$RescriptSchema.reverseConvertToJsonOrThrow(fieldValue, isArray ? queryField$1.arrayFieldSchema : queryField$1.fieldSchema);
};
let response = await sendRequest(proxy, {
type: "load",
tableName: table.tableName,
filter: EntityFilter.mapValues(filter, serializeLeafOrThrow)
});
return S$RescriptSchema.parseOrThrow(response, Table.rowsSchema(table));
},
dumpEffectCache: async () => {},
reset: async () => {},
setChainMeta: async param => {},
pruneStaleCheckpoints: async param => {},
pruneStaleEntityHistory: async (param, param$1, param$2) => {},
getRollbackTargetCheckpoint: async (param, param$1) => Stdlib_JsError.throwWithMessage("TestIndexer: Rollback is not supported. Set rollbackOnReorg to false in config."),
getRollbackProgressDiff: async param => Stdlib_JsError.throwWithMessage("TestIndexer: Rollback is not supported. Set rollbackOnReorg to false in config."),
getRollbackData: async (param, param$1) => Stdlib_JsError.throwWithMessage("TestIndexer: Rollback is not supported. Set rollbackOnReorg to false in config."),
writeBatch: async (batch, param, param$1, param$2, param$3, param$4, updatedEntities, param$5) => {
let serializableEntities = updatedEntities.map(param => {
let entityConfig = param.entityConfig;
let encodeChange = change => {
if (change.type === "SET") {
return {
type: "SET",
entityId: change.entityId,
entity: S$RescriptSchema.reverseConvertToJsonOrThrow(change.entity, entityConfig.schema),
checkpointId: change.checkpointId
};
} else {
return {
type: "DELETE",
entityId: change.entityId,
checkpointId: change.checkpointId
};
}
};
return {
entityName: entityConfig.name,
changes: param.changes.map(encodeChange)
};
});
await sendRequest(proxy, {
type: "writeBatch",
updatedEntities: serializableEntities,
checkpointIds: batch.checkpointIds,
checkpointChainIds: batch.checkpointChainIds,
checkpointBlockNumbers: batch.checkpointBlockNumbers,
checkpointBlockHashes: batch.checkpointBlockHashes,
checkpointEventsProcessed: batch.checkpointEventsProcessed
});
},
close: async () => {}
};
}
export {
make,
nextRequestId,
sendRequest,
makeStorage,
}
/* Table Not a pure module */