@jnode/request 2.0.0-beta.0 → 2.0.0-beta.1-doc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -0
- package/package.json +1 -1
- package/src/index.js +53 -0
package/README.md
CHANGED
|
@@ -150,3 +150,16 @@ The response stream or any readable stream.
|
|
|
150
150
|
- Type: [\<readline.Interface\>](https://nodejs.org/docs/latest/api/readline.html#class-readlineinterface)
|
|
151
151
|
|
|
152
152
|
An interface for reading data from response one line at a time.
|
|
153
|
+
|
|
154
|
+
### `eventReceiver[Symbol.asyncIterator]()`
|
|
155
|
+
|
|
156
|
+
- Returns: [\<AsyncIterator\>](https://tc39.github.io/ecma262/#sec-asynciterator-interface)
|
|
157
|
+
|
|
158
|
+
Create an AsyncIterator object that iterates through each event as an object. This method allows asynchronous iteration of InterfaceConstructor objects through for await...of loops.
|
|
159
|
+
|
|
160
|
+
```js
|
|
161
|
+
for await (const event of eventReceiver) {
|
|
162
|
+
// Each event in the event stream will be successively available here as
|
|
163
|
+
// { data, event, id }
|
|
164
|
+
}
|
|
165
|
+
```
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -196,6 +196,59 @@ class EventReceiver extends EventEmitter {
|
|
|
196
196
|
});
|
|
197
197
|
|
|
198
198
|
this.rl.on('close', () => { this.emit('close'); });
|
|
199
|
+
this.rl.on('error', (err) => {
|
|
200
|
+
this.emit('error', err);
|
|
201
|
+
this.emit('close');
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// async generator support
|
|
206
|
+
[Symbol.asyncIterator]() {
|
|
207
|
+
let queue = [];
|
|
208
|
+
let done = false;
|
|
209
|
+
let resolvers = [];
|
|
210
|
+
|
|
211
|
+
// event
|
|
212
|
+
const onEvent = (event) => {
|
|
213
|
+
if (resolvers.length > 0) {
|
|
214
|
+
resolvers.shift()({ value: event, done: false });
|
|
215
|
+
} else {
|
|
216
|
+
queue.push(event);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
this.on('event', onEvent);
|
|
220
|
+
|
|
221
|
+
// close (including error)
|
|
222
|
+
const onClose = () => {
|
|
223
|
+
done = true;
|
|
224
|
+
while (resolvers.length > 0) {
|
|
225
|
+
resolvers.shift()({ done: true });
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
this.on('close', onClose);
|
|
229
|
+
|
|
230
|
+
return {
|
|
231
|
+
next: () => {
|
|
232
|
+
return new Promise((resolve, reject) => {
|
|
233
|
+
if (queue.length > 0) {
|
|
234
|
+
resolve({ value: queue.shift(), done: false });
|
|
235
|
+
} else if (done) {
|
|
236
|
+
resolve({ done: true });
|
|
237
|
+
} else {
|
|
238
|
+
resolvers.push(resolve);
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
},
|
|
242
|
+
return: () => {
|
|
243
|
+
done = true;
|
|
244
|
+
while (resolvers.length > 0) {
|
|
245
|
+
resolvers.shift()({ done: true });
|
|
246
|
+
}
|
|
247
|
+
this.off('event', onEvent);
|
|
248
|
+
this.off('close', onClose);
|
|
249
|
+
},
|
|
250
|
+
[Symbol.asyncIterator]() { return this; }
|
|
251
|
+
}
|
|
199
252
|
}
|
|
200
253
|
}
|
|
201
254
|
|