Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Merge #176
Browse files Browse the repository at this point in the history
176: Add close method for bcoin wallet r=mergify[bot] a=luckysori

These are a couple of patches that need to be released before comit-network/comit-rs#2330 can be merged in.

Co-authored-by: Lucas Soriano del Pino <l.soriano.del.pino@gmail.com>
  • Loading branch information
bors[bot] and luckysori committed Mar 30, 2020
2 parents 6b0fd72 + b81982d commit 334fbd6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Method to close `InMemoryBitcoinWallet`.

## [0.15.1] - 2020-03-26

### Fixed
Expand Down Expand Up @@ -62,7 +65,7 @@ The SDK now recognizes this: Promises returned from calls on `Cnd` will now reje
## [0.11.1] - 2020-02-19

### Fixed
- Do not gitignore the `dist/` folder to ensure it is included in the published package, add a CI test to test it.
- Do not gitignore the `dist/` folder to ensure it is included in the published package, add a CI test to test it.

## [0.11.0] - 2020-02-19

Expand Down
13 changes: 9 additions & 4 deletions src/util/timeout_promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ export async function timeoutPromise<T>(
ms: number,
promise: Promise<T>
): Promise<T> {
let id: NodeJS.Timeout;

// Create a promise that rejects in <ms> milliseconds
const timeout = new Promise<T>((_, reject) => {
const id = setTimeout(() => {
clearTimeout(id);
reject(`Timed out in ${ms}ms.`);
id = setTimeout(() => {
reject(new Error(`timed out after ${ms}ms`));
}, ms);
});

return Promise.race([promise, timeout]);
// Returns a race between our timeout and the passed in promise
return Promise.race([promise, timeout]).finally(() => {
clearTimeout(id);
});
}

export async function sleep(ms: number): Promise<void> {
Expand Down
19 changes: 17 additions & 2 deletions src/wallet/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ export class InMemoryBitcoinWallet implements BitcoinWallet {
private constructor(
public readonly network: any,

// @ts-ignore
private readonly walletdb: any,
private readonly pool: any,

// @ts-ignore
private readonly chain: any,
private readonly wallet: any
) {}
Expand Down Expand Up @@ -170,6 +168,23 @@ export class InMemoryBitcoinWallet implements BitcoinWallet {
return "150";
}

public async close(): Promise<void> {
const tasks = [];
if (this.pool.opened) {
tasks.push(this.pool.close());
}

if (this.chain.opened) {
tasks.push(this.chain.close());
}

if (this.walletdb.opened) {
tasks.push(this.walletdb.close());
}

await Promise.all(tasks);
}

private assertNetwork(network: string): void {
if (network !== this.network.type) {
throw new Error(
Expand Down

0 comments on commit 334fbd6

Please sign in to comment.