1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Fixes #3449: Fixed style caching

This commit is contained in:
Laurent Cozic 2020-07-14 19:17:25 +00:00
parent b770ffda4d
commit 7f1c25793a
2 changed files with 17 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import { NoteBodyEditorProps } from '../../../utils/types';
const { buildStyle } = require('lib/theme');
export default function styles(props: NoteBodyEditorProps) {
return buildStyle('AceEditor', props.theme, (theme: any) => {
return buildStyle('CodeMirror', props.theme, (theme: any) => {
return {
root: {
position: 'relative',

View File

@ -336,19 +336,31 @@ function themeStyle(theme) {
return themeCache_[cacheKey];
}
const cachedStyles_ = {};
const cachedStyles_ = {
themeId: null,
styles: {},
};
// cacheKey must be a globally unique key, and must change whenever
// the dependencies of the style change. If the style depends only
// on the theme, a static string can be provided as a cache key.
function buildStyle(cacheKey, themeId, callback) {
if (cachedStyles_[cacheKey]) cachedStyles_[cacheKey].style;
// We clear the cache whenever switching themes
if (cachedStyles_.themeId !== themeId) {
cachedStyles_.themeId = themeId;
cachedStyles_.styles = {};
}
if (cachedStyles_.styles[cacheKey]) return cachedStyles_.styles[cacheKey].style;
const s = callback(themeStyle(themeId));
cachedStyles_[cacheKey] = {
cachedStyles_.styles[cacheKey] = {
style: s,
timestamp: Date.now(),
};
return cachedStyles_[cacheKey].style;
return cachedStyles_.styles[cacheKey].style;
}
module.exports = { themeStyle, buildStyle, themeById };