333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
sdk-base
---------------
 
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![David deps][david-image]][david-url]
[![npm download][download-image]][download-url]
 
[npm-image]: https://img.shields.io/npm/v/sdk-base.svg?style=flat-square
[npm-url]: https://npmjs.org/package/sdk-base
[travis-image]: https://img.shields.io/travis/node-modules/sdk-base.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/sdk-base
[coveralls-image]: https://img.shields.io/coveralls/node-modules/sdk-base.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/node-modules/sdk-base?branch=master
[david-image]: https://img.shields.io/david/node-modules/sdk-base.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/sdk-base
[download-image]: https://img.shields.io/npm/dm/sdk-base.svg?style=flat-square
[download-url]: https://npmjs.org/package/sdk-base
 
 
A base class for sdk with some common & useful functions.
 
## Installation
 
```bash
$ npm install sdk-base
```
 
## Usage
 
Constructor argument:
- {Object} options
  - {String} [initMethod] - the async init method name, the method should be a generator function or a function return promise. If set, will execute the function in the constructor.
 
  ```js
  'use strict';
 
  const co = require('co');
  const Base = require('sdk-base');
 
  class Client extends Base {
    constructor() {
      super({
        initMethod: 'init',
      });
    }
 
    * init() {
      // put your async init logic here
    }
    // support async function too
    // async init() {
    //   // put your async init logic here
    // }
  }
 
  co(function* () {
    const client = new Client();
    // wait client ready, if init failed, client will throw an error.
    yield client.ready();
 
    // support generator event listener
    client.on('data', function* (data) {
      // put your async process logic here
      //
      // @example
      // ----------
      // yield 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.
 
  ```js
  // 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 generator style call
  yield client.ready();
  ```
 
- `.isReady getter` detect client start ready or not.
- `.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 generator listener on events, except 'error' event.
- `once(event, listener)` wrap the [EventEmitter.prototype.once(event, listener)](https://nodejs.org/api/events.html#events_emitter_once_eventname_listener), the only difference is to support adding generator listener on events, except 'error' event.
- `prependListener(event, listener)` wrap the [EventEmitter.prototype.prependListener(event, listener)](https://nodejs.org/api/events.html#events_emitter_prependlistener_eventname_listener), the only difference is to support adding generator listener on events, except 'error' event.
- `prependOnceListener(event, listener)` wrap the [EventEmitter.prototype.prependOnceListener(event, listener)](https://nodejs.org/api/events.html#events_emitter_prependoncelistener_eventname_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)](https://nodejs.org/api/events.html#events_emitter_addlistener_eventname_listener), the only difference is to support adding generator listener on events, except 'error' event.
 
  ```js
  client.on('data', function* (data) {
    // your async process logic here
  });
  client.once('foo', function* (bar) {
    // ...
  });
 
  // listen error event
  client.on('error', err => {
    console.error(err.stack);
  });
  ```
 
- `.await(event)`: [await an event](https://github.com/cojs/await-event), return a promise, and it will resolve(reject if event is `error`) once this event emmited.
 
  ```js
  co(function* () {
    const data = yield client.await('data');
  });
  ```
 
- `.awaitFirst(event)`: [await the first event in a set of event pairs](https://github.com/node-modules/await-first), return a promise, and it will clean up after itself.
 
  ```js
  co(function* () {
    const o = yield 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](LICENSE)