Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#204 workaround #244

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ parser-test:
TINYGO_STACKS:=-stack-size=40mb

wasm: Makefile *.go */*.go $(GEN) wasm/wasm_exec.js wasm/wasm_exec.html wasm/grol_wasm.html
sed -i 's/console.warn("scheduleTimeoutEvent: missed timeout event");/this._scheduledTimeouts.delete(id);\nconsole.warn("scheduleTimeoutEvent: missed timeout event");/' wasm/wasm_exec.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do the sed in a pipe instead of the copy (where wasm/wasm_exec.js is being created/copied a bit below)

# GOOS=wasip1 GOARCH=wasm go build -o grol.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" .
GOOS=js GOARCH=wasm $(WASM_GO) build -o wasm/grol.wasm -trimpath -ldflags="-w -s" -tags "$(GO_BUILD_TAGS)" ./wasm
# GOOS=wasip1 GOARCH=wasm tinygo build -target=wasi -no-debug -o grol_tiny.wasm -tags "$(GO_BUILD_TAGS)" .
Expand Down
63 changes: 49 additions & 14 deletions wasm/grol_wasm.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!doctype html><html><head><meta charset="utf-8"><title>Grol</title></head>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a fragment that is reused mid page, can't have that here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<!--
Keep grol/wasm/grol_wasm.html and web-site/_includes/grol_wasm.html in sync
Using `make sync` in web-site/
Expand Down Expand Up @@ -38,12 +39,46 @@
</style>
<script src="wasm_exec.js"></script>
<script>
function debounce(func) {
/**
* A singular task ES6++ mutex
* inspired by this https://blog.jcoglan.com/2016/07/12/mutexes-and-javascript/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was asking on discord about JS mutexes... before being sent the debounce route

does that queue mean if I press the button 10 times (or hit return 10 times in text entry), it'll queue 10 runs? I'm not sure that's better than throwing them away ?

*/
class Lock {
#isLocked = false
#queue = []
constructor(){}

sync(task) {
console.log("SYNC")
if (this.#isLocked) {
console.log("Lock already has a task, ommiting another one at " + new Date().toString())
return
}

this.#queue.push(task)
if (!this.#isLocked) this.#dequeue()
}

#dequeue() {
this.#isLocked = true
const next = this.#queue.shift()

if (next) this.#execute(next)
else this.#isLocked = false
}

async #execute(task) {
const thisDequeue = () => this.#dequeue()
task().then(thisDequeue, thisDequeue)
}
}

function debounce(func, ms = 100) { // 100ms debounce by default
let timeout
return function (...args) {
const context = this
clearTimeout(timeout)
timeout = setTimeout(() => func.apply(context, args), 100) // 100ms debounce
timeout = setTimeout(() => func.apply(context, args), ms)
}
}
if (!WebAssembly.instantiateStreaming) { // polyfill
Expand All @@ -68,10 +103,8 @@
function formatError(error) {
return `Error: ${error.message}`;
}
let isRunning = false
async function run() {
if (isRunning) return; // Prevent running multiple times concurrently
isRunning = true;

async function _run() {
document.getElementById("runButton").disabled = true; // Disable button during execution
try {
// console.clear();
Expand Down Expand Up @@ -105,20 +138,21 @@
} finally {
inst = await WebAssembly.instantiate(mod, go.importObject)
console.log('Instance reset:', inst)
if (isRunning) {
isRunning = false; // Allow running again after reset
document.getElementById("runButton").disabled = false; // Re-enable the button
}
document.getElementById("runButton").disabled = false; // Re-enable the button
}
resizeTextarea(document.getElementById('input'));
resizeTextarea(document.getElementById('output'));
resizeTextarea(document.getElementById('errors'));
}
const debounceRun = debounce(run)


const lock = new Lock()
const run = debounce(() => lock.sync(_run), 100)

document.addEventListener('DOMContentLoaded', (event) => {
document.getElementById('input').addEventListener('keydown', function (e) {
if (e.key === 'Enter' && !isRunning) {
debounceRun();
if (e.key === 'Enter') {
run()
}
});
});
Expand All @@ -142,7 +176,7 @@
m={"str key": a, PI: "pi", 42: "str val", 1e3: "a lot"}</textarea>
</div>
<div>
Hit enter or click <button onClick="debounceRun()" id="runButton" disabled>Run</button> (will also format the code,
Hit enter or click <button onClick="run()" id="runButton" disabled>Run</button> (will also format the code,
also
try <input type="checkbox" id="compact">compact)
<button id="addParamButton">Share</button>
Expand Down Expand Up @@ -185,3 +219,4 @@
document.getElementById('input').value = decodeURIComponent(paramValue)
}
</script>
</html>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, can't be here