Files
react/packages/react-devtools-shared/src/backend/StyleX/utils.js
Ruslan Lesiutin 77ec61885f fix[devtools/inspectElement]: dont pause initial inspectElement call when user switches tabs (#27488)
There are not so many changes, most of them are changing imports,
because I've moved types for UI in a single file.

In https://github.com/facebook/react/pull/27357 I've added support for
pausing polling events: when user inspects an element, we start polling
React DevTools backend for updates in props / state. If user switches
tabs, extension's service worker can be killed by browser and this
polling will start spamming errors.

What I've missed is that we also have a separate call for this API, but
which is executed only once when user selects an element. We don't
handle promise rejection here and this can lead to some errors when user
selects an element and switches tabs right after it.

The only change here is that this API now has
`shouldListenToPauseEvents` param, which is `true` for polling, so we
will pause polling once user switches tabs. It is `false` by default, so
we won't pause initial call by accident.


af8beeebf6/packages/react-devtools-shared/src/backendAPI.js (L96)
2023-10-10 18:10:17 +01:00

131 lines
3.3 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {StyleXPlugin} from 'react-devtools-shared/src/frontend/types';
import isArray from 'react-devtools-shared/src/isArray';
const cachedStyleNameToValueMap: Map<string, string> = new Map();
export function getStyleXData(data: any): StyleXPlugin {
const sources = new Set<string>();
const resolvedStyles = {};
crawlData(data, sources, resolvedStyles);
return {
sources: Array.from(sources).sort(),
resolvedStyles,
};
}
export function crawlData(
data: any,
sources: Set<string>,
resolvedStyles: Object,
): void {
if (data == null) {
return;
}
if (isArray(data)) {
data.forEach(entry => {
if (entry == null) {
return;
}
if (isArray(entry)) {
crawlData(entry, sources, resolvedStyles);
} else {
crawlObjectProperties(entry, sources, resolvedStyles);
}
});
} else {
crawlObjectProperties(data, sources, resolvedStyles);
}
resolvedStyles = Object.fromEntries<string, any>(
Object.entries(resolvedStyles).sort(),
);
}
function crawlObjectProperties(
entry: Object,
sources: Set<string>,
resolvedStyles: Object,
): void {
const keys = Object.keys(entry);
keys.forEach(key => {
const value = entry[key];
if (typeof value === 'string') {
if (key === value) {
// Special case; this key is the name of the style's source/file/module.
sources.add(key);
} else {
const propertyValue = getPropertyValueForStyleName(value);
if (propertyValue != null) {
resolvedStyles[key] = propertyValue;
}
}
} else {
const nestedStyle = {};
resolvedStyles[key] = nestedStyle;
crawlData([value], sources, nestedStyle);
}
});
}
function getPropertyValueForStyleName(styleName: string): string | null {
if (cachedStyleNameToValueMap.has(styleName)) {
return ((cachedStyleNameToValueMap.get(styleName): any): string);
}
for (
let styleSheetIndex = 0;
styleSheetIndex < document.styleSheets.length;
styleSheetIndex++
) {
const styleSheet = ((document.styleSheets[
styleSheetIndex
]: any): CSSStyleSheet);
let rules: CSSRuleList | null = null;
// this might throw if CORS rules are enforced https://www.w3.org/TR/cssom-1/#the-cssstylesheet-interface
try {
rules = styleSheet.cssRules;
} catch (_e) {
continue;
}
for (let ruleIndex = 0; ruleIndex < rules.length; ruleIndex++) {
if (!(rules[ruleIndex] instanceof CSSStyleRule)) {
continue;
}
const rule = ((rules[ruleIndex]: any): CSSStyleRule);
const {cssText, selectorText, style} = rule;
if (selectorText != null) {
if (selectorText.startsWith(`.${styleName}`)) {
const match = cssText.match(/{ *([a-z\-]+):/);
if (match !== null) {
const property = match[1];
const value = style.getPropertyValue(property);
cachedStyleNameToValueMap.set(styleName, value);
return value;
} else {
return null;
}
}
}
}
}
return null;
}