sdk-base
Advanced tools
Comparing version 4.0.0 to 4.1.0
4.1.0 / 2022-12-03 | ||
================== | ||
**features** | ||
* [[`6e8a1c4`](http://github.com/node-modules/sdk-base/commit/6e8a1c4707908b28cc30a6019f164544c9033bb7)] - 📦 NEW: Support ready or timeout detect (#20) (fengmk2 <<fengmk2@gmail.com>>) | ||
4.0.0 / 2022-12-03 | ||
@@ -3,0 +9,0 @@ ================== |
@@ -32,2 +32,3 @@ // Type definitions for sdk-base 3.4 | ||
ready(readyCallback: Function): void; | ||
readyOrTimeout(milliseconds: number): Promise<void>; | ||
} |
36
index.js
@@ -7,2 +7,3 @@ const co = require('co'); | ||
const { EventEmitter } = require('events'); | ||
const pTimeout = require('p-timeout'); | ||
const CLOSE_PROMISE = Symbol('base#closePromise'); | ||
@@ -45,3 +46,3 @@ | ||
// support `yield this.await('event')` | ||
// support `await this.await('event')` | ||
this.await = awaitEvent; | ||
@@ -122,3 +123,3 @@ this.awaitFirst = awaitFirst; | ||
// return a promise | ||
// support `this.ready().then(onready);` and `yield this.ready()`; | ||
// support `this.ready().then(onready);` and `await this.ready()`; | ||
return new Promise((resolve, reject) => { | ||
@@ -159,2 +160,33 @@ if (this._ready) { | ||
async readyOrTimeout(milliseconds) { | ||
if (this._ready) { | ||
return; | ||
} else if (this._readyError) { | ||
throw this._readyError; | ||
} | ||
let readyWithTimeoutCallback; | ||
const waitForReady = new Promise((resolve, reject) => { | ||
readyWithTimeoutCallback = err => { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
this._readyCallbacks.push(readyWithTimeoutCallback); | ||
}); | ||
try { | ||
await pTimeout(waitForReady, milliseconds); | ||
} catch (err) { | ||
for (const [ index, callback ] of this._readyCallbacks.entries()) { | ||
if (callback === readyWithTimeoutCallback) { | ||
// remove timeout readyCallback | ||
this._readyCallbacks.splice(index, 1); | ||
break; | ||
} | ||
} | ||
throw err; | ||
} | ||
} | ||
_defaultErrorHandler(err) { | ||
@@ -161,0 +193,0 @@ if (this.listeners('error').length > 1) { |
{ | ||
"name": "sdk-base", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "a base class for sdk with default error handler", | ||
@@ -8,6 +8,7 @@ "main": "index.js", | ||
"lint": "eslint --ext .js .", | ||
"tsd": "tsd", | ||
"test": "npm run lint && npm run test-local", | ||
"test-local": "egg-bin test -r co-mocha", | ||
"cov": "egg-bin cov -r co-mocha", | ||
"ci": "npm run lint && npm run cov", | ||
"ci": "npm run lint && npm run tsd && npm run cov", | ||
"contributor": "git-contributor" | ||
@@ -36,3 +37,4 @@ }, | ||
"await-first": "^1.0.0", | ||
"co": "^4.6.0" | ||
"co": "^4.6.0", | ||
"p-timeout": "^4.1.0" | ||
}, | ||
@@ -49,2 +51,3 @@ "devDependencies": { | ||
"runscript": "^1.3.0", | ||
"tsd": "^0.25.0", | ||
"typescript": "^4.9.3" | ||
@@ -51,0 +54,0 @@ }, |
@@ -94,6 +94,12 @@ sdk-base | ||
// support generator style call | ||
// support async function style call | ||
await client.ready(); | ||
``` | ||
- `async readyOrTimeout(milliseconds)` ready or timeout, after milliseconds not ready will throw TimeoutError | ||
```js | ||
await client.readyOrTimeout(100); | ||
``` | ||
- `.isReady getter` detect client start ready or not. | ||
@@ -100,0 +106,0 @@ - `.on(event, listener)` wrap the [EventEmitter.prototype.on(event, listener)](https://nodejs.org/api/events.html#events_emitter_on_eventname_listener), the only difference is to support adding async function listener on events, except 'error' event. |
20307
245
164
4
11
+ Addedp-timeout@^4.1.0
+ Addedp-timeout@4.1.0(transitive)