loop.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. var Seq = require('seq');
  2. var Hash = require('hashish');
  3. var EventEmitter = require('events').EventEmitter;
  4. var Bin = require('binary');
  5. var Buf = require('bufferlist/binary');
  6. var BufferList = require('bufferlist');
  7. console.log('loop');
  8. function emitter () {
  9. var em = new EventEmitter;
  10. var i = 0;
  11. var iv = setInterval(function () {
  12. var buf = new Buffer(10000);
  13. buf[0] = 0xff;
  14. if (++ i >= 2000) {
  15. buf[0] = 0;
  16. clearInterval(iv);
  17. }
  18. em.emit('data', buf);
  19. }, 1);
  20. return em;
  21. }
  22. Seq()
  23. .seq(function () {
  24. var next = this.bind({}, null);
  25. bufferlist(next);
  26. })
  27. .seq(function () {
  28. var next = this.bind({}, null);
  29. binary(next);
  30. })
  31. ;
  32. function binary (next) {
  33. var em = emitter();
  34. var t0 = Date.now();
  35. Bin(em)
  36. .loop(function (end) {
  37. this
  38. .word8('x')
  39. .word8('y')
  40. .word32be('z')
  41. .word32le('w')
  42. .buffer('buf', 10000 - 10)
  43. .tap(function (vars) {
  44. if (vars.x === 0) {
  45. var tf = Date.now();
  46. console.log(' binary: ' + (tf - t0) + ' ms');
  47. end();
  48. setTimeout(next, 20);
  49. }
  50. })
  51. })
  52. ;
  53. }
  54. function bufferlist (next) {
  55. var em = emitter();
  56. var t0 = Date.now();
  57. var blist = new BufferList;
  58. em.on('data', function (buf) {
  59. blist.push(buf);
  60. });
  61. Buf(blist)
  62. .forever(function () {
  63. var top = this;
  64. this
  65. .getWord8('x')
  66. .getWord8('y')
  67. .getWord32be('z')
  68. .getWord32le('w')
  69. .getBuffer('buf', 10000 - 10)
  70. .tap(function (vars) {
  71. if (vars.x === 0) {
  72. var tf = Date.now();
  73. console.log(' bufferlist: ' + (tf - t0) + ' ms');
  74. top.exit();
  75. setTimeout(next, 20);
  76. }
  77. })
  78. })
  79. .end()
  80. ;
  81. }