Pages are now shared Yjs documents. docwell-server relays the y-websocket protocol at /collab (token sent as the first message), flushes updates to page_yupdates every second, and compacts them into page_ydoc with yrs compiled to WebAssembly under wazero (ADR-3) after 30 s idle or when the last editor leaves. Compaction refreshes content_json/content_text and records page_versions; versions can be listed, previewed and restored. The editor connects through a small y-protocols provider with carets and presence. Pages saved before this change are seeded from content_json. The yrs module lives in Server/ycrdt-wasm and is built by go generate, CI and the Dockerfiles rather than committed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
106 lines
3.1 KiB
Go
106 lines
3.1 KiB
Go
package ycrdt
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
// retireAbove is the memory size after which an instance is replaced:
|
|
// WebAssembly memory never shrinks after a large document.
|
|
const retireAbove = 64 << 20
|
|
|
|
// Pool hands out instances to one caller at a time.
|
|
type Pool struct {
|
|
rt *Runtime
|
|
idle chan *Instance
|
|
}
|
|
|
|
// NewPool keeps up to size instances; they are created on first use.
|
|
func NewPool(rt *Runtime, size int) *Pool {
|
|
p := &Pool{rt: rt, idle: make(chan *Instance, size)}
|
|
for range size {
|
|
p.idle <- nil
|
|
}
|
|
return p
|
|
}
|
|
|
|
// Do runs fn with an instance. An instance that returned an error is thrown
|
|
// away, since the module aborts on panics and may be left in a bad state.
|
|
func (p *Pool) Do(ctx context.Context, fn func(*Instance) error) error {
|
|
var inst *Instance
|
|
select {
|
|
case inst = <-p.idle:
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
if inst == nil {
|
|
var err error
|
|
if inst, err = p.rt.Instance(ctx); err != nil {
|
|
p.idle <- nil
|
|
return err
|
|
}
|
|
}
|
|
err := fn(inst)
|
|
if err != nil || inst.MemoryBytes() > retireAbove {
|
|
inst.Close(context.WithoutCancel(ctx))
|
|
inst = nil
|
|
}
|
|
p.idle <- inst
|
|
return err
|
|
}
|
|
|
|
// Merge combines a few updates into one without building a document. It is
|
|
// quadratic in the number of updates: use it for small batches only.
|
|
func (p *Pool) Merge(ctx context.Context, updates [][]byte) (out []byte, err error) {
|
|
if len(updates) == 1 {
|
|
return updates[0], nil
|
|
}
|
|
err = p.Do(ctx, func(i *Instance) error { out, err = i.Merge(ctx, updates); return err })
|
|
return out, err
|
|
}
|
|
|
|
// Compact merges updates into one snapshot update.
|
|
func (p *Pool) Compact(ctx context.Context, updates [][]byte) (out []byte, err error) {
|
|
err = p.Do(ctx, func(i *Instance) error { out, err = i.Compact(ctx, updates); return err })
|
|
return out, err
|
|
}
|
|
|
|
// Diff returns what a peer with state vector sv is missing from update.
|
|
func (p *Pool) Diff(ctx context.Context, update, sv []byte) (out []byte, err error) {
|
|
err = p.Do(ctx, func(i *Instance) error { out, err = i.Diff(ctx, update, sv); return err })
|
|
return out, err
|
|
}
|
|
|
|
// StateVector returns the state vector of update.
|
|
func (p *Pool) StateVector(ctx context.Context, update []byte) (out []byte, err error) {
|
|
err = p.Do(ctx, func(i *Instance) error { out, err = i.StateVector(ctx, update); return err })
|
|
return out, err
|
|
}
|
|
|
|
// Replace returns an update that replaces the document in updates with doc (ProseMirror JSON).
|
|
func (p *Pool) Replace(ctx context.Context, updates [][]byte, doc []byte) (out []byte, err error) {
|
|
err = p.Do(ctx, func(i *Instance) error { out, err = i.Replace(ctx, updates, doc); return err })
|
|
return out, err
|
|
}
|
|
|
|
// Read is the derived content of a snapshot.
|
|
type Read struct {
|
|
JSON []byte
|
|
Text string
|
|
}
|
|
|
|
// Read extracts ProseMirror JSON and plain text from a snapshot in one checkout.
|
|
func (p *Pool) Read(ctx context.Context, snapshot []byte) (r Read, err error) {
|
|
err = p.Do(ctx, func(i *Instance) error {
|
|
u := [][]byte{snapshot}
|
|
if r.JSON, err = i.JSON(ctx, u); err != nil {
|
|
return fmt.Errorf("json: %w", err)
|
|
}
|
|
if r.Text, err = i.Text(ctx, u); err != nil {
|
|
return fmt.Errorf("text: %w", err)
|
|
}
|
|
return nil
|
|
})
|
|
return r, err
|
|
}
|