PageRenderTime 22ms CodeModel.GetById 11ms app.highlight 8ms RepoModel.GetById 1ms 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
  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
 28	window.Socket = {
 29	
 30		init: function() {
 31			this.tasks = 0;
 32			this.port = chrome.extension.connect({name: "popupToBackground"});
 33			this.bind();
 34		},
 35
 36		/**
 37		 * Bind
 38		 */
 39		bind: function() {
 40			// Attach connection listener to port.
 41			chrome.extension.onConnect.addListener( function(port) {
 42				if(port.name === "popupToBackground") {
 43					Socket.port = chrome.extension.connect({name: "backgroundToPopup"});
 44				}
 45
 46				// Attach message listener to port.
 47				port.onMessage.addListener( function(msg) {
 48					Socket.onMessage(msg);
 49				});
 50
 51				// Attach disconnection listener to port.
 52				Socket.port.onDisconnect.addListener( function(port) {
 53					port.onMessage.removeListener();
 54					Socket.port.onMessage.removeListener();
 55				});
 56			});
 57		},
 58	
 59		/**
 60		 * On Message
 61		 * 
 62		 * Triggered when the port receives a new message.
 63		 * 
 64		 * @param msg Message received from the port.
 65		 */
 66		onMessage: function(msg) {
 67			try {
 68				// Increment task count if task message is posted to background.
 69				if(msg.type === "task") {
 70					Socket.tasks++;
 71				}
 72
 73				// Trigger posted message function.
 74				if(msg.type === "message" || msg.type === "task") {
 75					window[msg.namespace][msg.literal][msg.method].apply(this, msg.args);
 76				}
 77
 78				// If task is complete, hide the loading notification.
 79				else if(msg.type === "taskComplete") {
 80					jQuery('.user_links.loading').hide();
 81				}
 82			}
 83			catch(UnknownDestination) {
 84				// Catch errors for unknown message destinations.
 85			}
 86		},	
 87	
 88		/**
 89		 * Post Message
 90		 * 
 91		 * Posts a message to be sent through the socket.
 92		 * 
 93		 * @param - namespace - Namespace of message destination.
 94		 * @param - literal - Object of message destination.
 95		 * @param - method - Method of message destination.
 96		 * @param - args - Array of arguments to pass through socket.
 97		 */
 98		postMessage: function(msg) {
 99			try {
100				msg.type = "message";
101				this.port.postMessage(msg);
102			}
103			catch(SocketPostError) {
104				// Catch errors just in case.
105			}
106		},
107
108		/**
109		 * Post Task
110		 * 
111		 * Post a task message to be sent through the socket.  This will
112		 * increment the background page task counter.
113		 * 
114		 * @param - namespace - Namespace of message destination.
115		 * @param - literal - Object of message destination.
116		 * @param - method - Method of message destination.
117		 * @param - args - Array of arguments to pass through socket.
118		 */
119		postTask: function(msg) {
120
121			// Display loading notification for background task.
122			if(this.port.name === "popupToBackground") {
123				jQuery('.user_links.loading').show();
124			}
125	
126			try {
127				msg.type = "task";
128 				this.port.postMessage(msg);
129			}
130			catch(SocketPostError) {
131				// Catch errors just in case.
132			}
133		},
134
135		/**
136		 * Post Task Complete
137		 * 
138		 * Post a task complete message to be sent through the socket.  This
139		 * will decrement the background page task counter.
140		 */
141		postTaskComplete: function() {
142			if(--this.tasks === 0) {
143				try {
144					this.port.postMessage({type: "taskComplete"});
145				}
146				catch(SocketPostError) {
147					// Catch errors just in case.
148				}
149			}
150		}
151	};
152
153	Socket.init();
154
155})();