feat: Alice buys a widget from Bob, from the JS client
shop.mjs runs the ERTP story step by step as the principal of a key file; connect.js holds the session setup it shares with counter.mjs. The client stages any number of string, integer, and reference arguments, and reports why an answer broke. A new start from a caller closes that caller's old session, so a client can come back with the same key. Generated-by: Claude Code (claude-opus-5-5)
This commit is contained in:
1 parent
ed7d662d56
commit
9db91b1b9c
8 files changed
+287
-86
No files matched your search
+13
-11
@@ -78,7 +78,10 @@ export const openPeer = async ({ module, seed, carry }, canisterId) => {
|
||||
const valueOf = slot => {
|
||||
const kind = wasm.client_result_kind(slot);
|
||||
if (kind === 0) throw Error(`answer ${slot} still pending after the canister went quiet`);
|
||||
if (kind === 3) throw Error(`answer ${slot} broke`);
|
||||
if (kind === 3) {
|
||||
check(wasm.client_result_broken(slot), 'broken');
|
||||
throw Error(`answer ${slot} broke: ${decoder.decode(staged())}`);
|
||||
}
|
||||
if (kind === 4) return undefined;
|
||||
return kind === 2 ? Number(wasm.client_result_integer(slot)) : { slot };
|
||||
};
|
||||
@@ -94,22 +97,21 @@ export const openPeer = async ({ module, seed, carry }, canisterId) => {
|
||||
/** @returns {Answer} the capability published as `name` */
|
||||
fetch: name => ({ slot: slotOf(withBytes(encoder.encode(name), (p, n) => wasm.client_fetch(p, n)), 'fetch') }),
|
||||
/**
|
||||
* Send `verb` to `target` with the arguments in `args`: at most one
|
||||
* string, then at most one settled reference.
|
||||
* Send `verb` to `target`, without waiting for it to settle.
|
||||
*
|
||||
* @param {Answer} target
|
||||
* @param {string} verb
|
||||
* @param {...(string | Answer)} args
|
||||
* @param {...(string | bigint | Answer)} args strings, integers, and
|
||||
* answers that have settled to references
|
||||
* @returns {Answer} `target`'s answer
|
||||
*/
|
||||
send: (target, verb, ...args) => {
|
||||
const text = args.find(arg => typeof arg === 'string');
|
||||
const object = args.find(arg => typeof arg === 'object');
|
||||
const status = withBytes(encoder.encode(verb), (verbPtr, verbLen) =>
|
||||
withBytes(encoder.encode(text ?? ''), (textPtr, textLen) =>
|
||||
wasm.client_send(target.slot, verbPtr, verbLen, textPtr, text === undefined ? -1 : textLen, object ? object.slot : -1),
|
||||
),
|
||||
);
|
||||
for (const arg of args) {
|
||||
if (typeof arg === 'string') check(withBytes(encoder.encode(arg), wasm.client_arg_text), 'argument');
|
||||
else if (typeof arg === 'bigint') check(wasm.client_arg_integer(arg), 'argument');
|
||||
else check(wasm.client_arg_reference(arg.slot), 'argument');
|
||||
}
|
||||
const status = withBytes(encoder.encode(verb), (p, n) => wasm.client_send(target.slot, p, n));
|
||||
return { slot: slotOf(status, 'send') };
|
||||
},
|
||||
flush,
|
||||
|
||||
Reference in new issue
Block a user