Skip to content

Commit

Permalink
feat: mutation observer wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
MrExplode committed Feb 20, 2024
1 parent d12f899 commit 7e3dbe3
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/observer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const config: MutationObserverInit = { attributes: true, childList: true, subtree: true }

/**
* Creates a mutation observer.
*
* @param selector the query selector for target nodes
* @param mutationCallback the event callback for mutations
* @returns a callback for unsubscribing from the events
*/
export function observe(
selector: string,
mutationCallback: (mutations: MutationRecord[]) => void
): () => void {
const observer = new MutationObserver(mutationCallback)
observer.observe(document.querySelector(selector)!, config)
return () => observer.disconnect()
}

0 comments on commit 7e3dbe3

Please sign in to comment.