PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/js/socket.js

https://github.com/benjaminfisher/github-repositories
JavaScript | 155 lines | 67 code | 15 blank | 73 comment | 8 complexity | 3e731e041c62dbd78b7e94c59d4f8784 MD5 | raw file
Possible License(s): JSON
  1. /*
  2. * Chrome Extension Socket
  3. * <https://github.com/jjNford/chrome-extension-socket>
  4. *
  5. * Copyright (C) 2012, JJ Ford (jj.n.ford@gmail.com)
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is furnished to do
  12. * so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. */
  26. (function() {
  27. window.Socket = {
  28. init: function() {
  29. this.tasks = 0;
  30. this.port = chrome.extension.connect({name: "popupToBackground"});
  31. this.bind();
  32. },
  33. /**
  34. * Bind
  35. */
  36. bind: function() {
  37. // Attach connection listener to port.
  38. chrome.extension.onConnect.addListener( function(port) {
  39. if(port.name === "popupToBackground") {
  40. Socket.port = chrome.extension.connect({name: "backgroundToPopup"});
  41. }
  42. // Attach message listener to port.
  43. port.onMessage.addListener( function(msg) {
  44. Socket.onMessage(msg);
  45. });
  46. // Attach disconnection listener to port.
  47. Socket.port.onDisconnect.addListener( function(port) {
  48. port.onMessage.removeListener();
  49. Socket.port.onMessage.removeListener();
  50. });
  51. });
  52. },
  53. /**
  54. * On Message
  55. *
  56. * Triggered when the port receives a new message.
  57. *
  58. * @param msg Message received from the port.
  59. */
  60. onMessage: function(msg) {
  61. try {
  62. // Increment task count if task message is posted to background.
  63. if(msg.type === "task") {
  64. Socket.tasks++;
  65. }
  66. // Trigger posted message function.
  67. if(msg.type === "message" || msg.type === "task") {
  68. window[msg.namespace][msg.literal][msg.method].apply(this, msg.args);
  69. }
  70. // If task is complete, hide the loading notification.
  71. else if(msg.type === "taskComplete") {
  72. jQuery('.user_links.loading').hide();
  73. }
  74. }
  75. catch(UnknownDestination) {
  76. // Catch errors for unknown message destinations.
  77. }
  78. },
  79. /**
  80. * Post Message
  81. *
  82. * Posts a message to be sent through the socket.
  83. *
  84. * @param - namespace - Namespace of message destination.
  85. * @param - literal - Object of message destination.
  86. * @param - method - Method of message destination.
  87. * @param - args - Array of arguments to pass through socket.
  88. */
  89. postMessage: function(msg) {
  90. try {
  91. msg.type = "message";
  92. this.port.postMessage(msg);
  93. }
  94. catch(SocketPostError) {
  95. // Catch errors just in case.
  96. }
  97. },
  98. /**
  99. * Post Task
  100. *
  101. * Post a task message to be sent through the socket. This will
  102. * increment the background page task counter.
  103. *
  104. * @param - namespace - Namespace of message destination.
  105. * @param - literal - Object of message destination.
  106. * @param - method - Method of message destination.
  107. * @param - args - Array of arguments to pass through socket.
  108. */
  109. postTask: function(msg) {
  110. // Display loading notification for background task.
  111. if(this.port.name === "popupToBackground") {
  112. jQuery('.user_links.loading').show();
  113. }
  114. try {
  115. msg.type = "task";
  116. this.port.postMessage(msg);
  117. }
  118. catch(SocketPostError) {
  119. // Catch errors just in case.
  120. }
  121. },
  122. /**
  123. * Post Task Complete
  124. *
  125. * Post a task complete message to be sent through the socket. This
  126. * will decrement the background page task counter.
  127. */
  128. postTaskComplete: function() {
  129. if(--this.tasks === 0) {
  130. try {
  131. this.port.postMessage({type: "taskComplete"});
  132. }
  133. catch(SocketPostError) {
  134. // Catch errors just in case.
  135. }
  136. }
  137. }
  138. };
  139. Socket.init();
  140. })();