lint: collab-client (#4574)
* lint: collab-client * Undo incorrect lint fixes These will be re-fixed in a future commit. * Properly fix guard-for-in error * Properly fix prefer-rest-params errors * Move some code back to where it was Moving the code makes it hard to review the diff. * Delete DISCONNECT_REASON case Someone reading the code won't understand what "used to handle appLevelDisconnectReason" means until they dig through the Git history. Given the server never sends messages of type DISCONNECT_REASON anyway, just delete the case. * Refine lint fixes Co-authored-by: Richard Hansen <rhansen@rhansen.org>pull/4599/head
parent
d34a70c940
commit
4aef15cb11
|
@ -1,3 +1,5 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
||||||
* This helps other people to understand this code better and helps them to improve it.
|
* This helps other people to understand this code better and helps them to improve it.
|
||||||
|
@ -22,13 +24,12 @@
|
||||||
|
|
||||||
const chat = require('./chat').chat;
|
const chat = require('./chat').chat;
|
||||||
const hooks = require('./pluginfw/hooks');
|
const hooks = require('./pluginfw/hooks');
|
||||||
|
const browser = require('./browser');
|
||||||
|
|
||||||
// Dependency fill on init. This exists for `pad.socket` only.
|
// Dependency fill on init. This exists for `pad.socket` only.
|
||||||
// TODO: bind directly to the socket.
|
// TODO: bind directly to the socket.
|
||||||
let pad = undefined;
|
let pad = undefined;
|
||||||
function getSocket() {
|
const getSocket = () => pad && pad.socket;
|
||||||
return pad && pad.socket;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Call this when the document is ready, and a new Ace2Editor() has been created and inited.
|
/** Call this when the document is ready, and a new Ace2Editor() has been created and inited.
|
||||||
ACE's ready callback does not need to have fired yet.
|
ACE's ready callback does not need to have fired yet.
|
||||||
|
@ -38,13 +39,9 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
pad = _pad; // Inject pad to avoid a circular dependency.
|
pad = _pad; // Inject pad to avoid a circular dependency.
|
||||||
|
|
||||||
let rev = serverVars.rev;
|
let rev = serverVars.rev;
|
||||||
const padId = serverVars.padId;
|
|
||||||
|
|
||||||
let state = 'IDLE';
|
let state = 'IDLE';
|
||||||
let stateMessage;
|
let stateMessage;
|
||||||
let channelState = 'CONNECTING';
|
let channelState = 'CONNECTING';
|
||||||
let appLevelDisconnectReason = null;
|
|
||||||
|
|
||||||
let lastCommitTime = 0;
|
let lastCommitTime = 0;
|
||||||
let initialStartConnectTime = 0;
|
let initialStartConnectTime = 0;
|
||||||
|
|
||||||
|
@ -56,46 +53,34 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
const caughtErrors = [];
|
const caughtErrors = [];
|
||||||
const caughtErrorCatchers = [];
|
const caughtErrorCatchers = [];
|
||||||
const caughtErrorTimes = [];
|
const caughtErrorTimes = [];
|
||||||
const debugMessages = [];
|
|
||||||
const msgQueue = [];
|
const msgQueue = [];
|
||||||
|
|
||||||
let isPendingRevision = false;
|
let isPendingRevision = false;
|
||||||
|
|
||||||
tellAceAboutHistoricalAuthors(serverVars.historicalAuthorData);
|
|
||||||
tellAceActiveAuthorInfo(initialUserInfo);
|
|
||||||
|
|
||||||
const callbacks = {
|
const callbacks = {
|
||||||
onUserJoin() {},
|
onUserJoin: () => {},
|
||||||
onUserLeave() {},
|
onUserLeave: () => {},
|
||||||
onUpdateUserInfo() {},
|
onUpdateUserInfo: () => {},
|
||||||
onChannelStateChange() {},
|
onChannelStateChange: () => {},
|
||||||
onClientMessage() {},
|
onClientMessage: () => {},
|
||||||
onInternalAction() {},
|
onInternalAction: () => {},
|
||||||
onConnectionTrouble() {},
|
onConnectionTrouble: () => {},
|
||||||
|
onServerMessage: () => {},
|
||||||
};
|
};
|
||||||
if (browser.firefox) {
|
if (browser.firefox) {
|
||||||
// Prevent "escape" from taking effect and canceling a comet connection;
|
// Prevent "escape" from taking effect and canceling a comet connection;
|
||||||
// doesn't work if focus is on an iframe.
|
// doesn't work if focus is on an iframe.
|
||||||
$(window).bind('keydown', (evt) => {
|
$(window).bind('keydown', (evt) => {
|
||||||
if (evt.which == 27) {
|
if (evt.which === 27) {
|
||||||
evt.preventDefault();
|
evt.preventDefault();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
editor.setProperty('userAuthor', userId);
|
const handleUserChanges = () => {
|
||||||
editor.setBaseAttributedText(serverVars.initialAttributedText, serverVars.apool);
|
|
||||||
editor.setUserChangeNotificationCallback(wrapRecordingErrors('handleUserChanges', handleUserChanges));
|
|
||||||
|
|
||||||
function dmesg(str) {
|
|
||||||
if (typeof window.ajlog === 'string') window.ajlog += `${str}\n`;
|
|
||||||
debugMessages.push(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleUserChanges() {
|
|
||||||
if (editor.getInInternationalComposition()) return;
|
if (editor.getInInternationalComposition()) return;
|
||||||
if ((!getSocket()) || channelState == 'CONNECTING') {
|
if ((!getSocket()) || channelState === 'CONNECTING') {
|
||||||
if (channelState == 'CONNECTING' && (((+new Date()) - initialStartConnectTime) > 20000)) {
|
if (channelState === 'CONNECTING' && (((+new Date()) - initialStartConnectTime) > 20000)) {
|
||||||
setChannelState('DISCONNECTED', 'initsocketfail');
|
setChannelState('DISCONNECTED', 'initsocketfail');
|
||||||
} else {
|
} else {
|
||||||
// check again in a bit
|
// check again in a bit
|
||||||
|
@ -106,11 +91,11 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
|
|
||||||
const t = (+new Date());
|
const t = (+new Date());
|
||||||
|
|
||||||
if (state != 'IDLE') {
|
if (state !== 'IDLE') {
|
||||||
if (state == 'COMMITTING' && msgQueue.length == 0 && (t - lastCommitTime) > 20000) {
|
if (state === 'COMMITTING' && msgQueue.length === 0 && (t - lastCommitTime) > 20000) {
|
||||||
// a commit is taking too long
|
// a commit is taking too long
|
||||||
setChannelState('DISCONNECTED', 'slowcommit');
|
setChannelState('DISCONNECTED', 'slowcommit');
|
||||||
} else if (state == 'COMMITTING' && msgQueue.length == 0 && (t - lastCommitTime) > 5000) {
|
} else if (state === 'COMMITTING' && msgQueue.length === 0 && (t - lastCommitTime) > 5000) {
|
||||||
callbacks.onConnectionTrouble('SLOW');
|
callbacks.onConnectionTrouble('SLOW');
|
||||||
} else {
|
} else {
|
||||||
// run again in a few seconds, to detect a disconnect
|
// run again in a few seconds, to detect a disconnect
|
||||||
|
@ -121,17 +106,19 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
|
|
||||||
const earliestCommit = lastCommitTime + 500;
|
const earliestCommit = lastCommitTime + 500;
|
||||||
if (t < earliestCommit) {
|
if (t < earliestCommit) {
|
||||||
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), earliestCommit - t);
|
setTimeout(
|
||||||
|
wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges),
|
||||||
|
earliestCommit - t);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply msgQueue changeset.
|
// apply msgQueue changeset.
|
||||||
if (msgQueue.length != 0) {
|
if (msgQueue.length !== 0) {
|
||||||
let msg;
|
let msg;
|
||||||
while (msg = msgQueue.shift()) {
|
while ((msg = msgQueue.shift())) {
|
||||||
const newRev = msg.newRev;
|
const newRev = msg.newRev;
|
||||||
rev = newRev;
|
rev = newRev;
|
||||||
if (msg.type == 'ACCEPT_COMMIT') {
|
if (msg.type === 'ACCEPT_COMMIT') {
|
||||||
editor.applyPreparedChangesetToBase();
|
editor.applyPreparedChangesetToBase();
|
||||||
setStateIdle();
|
setStateIdle();
|
||||||
callCatchingErrors('onInternalAction', () => {
|
callCatchingErrors('onInternalAction', () => {
|
||||||
|
@ -141,7 +128,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
callbacks.onConnectionTrouble('OK');
|
callbacks.onConnectionTrouble('OK');
|
||||||
});
|
});
|
||||||
handleUserChanges();
|
handleUserChanges();
|
||||||
} else if (msg.type == 'NEW_CHANGES') {
|
} else if (msg.type === 'NEW_CHANGES') {
|
||||||
const changeset = msg.changeset;
|
const changeset = msg.changeset;
|
||||||
const author = (msg.author || '');
|
const author = (msg.author || '');
|
||||||
const apool = msg.apool;
|
const apool = msg.apool;
|
||||||
|
@ -181,67 +168,60 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
// run again in a few seconds, to detect a disconnect
|
// run again in a few seconds, to detect a disconnect
|
||||||
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), 3000);
|
setTimeout(wrapRecordingErrors('setTimeout(handleUserChanges)', handleUserChanges), 3000);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function setUpSocket() {
|
const setUpSocket = () => {
|
||||||
hiccupCount = 0;
|
|
||||||
setChannelState('CONNECTED');
|
setChannelState('CONNECTED');
|
||||||
doDeferredActions();
|
doDeferredActions();
|
||||||
|
|
||||||
initialStartConnectTime = +new Date();
|
initialStartConnectTime = +new Date();
|
||||||
}
|
};
|
||||||
|
|
||||||
var hiccupCount = 0;
|
const sendMessage = (msg) => {
|
||||||
|
|
||||||
function sendMessage(msg) {
|
|
||||||
getSocket().json.send(
|
getSocket().json.send(
|
||||||
{
|
{
|
||||||
type: 'COLLABROOM',
|
type: 'COLLABROOM',
|
||||||
component: 'pad',
|
component: 'pad',
|
||||||
data: msg,
|
data: msg,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function wrapRecordingErrors(catcher, func) {
|
const wrapRecordingErrors = (catcher, func) => function (...args) {
|
||||||
return function () {
|
try {
|
||||||
try {
|
return func.call(this, ...args);
|
||||||
return func.apply(this, Array.prototype.slice.call(arguments));
|
} catch (e) {
|
||||||
} catch (e) {
|
caughtErrors.push(e);
|
||||||
caughtErrors.push(e);
|
caughtErrorCatchers.push(catcher);
|
||||||
caughtErrorCatchers.push(catcher);
|
caughtErrorTimes.push(+new Date());
|
||||||
caughtErrorTimes.push(+new Date());
|
// console.dir({catcher: catcher, e: e});
|
||||||
// console.dir({catcher: catcher, e: e});
|
throw e;
|
||||||
throw e;
|
}
|
||||||
}
|
};
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function callCatchingErrors(catcher, func) {
|
const callCatchingErrors = (catcher, func) => {
|
||||||
try {
|
try {
|
||||||
wrapRecordingErrors(catcher, func)();
|
wrapRecordingErrors(catcher, func)();
|
||||||
} catch (e) { /* absorb*/
|
} catch (e) { /* absorb*/
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function handleMessageFromServer(evt) {
|
const handleMessageFromServer = (evt) => {
|
||||||
if (!getSocket()) return;
|
if (!getSocket()) return;
|
||||||
if (!evt.data) return;
|
if (!evt.data) return;
|
||||||
const wrapper = evt;
|
const wrapper = evt;
|
||||||
if (wrapper.type != 'COLLABROOM' && wrapper.type != 'CUSTOM') return;
|
if (wrapper.type !== 'COLLABROOM' && wrapper.type !== 'CUSTOM') return;
|
||||||
const msg = wrapper.data;
|
const msg = wrapper.data;
|
||||||
|
|
||||||
if (msg.type == 'NEW_CHANGES') {
|
if (msg.type === 'NEW_CHANGES') {
|
||||||
var newRev = msg.newRev;
|
const newRev = msg.newRev;
|
||||||
var changeset = msg.changeset;
|
const changeset = msg.changeset;
|
||||||
var author = (msg.author || '');
|
const author = (msg.author || '');
|
||||||
var apool = msg.apool;
|
const apool = msg.apool;
|
||||||
|
|
||||||
// When inInternationalComposition, msg pushed msgQueue.
|
// When inInternationalComposition, msg pushed msgQueue.
|
||||||
if (msgQueue.length > 0 || editor.getInInternationalComposition()) {
|
if (msgQueue.length > 0 || editor.getInInternationalComposition()) {
|
||||||
if (msgQueue.length > 0) var oldRev = msgQueue[msgQueue.length - 1].newRev;
|
const oldRev = msgQueue.length > 0 ? msgQueue[msgQueue.length - 1].newRev : rev;
|
||||||
else oldRev = rev;
|
if (newRev !== (oldRev + 1)) {
|
||||||
|
|
||||||
if (newRev != (oldRev + 1)) {
|
|
||||||
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${oldRev + 1}`);
|
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${oldRev + 1}`);
|
||||||
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
||||||
return;
|
return;
|
||||||
|
@ -250,7 +230,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newRev != (rev + 1)) {
|
if (newRev !== (rev + 1)) {
|
||||||
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${rev + 1}`);
|
window.console.warn(`bad message revision on NEW_CHANGES: ${newRev} not ${rev + 1}`);
|
||||||
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
// setChannelState("DISCONNECTED", "badmessage_newchanges");
|
||||||
return;
|
return;
|
||||||
|
@ -258,11 +238,12 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
rev = newRev;
|
rev = newRev;
|
||||||
|
|
||||||
editor.applyChangesToBase(changeset, author, apool);
|
editor.applyChangesToBase(changeset, author, apool);
|
||||||
} else if (msg.type == 'ACCEPT_COMMIT') {
|
} else if (msg.type === 'ACCEPT_COMMIT') {
|
||||||
var newRev = msg.newRev;
|
const newRev = msg.newRev;
|
||||||
if (msgQueue.length > 0) {
|
if (msgQueue.length > 0) {
|
||||||
if (newRev != (msgQueue[msgQueue.length - 1].newRev + 1)) {
|
if (newRev !== (msgQueue[msgQueue.length - 1].newRev + 1)) {
|
||||||
window.console.warn(`bad message revision on ACCEPT_COMMIT: ${newRev} not ${msgQueue[msgQueue.length - 1][0] + 1}`);
|
window.console.warn('bad message revision on ACCEPT_COMMIT: ' +
|
||||||
|
`${newRev} not ${msgQueue[msgQueue.length - 1][0] + 1}`);
|
||||||
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -270,7 +251,7 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newRev != (rev + 1)) {
|
if (newRev !== (rev + 1)) {
|
||||||
window.console.warn(`bad message revision on ACCEPT_COMMIT: ${newRev} not ${rev + 1}`);
|
window.console.warn(`bad message revision on ACCEPT_COMMIT: ${newRev} not ${rev + 1}`);
|
||||||
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
||||||
return;
|
return;
|
||||||
|
@ -285,9 +266,9 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
callbacks.onConnectionTrouble('OK');
|
callbacks.onConnectionTrouble('OK');
|
||||||
});
|
});
|
||||||
handleUserChanges();
|
handleUserChanges();
|
||||||
} else if (msg.type == 'CLIENT_RECONNECT') {
|
} else if (msg.type === 'CLIENT_RECONNECT') {
|
||||||
// Server sends a CLIENT_RECONNECT message when there is a client reconnect. Server also returns
|
// Server sends a CLIENT_RECONNECT message when there is a client reconnect.
|
||||||
// all pending revisions along with this CLIENT_RECONNECT message
|
// Server also returns all pending revisions along with this CLIENT_RECONNECT message
|
||||||
if (msg.noChanges) {
|
if (msg.noChanges) {
|
||||||
// If no revisions are pending, just make everything normal
|
// If no revisions are pending, just make everything normal
|
||||||
setIsPendingRevision(false);
|
setIsPendingRevision(false);
|
||||||
|
@ -295,14 +276,15 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
}
|
}
|
||||||
|
|
||||||
const headRev = msg.headRev;
|
const headRev = msg.headRev;
|
||||||
var newRev = msg.newRev;
|
const newRev = msg.newRev;
|
||||||
var changeset = msg.changeset;
|
const changeset = msg.changeset;
|
||||||
var author = (msg.author || '');
|
const author = (msg.author || '');
|
||||||
var apool = msg.apool;
|
const apool = msg.apool;
|
||||||
|
|
||||||
if (msgQueue.length > 0) {
|
if (msgQueue.length > 0) {
|
||||||
if (newRev != (msgQueue[msgQueue.length - 1].newRev + 1)) {
|
if (newRev !== (msgQueue[msgQueue.length - 1].newRev + 1)) {
|
||||||
window.console.warn(`bad message revision on CLIENT_RECONNECT: ${newRev} not ${msgQueue[msgQueue.length - 1][0] + 1}`);
|
window.console.warn('bad message revision on CLIENT_RECONNECT: ' +
|
||||||
|
`${newRev} not ${msgQueue[msgQueue.length - 1][0] + 1}`);
|
||||||
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -311,14 +293,14 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newRev != (rev + 1)) {
|
if (newRev !== (rev + 1)) {
|
||||||
window.console.warn(`bad message revision on CLIENT_RECONNECT: ${newRev} not ${rev + 1}`);
|
window.console.warn(`bad message revision on CLIENT_RECONNECT: ${newRev} not ${rev + 1}`);
|
||||||
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
// setChannelState("DISCONNECTED", "badmessage_acceptcommit");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
rev = newRev;
|
rev = newRev;
|
||||||
if (author == pad.getUserId()) {
|
if (author === pad.getUserId()) {
|
||||||
editor.applyPreparedChangesetToBase();
|
editor.applyPreparedChangesetToBase();
|
||||||
setStateIdle();
|
setStateIdle();
|
||||||
callCatchingErrors('onInternalAction', () => {
|
callCatchingErrors('onInternalAction', () => {
|
||||||
|
@ -332,19 +314,19 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
editor.applyChangesToBase(changeset, author, apool);
|
editor.applyChangesToBase(changeset, author, apool);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newRev == headRev) {
|
if (newRev === headRev) {
|
||||||
// Once we have applied all pending revisions, make everything normal
|
// Once we have applied all pending revisions, make everything normal
|
||||||
setIsPendingRevision(false);
|
setIsPendingRevision(false);
|
||||||
}
|
}
|
||||||
} else if (msg.type == 'NO_COMMIT_PENDING') {
|
} else if (msg.type === 'NO_COMMIT_PENDING') {
|
||||||
if (state == 'COMMITTING') {
|
if (state === 'COMMITTING') {
|
||||||
// server missed our commit message; abort that commit
|
// server missed our commit message; abort that commit
|
||||||
setStateIdle();
|
setStateIdle();
|
||||||
handleUserChanges();
|
handleUserChanges();
|
||||||
}
|
}
|
||||||
} else if (msg.type == 'USER_NEWINFO') {
|
} else if (msg.type === 'USER_NEWINFO') {
|
||||||
var userInfo = msg.userInfo;
|
const userInfo = msg.userInfo;
|
||||||
var id = userInfo.userId;
|
const id = userInfo.userId;
|
||||||
|
|
||||||
// Avoid a race condition when setting colors. If our color was set by a
|
// Avoid a race condition when setting colors. If our color was set by a
|
||||||
// query param, ignore our own "new user" message's color value.
|
// query param, ignore our own "new user" message's color value.
|
||||||
|
@ -361,21 +343,19 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
callbacks.onUserJoin(userInfo);
|
callbacks.onUserJoin(userInfo);
|
||||||
}
|
}
|
||||||
tellAceActiveAuthorInfo(userInfo);
|
tellAceActiveAuthorInfo(userInfo);
|
||||||
} else if (msg.type == 'USER_LEAVE') {
|
} else if (msg.type === 'USER_LEAVE') {
|
||||||
var userInfo = msg.userInfo;
|
const userInfo = msg.userInfo;
|
||||||
var id = userInfo.userId;
|
const id = userInfo.userId;
|
||||||
if (userSet[id]) {
|
if (userSet[id]) {
|
||||||
delete userSet[userInfo.userId];
|
delete userSet[userInfo.userId];
|
||||||
fadeAceAuthorInfo(userInfo);
|
fadeAceAuthorInfo(userInfo);
|
||||||
callbacks.onUserLeave(userInfo);
|
callbacks.onUserLeave(userInfo);
|
||||||
}
|
}
|
||||||
} else if (msg.type == 'DISCONNECT_REASON') {
|
} else if (msg.type === 'CLIENT_MESSAGE') {
|
||||||
appLevelDisconnectReason = msg.reason;
|
|
||||||
} else if (msg.type == 'CLIENT_MESSAGE') {
|
|
||||||
callbacks.onClientMessage(msg.payload);
|
callbacks.onClientMessage(msg.payload);
|
||||||
} else if (msg.type == 'CHAT_MESSAGE') {
|
} else if (msg.type === 'CHAT_MESSAGE') {
|
||||||
chat.addMessage(msg, true, false);
|
chat.addMessage(msg, true, false);
|
||||||
} else if (msg.type == 'CHAT_MESSAGES') {
|
} else if (msg.type === 'CHAT_MESSAGES') {
|
||||||
for (let i = msg.messages.length - 1; i >= 0; i--) {
|
for (let i = msg.messages.length - 1; i >= 0; i--) {
|
||||||
chat.addMessage(msg.messages[i], true, true);
|
chat.addMessage(msg.messages[i], true, true);
|
||||||
}
|
}
|
||||||
|
@ -389,12 +369,18 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
$('#chatloadmessagesball').css('display', 'none');
|
$('#chatloadmessagesball').css('display', 'none');
|
||||||
|
|
||||||
// there are less than 100 messages or we reached the top
|
// there are less than 100 messages or we reached the top
|
||||||
if (chat.historyPointer <= 0) { $('#chatloadmessagesbutton').css('display', 'none'); } else // there are still more messages, re-show the load-button
|
if (chat.historyPointer <= 0) {
|
||||||
{ $('#chatloadmessagesbutton').css('display', 'block'); }
|
$('#chatloadmessagesbutton').css('display', 'none');
|
||||||
|
} else {
|
||||||
|
// there are still more messages, re-show the load-button
|
||||||
|
$('#chatloadmessagesbutton').css('display', 'block');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// HACKISH: User messages do not have "payload" but "userInfo", so that all "handleClientMessage_USER_" hooks would work, populate payload
|
// HACKISH: User messages do not have "payload" but "userInfo", so that all
|
||||||
// FIXME: USER_* messages to have "payload" property instead of "userInfo", seems like a quite a big work
|
// "handleClientMessage_USER_" hooks would work, populate payload
|
||||||
|
// FIXME: USER_* messages to have "payload" property instead of "userInfo",
|
||||||
|
// seems like a quite a big work
|
||||||
if (msg.type.indexOf('USER_') > -1) {
|
if (msg.type.indexOf('USER_') > -1) {
|
||||||
msg.payload = msg.userInfo;
|
msg.payload = msg.userInfo;
|
||||||
}
|
}
|
||||||
|
@ -402,9 +388,9 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
if (msg.type === 'NEW_CHANGES') msg.payload = msg;
|
if (msg.type === 'NEW_CHANGES') msg.payload = msg;
|
||||||
|
|
||||||
hooks.callAll(`handleClientMessage_${msg.type}`, {payload: msg.payload});
|
hooks.callAll(`handleClientMessage_${msg.type}`, {payload: msg.payload});
|
||||||
}
|
};
|
||||||
|
|
||||||
function updateUserInfo(userInfo) {
|
const updateUserInfo = (userInfo) => {
|
||||||
userInfo.userId = userId;
|
userInfo.userId = userId;
|
||||||
userSet[userId] = userInfo;
|
userSet[userId] = userInfo;
|
||||||
tellAceActiveAuthorInfo(userInfo);
|
tellAceActiveAuthorInfo(userInfo);
|
||||||
|
@ -414,13 +400,13 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
type: 'USERINFO_UPDATE',
|
type: 'USERINFO_UPDATE',
|
||||||
userInfo,
|
userInfo,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function tellAceActiveAuthorInfo(userInfo) {
|
const tellAceActiveAuthorInfo = (userInfo) => {
|
||||||
tellAceAuthorInfo(userInfo.userId, userInfo.colorId);
|
tellAceAuthorInfo(userInfo.userId, userInfo.colorId);
|
||||||
}
|
};
|
||||||
|
|
||||||
function tellAceAuthorInfo(userId, colorId, inactive) {
|
const tellAceAuthorInfo = (userId, colorId, inactive) => {
|
||||||
if (typeof colorId === 'number') {
|
if (typeof colorId === 'number') {
|
||||||
colorId = clientVars.colorPalette[colorId];
|
colorId = clientVars.colorPalette[colorId];
|
||||||
}
|
}
|
||||||
|
@ -436,91 +422,81 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
bgcolor: cssColor,
|
bgcolor: cssColor,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function fadeAceAuthorInfo(userInfo) {
|
const fadeAceAuthorInfo = (userInfo) => {
|
||||||
tellAceAuthorInfo(userInfo.userId, userInfo.colorId, true);
|
tellAceAuthorInfo(userInfo.userId, userInfo.colorId, true);
|
||||||
}
|
};
|
||||||
|
|
||||||
function getConnectedUsers() {
|
const getConnectedUsers = () => valuesArray(userSet);
|
||||||
return valuesArray(userSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
function tellAceAboutHistoricalAuthors(hadata) {
|
const tellAceAboutHistoricalAuthors = (hadata) => {
|
||||||
for (const author in hadata) {
|
for (const [author, data] of Object.entries(hadata)) {
|
||||||
const data = hadata[author];
|
|
||||||
if (!userSet[author]) {
|
if (!userSet[author]) {
|
||||||
tellAceAuthorInfo(author, data.colorId, true);
|
tellAceAuthorInfo(author, data.colorId, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function setChannelState(newChannelState, moreInfo) {
|
const setChannelState = (newChannelState, moreInfo) => {
|
||||||
if (newChannelState != channelState) {
|
if (newChannelState !== channelState) {
|
||||||
channelState = newChannelState;
|
channelState = newChannelState;
|
||||||
callbacks.onChannelStateChange(channelState, moreInfo);
|
callbacks.onChannelStateChange(channelState, moreInfo);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
function valuesArray(obj) {
|
const valuesArray = (obj) => {
|
||||||
const array = [];
|
const array = [];
|
||||||
$.each(obj, (k, v) => {
|
$.each(obj, (k, v) => {
|
||||||
array.push(v);
|
array.push(v);
|
||||||
});
|
});
|
||||||
return array;
|
return array;
|
||||||
}
|
};
|
||||||
|
|
||||||
// We need to present a working interface even before the socket
|
// We need to present a working interface even before the socket
|
||||||
// is connected for the first time.
|
// is connected for the first time.
|
||||||
let deferredActions = [];
|
let deferredActions = [];
|
||||||
|
|
||||||
function defer(func, tag) {
|
const defer = (func, tag) => function (...args) {
|
||||||
return function () {
|
const action = () => {
|
||||||
const that = this;
|
func.call(this, ...args);
|
||||||
const args = arguments;
|
|
||||||
|
|
||||||
function action() {
|
|
||||||
func.apply(that, args);
|
|
||||||
}
|
|
||||||
action.tag = tag;
|
|
||||||
if (channelState == 'CONNECTING') {
|
|
||||||
deferredActions.push(action);
|
|
||||||
} else {
|
|
||||||
action();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
action.tag = tag;
|
||||||
|
if (channelState === 'CONNECTING') {
|
||||||
|
deferredActions.push(action);
|
||||||
|
} else {
|
||||||
|
action();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
function doDeferredActions(tag) {
|
const doDeferredActions = (tag) => {
|
||||||
const newArray = [];
|
const newArray = [];
|
||||||
for (let i = 0; i < deferredActions.length; i++) {
|
for (let i = 0; i < deferredActions.length; i++) {
|
||||||
const a = deferredActions[i];
|
const a = deferredActions[i];
|
||||||
if ((!tag) || (tag == a.tag)) {
|
if ((!tag) || (tag === a.tag)) {
|
||||||
a();
|
a();
|
||||||
} else {
|
} else {
|
||||||
newArray.push(a);
|
newArray.push(a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
deferredActions = newArray;
|
deferredActions = newArray;
|
||||||
}
|
};
|
||||||
|
|
||||||
function sendClientMessage(msg) {
|
const sendClientMessage = (msg) => {
|
||||||
sendMessage(
|
sendMessage(
|
||||||
{
|
{
|
||||||
type: 'CLIENT_MESSAGE',
|
type: 'CLIENT_MESSAGE',
|
||||||
payload: msg,
|
payload: msg,
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
function getCurrentRevisionNumber() {
|
const getCurrentRevisionNumber = () => rev;
|
||||||
return rev;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMissedChanges() {
|
const getMissedChanges = () => {
|
||||||
const obj = {};
|
const obj = {};
|
||||||
obj.userInfo = userSet[userId];
|
obj.userInfo = userSet[userId];
|
||||||
obj.baseRev = rev;
|
obj.baseRev = rev;
|
||||||
if (state == 'COMMITTING' && stateMessage) {
|
if (state === 'COMMITTING' && stateMessage) {
|
||||||
obj.committedChangeset = stateMessage.changeset;
|
obj.committedChangeset = stateMessage.changeset;
|
||||||
obj.committedChangesetAPool = stateMessage.apool;
|
obj.committedChangesetAPool = stateMessage.apool;
|
||||||
editor.applyPreparedChangesetToBase();
|
editor.applyPreparedChangesetToBase();
|
||||||
|
@ -531,56 +507,56 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
obj.furtherChangesetAPool = userChangesData.apool;
|
obj.furtherChangesetAPool = userChangesData.apool;
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
};
|
||||||
|
|
||||||
function setStateIdle() {
|
const setStateIdle = () => {
|
||||||
state = 'IDLE';
|
state = 'IDLE';
|
||||||
callbacks.onInternalAction('newlyIdle');
|
callbacks.onInternalAction('newlyIdle');
|
||||||
schedulePerhapsCallIdleFuncs();
|
schedulePerhapsCallIdleFuncs();
|
||||||
}
|
};
|
||||||
|
|
||||||
function setIsPendingRevision(value) {
|
const setIsPendingRevision = (value) => {
|
||||||
isPendingRevision = value;
|
isPendingRevision = value;
|
||||||
}
|
};
|
||||||
|
|
||||||
function callWhenNotCommitting(func) {
|
const idleFuncs = [];
|
||||||
|
|
||||||
|
const callWhenNotCommitting = (func) => {
|
||||||
idleFuncs.push(func);
|
idleFuncs.push(func);
|
||||||
schedulePerhapsCallIdleFuncs();
|
schedulePerhapsCallIdleFuncs();
|
||||||
}
|
};
|
||||||
|
|
||||||
var idleFuncs = [];
|
const schedulePerhapsCallIdleFuncs = () => {
|
||||||
|
|
||||||
function schedulePerhapsCallIdleFuncs() {
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (state == 'IDLE') {
|
if (state === 'IDLE') {
|
||||||
while (idleFuncs.length > 0) {
|
while (idleFuncs.length > 0) {
|
||||||
const f = idleFuncs.shift();
|
const f = idleFuncs.shift();
|
||||||
f();
|
f();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, 0);
|
}, 0);
|
||||||
}
|
};
|
||||||
|
|
||||||
const self = {
|
const self = {
|
||||||
setOnUserJoin(cb) {
|
setOnUserJoin: (cb) => {
|
||||||
callbacks.onUserJoin = cb;
|
callbacks.onUserJoin = cb;
|
||||||
},
|
},
|
||||||
setOnUserLeave(cb) {
|
setOnUserLeave: (cb) => {
|
||||||
callbacks.onUserLeave = cb;
|
callbacks.onUserLeave = cb;
|
||||||
},
|
},
|
||||||
setOnUpdateUserInfo(cb) {
|
setOnUpdateUserInfo: (cb) => {
|
||||||
callbacks.onUpdateUserInfo = cb;
|
callbacks.onUpdateUserInfo = cb;
|
||||||
},
|
},
|
||||||
setOnChannelStateChange(cb) {
|
setOnChannelStateChange: (cb) => {
|
||||||
callbacks.onChannelStateChange = cb;
|
callbacks.onChannelStateChange = cb;
|
||||||
},
|
},
|
||||||
setOnClientMessage(cb) {
|
setOnClientMessage: (cb) => {
|
||||||
callbacks.onClientMessage = cb;
|
callbacks.onClientMessage = cb;
|
||||||
},
|
},
|
||||||
setOnInternalAction(cb) {
|
setOnInternalAction: (cb) => {
|
||||||
callbacks.onInternalAction = cb;
|
callbacks.onInternalAction = cb;
|
||||||
},
|
},
|
||||||
setOnConnectionTrouble(cb) {
|
setOnConnectionTrouble: (cb) => {
|
||||||
callbacks.onConnectionTrouble = cb;
|
callbacks.onConnectionTrouble = cb;
|
||||||
},
|
},
|
||||||
updateUserInfo: defer(updateUserInfo),
|
updateUserInfo: defer(updateUserInfo),
|
||||||
|
@ -597,6 +573,14 @@ function getCollabClient(ace2editor, serverVars, initialUserInfo, options, _pad)
|
||||||
setIsPendingRevision,
|
setIsPendingRevision,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tellAceAboutHistoricalAuthors(serverVars.historicalAuthorData);
|
||||||
|
tellAceActiveAuthorInfo(initialUserInfo);
|
||||||
|
|
||||||
|
editor.setProperty('userAuthor', userId);
|
||||||
|
editor.setBaseAttributedText(serverVars.initialAttributedText, serverVars.apool);
|
||||||
|
editor.setUserChangeNotificationCallback(
|
||||||
|
wrapRecordingErrors('handleUserChanges', handleUserChanges));
|
||||||
|
|
||||||
setUpSocket();
|
setUpSocket();
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue