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.

49 lines
1.2 KiB

8 years ago
  1. var io = {
  2. connect: createMockSocketObject
  3. };
  4. function createMockSocketObject () {
  5. var socket = {
  6. on: function (ev, fn) {
  7. (this._listeners[ev] = this._listeners[ev] || []).push(fn);
  8. },
  9. once: function (ev, fn) {
  10. (this._listeners[ev] = this._listeners[ev] || []).push(fn);
  11. fn._once = true;
  12. },
  13. emit: function (ev, data) {
  14. if (this._listeners[ev]) {
  15. var args = arguments;
  16. this._listeners[ev].forEach(function (listener) {
  17. if (listener._once) {
  18. this.removeListener(ev, listener);
  19. }
  20. listener.apply(null, Array.prototype.slice.call(args, 1));
  21. }.bind(this));
  22. }
  23. },
  24. _listeners: {},
  25. removeListener: function (ev, fn) {
  26. if (fn) {
  27. var index = this._listeners[ev].indexOf(fn);
  28. if (index > -1) {
  29. this._listeners[ev].splice(index, 1);
  30. }
  31. } else {
  32. delete this._listeners[ev];
  33. }
  34. },
  35. removeAllListeners: function (ev) {
  36. if (ev) {
  37. delete this._listeners[ev];
  38. } else {
  39. this._listeners = {};
  40. }
  41. },
  42. disconnect: function () {},
  43. connect: function () {}
  44. };
  45. return socket;
  46. }