Skip to content

Commit

Permalink
Improve documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Nov 5, 2021
1 parent 4a52d34 commit 6baf95e
Show file tree
Hide file tree
Showing 4 changed files with 1,684 additions and 20 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ Use `DEBUG=p-transform:*` environment variable.

For custom debug name set `logName` option at PTransform constructor or `transform` argument.

<a name="PTransform"></a>

## PTransform

**Kind**: global class

- [PTransform](#PTransform)
- [new PTransform([options])](#new_PTransform_new)
- [.flushQueue()](#PTransform+flushQueue)
- [.queuedTransform(chunk, encoding)](#PTransform+queuedTransform)

<a name="new_PTransform_new"></a>

### new PTransform([options])

PTransform

| Param | Type | Description |
| ---------------------- | --------------------- | -------------------------------------- |
| [options] | <code>Object</code> | Options object forwarded to Transform. |
| [options.logName] | <code>String</code> | Custom name for logger. |
| [options.transform] | <code>function</code> | Transform function. |
| [options.queueOptions] | <code>Object</code> | Options forwarded to PQueue instance. |

<a name="PTransform+flushQueue"></a>

### pTransform.flushQueue() ⇒

Wait for queue idle.

**Kind**: instance method of [<code>PTransform</code>](#PTransform)
**Returns**: Promise<void>
<a name="PTransform+queuedTransform"></a>

### pTransform.queuedTransform(chunk, encoding) ⇒

Queued transform operation.

**Kind**: instance method of [<code>PTransform</code>](#PTransform)
**Returns**: Promise

| Param | Type |
| -------- | ------------------- |
| chunk | <code>Object</code> |
| encoding | <code>String</code> |

## License

Apache-2.0
22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const { pipeline: _pipeline, Transform } = require('stream');
const pipeline = promisify(_pipeline);

class PTransform extends Transform {
/**
* PTransform
*
* @param {Object} [options] - Options object forwarded to Transform.
* @param {String} [options.logName] - Custom name for logger.
* @param {Function} [options.transform] - Transform function.
* @param {Object} [options.queueOptions] - Options forwarded to PQueue instance.
*/
constructor(options = {}) {
// transform is used locally, forward undefined to prevent conflicts.
super({ objectMode: true, ...options });
Expand Down Expand Up @@ -33,19 +41,23 @@ class PTransform extends Transform {

/**
* Wait for queue idle.
* @return Promise
*
* @return Promise<void>
*/
flushQueue() {
return this.queue.onIdle();
}

/**
* Queued transform operation.
*
* @param {Object} chunk
* @param {String} encoding
* @return Promise
*/
async queuedTransform(chunk, enc) {
async queuedTransform(chunk, encoding) {
try {
const maybeChunk = await this._transform(chunk, enc);
const maybeChunk = await this._transform(chunk, encoding);
if (maybeChunk) {
this.push(maybeChunk);
}
Expand All @@ -61,9 +73,9 @@ class PTransform extends Transform {
setTimeout(() => callback());
}

_flush(cb) {
_flush(callback) {
this.debug('_flush');
this.flushQueue().then(() => cb());
this.flushQueue().then(() => callback());
}
}

Expand Down
Loading

0 comments on commit 6baf95e

Please sign in to comment.