Skip to content

Commit 425ca55

Browse files
committed
Refactor around just sendEvent
1 parent e343ebd commit 425ca55

File tree

2 files changed

+17
-24
lines changed

2 files changed

+17
-24
lines changed

src/entrypoints/content.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CommentEvent, CommentSpot, StrippedLocation } from '../lib/enhancer'
1+
import type { CommentEvent, StrippedLocation } from '../lib/enhancer'
22
import { logger } from '../lib/logger'
33
import { EnhancerRegistry, TextareaRegistry } from '../lib/registries'
44

@@ -20,27 +20,13 @@ function detectLocation(): StrippedLocation {
2020
return result
2121
}
2222

23-
function sendEventToBackground(
24-
type: 'ENHANCED' | 'DESTROYED',
25-
spot: CommentSpot,
26-
draft: string,
27-
): void {
28-
const message: CommentEvent = {
29-
draft,
30-
spot,
31-
type,
32-
}
23+
function sendEventToBackground(message: CommentEvent): void {
3324
browser.runtime.sendMessage(message).catch((error) => {
3425
logger.error('Failed to send event to background:', error)
3526
})
3627
}
3728

38-
enhancedTextareas.setEventHandlers(
39-
(textareaInfo) =>
40-
sendEventToBackground('ENHANCED', textareaInfo.spot, textareaInfo.textarea.value),
41-
(textareaInfo) =>
42-
sendEventToBackground('DESTROYED', textareaInfo.spot, textareaInfo.textarea.value),
43-
)
29+
enhancedTextareas.setCommentEventSender(sendEventToBackground)
4430

4531
export default defineContentScript({
4632
main() {

src/lib/registries.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OverTypeInstance } from 'overtype'
22
import OverType from 'overtype'
3-
import type { CommentEnhancer, CommentSpot, StrippedLocation } from './enhancer'
3+
import type { CommentEnhancer, CommentEvent, CommentSpot, StrippedLocation } from './enhancer'
44
import { CommentEnhancerMissing } from './enhancers/CommentEnhancerMissing'
55
import { GitHubEditEnhancer } from './enhancers/github/GitHubEditEnhancer'
66
import { GitHubIssueAppendEnhancer } from './enhancers/github/GitHubIssueAppendEnhancer'
@@ -104,12 +104,19 @@ export class TextareaRegistry {
104104
private onEnhanced?: (textareaInfo: EnhancedTextarea) => void
105105
private onDestroyed?: (textareaInfo: EnhancedTextarea) => void
106106

107-
setEventHandlers(
108-
onEnhanced: (textareaInfo: EnhancedTextarea) => void,
109-
onDestroyed: (textareaInfo: EnhancedTextarea) => void,
110-
): void {
111-
this.onEnhanced = onEnhanced
112-
this.onDestroyed = onDestroyed
107+
setCommentEventSender(sendEvent: (event: CommentEvent) => void): void {
108+
this.onEnhanced = (textareaInfo) =>
109+
sendEvent({
110+
draft: textareaInfo.textarea.value,
111+
spot: textareaInfo.spot,
112+
type: 'ENHANCED',
113+
})
114+
this.onDestroyed = (textareaInfo) =>
115+
sendEvent({
116+
draft: textareaInfo.textarea.value,
117+
spot: textareaInfo.spot,
118+
type: 'DESTROYED',
119+
})
113120
}
114121

115122
register<T extends CommentSpot>(textareaInfo: EnhancedTextarea<T>): void {

0 commit comments

Comments
 (0)