Skip to content

Commit a47225b

Browse files
gnoffgaearon
authored andcommitted
[Fiber] Use a safer strategy to track the last precedence (#28110)
Uses a safer strategy to track the last precedence to avoid the need to consistently remember to preprend `'p'` to the precedence value
1 parent a30ac92 commit a47225b

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

packages/react-dom-bindings/src/client/ReactFiberConfigDOM.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -3481,10 +3481,20 @@ function onUnsuspend(this: SuspendedState) {
34813481
}
34823482
}
34833483

3484+
// We use a value that is type distinct from precedence to track which one is last.
3485+
// This ensures there is no collision with user defined precedences. Normally we would
3486+
// just track this in module scope but since the precedences are tracked per HoistableRoot
3487+
// we need to associate it to something other than a global scope hence why we try to
3488+
// colocate it with the map of precedences in the first place
3489+
const LAST_PRECEDENCE = null;
3490+
34843491
// This is typecast to non-null because it will always be set before read.
34853492
// it is important that this not be used except when the stack guarantees it exists.
34863493
// Currentlyt his is only during insertSuspendedStylesheet.
3487-
let precedencesByRoot: Map<HoistableRoot, Map<string, Instance>> = (null: any);
3494+
let precedencesByRoot: Map<
3495+
HoistableRoot,
3496+
Map<string | typeof LAST_PRECEDENCE, Instance>,
3497+
> = (null: any);
34883498

34893499
function insertSuspendedStylesheets(
34903500
state: SuspendedState,
@@ -3539,27 +3549,27 @@ function insertStylesheetIntoRoot(
35393549
// and will be hoisted by the Fizz runtime imminently.
35403550
node.getAttribute('media') !== 'not all'
35413551
) {
3542-
precedences.set('p' + node.dataset.precedence, node);
3552+
precedences.set(node.dataset.precedence, node);
35433553
last = node;
35443554
}
35453555
}
35463556
if (last) {
3547-
precedences.set('last', last);
3557+
precedences.set(LAST_PRECEDENCE, last);
35483558
}
35493559
} else {
3550-
last = precedences.get('last');
3560+
last = precedences.get(LAST_PRECEDENCE);
35513561
}
35523562

35533563
// We only call this after we have constructed an instance so we assume it here
35543564
const instance: HTMLLinkElement = (resource.instance: any);
35553565
// We will always have a precedence for stylesheet instances
35563566
const precedence: string = (instance.getAttribute('data-precedence'): any);
35573567

3558-
const prior = precedences.get('p' + precedence) || last;
3568+
const prior = precedences.get(precedence) || last;
35593569
if (prior === last) {
3560-
precedences.set('last', instance);
3570+
precedences.set(LAST_PRECEDENCE, instance);
35613571
}
3562-
precedences.set('p' + precedence, instance);
3572+
precedences.set(precedence, instance);
35633573

35643574
this.count++;
35653575
const onComplete = onUnsuspend.bind(this);

0 commit comments

Comments
 (0)