##// END OF EJS Templates
connection: remove unused code
ergo -
r779:233ee50e default
parent child Browse files
Show More
@@ -1,225 +1,208 b''
1 "use strict";
1 "use strict";
2 /** leak object to top level scope **/
2 /** leak object to top level scope **/
3 var ccLog = undefined;
3 var ccLog = undefined;
4 // global code-mirror logger;, to enable run
4 // global code-mirror logger;, to enable run
5 // Logger.get('ConnectionController').setLevel(Logger.DEBUG)
5 // Logger.get('ConnectionController').setLevel(Logger.DEBUG)
6 ccLog = Logger.get('ConnectionController');
6 ccLog = Logger.get('ConnectionController');
7 ccLog.setLevel(Logger.OFF);
7 ccLog.setLevel(Logger.OFF);
8
8
9 var ConnectionController;
9 var ConnectionController;
10 var connCtrlr;
10 var connCtrlr;
11 var registerViewChannels;
11 var registerViewChannels;
12
12
13 (function () {
13 (function () {
14 ConnectionController = function (webappUrl, serverUrl, urls) {
14 ConnectionController = function (webappUrl, serverUrl, urls) {
15 var self = this;
15 var self = this;
16
16
17 var channels = ['broadcast'];
17 var channels = ['broadcast'];
18 this.state = {
18 this.state = {
19 open: false,
19 open: false,
20 webappUrl: webappUrl,
20 webappUrl: webappUrl,
21 serverUrl: serverUrl,
21 serverUrl: serverUrl,
22 connId: null,
22 connId: null,
23 socket: null,
23 socket: null,
24 channels: channels,
24 channels: channels,
25 heartbeat: null,
25 heartbeat: null,
26 channelsInfo: {},
26 channelsInfo: {},
27 urls: urls
27 urls: urls
28 };
28 };
29 this.channelNameParsers = [];
30
31 this.addChannelNameParser = function (fn) {
32 if (this.channelNameParsers.indexOf(fn) === -1) {
33 this.channelNameParsers.push(fn);
34 }
35 };
36
29
37 this.listen = function () {
30 this.listen = function () {
38 if (window.WebSocket) {
31 if (window.WebSocket) {
39 ccLog.debug('attempting to create socket');
32 ccLog.debug('attempting to create socket');
40 var socket_url = self.state.serverUrl + "/ws?conn_id=" + self.state.connId;
33 var socket_url = self.state.serverUrl + "/ws?conn_id=" + self.state.connId;
41 var socket_conf = {
34 var socket_conf = {
42 url: socket_url,
35 url: socket_url,
43 handleAs: 'json',
36 handleAs: 'json',
44 headers: {
37 headers: {
45 "Accept": "application/json",
38 "Accept": "application/json",
46 "Content-Type": "application/json"
39 "Content-Type": "application/json"
47 }
40 }
48 };
41 };
49 self.state.socket = new WebSocket(socket_conf.url);
42 self.state.socket = new WebSocket(socket_conf.url);
50
43
51 self.state.socket.onopen = function (event) {
44 self.state.socket.onopen = function (event) {
52 ccLog.debug('open event', event);
45 ccLog.debug('open event', event);
53 if (self.state.heartbeat === null) {
46 if (self.state.heartbeat === null) {
54 self.state.heartbeat = setInterval(function () {
47 self.state.heartbeat = setInterval(function () {
55 if (self.state.socket.readyState === WebSocket.OPEN) {
48 if (self.state.socket.readyState === WebSocket.OPEN) {
56 self.state.socket.send('heartbeat');
49 self.state.socket.send('heartbeat');
57 }
50 }
58 }, 10000)
51 }, 10000)
59 }
52 }
60 };
53 };
61 self.state.socket.onmessage = function (event) {
54 self.state.socket.onmessage = function (event) {
62 var data = $.parseJSON(event.data);
55 var data = $.parseJSON(event.data);
63 for (var i = 0; i < data.length; i++) {
56 for (var i = 0; i < data.length; i++) {
64 if (data[i].message.topic) {
57 if (data[i].message.topic) {
65 ccLog.debug('publishing',
58 ccLog.debug('publishing',
66 data[i].message.topic, data[i]);
59 data[i].message.topic, data[i]);
67 $.Topic(data[i].message.topic).publish(data[i])
60 $.Topic(data[i].message.topic).publish(data[i])
68 }
61 }
69 else {
62 else {
70 ccLog.warn('unhandled message', data);
63 ccLog.warn('unhandled message', data);
71 }
64 }
72 }
65 }
73 };
66 };
74 self.state.socket.onclose = function (event) {
67 self.state.socket.onclose = function (event) {
75 ccLog.debug('closed event', event);
68 ccLog.debug('closed event', event);
76 setTimeout(function () {
69 setTimeout(function () {
77 self.connect(true);
70 self.connect(true);
78 }, 5000);
71 }, 5000);
79 };
72 };
80
73
81 self.state.socket.onerror = function (event) {
74 self.state.socket.onerror = function (event) {
82 ccLog.debug('error event', event);
75 ccLog.debug('error event', event);
83 };
76 };
84 }
77 }
85 else {
78 else {
86 ccLog.debug('attempting to create long polling connection');
79 ccLog.debug('attempting to create long polling connection');
87 var poolUrl = self.state.serverUrl + "/listen?conn_id=" + self.state.connId;
80 var poolUrl = self.state.serverUrl + "/listen?conn_id=" + self.state.connId;
88 self.state.socket = $.ajax({
81 self.state.socket = $.ajax({
89 url: poolUrl
82 url: poolUrl
90 }).done(function (data) {
83 }).done(function (data) {
91 ccLog.debug('data', data);
84 ccLog.debug('data', data);
92 var data = $.parseJSON(data);
85 var data = $.parseJSON(data);
93 for (var i = 0; i < data.length; i++) {
86 for (var i = 0; i < data.length; i++) {
94 if (data[i].message.topic) {
87 if (data[i].message.topic) {
95 ccLog.info('publishing',
88 ccLog.info('publishing',
96 data[i].message.topic, data[i]);
89 data[i].message.topic, data[i]);
97 $.Topic(data[i].message.topic).publish(data[i])
90 $.Topic(data[i].message.topic).publish(data[i])
98 }
91 }
99 else {
92 else {
100 ccLog.warn('unhandled message', data);
93 ccLog.warn('unhandled message', data);
101 }
94 }
102 }
95 }
103 self.listen();
96 self.listen();
104 }).fail(function () {
97 }).fail(function () {
105 ccLog.debug('longpoll error');
98 ccLog.debug('longpoll error');
106 setTimeout(function () {
99 setTimeout(function () {
107 self.connect(true);
100 self.connect(true);
108 }, 5000);
101 }, 5000);
109 });
102 });
110 }
103 }
111
104
112 };
105 };
113
106
114 this.connect = function (create_new_socket) {
107 this.connect = function (create_new_socket) {
115 var connReq = {'channels': self.state.channels};
108 var connReq = {'channels': self.state.channels};
116 ccLog.debug('try obtaining connection info', connReq);
109 ccLog.debug('try obtaining connection info', connReq);
117 $.ajax({
110 $.ajax({
118 url: self.state.urls.connect,
111 url: self.state.urls.connect,
119 type: "POST",
112 type: "POST",
120 contentType: "application/json",
113 contentType: "application/json",
121 data: JSON.stringify(connReq),
114 data: JSON.stringify(connReq),
122 dataType: "json"
115 dataType: "json"
123 }).done(function (data) {
116 }).done(function (data) {
124 ccLog.debug('Got connection:', data.conn_id);
117 ccLog.debug('Got connection:', data.conn_id);
125 self.state.channels = data.channels;
118 self.state.channels = data.channels;
126 self.state.channelsInfo = data.channels_info;
119 self.state.channelsInfo = data.channels_info;
127 self.state.connId = data.conn_id;
120 self.state.connId = data.conn_id;
128 if (create_new_socket) {
121 if (create_new_socket) {
129 self.listen();
122 self.listen();
130 }
123 }
131 self.update();
124 self.update();
132 }).fail(function () {
125 }).fail(function () {
133 setTimeout(function () {
126 setTimeout(function () {
134 self.connect(create_new_socket);
127 self.connect(create_new_socket);
135 }, 5000);
128 }, 5000);
136 });
129 });
137 self.update();
130 self.update();
138 };
131 };
139
132
140 this.subscribeToChannels = function (channels) {
133 this.subscribeToChannels = function (channels) {
141 var new_channels = [];
134 var new_channels = [];
142 for (var i = 0; i < channels.length; i++) {
135 for (var i = 0; i < channels.length; i++) {
143 var channel = channels[i];
136 var channel = channels[i];
144 if (self.state.channels.indexOf(channel)) {
137 if (self.state.channels.indexOf(channel)) {
145 self.state.channels.push(channel);
138 self.state.channels.push(channel);
146 new_channels.push(channel)
139 new_channels.push(channel)
147 }
140 }
148 }
141 }
149 /**
142 /**
150 * only execute the request if socket is present because subscribe
143 * only execute the request if socket is present because subscribe
151 * can actually add channels before initial app connection
144 * can actually add channels before initial app connection
152 **/
145 **/
153 if (new_channels && self.state.socket !== null) {
146 if (new_channels && self.state.socket !== null) {
154 var connReq = {
147 var connReq = {
155 'channels': self.state.channels,
148 'channels': self.state.channels,
156 'conn_id': self.state.connId
149 'conn_id': self.state.connId
157 };
150 };
158 $.ajax({
151 $.ajax({
159 url: self.state.urls.subscribe,
152 url: self.state.urls.subscribe,
160 type: "POST",
153 type: "POST",
161 contentType: "application/json",
154 contentType: "application/json",
162 data: JSON.stringify(connReq),
155 data: JSON.stringify(connReq),
163 dataType: "json"
156 dataType: "json"
164 }).done(function (data) {
157 }).done(function (data) {
165 self.state.channels = data.channels;
158 self.state.channels = data.channels;
166 self.state.channelsInfo = data.channels_info;
159 self.state.channelsInfo = data.channels_info;
167 self.update();
160 self.update();
168 });
161 });
169 }
162 }
170 self.update();
163 self.update();
171 };
164 };
172
165
173 this.update = function () {
166 this.update = function () {
174 for (var key in this.state.channelsInfo) {
167 for (var key in this.state.channelsInfo) {
175 if (this.state.channelsInfo.hasOwnProperty(key)) {
168 if (this.state.channelsInfo.hasOwnProperty(key)) {
176 // update channels with latest info
169 // update channels with latest info
177 $.Topic('/connection_controller/channel_update').publish(
170 $.Topic('/connection_controller/channel_update').publish(
178 {channel: key, state: this.state.channelsInfo[key]});
171 {channel: key, state: this.state.channelsInfo[key]});
179 }
172 }
180 }
173 }
181 /**
182 * checks current channel list in state and if channel is not present
183 * converts them into executable "commands" and pushes them on topics
184 */
185 for (var i = 0; i < this.state.channels.length; i++) {
186 var channel = this.state.channels[i];
187 for (var j = 0; j < this.channelNameParsers.length; j++) {
188 this.channelNameParsers[j](channel);
189 }
190 }
191 };
174 };
192
175
193 this.run = function () {
176 this.run = function () {
194 this.connect(true);
177 this.connect(true);
195 };
178 };
196
179
197 $.Topic('/connection_controller/subscribe').subscribe(
180 $.Topic('/connection_controller/subscribe').subscribe(
198 self.subscribeToChannels);
181 self.subscribeToChannels);
199 };
182 };
200
183
201 $.Topic('/plugins/__REGISTER__').subscribe(function (data) {
184 $.Topic('/plugins/__REGISTER__').subscribe(function (data) {
202 if (window.CHANNELSTREAM_SETTINGS && window.CHANNELSTREAM_SETTINGS.enabled) {
185 if (window.CHANNELSTREAM_SETTINGS && window.CHANNELSTREAM_SETTINGS.enabled) {
203 connCtrlr = new ConnectionController(
186 connCtrlr = new ConnectionController(
204 CHANNELSTREAM_SETTINGS.webapp_location,
187 CHANNELSTREAM_SETTINGS.webapp_location,
205 CHANNELSTREAM_SETTINGS.ws_location,
188 CHANNELSTREAM_SETTINGS.ws_location,
206 CHANNELSTREAM_URLS
189 CHANNELSTREAM_URLS
207 );
190 );
208 registerViewChannels();
191 registerViewChannels();
209
192
210 $(document).ready(function () {
193 $(document).ready(function () {
211 connCtrlr.run();
194 connCtrlr.run();
212 });
195 });
213 }
196 }
214 });
197 });
215
198
216 registerViewChannels = function (){
199 registerViewChannels = function (){
217 // subscribe to PR repo channel for PR's'
200 // subscribe to PR repo channel for PR's'
218 if (templateContext.pull_request_data.pull_request_id) {
201 if (templateContext.pull_request_data.pull_request_id) {
219 var channelName = '/repo$' + templateContext.repo_name + '$/pr/' +
202 var channelName = '/repo$' + templateContext.repo_name + '$/pr/' +
220 String(templateContext.pull_request_data.pull_request_id);
203 String(templateContext.pull_request_data.pull_request_id);
221 connCtrlr.state.channels.push(channelName);
204 connCtrlr.state.channels.push(channelName);
222 }
205 }
223 }
206 }
224
207
225 })();
208 })();
General Comments 0
You need to be logged in to leave comments. Login now