Skip to content

Commit f5e5b37

Browse files
committed
Maintain a map to retrieve an enhancer for a commentspot.
1 parent 9a0e101 commit f5e5b37

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

browser-extension/src/lib/registries.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ export interface EnhancedTextarea<T extends CommentSpot = CommentSpot> {
1313
export class EnhancerRegistry {
1414
private enhancers = new Set<CommentEnhancer>()
1515
private preparedEnhancers = new Set<CommentEnhancer>()
16+
private byType = new Map<string, CommentEnhancer>()
1617

1718
constructor() {
1819
// Register all available handlers
1920
this.register(new GitHubIssueAddCommentEnhancer())
2021
this.register(new GitHubPRAddCommentEnhancer())
2122
}
2223

23-
private register<T extends CommentSpot>(handler: CommentEnhancer<T>): void {
24-
this.enhancers.add(handler)
24+
private register<T extends CommentSpot>(enhancer: CommentEnhancer<T>): void {
25+
this.enhancers.add(enhancer)
26+
for (const spotType in enhancer.forSpotTypes()) {
27+
this.byType.set(spotType, enhancer)
28+
}
29+
}
30+
31+
enhancerFor<T extends CommentSpot>(spot: T): CommentEnhancer<T> {
32+
return this.byType.get(spot.type)! as CommentEnhancer<T>
2533
}
2634

2735
tryToEnhance(textarea: HTMLTextAreaElement): EnhancedTextarea | null {

0 commit comments

Comments
 (0)