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