You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.4 KiB

4 years ago
  1. # parallel-transform
  2. [Transform stream](http://nodejs.org/api/stream.html#stream_class_stream_transform_1) for Node.js that allows you to run your transforms
  3. in parallel without changing the order of the output.
  4. npm install parallel-transform
  5. It is easy to use
  6. ``` js
  7. var transform = require('parallel-transform');
  8. var stream = transform(10, function(data, callback) { // 10 is the parallism level
  9. setTimeout(function() {
  10. callback(null, data);
  11. }, Math.random() * 1000);
  12. });
  13. for (var i = 0; i < 10; i++) {
  14. stream.write(''+i);
  15. }
  16. stream.end();
  17. stream.on('data', function(data) {
  18. console.log(data); // prints 0,1,2,...
  19. });
  20. stream.on('end', function() {
  21. console.log('stream has ended');
  22. });
  23. ```
  24. If you run the above example you'll notice that it runs in parallel
  25. (does not take ~1 second between each print) and that the order is preserved
  26. ## Stream options
  27. All transforms are Node 0.10 streams. Per default they are created with the options `{objectMode:true}`.
  28. If you want to use your own stream options pass them as the second parameter
  29. ``` js
  30. var stream = transform(10, {objectMode:false}, function(data, callback) {
  31. // data is now a buffer
  32. callback(null, data);
  33. });
  34. fs.createReadStream('filename').pipe(stream).pipe(process.stdout);
  35. ```
  36. ### Unordered
  37. Passing the option `{ordered:false}` will output the data as soon as it's processed by a transform, without waiting to respect the order.
  38. ## License
  39. MIT