diff options
| author | Henry Jameson <me@hjkos.com> | 2024-02-27 00:07:45 +0200 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2024-02-27 00:07:45 +0200 |
| commit | ef2c8f077dee094c533ba2bdb0c8694a21760868 (patch) | |
| tree | ab349d0364bdb5bfd358358553fdfc22d93b53f8 /src/services/style_setter | |
| parent | dc22386599c77fdd5a8b88ccfd167cff36d14c67 (diff) | |
refactor and optimize: now lazy rules are processed in chunks
Diffstat (limited to 'src/services/style_setter')
| -rw-r--r-- | src/services/style_setter/style_setter.js | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/services/style_setter/style_setter.js b/src/services/style_setter/style_setter.js index b1722295..52cf06ed 100644 --- a/src/services/style_setter/style_setter.js +++ b/src/services/style_setter/style_setter.js @@ -4,6 +4,7 @@ import { init } from '../theme_data/theme_data_3.service.js' import { convertTheme2To3 } from '../theme_data/theme2_to_theme3.js' import { getCssRules } from '../theme_data/css_utils.js' import { defaultState } from '../../modules/config.js' +import { chunk } from 'lodash' export const applyTheme = async (input) => { let extraRules @@ -43,15 +44,17 @@ export const applyTheme = async (input) => { body.classList.remove('hidden') - setTimeout(() => { - themes3.lazy().then(lazyRules => { - const t2 = performance.now() - getCssRules(lazyRules, themes3.staticVars).forEach(rule => { - styleSheet.insertRule(rule, 'index-max') + // Optimization - instead of processing all lazy rules in one go, process them in small chunks + // so that UI can do other things and be somewhat responsive while less important rules are being + // processed + chunk(themes3.lazy, 5).forEach(chunk => { + setTimeout(() => { + Promise.all(chunk.map(x => x())).then(result => { + getCssRules(result.filter(x => x), themes3.staticVars).forEach(rule => { + styleSheet.insertRule(rule, 'index-max') + }) }) - const t3 = performance.now() - console.debug('Themes 3 finalization (lazy) took ' + (t3 - t2) + 'ms') - }) + }, 50) }) return Promise.resolve() |
