Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/core/elementNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1542,7 +1542,14 @@ export class ElementNode {

const states = this.states;

if (this._undoStyles || keyExists(this, states)) {
// An empty _undoStyles (left behind once a state style has been undone)
// must not force the resolution branch: with nothing to undo and no style
// matching any active state, the branch provably assigns nothing, and it
// runs on every path element of every focus change.
if (
(this._undoStyles !== undefined && this._undoStyles.length > 0) ||
keyExists(this, states)
) {
let stylesToUndo: { [key: string]: any } | undefined;
if (this._undoStyles && this._undoStyles.length) {
stylesToUndo = {};
Expand Down
12 changes: 7 additions & 5 deletions src/core/focusManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,10 @@ const updateFocusPath = (
prevFocusedElm: ElementNode | undefined,
) => {
let current: ElementNode | undefined = currentFocusedElm;
// fp escapes through the focusPath signal, so it must be a fresh array; the
// membership test below runs on paths of a handful of elements every single
// keypress, where a linear scan beats allocating and hashing a Set.
const fp: ElementNode[] = [];
const fpSet = new Set<ElementNode>();
while (current) {
if (
!current.states.has(Config.focusStateKey) ||
Expand All @@ -251,13 +253,13 @@ const updateFocusPath = (
);
}
fp.push(current);
fpSet.add(current);
current = current.parent;
}

const prevFp = focusPath();
prevFp.forEach((elm) => {
if (!fpSet.has(elm)) {
for (let i = 0; i < prevFp.length; i++) {
const elm = prevFp[i]!;
if (fp.indexOf(elm) === -1) {
elm.states.remove(Config.focusStateKey);
elm.onBlur?.call(elm, currentFocusedElm, prevFocusedElm!, elm);
elm.onFocusChanged?.call(
Expand All @@ -268,7 +270,7 @@ const updateFocusPath = (
elm,
);
}
});
}

if (Config.focusDebug) {
addFocusDebug(prevFp, fp);
Expand Down
13 changes: 11 additions & 2 deletions src/core/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@ export default class States extends Array<DollarString> {
}

has(state: DollarString) {
// temporary check for $ prefix
return this.indexOf(state) >= 0 || this.indexOf(`$${state}`) >= 0;
if (this.indexOf(state) >= 0) {
return true;
}
// temporary check for $ prefix, so has('focus') matches '$focus'. A query
// that already starts with '$' could only match a doubled prefix, which
// nothing produces, so skip the lookup: this runs per path element on
// every focus change and the template string was a per-call allocation.
return (
state.charCodeAt(0) !== 36 &&
this.indexOf(('$' + state) as DollarString) >= 0
);
}

is(state: DollarString) {
Expand Down
Loading