fix(components): deduplicate plugin handler registration
This commit is contained in:
@@ -90,7 +90,12 @@ class ComponentRegistry {
|
|||||||
try {
|
try {
|
||||||
let instance: QuartzComponent
|
let instance: QuartzComponent
|
||||||
if (typeof r.component === "function") {
|
if (typeof r.component === "function") {
|
||||||
instance = this.instantiate(r.component as QuartzComponentConstructor, undefined)
|
// Check if this constructor was already instantiated (with any options).
|
||||||
|
// Re-instantiating with `undefined` when options were provided would create
|
||||||
|
// a duplicate instance with separate afterDOMLoaded scripts.
|
||||||
|
const existing = this.findCachedInstance(r.component as QuartzComponentConstructor)
|
||||||
|
instance =
|
||||||
|
existing ?? this.instantiate(r.component as QuartzComponentConstructor, undefined)
|
||||||
} else {
|
} else {
|
||||||
instance = r.component as QuartzComponent
|
instance = r.component as QuartzComponent
|
||||||
}
|
}
|
||||||
@@ -103,6 +108,17 @@ class ComponentRegistry {
|
|||||||
}
|
}
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private findCachedInstance(
|
||||||
|
constructor: QuartzComponentConstructor<any>,
|
||||||
|
): QuartzComponent | undefined {
|
||||||
|
const ctorId = (constructor as unknown as { __cacheId?: string }).__cacheId
|
||||||
|
if (!ctorId) return undefined
|
||||||
|
for (const [key, instance] of this.instanceCache) {
|
||||||
|
if (key.startsWith(`${ctorId}:`)) return instance
|
||||||
|
}
|
||||||
|
return undefined
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const componentRegistry = new ComponentRegistry()
|
export const componentRegistry = new ComponentRegistry()
|
||||||
|
|||||||
Reference in New Issue
Block a user