Skip to content

Commit

Permalink
Merge pull request #132 from JOTSR/refactor/improve-js-bridge
Browse files Browse the repository at this point in the history
refactor, fix and add feat to js bridge
  • Loading branch information
AlbertShown authored Jul 13, 2023
2 parents af06ebe + 21fdee9 commit 979b0b5
Show file tree
Hide file tree
Showing 10 changed files with 546 additions and 340 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
- name: Windows-WebUI
shell: cmd
run: |
npm install --save-exact -g esbuild
esbuild --bundle --target="chrome90,firefox90,safari15" --format=esm --log-level=silent --outdir=./src/client ./src/client/webui.ts
cd src
xxd -i client\webui.js client\webui.h
cd ..
Expand All @@ -30,6 +32,8 @@ jobs:
- uses: actions/checkout@v2
- name: Linux-WebUI
run: |
npm install --save-exact -g esbuild
esbuild --bundle --target="chrome90,firefox90,safari15" --format=esm --log-level=silent --outdir=./src/client ./src/client/webui.ts
cd src
xxd -i client/webui.js client/webui.h
cd ..
Expand All @@ -47,6 +51,8 @@ jobs:
- uses: actions/checkout@v2
- name: macOS-WebUI
run: |
npm install --save-exact -g esbuild
esbuild --bundle --target="chrome90,firefox90,safari15" --format=esm --log-level=silent --outdir=./src/client ./src/client/webui.ts
cd src
xxd -i client/webui.js client/webui.h
cd ..
Expand Down
7 changes: 6 additions & 1 deletion build/linux_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ echo "";
echo "Converting JS source to C-String using xxd"
echo "";

#Converting JS source to C-String using xxd
# Transpiling TS to JS
echo "Transpile and bundle TS sources to webui.js";
cd "%RootPath%"
esbuild --bundle --target="chrome90,firefox90,safari15" --format=esm --outdir=./src/client ./src/client/webui.ts

# Converting JS source to C-String using xxd
cd "$RootPath"
cd "src"
xxd -i client/webui.js client/webui.h
Expand Down
7 changes: 6 additions & 1 deletion build/macos_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ echo "";
echo "Converting JS source to C-String using xxd"
echo "";

#Converting JS source to C-String using xxd
# Transpiling TS to JS
echo "Transpile and bundle TS sources to webui.js";
cd "%RootPath%"
esbuild --bundle --target="chrome90,firefox90,safari15" --format=esm --outdir=./src/client ./src/client/webui.ts

# Converting JS source to C-String using xxd
cd "$RootPath"
cd "src"
xxd -i client/webui.js client/webui.h
Expand Down
5 changes: 5 additions & 0 deletions build/windows_build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ echo Compiler: MSVC, GCC and TCC
Set RootPath=%CD%\..\
cd "%RootPath%"

REM Transpiling TS to JS
echo Transpile and bundle TS sources to webui.js
cd "%RootPath%"
cmd /c esbuild --bundle --target="chrome90,firefox90,safari15" --format=esm --log-level=silent --outdir=.\src\client .\src\client\webui.ts

REM Converting JS source to C-String using xxd
echo Converting JS source to C-String using xxd
cd "%RootPath%"
Expand Down
3 changes: 2 additions & 1 deletion src/client/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Do not track xxd output files
*.c
*.h
*.h
*.js
41 changes: 41 additions & 0 deletions src/client/utils.ts
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
}
47 changes: 0 additions & 47 deletions src/client/webui.d.ts

This file was deleted.

Loading

0 comments on commit 979b0b5

Please sign in to comment.