Advertisement
Guest User

Untitled

a guest
Oct 28th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. /* global io */
  2. 'use strict';
  3.  
  4. angular.module('testFullstackApp')
  5. .factory('socket', function(socketFactory) {
  6.  
  7. // socket.io now auto-configures its connection when we ommit a connection url
  8. var ioSocket = io('', {
  9. // Send auth token on connection, you will need to DI the Auth service above
  10. // 'query': 'token=' + Auth.getToken()
  11. path: '/socket.io-client'
  12. });
  13.  
  14. var socket = socketFactory({
  15. ioSocket: ioSocket
  16. });
  17.  
  18. var watches = [],
  19. actions = [];
  20.  
  21. var Action = function(modelName, arr, cb) {
  22. this.array = arr;
  23. this.cb = cb;
  24. this.modelName = modelName;
  25.  
  26. // Bound Methods
  27. this.join = angular.bind(this, this.join);
  28. this.leave = angular.bind(this, this.leave);
  29. this.save = angular.bind(this, this.save);
  30. this.remove = angular.bind(this, this.remove);
  31. };
  32.  
  33. Action.prototype = {
  34. sync: function() {
  35. socket.on(this.modelName + ':save', this.save);
  36. socket.on(this.modelName + ':remove', this.remove);
  37. socket.on('reconnect', this.join);
  38. return this.join();
  39. },
  40. unsync: function() {
  41. socket.removeListener(this.modelName + ':save', this.save);
  42. socket.removeListener(this.modelName + ':remove', this.remove);
  43. socket.removeListener('reconnect', this.join);
  44. return this.leave();
  45. },
  46.  
  47. join: function() {
  48. socket.emit('join', this.modelName);
  49. return this;
  50. },
  51. leave: function() {
  52. socket.emit('leave', this.modelName);
  53. return this;
  54. },
  55.  
  56. save: function(item) {
  57. var array = this.array;
  58. var oldItem = _.find(array, {_id: item._id});
  59. var index = array.indexOf(oldItem);
  60. var event = 'created';
  61.  
  62. // replace oldItem if it exists
  63. // otherwise just add item to the collection
  64. if (oldItem) {
  65. array.splice(index, 1, item);
  66. event = 'updated';
  67. } else {
  68. array.push(item);
  69. }
  70.  
  71. this.cb(event, item, array);
  72. },
  73. remove: function(item) {
  74. var array = this.array;
  75. var event = 'deleted';
  76. _.remove(array, {_id: item._id});
  77. this.cb(event, item, array);
  78. }
  79. };
  80.  
  81. return {
  82. socket: socket,
  83.  
  84. /**
  85. * Register listeners to sync an array with updates on a model
  86. *
  87. * Takes the array we want to sync, the model name that socket updates are sent from,
  88. * and an optional callback function after new items are updated.
  89. *
  90. * @param {String} modelName
  91. * @param {Array} array
  92. * @param {Function} cb
  93. */
  94. syncUpdates: function (modelName, array, cb) {
  95. cb = cb || angular.noop;
  96.  
  97. watches.push(array);
  98. actions.push(new Action(modelName, array, cb).sync());
  99.  
  100. },
  101.  
  102. /**
  103. * Removes listeners for a models updates on the socket
  104. *
  105. * @param {Array} array
  106. */
  107. unsyncUpdates: function (array) {
  108. var index = watches.indexOf(array);
  109. if (index >= 0) {
  110. watches.splice(index, 1);
  111. actions.splice(index, 1)[0].unsync();
  112. }
  113. }
  114. };
  115. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement