5.0.1 • Published 6 months ago

sdk-base v5.0.1

Weekly downloads
68,029
License
MIT
Repository
github
Last release
6 months ago

sdk-base

NPM version Node.js CI Test coverage Known Vulnerabilities npm download Node.js Version

A base class for sdk with some common & useful functions.

Installation

npm install sdk-base

Usage

Constructor argument:

  • {Object} options

    • {String} initMethod - the async init method name, the method should be a function return promise. If set, will execute the function in the constructor.
    • {AsyncLocalStorage} localStorage - async localStorage instance.
    const { Base } = require('sdk-base');
    
    class Client extends Base {
      constructor() {
        super({
          initMethod: 'init',
          localStorage: app.ctxStorage,
        });
      }
    
      async init() {
        // put your async init logic here
      }
      // support async function too
      // async init() {
      //   // put your async init logic here
      // }
    }
    
    (async function main() {
      const client = new Client();
      // wait client ready, if init failed, client will throw an error.
      await client.ready();
    
      // support async event listener
      client.on('data', async function (data) {
        // put your async process logic here
        //
        // @example
        // ----------
        // await submit(data);
      });
    
      client.emit('data', { foo: 'bar' });
    
    })().catch(err => { console.error(err); });

API

  • .ready(flagOrFunction) flagOrFunction is optional, and the argument type can be Boolean, Error or Function.

    // init ready
    client.ready(true);
    // init failed
    client.ready(new Error('init failed'));
    
    // listen client ready
    client.ready(err => {
      if (err) {
        console.log('client init failed');
        console.error(err);
        return;
      }
      console.log('client is ready');
    });
    
    // support promise style call
    client.ready()
      .then(() => { ... })
      .catch(err => { ... });
    
    // support async function style call
    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) {
      // your async process logic here
    });
    client.once('foo', async function(bar) {
      // ...
    });
    
    // listen error event
    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');
  • ._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.

Breaking changes between v4 and v5

  • Drop .awaitFirst(events) support
  • Drop generator function support
  • Don't catch event listener inside error

License

MIT

Contributors

Contributors

Made with contributors-img.

ali-oss-weiyieali-oss-weiyie1sofa-rpc-node2egg-dubbo-rpc2wlite-avsdk-vod-upload-node-userswlite-avsdk-vod-upload-node-v2-uali-oss-weiyiweiyie-test-ts-ali-ossnotespod-ali-oss@infinitebrahmanuniverse/nolb-sdk@everything-registry/sub-chunk-2723egg-grpc-servicetcp-basewil-parserwlite-avsdk-vod-upload-nodewlite-avsdk-vod-upload-node-testxdata-sentinelwtone-nacos-configwtone-nacos-namingwtxiaoman-oss-sdk@bxjs/acm-clientjdwpiotgatewaykagentumaru.jsuki-egg-prometheus@aliengencorp/ali-ons@beajer-test/ali-oss@agile-framework/agile-rocketmq@amber_river/amber_utf-8_test@catalystclan/egg-logger-slsyc-acm-clientyg-view-assetszookeeper-cluster-clientzookeeper-cluster-client-fixzookeeper-querierwatch-fileszk-cluster-client@hemyn/egg-rpc-generator@elephant-wownow/egg-bin-dev@flyhh/nacos-sdk-naming@inner-desktop/ali-oss@luckydrq/tcp-net@extclp/ali-oss@eggjs/dal-runtime@eggjs/tegg-orm-plugin@eggjs/tegg-plugin@eggjs/tegg-controller-plugin@s524797336/ali-oss@pandorajs/messengermy-onsmongodb-clientmini-loggernacos-config-sdknacos-config2nacos-confignacos-config-nodenacos-namingnacos-naming-nodenacos-naming2ossnolookuposs-get-current-dateotsignclipandora-messengerrocketmqserverless-func-sdkpomelo-coresingleton-tsservershimo-ali-onsshimo-rocketmqsofa-lookout-nodesofa-rpc-nodesofa-bolt-nodesofa-registry-nodesocket-messengerinvocakuaihe-ali-oss@unionfab/ali-oss@y2nk4/nacos-config@y2nk4/nacos-namingali-kmsamber_utf-8amber_utf-8_testali-onsali-ons-httpali-ons-sdkali-mcali-ossali-oss-csgali-oss-lcbali-oss-upali-oss-with-react-nativeali-oss-xcbali-oss-reactmaali-oss-sdkacm-clientalionsax-rocketmqantfin-sofa-node-dsr
5.0.1

6 months ago

5.0.0

6 months ago

4.1.0

3 years ago

4.0.0

3 years ago

4.2.1

2 years ago

4.2.0

3 years ago

3.6.0

6 years ago

3.5.1

7 years ago

3.5.0

7 years ago

3.4.0

8 years ago

3.3.0

8 years ago

3.2.0

8 years ago

3.1.1

8 years ago

3.1.0

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.1.0

10 years ago

1.0.1

11 years ago

1.0.0

11 years ago