Skip to content

Commit

Permalink
feat: Update StyleManager support cached result
Browse files Browse the repository at this point in the history
  • Loading branch information
myxvisual committed Aug 8, 2017
1 parent 99dbadf commit 7eac2df
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/styles/StyleManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down Expand Up @@ -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);
}
});
Expand Down

0 comments on commit 7eac2df

Please sign in to comment.