PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/pusher-angular/0.1.4/pusher-angular.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 340 lines | 192 code | 40 blank | 108 comment | 22 complexity | a45083ab00efc504b6221f4361ff7c3b MD5 | raw file
  1. 'use strict';
  2. angular.module('pusher-angular', [])
  3. .factory('$pusher', ['$rootScope', '$channel', '$connection',
  4. function ($rootScope, $channel, $connection) {
  5. function PusherAngular (pusherClient) {
  6. if (!(this instanceof PusherAngular)) {
  7. return new PusherAngular(pusherClient);
  8. }
  9. this._assertValidClient(pusherClient);
  10. this.client = pusherClient;
  11. this.connection = $connection(pusherClient.connection, pusherClient);
  12. this.channels = {};
  13. }
  14. PusherAngular.prototype = {
  15. /**
  16. * Subscribe the client to the specified channelName and returns the channel object.
  17. * {@link https://pusher.com/docs/client_api_guide/client_public_channels#subscribe}
  18. *
  19. * @param {String} channelName name of the channel
  20. * @returns {Object} channel object
  21. */
  22. subscribe: function (channelName) {
  23. var channel = $channel(this.client.subscribe(channelName), this);
  24. this.channels[channelName] = channel;
  25. return channel;
  26. },
  27. /**
  28. * Unsubscribes the client from the specified channel
  29. * {@link https://pusher.com/docs/client_api_guide/client_public_channels#unsubscribe}
  30. *
  31. * @param {String} channelName name of the channel
  32. */
  33. unsubscribe: function (channelName) {
  34. if (this.client.channel(channelName)) {
  35. this.client.unsubscribe(channelName);
  36. if (this.channels[channelName]) { delete this.channels[channelName]; }
  37. }
  38. },
  39. /**
  40. * Binds to global events on the pusher client. You can attach behaviour to these events
  41. * regardless of the channel the event is broadcast to.
  42. *
  43. * @param {String} eventName name of the event you want to bind to
  44. * @param {Function|undefined} callback callback that you want called upon the event occurring
  45. */
  46. bind: function (eventName, callback) {
  47. this.client.bind(eventName, function (data) {
  48. callback(data);
  49. $rootScope.$digest();
  50. });
  51. },
  52. /**
  53. * Binds to all of the global client messages.
  54. *
  55. * @param {Function|undefined} callback callback that you want called upon a message being received
  56. */
  57. bind_all: function (callback) {
  58. this.client.bind_all(function (eventName, data) {
  59. callback(eventName, data);
  60. $rootScope.$digest();
  61. });
  62. },
  63. /**
  64. * Disconnects the pusher client.
  65. * {@link http://pusher.com/docs/client_api_guide/client_connect#disconnecting}
  66. */
  67. disconnect: function () {
  68. this.client.disconnect();
  69. },
  70. /**
  71. * Returns a pusher channel object.
  72. * {@link https://pusher.com/docs/client_api_guide/client_channels#access}
  73. *
  74. * @param {String} channelName name of the channel
  75. * @returns {Array} channel object
  76. */
  77. channel: function (channelName) {
  78. return this.channels[channelName];
  79. },
  80. /**
  81. * Returns a an array of the channels that the client is subscribed to.
  82. * {@link https://pusher.com/docs/client_api_guide/client_channels#access}
  83. *
  84. * @returns {Array} array of subscribed channels
  85. */
  86. allChannels: function () {
  87. return this.channels;
  88. },
  89. /**
  90. * Asserts that the $pusher object is being initialised with valid pusherClient.
  91. * Throws an error if pusherClient is invalid.
  92. *
  93. * @param {Object} pusherClient members object from base pusher channel object
  94. */
  95. _assertValidClient: function (pusherClient) {
  96. if (!angular.isObject(pusherClient) ||
  97. !angular.isObject(pusherClient.connection) ||
  98. typeof(pusherClient.channel) !== 'function') {
  99. throw new Error('Invalid Pusher client object');
  100. }
  101. }
  102. };
  103. return PusherAngular;
  104. }
  105. ])
  106. .factory('$channel', ['$rootScope', '$members',
  107. function ($rootScope, $members) {
  108. function checkPresenceOrPrivateChannel (channelName) {
  109. if (channelName.indexOf('presence-') == -1 && channelName.indexOf('private-') == -1) {
  110. throw new Error('Presence or private channel required');
  111. }
  112. }
  113. function $channel (baseChannel, $pusherClient) {
  114. if (!(this instanceof $channel)) {
  115. return new $channel(baseChannel, $pusherClient);
  116. }
  117. this._assertValidChannel(baseChannel);
  118. this.baseChannel = baseChannel;
  119. this.client = $pusherClient;
  120. this.name = baseChannel.name;
  121. if (baseChannel.name.indexOf('presence') == -1) {
  122. this.members = function () { throw new Error('Members object only exists for presence channels'); }
  123. } else {
  124. this.members = $members(baseChannel.members, baseChannel);
  125. }
  126. }
  127. $channel.prototype = {
  128. /**
  129. * Binds to the given event name on the channel.
  130. *
  131. * @param {String} eventName name of the event you want to bind to
  132. * @param {Function|undefined} callback callback that you want called upon the event occurring
  133. */
  134. bind: function (eventName, callback) {
  135. this.baseChannel.bind(eventName, function (data) {
  136. callback(data);
  137. $rootScope.$digest();
  138. });
  139. },
  140. /**
  141. * Binds to all of the channel events.
  142. *
  143. * @param {Function|undefined} callback callback that you want called upon the event occurring
  144. */
  145. bind_all: function (callback) {
  146. this.baseChannel.bind_all(function (eventName, data) {
  147. callback(eventName, data);
  148. $rootScope.$digest();
  149. });
  150. },
  151. /**
  152. * Triggers a client event.
  153. * {@link https://pusher.com/docs/client_api_guide/client_events#trigger-events}
  154. *
  155. * @param {String} channelName name of the channel
  156. * @param {String} eventName name of the event
  157. * @param {Object} obj object that you wish to pass along with your client event
  158. * @returns {}
  159. */
  160. trigger: function (eventName, obj) {
  161. checkPresenceOrPrivateChannel(this.name);
  162. if (eventName.indexOf('client-') == -1) { throw new Error('Event name requires \'client-\' prefix'); }
  163. return this.baseChannel.trigger(eventName, obj);
  164. },
  165. /**
  166. * Asserts that the $channel object is being initialised with valid baseChannel.
  167. * Throws an error if baseChannel is invalid.
  168. *
  169. * @param {Object} baseChannel channel object from base pusher channel object
  170. */
  171. _assertValidChannel: function (baseChannel) {
  172. if (!angular.isObject(baseChannel) ||
  173. typeof(baseChannel.name) !== 'string') {
  174. throw new Error('Invalid Pusher channel object');
  175. }
  176. }
  177. };
  178. return $channel;
  179. }
  180. ])
  181. .factory('$members', ['$rootScope',
  182. function ($rootScope) {
  183. function $members (baseMembers, baseChannel) {
  184. if (!(this instanceof $members)) {
  185. return new $members(baseMembers, baseChannel);
  186. }
  187. var self = this;
  188. this._assertValidMembers(baseMembers);
  189. this.baseMembers = baseMembers;
  190. this.baseChannel = baseChannel;
  191. this.me = {};
  192. this.count = 0;
  193. this.members = {};
  194. baseChannel.bind('pusher:subscription_succeeded', function (members) {
  195. self.me = members.me;
  196. self.count = members.count;
  197. self.members = members.members;
  198. $rootScope.$digest();
  199. });
  200. baseChannel.bind('pusher:member_added', function (member) {
  201. self.count++;
  202. if (member.info) {
  203. self.members[member.id.toString()] = member.info;
  204. } else {
  205. self.members[member.id.toString()] = null;
  206. }
  207. $rootScope.$digest();
  208. });
  209. baseChannel.bind('pusher:member_removed', function (member) {
  210. self.count--;
  211. delete self.members[member.id.toString()];
  212. $rootScope.$digest();
  213. });
  214. }
  215. $members.prototype = {
  216. /**
  217. * Returns member's info for given id. Resulting object containts two fields - id and info.
  218. *
  219. * @param {Number} id user's id
  220. * @return {Object} member's info or null
  221. */
  222. get: function (id) {
  223. return this.baseMembers.get(id);
  224. },
  225. /**
  226. * Calls back for each member in unspecified order.
  227. *
  228. * @param {Function} callback callback function
  229. */
  230. each: function (callback) {
  231. this.baseMembers.each(function (member) {
  232. callback(member);
  233. $rootScope.$digest();
  234. });
  235. },
  236. /**
  237. * Asserts that the $members object is being initialised with valid baseMembers.
  238. * Throws an error if baseMembers is invalid.
  239. *
  240. * @param {Object} baseMembers members object from base pusher channel object
  241. */
  242. _assertValidMembers: function (baseMembers) {
  243. if (!angular.isObject(baseMembers) ||
  244. typeof(baseMembers.me) !== 'object') {
  245. throw new Error('Invalid Pusher channel members object');
  246. }
  247. }
  248. };
  249. return $members;
  250. }
  251. ])
  252. .factory('$connection', ['$rootScope',
  253. function ($rootScope) {
  254. function $connection (baseConnection, baseClient) {
  255. if (!(this instanceof $connection)) {
  256. return new $connection(baseConnection, baseClient);
  257. }
  258. this._assertValidConnection(baseConnection);
  259. this.baseConnection = baseConnection;
  260. this.baseClient = baseClient;
  261. }
  262. $connection.prototype = {
  263. /**
  264. * Binds to the given event name on the connection.
  265. *
  266. * @param {String} eventName name of the event you want to bind to
  267. * @param {Function|undefined} callback callback that you want called upon the event occurring
  268. */
  269. bind: function (eventName, callback) {
  270. this.baseConnection.bind(eventName, function (data) {
  271. callback(data);
  272. $rootScope.$digest();
  273. });
  274. },
  275. /**
  276. * Binds to all of the global connection events.
  277. *
  278. * @param {Function|undefined} callback callback that you want called upon the event occurring
  279. */
  280. bind_all: function (callback) {
  281. this.baseConnection.bind_all(function (eventName, data) {
  282. callback(eventName, data);
  283. $rootScope.$digest();
  284. });
  285. },
  286. /**
  287. * Asserts that the $connection object is being initialised with valid baseConnection.
  288. * Throws an error if baseConnection is invalid.
  289. *
  290. * @param {Object} baseConnection connection object from base pusher object
  291. */
  292. _assertValidConnection: function (baseConnection) {
  293. if (!angular.isObject(baseConnection)) {
  294. throw new Error('Invalid Pusher connection object');
  295. }
  296. }
  297. };
  298. return $connection;
  299. }
  300. ]);