npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sdk-base

v4.2.1

Published

a base class for sdk with default error handler

Downloads

552,074

Readme

sdk-base

NPM version Node.js CI Test coverage npm download

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');
  • .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.

License

MIT

Contributors

|dead-horse|fengmk2|gxcsoccer|popomore|sang4lv|luckydrq| | :---: | :---: | :---: | :---: | :---: | :---: | brizer|killagu

This project follows the git-contributor spec, auto updated at Sat Dec 03 2022 16:27:57 GMT+0800.