Skip to content

Commit 8ab1dec

Browse files
committed
Add checks
1 parent 156fa0b commit 8ab1dec

2 files changed

Lines changed: 60 additions & 37 deletions

File tree

nodejshelper/design/nodejshelpertheme/js/customjs-admin.js

Lines changed: 59 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,27 @@ var channelList = [];
2323
});
2424

2525
function addChatToNodeJS(chat_id) {
26-
if (typeof channelList[chat_id] === 'undefined')
27-
{
28-
channelList[chat_id] = socket.subscribe('chat_' + chat_id);
29-
30-
channelList[chat_id].on('subscribeFail', function (err) {
31-
console.error('Failed to subscribe to the sample channel due to error: ' + err);
32-
});
33-
34-
var typingIndicator = $('#user-is-typing-'+chat_id);
35-
36-
channelList[chat_id].watch(function (op) {
37-
if (op.op == 'vt') { // Visitor typing text
38-
typingIndicator.text(op.msg).css('visibility','visible');
39-
} else if (op.op == 'vts') { // Visitor typing stopped
40-
typingIndicator.text(op.msg).css('visibility','hidden');
41-
}
42-
});
26+
try {
27+
if (typeof channelList[chat_id] === 'undefined')
28+
{
29+
channelList[chat_id] = socket.subscribe('chat_' + chat_id);
30+
31+
channelList[chat_id].on('subscribeFail', function (err) {
32+
console.error('Failed to subscribe to the sample channel due to error: ' + err);
33+
});
34+
35+
var typingIndicator = $('#user-is-typing-'+chat_id);
36+
37+
channelList[chat_id].watch(function (op) {
38+
if (op.op == 'vt') { // Visitor typing text
39+
typingIndicator.text(op.msg).css('visibility','visible');
40+
} else if (op.op == 'vts') { // Visitor typing stopped
41+
typingIndicator.text(op.msg).css('visibility','hidden');
42+
}
43+
});
44+
}
45+
} catch (e) {
46+
console.log(e);
4347
}
4448
}
4549

@@ -50,33 +54,52 @@ var channelList = [];
5054
}
5155

5256
function removeSynchroChatListener(chat_id) {
53-
channelList[chat_id].destroy();
54-
delete channelList[chat_id];
57+
try {
58+
if (typeof channelList[chat_id] !== 'undefined') {
59+
channelList[chat_id].destroy();
60+
delete channelList[chat_id];
61+
}
62+
} catch (e) {
63+
console.log(e);
64+
}
5565
}
5666

5767
socket.on('close', function() {
58-
lhinst.nodeJsMode = false;
59-
channelList.forEach(function(channel){
60-
channel.destroy();
61-
});
62-
channelList = [];
63-
64-
ee.removeListener('chatTabLoaded', addChatToNodeJS);
65-
ee.removeListener('operatorTyping', operatorTypingListener);
66-
ee.removeListener('removeSynchroChat', removeSynchroChatListener);
68+
try {
69+
70+
lhinst.nodeJsMode = false;
71+
channelList.forEach(function(channel){
72+
if (typeof channel !== 'undefined') {
73+
channel.destroy();
74+
}
75+
});
76+
77+
channelList = [];
78+
79+
ee.removeListener('chatTabLoaded', addChatToNodeJS);
80+
ee.removeListener('operatorTyping', operatorTypingListener);
81+
ee.removeListener('removeSynchroChat', removeSynchroChatListener);
82+
83+
} catch (e) {
84+
console.log(e);
85+
}
6786
});
6887

6988
socket.on('connect', function () {
7089

71-
lhinst.nodeJsMode = true;
90+
try {
91+
lhinst.nodeJsMode = true;
7292

73-
lhinst.chatsSynchronising.forEach(function (chat_id) {
74-
addChatToNodeJS(chat_id);
75-
});
76-
77-
ee.addListener('chatTabLoaded', addChatToNodeJS);
78-
ee.addListener('operatorTyping', operatorTypingListener);
79-
ee.addListener('removeSynchroChat', removeSynchroChatListener);
93+
lhinst.chatsSynchronising.forEach(function (chat_id) {
94+
addChatToNodeJS(chat_id);
95+
});
96+
97+
ee.addListener('chatTabLoaded', addChatToNodeJS);
98+
ee.addListener('operatorTyping', operatorTypingListener);
99+
ee.addListener('removeSynchroChat', removeSynchroChatListener);
100+
} catch (e) {
101+
console.log(e);
102+
}
80103
});
81104

82105
})();

0 commit comments

Comments
 (0)