syndicate-js/packages/create/template/src/index.ts

53 lines
1.9 KiB
TypeScript

import { Dataspace, Ref, Sturdy, Reader, Schemas, Embedded } from "@syndicate-lang/core";
import html from "@syndicate-lang/html";
import wsRelay from "@syndicate-lang/ws-relay";
import { ExampleDefinition } from './gen/example';
export function main() {
Dataspace.boot(ds => {
html.boot(ds);
wsRelay.boot(ds, true /* remove this `true` to turn off client/server debug logging */);
bootApp(ds);
});
}
function bootApp(ds: Ref) {
spawn named 'app' {
at ds {
/*
* This example expects a syndicate-server instance running on port 9001 on the
* server hosting index.html, exposing a dataspace entity via a capability called
* `"syndicate"` with empty "secret". See syndicate-server.config.pr.
*/
const serverCap = Sturdy.asSturdyRef(new Reader<Ref>(
'<ref "syndicate" [] #[pkgN9TBmEd3Q04grVG4Zdw==]>').next());
const this_instance = crypto.randomUUID();
during wsRelay.Resolved({
"addr": wsRelay.RelayAddress(Schemas.transportAddress.WebSocket(
`ws://${document.location.hostname}:9001/`)),
"sturdyref": serverCap,
"resolved": $remoteDs_e: Embedded,
}) => {
const remoteDs = remoteDs_e.embeddedValue;
at remoteDs {
assert ExampleDefinition(this_instance);
during ExampleDefinition($who: string) => {
console.log('saw', who);
const ui = new html.Anchor();
at ds {
assert ui.html(
'#main',
html.template`<p>We see <span class="example-definition">${who}</span></p>`,
who);
}
}
}
}
}
}
}