event_buffer.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. /*
  19. This is a very simple buffer for a predetermined set of events. It is unbounded.
  20. It forwards all arguments to any outlet emitter attached with sync().
  21. Example:
  22. var source = new Stream()
  23. , dest = new EventEmitter()
  24. , buff = new EventBuffer(source)
  25. , data = '';
  26. dest.on('data', function (d) { data += d; });
  27. source.writeable = true;
  28. source.readable = true;
  29. source.emit('data', 'abcdef');
  30. source.emit('data', '123456');
  31. buff.sync(dest);
  32. */
  33. /**
  34. @name EventBuffer
  35. @namespace EventBuffer
  36. @constructor
  37. */
  38. var EventBuffer = function (src, events) {
  39. // By default, we service the default stream events
  40. var self = this
  41. , streamEvents = ['data', 'end', 'error', 'close', 'fd', 'drain', 'pipe'];
  42. this.events = events || streamEvents;
  43. this.emitter = src;
  44. this.eventBuffer = [];
  45. this.outlet = null;
  46. this.events.forEach(function (name) {
  47. self.emitter.addListener(name, function () {
  48. self.proxyEmit(name, arguments);
  49. });
  50. });
  51. };
  52. EventBuffer.prototype = new (function () {
  53. /**
  54. @name EventBuffer#proxyEmit
  55. @public
  56. @function
  57. @description Emit an event by name and arguments or add it to the buffer if
  58. no outlet is set
  59. @param {String} name The name to use for the event
  60. @param {Array} args An array of arguments to emit
  61. */
  62. this.proxyEmit = function (name, args) {
  63. if (this.outlet) {
  64. this.emit(name, args);
  65. }
  66. else {
  67. this.eventBuffer.push({name: name, args: args});
  68. }
  69. };
  70. /**
  71. @name EventBuffer#emit
  72. @public
  73. @function
  74. @description Emit an event by name and arguments
  75. @param {String} name The name to use for the event
  76. @param {Array} args An array of arguments to emit
  77. */
  78. this.emit = function (name, args) {
  79. // Prepend name to args
  80. var outlet = this.outlet;
  81. Array.prototype.splice.call(args, 0, 0, name);
  82. outlet.emit.apply(outlet, args);
  83. };
  84. /**
  85. @name EventBuffer#sync
  86. @public
  87. @function
  88. @description Flush the buffer and continue piping new events to the outlet
  89. @param {Object} outlet The emitter to send events to
  90. */
  91. this.sync = function (outlet) {
  92. var buffer = this.eventBuffer
  93. , bufferItem;
  94. this.outlet = outlet;
  95. while ((bufferItem = buffer.shift())) {
  96. this.emit(bufferItem.name, bufferItem.args);
  97. }
  98. };
  99. })();
  100. EventBuffer.prototype.constructor = EventBuffer;
  101. module.exports.EventBuffer = EventBuffer;