Files
Docwell/Server/internal/ycrdt/pool.go
T
tajniak81andClaude Opus 5.5 ba394b8fac Finish Phase 0: lint, codegen, contributor docs and the two remaining spikes
Repository basics:
- CONTRIBUTING.md and CODE_OF_CONDUCT.md (Contributor Covenant 2.1)
- SPDX AGPL-3.0-or-later headers on all hand-written sources, checked in CI
- golangci-lint (shared .golangci.yml), ESLint and Prettier, all run in CI

API codegen from api/openapi.yaml (npm run generate, drift fails CI):
- Go types via oapi-codegen in Server/internal/api
- a contract test that the router and the spec list the same routes
- Dart models via Phone-App/tool/generate_api.dart; the app uses them

Spike: shared Tiptap editor in a Flutter WebView (ADR-4)
- @docwell/editor-bridge builds the editor into one HTML page with a
  versioned JSON protocol; Phone-App hosts it with webview_flutter and a
  native toolbar. Integration test passes on an Android API 35 emulator.
- flutter_inappwebview dropped: its Android plugin fails on AGP 9.

Spike: Wails v3 desktop (ADR-11)
- system tray, docwell:// deep links (page and space only) with
  single-instance hand-off, and the built-in updater with a required
  ed25519 signing key. Verified on Windows.
- regenerated build assets register the scheme and drop template
  placeholders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 16:11:07 +01:00

108 lines
3.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
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
}