collab_client: Factor out duplicate ACCEPT_COMMIT code

This commit is contained in:
Richard Hansen 2021-03-26 19:33:14 -04:00
parent dd09a3f12b
commit 81b9a2544d

View file

@ -119,15 +119,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
const newRev = msg.newRev; const newRev = msg.newRev;
rev = newRev; rev = newRev;
if (msg.type === 'ACCEPT_COMMIT') { if (msg.type === 'ACCEPT_COMMIT') {
editor.applyPreparedChangesetToBase(); acceptCommit();
setStateIdle();
callCatchingErrors('onInternalAction', () => {
callbacks.onInternalAction('commitAcceptedByServer');
});
callCatchingErrors('onConnectionTrouble', () => {
callbacks.onConnectionTrouble('OK');
});
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 || '');
@ -170,6 +162,18 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
} }
}; };
const acceptCommit = () => {
editor.applyPreparedChangesetToBase();
setStateIdle();
callCatchingErrors('onInternalAction', () => {
callbacks.onInternalAction('commitAcceptedByServer');
});
callCatchingErrors('onConnectionTrouble', () => {
callbacks.onConnectionTrouble('OK');
});
handleUserChanges();
};
const setUpSocket = () => { const setUpSocket = () => {
setChannelState('CONNECTED'); setChannelState('CONNECTED');
doDeferredActions(); doDeferredActions();
@ -257,15 +261,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
return; return;
} }
rev = newRev; rev = newRev;
editor.applyPreparedChangesetToBase(); acceptCommit();
setStateIdle();
callCatchingErrors('onInternalAction', () => {
callbacks.onInternalAction('commitAcceptedByServer');
});
callCatchingErrors('onConnectionTrouble', () => {
callbacks.onConnectionTrouble('OK');
});
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 sends a CLIENT_RECONNECT message when there is a client reconnect.
// Server also returns all pending revisions along with this CLIENT_RECONNECT message // Server also returns all pending revisions along with this CLIENT_RECONNECT message
@ -301,15 +297,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
rev = newRev; rev = newRev;
if (author === pad.getUserId()) { if (author === pad.getUserId()) {
editor.applyPreparedChangesetToBase(); acceptCommit();
setStateIdle();
callCatchingErrors('onInternalAction', () => {
callbacks.onInternalAction('commitAcceptedByServer');
});
callCatchingErrors('onConnectionTrouble', () => {
callbacks.onConnectionTrouble('OK');
});
handleUserChanges();
} else { } else {
editor.applyChangesToBase(changeset, author, apool); editor.applyChangesToBase(changeset, author, apool);
} }