Skip to content

Commit 06fffba

Browse files
committed
Find attr names with a loop instead of recursion
1 parent 1bff7c6 commit 06fffba

1 file changed

Lines changed: 11 additions & 5 deletions

File tree

src/attr.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function attr<K extends string>(proto: Record<K, attrValue>, key: K): voi
3535
* `@controller` decorator it should not call this manually.
3636
*/
3737
export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>): void {
38-
if (!names) names = new Set(getAttrNames(instance))
38+
if (!names) names = getAttrNames(instance)
3939
for (const key of names) {
4040
const value = (<Record<PropertyKey, unknown>>(<unknown>instance))[key]
4141
const name = attrToAttributeName(key)
@@ -73,10 +73,16 @@ export function initializeAttrs(instance: HTMLElement, names?: Iterable<string>)
7373
}
7474
}
7575

76-
function getAttrNames(instance: HTMLElement | typeof HTMLElement): string[] {
77-
if (!instance || instance === HTMLElement) return []
78-
const proto = Object.getPrototypeOf(instance)
79-
return (attrs.get(proto) || []).concat(getAttrNames(proto))
76+
function getAttrNames(instance: HTMLElement): Set<string> {
77+
let names: string[] = []
78+
let proto = Object.getPrototypeOf(instance)
79+
80+
while (proto && proto !== HTMLElement) {
81+
names = names.concat(attrs.get(proto) || [])
82+
proto = Object.getPrototypeOf(proto)
83+
}
84+
85+
return new Set(names)
8086
}
8187

8288
function attrToAttributeName(name: string): string {

0 commit comments

Comments
 (0)