Skip to content

Commit

Permalink
feat: implemented an FileTree serializer/de-serializer for flatteni…
Browse files Browse the repository at this point in the history
…ng `FileTree`s over a raw RPC stream
  • Loading branch information
tegefaulkes committed Jul 25, 2024
1 parent 2a88416 commit 262b213
Show file tree
Hide file tree
Showing 9 changed files with 1,579 additions and 586 deletions.
39 changes: 39 additions & 0 deletions src/vaults/VaultManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,45 @@ class VaultManager {
});
}

/**
* Takes a generator and runs it with the listed vaults. locking is handled automatically
* @param vaultIds List of vault ID for vaults you wish to use
* @param g Generator you wish to run with the provided vaults
* @param tran
*/
@ready(new vaultsErrors.ErrorVaultManagerNotRunning())
public async *withVaultsG<T, Treturn, Tnext>(
vaultIds: Array<VaultId>,
g: (...args: Array<Vault>) => AsyncGenerator<T, Treturn, Tnext>,
tran?: DBTransaction,
): AsyncGenerator<T, Treturn, Tnext> {
if (tran == null) {
return yield* this.db.withTransactionG((tran) =>
this.withVaultsG(vaultIds, g, tran),
);
}

// Obtaining locks
const vaultLocks: Array<LockRequest<RWLockWriter>> = vaultIds.map(
(vaultId) => {
return [vaultId.toString(), RWLockWriter, 'read'];
},
);
// Running the function with locking
return yield* this.vaultLocks.withG(
...vaultLocks,
async function* (): AsyncGenerator<T, Treturn, Tnext> {
// Getting the vaults while locked
const vaults = await Promise.all(
vaultIds.map(async (vaultId) => {
return await this.getVault(vaultId, tran);
}),
);
return yield* g(...vaults);
},
);
}

protected async setupKey(tran: DBTransaction): Promise<Buffer> {
let key: Buffer | undefined;
key = await tran.get([...this.vaultsDbPath, 'key'], true);
Expand Down
Loading

0 comments on commit 262b213

Please sign in to comment.