-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from JOTSR/refactor/improve-js-bridge
refactor, fix and add feat to js bridge
- Loading branch information
Showing
10 changed files
with
546 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# Do not track xxd output files | ||
*.c | ||
*.h | ||
*.h | ||
*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* Allows you to automatically bind an event listener to newly added | ||
* elements that match a specific selector within a given root element. | ||
* Track dom update to rebind event listeners automatically. | ||
* | ||
* @param {HTMLElement} root - The root element to observe for changes. | ||
* @param {string} targetSelector - Query selector matching elements that you want to bind the event listener to. | ||
* @param {K} type - Type of event listener to bind (same as for addEventListener). | ||
* @param listener - Event listener to bind (same as for addEventListener). | ||
* @param {boolean | AddEventListenerOptions} [options] - Event listener options (same as for addEventListener). | ||
* @returns the used observer to allow disconnect. | ||
*/ | ||
export function addRefreshableEventListener< | ||
K extends keyof HTMLElementEventMap | ||
>( | ||
root: HTMLElement, | ||
targetSelector: string, | ||
type: K, | ||
listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => unknown, | ||
options?: boolean | AddEventListenerOptions | ||
) { | ||
function rebindListener(mutations: MutationRecord[]) { | ||
for (const mutation of mutations) { | ||
for (const node of mutation.addedNodes) { | ||
if (!(node instanceof HTMLElement)) return // Target only html elements | ||
if (node.matches(targetSelector)) { | ||
// Bind event on added nodes | ||
node.addEventListener<K>(type, listener, options) | ||
} | ||
for (const child of node.querySelectorAll(targetSelector)) { | ||
if (!(child instanceof HTMLElement)) continue //Target only html elements | ||
child.addEventListener<K>(type, listener, options) | ||
} | ||
} | ||
} | ||
} | ||
|
||
const observer = new MutationObserver(rebindListener) //Set mutation observer callback | ||
observer.observe(root, { subtree: true, childList: true }) // Observe root element and all his children | ||
return observer // Allow user to stop observer for performance issues | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.