Skip to content

Commit

Permalink
Add cache of ContractInstanceAPIs
Browse files Browse the repository at this point in the history
  • Loading branch information
hrajchert authored and nhenin committed Apr 18, 2024
1 parent 7634a43 commit 3a29a1b
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/runtime/lifecycle/src/generic/new-contract-api.ts
Expand Up @@ -55,14 +55,21 @@ export interface ContractsAPI {
}

export function mkContractsAPI(di: ContractsDI): ContractsAPI {
// TODO: Cache of API's
// The ContractInstance API is stateful as it has some cache, so whenever
// possible we want to reuse the same instance of the API for the same contractId
const apis = new Map<ContractId, ContractInstanceAPI>();
return {
createContract: async (request) => {
const [contractId, _] = await createContract(di)(request);
return mkContractInstanceAPI(di, contractId);
apis.set(contractId, mkContractInstanceAPI(di, contractId));
return apis.get(contractId)!;
},
loadContract: async (contractId) => {
return mkContractInstanceAPI(di, contractId);
if (apis.has(contractId)) {
return apis.get(contractId)!;
} else {
return mkContractInstanceAPI(di, contractId);
}
},
};
}
Expand Down

0 comments on commit 3a29a1b

Please sign in to comment.