Skip to content

Commit

Permalink
Convert getProperty calls into properties array.
Browse files Browse the repository at this point in the history
  • Loading branch information
phulin committed Nov 10, 2024
1 parent 6066e0f commit 750bdce
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions packages/tome-kolmafia-lib/src/api/batch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,43 @@ import { getHash } from "./hash";
export async function batchFunction(
functions: readonly { name: string; args: unknown[] }[],
) {
const map = functions.map(({ name, args }) =>
name === "getProperty" && args.length === 1 && typeof args[0] === "string"
? "P"
: "F",
);
const apiFunctions = functions.filter((_, index) => map[index] === "F");
const properties = functions
.filter((_, index) => map[index] === "P")
.map(({ args }) => args[0]) as string[];
const result = await apiCall(
{
functions,
properties,
functions: apiFunctions,
},
await getHash(),
);
if (!result?.functions) {
if (!result?.functions || !result?.properties) {
throw new Error(
result && "error" in result && typeof result.error === "string"
? result.error
: undefined,
: "No functions property in response.",
);
} else if (result.functions.length !== apiFunctions.length) {
throw new Error("Mismatched functions response.");
} else if (result.properties.length !== properties.length) {
throw new Error("Mismatched properties result.");
}

const outFunctions = [];
let functionsIdx = 0;
let propertiesIdx = 0;
for (let i = 0; i < map.length; i++) {
if (map[i] === "P") {
outFunctions.push(result.properties[propertiesIdx++]);
} else if (map[i] === "F") {
outFunctions.push(result.functions[functionsIdx++]);
}
}
return result?.functions;
return outFunctions;
}

0 comments on commit 750bdce

Please sign in to comment.