Skip to content

Commit

Permalink
chore(Lit): Reuse event listener targets for disconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
a11delavar committed Jun 7, 2024
1 parent 5fada36 commit 5b9dda8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 9 additions & 8 deletions packages/Lit/eventListener/EventListenerController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function extractOptions(options: EventListenerControllerOptions): FullEventListe

export class EventListenerController extends Controller {
protected readonly options: FullEventListenerControllerOptions
private targets?: Array<EventTarget>

constructor(
protected override readonly host: ReactiveElement,
Expand All @@ -42,15 +43,15 @@ export class EventListenerController extends Controller {
}

async subscribe() {
const targets = await extractEventTargets.call(this.context, this.host, this.options.target)
for (const target of targets) {
this.targets = await extractEventTargets.call(this.context, this.host, this.options.target)
for (const target of this.targets) {
target.addEventListener(this.options.type, this.options.listener, this.options.options)
}
}

async unsubscribe() {
const targets = await extractEventTargets.call(this.context, this.host, this.options.target)
for (const target of targets) {
this.targets ??= await extractEventTargets.call(this.context, this.host, this.options.target)
for (const target of this.targets) {
target?.removeEventListener(this.options.type, this.options.listener, this.options.options)
}
}
Expand All @@ -60,11 +61,11 @@ export class EventListenerController extends Controller {
await this.subscribe()
}

override async hostConnected() {
await this.subscribe()
override hostConnected() {
this.subscribe()
}

override async hostDisconnected() {
await this.unsubscribe()
override hostDisconnected() {
this.unsubscribe()
}
}
2 changes: 1 addition & 1 deletion packages/Lit/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@a11d/lit",
"version": "0.9.3",
"version": "0.9.4",
"description": "A thin wrapper around the Lit library",
"repository": {
"type": "git",
Expand Down

0 comments on commit 5b9dda8

Please sign in to comment.