diff --git a/sdk/typescript/src/node.ts b/sdk/typescript/src/node.ts index 2683ba0..edfb1bc 100644 --- a/sdk/typescript/src/node.ts +++ b/sdk/typescript/src/node.ts @@ -14,6 +14,10 @@ export class Node { resolve(): void; reject(error: any): void; }>(); + private closingWaitingPool = new Set<{ + resolve(): void; + reject(error: any): void; + }>(); private _alive = false; get alive() { return this._alive; @@ -21,7 +25,7 @@ export class Node { private endpoints = new Map; private tempMailbox = new Map>; public textDecoder = new TextDecoder(); - + /** * Create a new Node by connecting to the given URL * @param options options for the connection @@ -84,6 +88,9 @@ export class Node { } socket.onclose = (_evt) => { node._alive = false; + node.closingWaitingPool.forEach((channel) => { + channel.resolve(); + }) } return node; } @@ -112,7 +119,7 @@ export class Node { }) }) } - private waitSocketOpen() { + public waitSocketOpen() { return new Promise((resolve, reject) => { if (this._alive) { resolve(); @@ -124,6 +131,18 @@ export class Node { } }) } + public waitSocketClose() { + return new Promise((resolve, reject) => { + if (!this._alive) { + resolve(); + } else { + this.closingWaitingPool.add({ + resolve, + reject + }) + } + }) + } /** * create a new endpoint * @param topic topic code