collab_client: Use `Date.now()` instead of casting a Date object

Also rename the `t` variable to `now` to improve readability.
pull/4986/head
Richard Hansen 2021-03-26 20:20:54 -04:00
parent 5c445eac21
commit e99fe88537
1 changed files with 8 additions and 9 deletions

View File

@ -76,8 +76,9 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
const handleUserChanges = () => { const handleUserChanges = () => {
if (editor.getInInternationalComposition()) return; if (editor.getInInternationalComposition()) return;
const now = Date.now();
if ((!getSocket()) || channelState === 'CONNECTING') { if ((!getSocket()) || channelState === 'CONNECTING') {
if (channelState === 'CONNECTING' && (((+new Date()) - initialStartConnectTime) > 20000)) { if (channelState === 'CONNECTING' && (now - initialStartConnectTime) > 20000) {
setChannelState('DISCONNECTED', 'initsocketfail'); setChannelState('DISCONNECTED', 'initsocketfail');
} else { } else {
// check again in a bit // check again in a bit
@ -86,13 +87,11 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
return; return;
} }
const t = (+new Date());
if (committing) { if (committing) {
if (msgQueue.length === 0 && (t - lastCommitTime) > 20000) { if (msgQueue.length === 0 && (now - lastCommitTime) > 20000) {
// a commit is taking too long // a commit is taking too long
setChannelState('DISCONNECTED', 'slowcommit'); setChannelState('DISCONNECTED', 'slowcommit');
} else if (msgQueue.length === 0 && (t - lastCommitTime) > 5000) { } else if (msgQueue.length === 0 && (now - 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
@ -102,8 +101,8 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
} }
const earliestCommit = lastCommitTime + 500; const earliestCommit = lastCommitTime + 500;
if (t < earliestCommit) { if (now < earliestCommit) {
setTimeout(handleUserChanges, earliestCommit - t); setTimeout(handleUserChanges, earliestCommit - now);
return; return;
} }
@ -134,7 +133,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
if (!isPendingRevision) { if (!isPendingRevision) {
const userChangesData = editor.prepareUserChangeset(); const userChangesData = editor.prepareUserChangeset();
if (userChangesData.changeset) { if (userChangesData.changeset) {
lastCommitTime = t; lastCommitTime = now;
committing = true; committing = true;
stateMessage = { stateMessage = {
type: 'USER_CHANGES', type: 'USER_CHANGES',
@ -171,7 +170,7 @@ const getCollabClient = (ace2editor, serverVars, initialUserInfo, options, _pad)
setChannelState('CONNECTED'); setChannelState('CONNECTED');
doDeferredActions(); doDeferredActions();
initialStartConnectTime = +new Date(); initialStartConnectTime = Date.now();
}; };
const sendMessage = (msg) => { const sendMessage = (msg) => {