feat(wasm): C4 async fetch/after effects with bridge continuations
Accept ADR 0003: handlers return fetch/after maps; the official bridge runs Promise/timer work and resumes via mq_call. Add fetch.mq.md demo. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
aac896b044
commit
ab52db2037
12 files changed
+374
-52
No files matched your search
@@ -15,7 +15,7 @@ python3 -m http.server 8765
|
||||
|----|------|
|
||||
| [/](http://127.0.0.1:8765/) | C1:编辑源码 → `mq_run` 打印 |
|
||||
| [/interact.html](http://127.0.0.1:8765/interact.html) | C3:`counter.mq.md` 交互表 → 点击改 DOM |
|
||||
| [/fetch.html](http://127.0.0.1:8765/fetch.html) | C4:`fetch` / `after` 效应续体 |
|
||||
|
||||
- `marqdo-bridge.js`:官方宿主桥(`boot` / `call` / `wireEvents` / `applyDomPatch`)
|
||||
- `counter.mq.md`:作者面只有 `.mq.md`(业务逻辑)
|
||||
- 设计:[browser-marqdo-wasm.md](../../doc/design/browser-marqdo-wasm.md)
|
||||
- `marqdo-bridge.js`:官方宿主桥(`boot` / `call` / `wireEvents` / `applyEffects`)
|
||||
- 业务源码只有 `.mq.md`;异步见 [ADR 0003](../../doc/adr/0003-browser-async-effects.md)
|
||||
@@ -0,0 +1,79 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-Hans">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>Marqdo · fetch / after (C4)</title>
|
||||
<style>
|
||||
:root {
|
||||
--ink: #1a1f1c;
|
||||
--paper: #e8efe6;
|
||||
--accent: #2f6f4e;
|
||||
--muted: #5a6b60;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
margin: 0;
|
||||
min-height: 100vh;
|
||||
font-family: "IBM Plex Sans", "Noto Sans SC", sans-serif;
|
||||
color: var(--ink);
|
||||
background:
|
||||
radial-gradient(800px 420px at 0% 100%, #d5e4d8 0%, transparent 55%),
|
||||
linear-gradient(165deg, #f3f7f2, var(--paper));
|
||||
padding: 2rem clamp(1rem, 4vw, 3rem);
|
||||
}
|
||||
h1 {
|
||||
font-family: "IBM Plex Serif", "Noto Serif SC", serif;
|
||||
font-weight: 600;
|
||||
font-size: clamp(1.6rem, 3vw, 2.2rem);
|
||||
margin: 0 0 0.35rem;
|
||||
}
|
||||
.lead { color: var(--muted); margin: 0 0 1.25rem; max-width: 38rem; }
|
||||
#status {
|
||||
font-family: "IBM Plex Mono", ui-monospace, monospace;
|
||||
font-size: 0.95rem;
|
||||
line-height: 1.45;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-all;
|
||||
min-height: 4rem;
|
||||
margin: 1rem 0 1.25rem;
|
||||
padding: 0.85rem 1rem;
|
||||
background: rgba(255,255,255,0.55);
|
||||
border: 1px solid #b7c7bb;
|
||||
}
|
||||
button {
|
||||
margin-right: 0.5rem;
|
||||
margin-bottom: 0.4rem;
|
||||
padding: 0.55rem 1.1rem;
|
||||
border: 0;
|
||||
background: var(--accent);
|
||||
color: #f5faf6;
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
button.secondary { background: #3a4740; }
|
||||
button:disabled { opacity: 0.5; cursor: wait; }
|
||||
pre {
|
||||
margin-top: 1.5rem;
|
||||
width: min(42rem, 100%);
|
||||
padding: 0.9rem 1rem;
|
||||
background: #122018;
|
||||
color: #d7eadc;
|
||||
overflow: auto;
|
||||
font-size: 0.8rem;
|
||||
line-height: 1.4;
|
||||
}
|
||||
.err { color: #ffb4a8; }
|
||||
a { color: var(--accent); }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Marqdo</h1>
|
||||
<p class="lead">C4:返回 <code>fetch</code> / <code>after</code> 效应,由官方 bridge 续体回调(ADR 0003)。业务只在 <code>fetch.mq.md</code>。<a href="./interact.html">计数器</a> · <a href="./">打印</a></p>
|
||||
<div id="status">idle</div>
|
||||
<button id="load" type="button" disabled>请求 UUID</button>
|
||||
<button id="ping" class="secondary" type="button" disabled>延迟 pong</button>
|
||||
<pre id="log">loading…</pre>
|
||||
<script type="module" src="./fetch.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,52 @@
|
||||
import {
|
||||
applyDomPatch,
|
||||
boot,
|
||||
loadWasm,
|
||||
readCString,
|
||||
wireEvents,
|
||||
} from "./marqdo-bridge.js";
|
||||
|
||||
async function main() {
|
||||
const log = document.getElementById("log");
|
||||
const loadBtn = document.getElementById("load");
|
||||
const pingBtn = document.getElementById("ping");
|
||||
|
||||
let exports;
|
||||
try {
|
||||
exports = await loadWasm("./marqdo_wasm.wasm");
|
||||
} catch (e) {
|
||||
log.classList.add("err");
|
||||
log.textContent = String(e);
|
||||
return;
|
||||
}
|
||||
|
||||
const srcRes = await fetch("./fetch.mq.md");
|
||||
if (!srcRes.ok) {
|
||||
log.classList.add("err");
|
||||
log.textContent = `failed to load fetch.mq.md (${srcRes.status})`;
|
||||
return;
|
||||
}
|
||||
const source = await srcRes.text();
|
||||
const result = boot(exports, source);
|
||||
if (!result.ok) {
|
||||
log.classList.add("err");
|
||||
log.textContent = result.error || "boot failed";
|
||||
return;
|
||||
}
|
||||
|
||||
const n = wireEvents(exports, result.value, {
|
||||
onError: (err) => {
|
||||
log.classList.add("err");
|
||||
log.textContent = err;
|
||||
},
|
||||
});
|
||||
applyDomPatch({ set_text: { "#status": "idle" } });
|
||||
|
||||
const ver = readCString(exports.memory, exports.mq_version());
|
||||
log.classList.remove("err");
|
||||
log.textContent = `marqdo-wasm ${ver} · wired ${n} · ADR 0003 effects\nsource: fetch.mq.md`;
|
||||
loadBtn.disabled = false;
|
||||
pingBtn.disabled = false;
|
||||
}
|
||||
|
||||
main();
|
||||
@@ -0,0 +1,50 @@
|
||||
---
|
||||
title: browser fetch (C4)
|
||||
description: return fetch/after effects; bridge continues via mq_call
|
||||
import json:lib/json.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
`wire` =
|
||||
|
||||
| @ | 选择器 | 事件 | 调用 |
|
||||
|---|--------|------|------|
|
||||
| 1 | "#load" | click | load |
|
||||
| 2 | "#ping" | click | ping |
|
||||
|
||||
**`wire`**
|
||||
|
||||
## load
|
||||
*`patch` = > json.set map=None key="#status" value="loading…"*
|
||||
*`ret` = > json.set map=None key="set_text" value=patch*
|
||||
*`req` = > json.set map=None key="url" value="https://httpbin.org/uuid"*
|
||||
*`req` = > json.set map=req key="then" value="on_uuid"*
|
||||
**> json.set map=ret key="fetch" value=req**
|
||||
|
||||
## on_uuid
|
||||
+ `ok`
|
||||
+ `status`
|
||||
+ `body`=""
|
||||
+ `error`=""
|
||||
|
||||
1. ok
|
||||
*`p` = > json.set map=None key="#status" value=body*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
2. *
|
||||
*`msg` = "fetch failed"*
|
||||
*`p` = > json.set map=None key="#status" value=msg*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
|
||||
## ping
|
||||
*`patch` = > json.set map=None key="#status" value="wait 600ms…"*
|
||||
*`ret` = > json.set map=None key="set_text" value=patch*
|
||||
*`t` = > json.set map=None key="ms" value=600*
|
||||
*`t` = > json.set map=t key="then" value="on_ping"*
|
||||
**> json.set map=ret key="after" value=t**
|
||||
|
||||
## on_ping
|
||||
+ `ok`=True
|
||||
|
||||
*`p` = > json.set map=None key="#status" value="pong"*
|
||||
**> json.set map=None key="set_text" value=p**
|
||||
@@ -62,7 +62,7 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Marqdo</h1>
|
||||
<p class="lead">C3:GFM 交互表装配事件;点击由 WASM 会话里的 Marqdo 函数处理并回写 DOM。<a href="./">打印示例</a></p>
|
||||
<p class="lead">C3:GFM 交互表装配事件;点击由 WASM 会话里的 Marqdo 函数处理并回写 DOM。<a href="./fetch.html">fetch (C4)</a> · <a href="./">打印示例</a></p>
|
||||
<div id="count">0</div>
|
||||
<button id="bump" type="button" disabled>加一</button>
|
||||
<button id="reset" class="secondary" type="button" disabled>清零</button>
|
||||
|
||||
@@ -64,7 +64,7 @@ export async function loadWasm(url = "./marqdo_wasm.wasm") {
|
||||
return instance.exports;
|
||||
}
|
||||
|
||||
/** Apply C3 return patches: { set_text: { "#id": "…" } } */
|
||||
/** Apply C3/C4 return effects: set_text, fetch, after */
|
||||
export function applyDomPatch(value) {
|
||||
if (!value || typeof value !== "object") return;
|
||||
const setText = value.set_text || value.setText;
|
||||
@@ -76,6 +76,96 @@ export function applyDomPatch(value) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Run sync DOM patches then schedule async effects (ADR 0003).
|
||||
* Returns a Promise that settles when immediate scheduling is done
|
||||
* (not when fetch/after callbacks finish).
|
||||
*/
|
||||
export function applyEffects(exports, value, { onError } = {}) {
|
||||
applyDomPatch(value);
|
||||
if (!value || typeof value !== "object") return Promise.resolve();
|
||||
|
||||
const tasks = [];
|
||||
|
||||
const fetchSpec = value.fetch;
|
||||
if (fetchSpec && typeof fetchSpec === "object" && fetchSpec.url && fetchSpec.then) {
|
||||
tasks.push(runFetchEffect(exports, fetchSpec, onError));
|
||||
}
|
||||
|
||||
const afterSpec = value.after;
|
||||
if (afterSpec && typeof afterSpec === "object" && afterSpec.then) {
|
||||
tasks.push(runAfterEffect(exports, afterSpec, onError));
|
||||
}
|
||||
|
||||
return Promise.all(tasks);
|
||||
}
|
||||
|
||||
async function runFetchEffect(exports, spec, onError) {
|
||||
const thenFn = spec.then;
|
||||
const method = (spec.method || "GET").toUpperCase();
|
||||
const init = { method };
|
||||
if (spec.headers && typeof spec.headers === "object") {
|
||||
init.headers = spec.headers;
|
||||
}
|
||||
if (spec.body != null && method !== "GET" && method !== "HEAD") {
|
||||
init.body = typeof spec.body === "string" ? spec.body : JSON.stringify(spec.body);
|
||||
}
|
||||
try {
|
||||
const res = await fetch(String(spec.url), init);
|
||||
const body = await res.text();
|
||||
const result = call(exports, thenFn, {
|
||||
ok: res.ok,
|
||||
status: res.status,
|
||||
body,
|
||||
});
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
else console.error(result.error);
|
||||
return;
|
||||
}
|
||||
await applyEffects(exports, result.value, { onError });
|
||||
if (result.stdout) console.log(result.stdout);
|
||||
} catch (e) {
|
||||
try {
|
||||
const result = call(exports, thenFn, {
|
||||
ok: false,
|
||||
status: 0,
|
||||
body: "",
|
||||
error: String(e),
|
||||
});
|
||||
if (result.ok) await applyEffects(exports, result.value, { onError });
|
||||
else if (onError) onError(result.error);
|
||||
} catch (e2) {
|
||||
if (onError) onError(String(e2));
|
||||
else console.error(e2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function runAfterEffect(exports, spec, onError) {
|
||||
const ms = Math.max(0, Number(spec.ms) || 0);
|
||||
const thenFn = spec.then;
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
try {
|
||||
const result = call(exports, thenFn, { ok: true });
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
else console.error(result.error);
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
Promise.resolve(applyEffects(exports, result.value, { onError })).then(resolve);
|
||||
if (result.stdout) console.log(result.stdout);
|
||||
} catch (e) {
|
||||
if (onError) onError(String(e));
|
||||
else console.error(e);
|
||||
resolve();
|
||||
}
|
||||
}, ms);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Wire events from `# main` return value.
|
||||
* Accepts a list of row maps, or `{ wire: [...] }`.
|
||||
@@ -101,7 +191,7 @@ export function wireEvents(exports, bootValue, { onError } = {}) {
|
||||
else console.error(result.error);
|
||||
return;
|
||||
}
|
||||
applyDomPatch(result.value);
|
||||
applyEffects(exports, result.value, { onError });
|
||||
if (result.stdout) console.log(result.stdout);
|
||||
} catch (e) {
|
||||
if (onError) onError(String(e));
|
||||
|
||||
Reference in new issue
Block a user