Skip to content

Commit 1459445

Browse files
authored
perf: avoid duplicated findStyles in updateCSS (#494)
1 parent 5a3231f commit 1459445

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/Dom/dynamicCSS.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface Options {
2121
*/
2222
priority?: number;
2323
mark?: string;
24+
styles?: HTMLElement[];
2425
}
2526

2627
function getMark({ mark }: Options = {}) {
@@ -83,18 +84,22 @@ export function injectCSS(css: string, option: Options = {}) {
8384
if (prepend) {
8485
// If is queue `prepend`, it will prepend first style and then append rest style
8586
if (isPrependQueue) {
86-
const existStyle = findStyles(container).filter(node => {
87-
// Ignore style which not injected by rc-util with prepend
88-
if (
89-
!['prepend', 'prependQueue'].includes(node.getAttribute(APPEND_ORDER))
90-
) {
91-
return false;
92-
}
93-
94-
// Ignore style which priority less then new style
95-
const nodePriority = Number(node.getAttribute(APPEND_PRIORITY) || 0);
96-
return priority >= nodePriority;
97-
});
87+
const existStyle = (option.styles || findStyles(container)).filter(
88+
node => {
89+
// Ignore style which not injected by rc-util with prepend
90+
if (
91+
!['prepend', 'prependQueue'].includes(
92+
node.getAttribute(APPEND_ORDER),
93+
)
94+
) {
95+
return false;
96+
}
97+
98+
// Ignore style which priority less then new style
99+
const nodePriority = Number(node.getAttribute(APPEND_PRIORITY) || 0);
100+
return priority >= nodePriority;
101+
},
102+
);
98103

99104
if (existStyle.length) {
100105
container.insertBefore(
@@ -118,7 +123,7 @@ export function injectCSS(css: string, option: Options = {}) {
118123
function findExistNode(key: string, option: Options = {}) {
119124
const container = getContainer(option);
120125

121-
return findStyles(container).find(
126+
return (option.styles || findStyles(container)).find(
122127
node => node.getAttribute(getMark(option)) === key,
123128
);
124129
}
@@ -153,8 +158,14 @@ export function clearContainerCache() {
153158
containerCache.clear();
154159
}
155160

156-
export function updateCSS(css: string, key: string, option: Options = {}) {
157-
const container = getContainer(option);
161+
export function updateCSS(
162+
css: string,
163+
key: string,
164+
originOption: Options = {},
165+
) {
166+
const container = getContainer(originOption);
167+
const styles = findStyles(container);
168+
const option = { ...originOption, styles };
158169

159170
// Sync real parent
160171
syncRealContainer(container, option);

0 commit comments

Comments
 (0)