basic version of forms

This commit is contained in:
metacryst
2025-12-22 04:53:00 -06:00
parent 80ceb48a62
commit 97bd02c59a
14 changed files with 153 additions and 1742 deletions

View File

@@ -1,25 +1,47 @@
import WebSocket from "ws"
export default class FormsClient {
constructor(url = "ws://localhost:4001") {
this.url = url
class client {
constructor() {
this.url = `ws://localhost:${10000}`
this.ws = null
this.seq = 0
this.handlers = new Map()
console.log(process.cwd())
}
connect() {
this.ws = new WebSocket(this.url)
return new Promise((resolve, reject) => {
this.ws = new WebSocket(this.url)
this.ws.on("message", msg => this._onMessage(msg))
this.ws.on("open", () => console.log("[forms] connected"))
this.ws.on("close", () => console.log("[forms] disconnected"))
this.ws.on("message", msg => this.onMessage(msg))
this.ws.on("open", () => {
console.log("[forms] connected")
resolve()
})
this.ws.on("error", err => {
reject(err)
})
this.ws.on("close", () => {
console.log("[forms] disconnected")
})
})
}
append(form, data) {
register(type, schema) {
this.ws.send(JSON.stringify({
op: "append",
form,
op: "register",
type,
schema
}))
}
add(type, data) {
this.ws.send(JSON.stringify({
op: "add",
type,
data
}))
}
@@ -38,15 +60,16 @@ export default class FormsClient {
}))
}
_onMessage(raw) {
onMessage(raw) {
const msg = JSON.parse(raw.toString())
console.log("msg received: ", msg)
if (msg.type === "hello") {
this.seq = msg.seq
return
}
if (msg.type === "form:update") {
if (msg.type === "add") {
this.seq = msg.seq
const set = this.handlers.get(msg.form)
@@ -58,3 +81,7 @@ export default class FormsClient {
}
}
}
export default {
client: client
}