A base class for sdk with some common & useful functions.
-
.ready(flagOrFunction)
flagOrFunction is optional, and the argument type can be Boolean, Error or Function.
client.ready(true);
client.ready(new Error('init failed'));
client.ready(err => {
if (err) {
console.log('client init failed');
console.error(err);
return;
}
console.log('client is ready');
});
client.ready()
.then(() => { ... })
.catch(err => { ... });
await client.ready();
-
async readyOrTimeout(milliseconds)
ready or timeout, after milliseconds not ready will throw TimeoutError
await client.readyOrTimeout(100);
-
.isReady getter
detect client start ready or not.
-
.on(event, listener)
wrap the EventEmitter.prototype.on(event, listener), the only difference is to support adding async function listener on events, except 'error' event.
-
once(event, listener)
wrap the EventEmitter.prototype.once(event, listener), the only difference is to support adding async function listener on events, except 'error' event.
-
prependListener(event, listener)
wrap the EventEmitter.prototype.prependListener(event, listener), the only difference is to support adding async function listener on events, except 'error' event.
-
prependOnceListener(event, listener)
wrap the EventEmitter.prototype.prependOnceListener(event, listener), the only difference is to support adding generator listener on events, except 'error' event.
-
addListener(event, listener)
wrap the EventEmitter.prototype.addListener(event, listener), the only difference is to support adding async function listener on events, except 'error' event.
client.on('data', async function(data) {
});
client.once('foo', async function(bar) {
});
client.on('error', err => {
console.error(err.stack);
});
-
.await(event)
: await an event, return a promise, and it will resolve(reject if event is error
) once this event emmited.
const data = await client.await('data');
-
.awaitFirst(event)
: await the first event in a set of event pairs, return a promise, and it will clean up after itself.
(async function main() {
const o = await client.awaitFirst([ 'foo', 'bar' ]);
if (o.event === 'foo') {
}
if (o.event === 'bar') {
}
})();
-
._close()
: The _close()
method is called by close
, It can be overridden by child class, but should not be called directly. It must return promise or generator.
-
.close()
: The close()
method is used to close the instance.