##// END OF EJS Templates
channelstream: connect only when enabled
ergo -
r798:a3510314 default
parent child Browse files
Show More
@@ -1,127 +1,129 b''
1 1 ccLog = Logger.get('RhodeCodeApp');
2 2 ccLog.setLevel(Logger.OFF);
3 3
4 4 var rhodeCodeApp = Polymer({
5 5 is: 'rhodecode-app',
6 6 created: function () {
7 7 ccLog.debug('rhodeCodeApp created');
8 8 $.Topic('/notifications').subscribe(this.handleNotifications.bind(this));
9 9
10 10 $.Topic('/plugins/__REGISTER__').subscribe(
11 11 this.kickoffChannelstreamPlugin.bind(this)
12 12 );
13 13
14 14 $.Topic('/connection_controller/subscribe').subscribe(
15 15 this.subscribeToChannelTopic.bind(this));
16 16 },
17 17
18 18 /** proxy to channelstream connection */
19 19 getChannelStreamConnection: function () {
20 20 return this.$['channelstream-connection'];
21 21 },
22 22
23 23 handleNotifications: function (data) {
24 24 this.$['notifications'].handleNotification(data);
25 25 },
26 26
27 27 /** opens connection to ws server */
28 28 kickoffChannelstreamPlugin: function (data) {
29 29 ccLog.debug('kickoffChannelstreamPlugin');
30 30 var channels = ['broadcast'];
31 31 var addChannels = this.checkViewChannels();
32 32 for (var i = 0; i < addChannels.length; i++) {
33 33 channels.push(addChannels[i]);
34 34 }
35 if (window.CHANNELSTREAM_SETTINGS && CHANNELSTREAM_SETTINGS.enabled){
35 36 var channelstreamConnection = this.$['channelstream-connection'];
36 37 channelstreamConnection.connectUrl = CHANNELSTREAM_URLS.connect;
37 38 channelstreamConnection.subscribeUrl = CHANNELSTREAM_URLS.subscribe;
38 39 channelstreamConnection.websocketUrl = CHANNELSTREAM_URLS.ws + '/ws';
39 40 channelstreamConnection.longPollUrl = CHANNELSTREAM_URLS.longpoll + '/listen';
40 41 // some channels might already be registered by topic
41 42 for (var i = 0; i < channels.length; i++) {
42 43 channelstreamConnection.push('channels', channels[i]);
43 44 }
44 45 // append any additional channels registered in other plugins
45 46 $.Topic('/connection_controller/subscribe').processPrepared();
46 47 channelstreamConnection.connect();
48 }
47 49 },
48 50
49 51 checkViewChannels: function () {
50 52 var channels = []
51 53 // subscribe to PR repo channel for PR's'
52 54 if (templateContext.pull_request_data.pull_request_id) {
53 55 var channelName = '/repo$' + templateContext.repo_name + '$/pr/' +
54 56 String(templateContext.pull_request_data.pull_request_id);
55 57 channels.push(channelName);
56 58 }
57 59 return channels;
58 60 },
59 61
60 62 /** subscribes users from channels in channelstream */
61 63 subscribeToChannelTopic: function (channels) {
62 64 var channelstreamConnection = this.$['channelstream-connection'];
63 65 var toSubscribe = channelstreamConnection.calculateSubscribe(channels);
64 66 ccLog.debug('subscribeToChannelTopic', toSubscribe);
65 67 if (toSubscribe.length > 0) {
66 68 // if we are connected then subscribe
67 69 if (channelstreamConnection.connected) {
68 70 channelstreamConnection.subscribe(toSubscribe);
69 71 }
70 72 // not connected? just push channels onto the stack
71 73 else {
72 74 for (var i = 0; i < toSubscribe.length; i++) {
73 75 channelstreamConnection.push('channels', toSubscribe[i]);
74 76 }
75 77 }
76 78 }
77 79 },
78 80
79 81 /** publish received messages into correct topic */
80 82 receivedMessage: function (event) {
81 83 for (var i = 0; i < event.detail.length; i++) {
82 84 var message = event.detail[i];
83 85 if (message.message.topic) {
84 86 ccLog.debug('publishing', message.message.topic);
85 87 $.Topic(message.message.topic).publish(message);
86 88 }
87 89 else if (message.type === 'presence'){
88 90 $.Topic('/connection_controller/presence').publish(message);
89 91 }
90 92 else {
91 93 ccLog.warn('unhandled message', message);
92 94 }
93 95 }
94 96 },
95 97
96 98 handleConnected: function (event) {
97 99 var channelstreamConnection = this.$['channelstream-connection'];
98 100 channelstreamConnection.set('channelsState',
99 101 event.detail.channels_info);
100 102 channelstreamConnection.set('userState', event.detail.state);
101 103 channelstreamConnection.set('channels', event.detail.channels);
102 104 this.propagageChannelsState();
103 105 },
104 106 handleSubscribed: function (event) {
105 107 var channelstreamConnection = this.$['channelstream-connection'];
106 108 var channelInfo = event.detail.channels_info;
107 109 var channelKeys = Object.keys(event.detail.channels_info);
108 110 for (var i = 0; i < channelKeys.length; i++) {
109 111 var key = channelKeys[i];
110 112 channelstreamConnection.set(['channelsState', key], channelInfo[key]);
111 113 }
112 114 channelstreamConnection.set('channels', event.detail.channels);
113 115 this.propagageChannelsState();
114 116 },
115 117 /** propagates channel states on topics */
116 118 propagageChannelsState: function (event) {
117 119 var channelstreamConnection = this.$['channelstream-connection'];
118 120 var channel_data = channelstreamConnection.channelsState;
119 121 var channels = channelstreamConnection.channels;
120 122 for (var i = 0; i < channels.length; i++) {
121 123 var key = channels[i];
122 124 $.Topic('/connection_controller/channel_update').publish(
123 125 {channel: key, state: channel_data[key]}
124 126 );
125 127 }
126 128 }
127 129 });
General Comments 0
You need to be logged in to leave comments. Login now