command.js 940 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var EventEmitter = require('events').EventEmitter;
  2. var util = require('util');
  3. function Command() {
  4. EventEmitter.call(this);
  5. this.next = null;
  6. }
  7. util.inherits(Command, EventEmitter);
  8. // slow. debug only
  9. Command.prototype.stateName = function() {
  10. var state = this.next;
  11. for (i in this)
  12. if (this[i] == state && i != 'next')
  13. return i;
  14. };
  15. Command.prototype.execute = function(packet, connection) {
  16. // TODO: hack
  17. if (!this.next) {
  18. this.next = this.start;
  19. }
  20. if (packet && packet.isError()) {
  21. var err = packet.asError();
  22. if (this.onResult)
  23. this.onResult(err);
  24. else
  25. this.emit('error', err);
  26. return true;
  27. }
  28. // TODO: don't return anything from execute, it's ugly and error-prone. Listen for 'end' event in connection
  29. this.next = this.next(packet, connection);
  30. if (this.next) {
  31. return false;
  32. } else {
  33. this.emit('end');
  34. return true;
  35. }
  36. };
  37. module.exports = Command;