feat(editor): add wireCollab extension point + typed mutation events#38
Merged
Conversation
Adds a small extension point that downstream consumers can use to
mirror the editor's in-memory graph into an external store without
copying code or shadowing internal modules.
Two pieces:
1. Extended `wireMutations` (src/editor/mutations.ts) — `onMutate`
callback fires alongside `scheduleSave` for every mutation.
The event carries the kind ("box" | "edge" | "text" | "line" |
"stroke" | "currentMap" | "doc") and the current map path so a
consumer can scope a diff to the affected slice.
`getMapPath` is also new and optional (defaults to "/"). All
existing wireMutations callers continue to compile and run
unchanged.
2. New `src/editor/collab-api.ts` — exports `whenCollabReady(cb)`
and a `CollabHandle` interface:
interface CollabHandle {
snapshot(): FlowgoGraph;
applyRemotePatch(fn: (g: FlowgoGraph) => void): void;
onLocalMutation(cb: (e: MutationEvent) => void): () => void;
}
Plugins call whenCollabReady from a separate bundle; the editor's
main.ts calls exposeCollabHandle at the end of bootstrap with an
impl that snapshots the live graph (structuredClone), applies
remote patches in place + re-renders, and fans onMutate events
out to subscribers.
FlowgoGraph + FlowgoBox/Edge/Text/Line/Stroke types mirror the
pkg/graph.Graph shape verbatim so a plugin can pass snapshots to
a Go/Rust sidecar via JSON without translation.
`./collab` added to the package.json exports map so consumers can
import "@flowgo/editor/collab".
Bundle size: 103.82 KB (+414 bytes vs main, almost all from the
type-only declarations that get erased at build time).
All 120 vitest tests pass; tsc --noEmit clean; vite build clean.
Zero behaviour change for CLI / single-user consumers — the new
file ships in every bundle but does nothing until a downstream
plugin calls whenCollabReady.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a small extension point that downstream consumers can use to mirror the editor's in-memory graph into an external store without copying code or shadowing internal modules.
Two pieces:
Extended
wireMutations(src/editor/mutations.ts) —onMutatecallback fires alongsidescheduleSavefor every mutation. The event carries the kind ("box" | "edge" | "text" | "line" | "stroke" | "currentMap" | "doc") and the current map path so a consumer can scope a diff to the affected slice.getMapPathis also new and optional (defaults to "/"). All existing wireMutations callers continue to compile and run unchanged.New
src/editor/collab-api.ts— exportswhenCollabReady(cb)and aCollabHandleinterface:interface CollabHandle {
snapshot(): FlowgoGraph;
applyRemotePatch(fn: (g: FlowgoGraph) => void): void;
onLocalMutation(cb: (e: MutationEvent) => void): () => void;
}
Plugins call whenCollabReady from a separate bundle; the editor's main.ts calls exposeCollabHandle at the end of bootstrap with an impl that snapshots the live graph (structuredClone), applies remote patches in place + re-renders, and fans onMutate events out to subscribers.
FlowgoGraph + FlowgoBox/Edge/Text/Line/Stroke types mirror the pkg/graph.Graph shape verbatim so a plugin can pass snapshots to a Go/Rust sidecar via JSON without translation.
./collabadded to the package.json exports map so consumers can import "@flowgo/editor/collab".Bundle size: 103.82 KB (+414 bytes vs main, almost all from the type-only declarations that get erased at build time).
All 120 vitest tests pass; tsc --noEmit clean; vite build clean. Zero behaviour change for CLI / single-user consumers — the new file ships in every bundle but does nothing until a downstream plugin calls whenCollabReady.