Skip to content

Commit fa15f8a

Browse files
committed
use scheduler.yield()
1 parent 3f2f811 commit fa15f8a

6 files changed

Lines changed: 12 additions & 10 deletions

File tree

src/background/broadcast.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@/js/browser';
22
import {rxIgnorableError} from '@/js/msg-api';
33
import {ownRoot} from '@/js/urls';
4-
import {sleep} from '@/js/util';
4+
import {sleep0} from '@/js/util';
55
import {getWindowClients} from './util';
66

77
let toBroadcast;
@@ -30,7 +30,7 @@ async function doBroadcast() {
3030
so we're yielding for a possible navigation/messaging event. */
3131
if (++cnt > 50) {
3232
cnt = 0;
33-
await sleep();
33+
await sleep0();
3434
}
3535
}
3636
}

src/background/content-scripts.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import '@/js/browser';
22
import {kUrl} from '@/js/consts';
33
import * as URLS from '@/js/urls';
4-
import {sleep, stringAsRegExpStr} from '@/js/util';
4+
import {sleep0, stringAsRegExpStr} from '@/js/util';
55
import {ignoreChromeError, MF} from '@/js/util-webext';
66
import {sendTab} from './broadcast';
77
import {webNavigation} from './navigation-manager';
@@ -40,7 +40,7 @@ export default async function reinjectContentScripts(targetTab) {
4040
}
4141
let busyTabsTimer;
4242

43-
if (!targetTab) await sleep();
43+
if (!targetTab) await sleep0();
4444

4545
for (const tab of targetTab ? [targetTab] : await browser.tabs.query({})) {
4646
const url = tab.pendingUrl || tab.url;

src/edit/moz-section-widget.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {$create} from '@/js/dom';
33
import {messageBox} from '@/js/dom-util';
44
import {htmlToTemplate, templateCache} from '@/js/localization';
55
import {onMessage} from '@/js/msg';
6-
import {sleep, t} from '@/js/util';
6+
import {sleep0, t} from '@/js/util';
77
import {CodeMirror} from '@/cm';
88
import editor from './editor';
99
import MozSectionFinder from './moz-section-finder';
@@ -83,7 +83,7 @@ export default function MozSectionWidget(cm, finder = MozSectionFinder(cm)) {
8383
async '.add-applies-to'(elItem, func) {
8484
const pos = func.item.find(1);
8585
cm.replaceRange(`, ${func.str.type}("")`, pos, pos);
86-
await sleep();
86+
await sleep0();
8787
elItem.nextElementSibling.$('input').focus();
8888
},
8989
};

src/edit/sections-editor.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {htmlToTemplateCache, templateCache} from '@/js/localization';
66
import {API} from '@/js/msg-api';
77
import * as prefs from '@/js/prefs';
88
import {styleSectionsEqual, styleToCss} from '@/js/sections-util';
9-
import {clipString, RX_META, sleep, t} from '@/js/util';
9+
import {clipString, RX_META, sleep0, t} from '@/js/util';
1010
import editor from './editor';
1111
import * as linterMan from './linter';
1212
import EditorSection from './sections-editor-section';
@@ -524,7 +524,7 @@ export default function SectionsEditor() {
524524
} else if (now - tPrev > 100) {
525525
tPrev = 0;
526526
forceRefresh = false;
527-
await sleep();
527+
await sleep0();
528528
}
529529
if (si) forceRefresh = y < si.scrollY2 && (y += si.cms[i].parentHeight) > si.scrollY;
530530
insertSectionAfter(src[i], null, forceRefresh, si && si.cms[i]);

src/js/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const hasOwn = /*@__PURE__*/Object.call.bind({}.hasOwnProperty);
1414
export const isCssDarkScheme = () => matchMedia('(prefers-color-scheme:dark)').matches;
1515
export const isObject = val => typeof val === 'object' && val;
1616
export const sleep = ms => new Promise(ms > 0 ? cb => setTimeout(cb, ms) : setTimeout);
17+
/** uses scheduler.yield() to prioritize continuation of the initiator over another task */
18+
export const sleep0 = () => global.scheduler?.yield?.() || new Promise(setTimeout);
1719
export const stringAsRegExpStr = s => s.replace(/[{}()[\]\\.+*?^$|]/g, '\\$&');
1820
export const stringAsRegExp = (s, flags) => new RegExp(stringAsRegExpStr(s), flags);
1921
export const RX_META = /\/\*!?\s*==userstyle==[\s\S]*?==\/userstyle==\s*\*\//i;

src/popup/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as prefs from '@/js/prefs';
99
import {isDark, onDarkChanged} from '@/js/themer';
1010
import {CHROME, FIREFOX, MOBILE, OPERA} from '@/js/ua';
1111
import {ownRoot} from '@/js/urls';
12-
import {capitalize, clamp, clipString, sleep, stringAsRegExpStr, t} from '@/js/util';
12+
import {capitalize, clamp, clipString, sleep0, stringAsRegExpStr, t} from '@/js/util';
1313
import {CHROME_POPUP_BORDER_BUG, getActiveTab, MF} from '@/js/util-webext';
1414
import * as Events from './events';
1515
import './hotkeys';
@@ -168,7 +168,7 @@ async function initPopup(frames, ping0, tab, urlSupported) {
168168
}
169169
// FF and some Chrome forks (e.g. CentBrowser) implement tab-on-demand
170170
// so we'll wait a bit to handle popup being invoked right after switching
171-
await sleep();
171+
await sleep0();
172172
tab = await getActiveTab();
173173
}
174174

0 commit comments

Comments
 (0)