Replace timeOutAction() with debounce

Replace delegateRun()
Revert my throttle/debounce setTimeout() to Function.prototype[throttle/debounce]
This commit is contained in:
djmaze 2020-08-18 20:24:17 +02:00
parent f6a55898c7
commit 97a73c6639
28 changed files with 225 additions and 372 deletions

View file

@ -1,5 +1,4 @@
import ko from 'ko';
import { delegateRun } from 'Common/Utils';
import PgpStore from 'Stores/User/Pgp';
@ -83,7 +82,7 @@ class AddOpenPgpKeyPopupView extends AbstractViewNext {
return false;
}
delegateRun(this, 'cancelCommand');
this.cancelCommand && this.cancelCommand();
return true;
}

View file

@ -12,7 +12,6 @@ import {
} from 'Common/Enums';
import {
delegateRun,
isNonEmptyArray,
clearBqSwitcher,
replySubjectAdd,
@ -85,7 +84,7 @@ class ComposePopupView extends AbstractViewNext {
this.sLastFocusedField = 'to';
this.resizerTrigger = this.resizerTrigger.bind(this);
this.resizerTrigger = this.resizerTrigger.debounce(50).bind(this);
this.allowContacts = !!AppStore.contactsIsAllowed();
this.allowFolders = !!Settings.capa(Capa.Folders);
@ -148,17 +147,9 @@ class ComposePopupView extends AbstractViewNext {
}
});
this.savedError.subscribe((value) => {
if (!value) {
this.savedErrorDesc('');
}
});
this.savedError.subscribe(value => !value && this.savedErrorDesc(''));
this.sendSuccessButSaveError.subscribe((value) => {
if (!value) {
this.savedErrorDesc('');
}
});
this.sendSuccessButSaveError.subscribe(value => !value && this.savedErrorDesc(''));
this.savedTime = ko.observable(0);
this.savedTimeText = ko.computed(() =>
@ -297,9 +288,6 @@ class ComposePopupView extends AbstractViewNext {
this.canBeSentOrSaved = ko.computed(() => !this.sending() && !this.saving());
this.sendMessageResponse = this.sendMessageResponse.bind(this);
this.saveMessageResponse = this.saveMessageResponse.bind(this);
setInterval(() => {
if (
this.modalVisibility() &&
@ -318,20 +306,10 @@ class ComposePopupView extends AbstractViewNext {
this.showBcc.subscribe(this.resizerTrigger);
this.showReplyTo.subscribe(this.resizerTrigger);
this.onMessageUploadAttachments = this.onMessageUploadAttachments.bind(this);
this.bDisabeCloseOnEsc = true;
this.sDefaultKeyScope = KeyState.Compose;
// debounce
var d, fn = this.tryToClosePopup.bind(this);
this.tryToClosePopup = ()=>{
clearTimeout(d);
d = setTimeout(fn, 200);
};
this.emailsSource = this.emailsSource.bind(this);
this.autosaveFunction = this.autosaveFunction.bind(this);
this.tryToClosePopup = this.tryToClosePopup.debounce(200);
this.iTimer = 0;
}
@ -424,7 +402,7 @@ class ComposePopupView extends AbstractViewNext {
setFolderHash(sSentFolder, '');
Remote.sendMessage(
this.sendMessageResponse,
this.sendMessageResponse.bind(this),
this.getMessageRequestParams(sSentFolder)
);
}
@ -448,7 +426,7 @@ class ComposePopupView extends AbstractViewNext {
setFolderHash(FolderStore.draftFolder(), '');
Remote.saveMessage(
this.saveMessageResponse,
this.saveMessageResponse.bind(this),
this.getMessageRequestParams(FolderStore.draftFolder())
);
}
@ -517,7 +495,7 @@ class ComposePopupView extends AbstractViewNext {
autosaveStart() {
clearTimeout(this.iTimer);
this.iTimer = setTimeout(this.autosaveFunction, 60000);
this.iTimer = setTimeout(()=>this.autosaveFunction(), 60000);
}
autosaveStop() {
@ -525,9 +503,7 @@ class ComposePopupView extends AbstractViewNext {
}
emailsSource(oData, fResponse) {
getApp().getAutocomplete(oData.term, (aData) => {
fResponse(aData.map(oEmailItem => oEmailItem.toLine(false)));
});
getApp().getAutocomplete(oData.term, aData => fResponse(aData.map(oEmailItem => oEmailItem.toLine(false))));
}
openOpenPgpPopup() {
@ -615,9 +591,7 @@ class ComposePopupView extends AbstractViewNext {
if (StorageResultType.Success === statusResult && data && data.Result) {
result = true;
if (this.modalVisibility()) {
delegateRun(this, 'closeCommand');
}
this.modalVisibility() && this.closeCommand && this.closeCommand();
}
if (this.modalVisibility() && !result) {
@ -1096,7 +1070,7 @@ class ComposePopupView extends AbstractViewNext {
const downloads = this.getAttachmentsDownloadsForUpload();
if (isNonEmptyArray(downloads)) {
Remote.messageUploadAttachments(this.onMessageUploadAttachments, downloads);
Remote.messageUploadAttachments(()=>this.onMessageUploadAttachments(), downloads);
}
if (identity) {
@ -1147,13 +1121,13 @@ class ComposePopupView extends AbstractViewNext {
const PopupsAskViewModel = require('View/Popup/Ask');
if (!isPopupVisible(PopupsAskViewModel) && this.modalVisibility()) {
if (this.bSkipNextHide || (this.isEmptyForm() && !this.draftUid())) {
delegateRun(this, 'closeCommand');
this.closeCommand && this.closeCommand();
} else {
showScreenPopup(PopupsAskViewModel, [
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
() => {
if (this.modalVisibility()) {
delegateRun(this, 'closeCommand');
this.closeCommand && this.closeCommand();
}
}
]);
@ -1206,12 +1180,7 @@ class ComposePopupView extends AbstractViewNext {
return false;
});
var d, o = this;
addEventListener('resize.real', ()=>{
// debounce
clearTimeout(d);
d = setTimeout(o.resizerTrigger, 50);
});
addEventListener('resize.real', this.resizerTrigger);
setInterval(() => {
if (this.modalVisibility() && this.oEditor) {

View file

@ -2,7 +2,7 @@ import ko from 'ko';
import { FiltersAction, FilterConditionField, FilterConditionType } from 'Common/Enums';
import { bMobileDevice } from 'Common/Globals';
import { defautOptionsAfterRender, delegateRun } from 'Common/Utils';
import { defautOptionsAfterRender } from 'Common/Utils';
import { i18n, initOnStartOrLangChange } from 'Common/Translator';
import FilterStore from 'Stores/User/Filter';
@ -59,13 +59,9 @@ class FilterPopupView extends AbstractViewNext {
return false;
}
if (this.fTrueCallback) {
this.fTrueCallback(this.filter());
}
this.fTrueCallback && this.fTrueCallback(this.filter());
if (this.modalVisibility()) {
delegateRun(this, 'closeCommand');
}
this.modalVisibility() && this.closeCommand && this.closeCommand();
}
return true;

View file

@ -55,7 +55,6 @@ class FolderSystemPopupView extends AbstractViewNext {
this.trashFolder = FolderStore.trashFolder;
this.archiveFolder = FolderStore.archiveFolder;
var d;
const fSetSystemFolders = () => {
Settings.settingsSet('SentFolder', FolderStore.sentFolder());
Settings.settingsSet('DraftFolder', FolderStore.draftFolder());
@ -63,21 +62,17 @@ class FolderSystemPopupView extends AbstractViewNext {
Settings.settingsSet('TrashFolder', FolderStore.trashFolder());
Settings.settingsSet('ArchiveFolder', FolderStore.archiveFolder());
},
fSaveSystemFolders = ()=>{
// debounce
clearTimeout(d);
d = setTimeout(()=>{
fSetSystemFolders();
Remote.saveSystemFolders(()=>{}, {
SentFolder: FolderStore.sentFolder(),
DraftFolder: FolderStore.draftFolder(),
SpamFolder: FolderStore.spamFolder(),
TrashFolder: FolderStore.trashFolder(),
ArchiveFolder: FolderStore.archiveFolder(),
NullFolder: 'NullFolder'
});
}, 1000);
},
fSaveSystemFolders = (()=>{
fSetSystemFolders();
Remote.saveSystemFolders(()=>{}, {
SentFolder: FolderStore.sentFolder(),
DraftFolder: FolderStore.draftFolder(),
SpamFolder: FolderStore.spamFolder(),
TrashFolder: FolderStore.trashFolder(),
ArchiveFolder: FolderStore.archiveFolder(),
NullFolder: 'NullFolder'
});
}).debounce(1000),
fCallback = () => {
fSetSystemFolders();
fSaveSystemFolders();

View file

@ -14,39 +14,29 @@ class KeyboardShortcutsHelpPopupView extends AbstractViewNext {
}
onBuild(dom) {
var t;
key(
'tab, shift+tab, left, right',
KeyState.PopupKeyboardShortcutsHelp,
(event, handler)=>{
// throttle
if (!t) {
t = setTimeout(()=>{
t = 0;
if (event && handler) {
const $tabs = dom.find('.nav.nav-tabs > li'),
isNext = handler && ('tab' === handler.shortcut || 'right' === handler.shortcut);
((event, handler)=>{
if (event && handler) {
const $tabs = dom.find('.nav.nav-tabs > li'),
isNext = handler && ('tab' === handler.shortcut || 'right' === handler.shortcut);
let index = $tabs.index($tabs.filter('.active'));
if (!isNext && 0 < index) {
index -= 1;
} else if (isNext && index < $tabs.length - 1) {
index += 1;
} else {
index = isNext ? 0 : $tabs.length - 1;
}
let index = $tabs.index($tabs.filter('.active'));
if (!isNext && 0 < index) {
index -= 1;
} else if (isNext && index < $tabs.length - 1) {
index += 1;
} else {
index = isNext ? 0 : $tabs.length - 1;
}
$tabs
.eq(index)
.find('a[data-toggle="tab"]')
.tab('show');
return false;
}
return true;
}, 100);
$tabs
.eq(index)
.find('a[data-toggle="tab"]')
.tab('show');
}
}
}).throttle(100)
);
}
}

View file

@ -1,6 +1,6 @@
import ko from 'ko';
import { delegateRun, pInt } from 'Common/Utils';
import { pInt } from 'Common/Utils';
import PgpStore from 'Stores/User/Pgp';
@ -69,7 +69,7 @@ class NewOpenPgpKeyPopupView extends AbstractViewNext {
openpgpKeyring.store();
getApp().reloadOpenPgpKeys();
delegateRun(this, 'cancelCommand');
this.cancelCommand && this.cancelCommand();
}
})
.catch((e) => {

View file

@ -1,7 +1,6 @@
import ko from 'ko';
import { KeyState, StorageResultType, Notification } from 'Common/Enums';
import { isNonEmptyArray, delegateRun } from 'Common/Utils';
import { getNotification, i18n } from 'Common/Translator';
import Remote from 'Remote/Admin/Ajax';
@ -41,12 +40,7 @@ class PluginPopupView extends AbstractViewNext {
this.bDisabeCloseOnEsc = true;
this.sDefaultKeyScope = KeyState.All;
var d, fn = this.tryToClosePopup.bind(this);
this.tryToClosePopup = ()=>{
// debounce
clearTimeout(d);
d = setTimeout(fn, 200);
};
this.tryToClosePopup = this.tryToClosePopup.debounce(200);
}
@command((self) => self.hasConfiguration())
@ -89,7 +83,7 @@ class PluginPopupView extends AbstractViewNext {
this.readme(oPlugin.Readme);
const config = oPlugin.Config;
if (isNonEmptyArray(config)) {
if (Array.isArray(config) && config.length) {
this.configures(
config.map(item => ({
'value': ko.observable(item[0]),
@ -110,11 +104,7 @@ class PluginPopupView extends AbstractViewNext {
if (!isPopupVisible(PopupsAskViewModel)) {
showScreenPopup(PopupsAskViewModel, [
i18n('POPUPS_ASK/DESC_WANT_CLOSE_THIS_WINDOW'),
() => {
if (this.modalVisibility()) {
delegateRun(this, 'cancelCommand');
}
}
() => this.modalVisibility() && this.cancelCommand && this.cancelCommand()
]);
}
}

View file

@ -539,20 +539,13 @@ class MessageViewMailBoxUserView extends AbstractViewNext {
this.showFullInfo.subscribe(fCheckHeaderHeight);
this.message.subscribe(fCheckHeaderHeight);
var t;
addEventListener(
'resize',
()=>{
// throttle
if (!t) {
t = setTimeout(()=>{
setTimeout(fCheckHeaderHeight, 1);
setTimeout(fCheckHeaderHeight, 200);
setTimeout(fCheckHeaderHeight, 500);
t = 0;
}, 50);
}
}
(()=>{
setTimeout(fCheckHeaderHeight, 1);
setTimeout(fCheckHeaderHeight, 200);
setTimeout(fCheckHeaderHeight, 500);
}).throttle(50)
);
this.showFullInfo.subscribe((value) => {