From 7eac2dfc5c7b5cd4030e8e19632fa8906552b9b6 Mon Sep 17 00:00:00 2001 From: myxvisual Date: Tue, 25 Jul 2017 09:23:40 +0800 Subject: [PATCH] feat: Update StyleManager support cached result --- src/styles/StyleManager.ts | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/styles/StyleManager.ts b/src/styles/StyleManager.ts index 611143c5..39d9b7c9 100644 --- a/src/styles/StyleManager.ts +++ b/src/styles/StyleManager.ts @@ -86,6 +86,8 @@ export class StyleManager { addStyle = (style: CustomCSSProperties, className = "", callback = () => {}) => { const id = createHash(`${this.themeId}: ${JSON.stringify(style)}`); + if (this.sheets[id]) return this.sheets[id]; + const classNameWithHash = `${this.globalClassName}${className}-${id}`; const styleKeys = Object.keys(style); let CSSText = ""; @@ -115,18 +117,19 @@ export class StyleManager { return this.addStyle(style, className, this.renderSheets); } - addCSSText = (CSSText: string, callback = () => {}) => { + addCSSText = (CSSText: string, callback: (shouldUpdate?: boolean) => void = () => {}) => { const hash = createHash(CSSText); - if (!this.addedCSSText[hash]) { + const shouldUpdate = !this.addedCSSText[hash]; + if (shouldUpdate) { this.addedCSSText[hash] = true; this.CSSText += CSSText; - callback(); } + callback(shouldUpdate); } addCSSTextWithUpdate = (CSSText: string) => { - this.addCSSText(CSSText, () => { - if (this.styleElement) { + this.addCSSText(CSSText, shouldUpdate => { + if (this.styleElement && shouldUpdate) { this.updateStyleElement(this.styleElement.textContent += CSSText); } });