bus.js 946 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var events = require('events');
  2. var debug = require('debug')('nodemon');
  3. var util = require('util');
  4. var Bus = function () {
  5. events.EventEmitter.call(this);
  6. };
  7. util.inherits(Bus, events.EventEmitter);
  8. var bus = new Bus();
  9. // /*
  10. var collected = {};
  11. bus.on('newListener', function (event) {
  12. debug('bus new listener: %s (%s)', event, bus.listeners(event).length);
  13. if (!collected[event]) {
  14. collected[event] = true;
  15. bus.on(event, function () {
  16. debug('bus emit: %s', event);
  17. });
  18. }
  19. });
  20. // */
  21. // proxy process messages (if forked) to the bus
  22. process.on('message', function (event) {
  23. debug('process.message(%s)', event);
  24. bus.emit(event);
  25. });
  26. var emit = bus.emit;
  27. // if nodemon was spawned via a fork, allow upstream communication
  28. // via process.send
  29. if (process.send) {
  30. bus.emit = function (event, data) {
  31. process.send({ type: event, data: data });
  32. emit.apply(bus, arguments);
  33. };
  34. }
  35. module.exports = bus;