Skip to content

Models and ownership

A Plexus model is a TypeScript object first. Decorating an accessor with @syncing makes it eligible for shared state. Constructing the object does not put it in a Yjs document.

import { Plexus, PlexusModel, syncing } from '@here.build/plexus';
@syncing('Task')
class Task extends PlexusModel<Project> {
@syncing accessor title = '';
}
@syncing('Project')
class Project extends PlexusModel {
@syncing.child.list accessor tasks: Task[];
}
const root = Plexus.bootstrap(new Project()).root;
const task = new Task({ title: 'draft' }); // not in the document yet
root.tasks.push(task); // now in the document

A new Task is not in the document. It can be read and changed. Pushing it onto root.tasks puts it in the document. A new object stays out until it is a child of a parent already there, until it adopts something already there, or until it is planted as the document’s root.

A new child under a parent that is already in the document joins that document. A child that is already in a document, adopted by a parent that is not, brings that parent into its document.

An object that already belongs to one document cannot move to another. Plexus raises PlexusDocMismatchError. Once an object is in a document, it stays in that document.

.uuid is an identity other peers can see, and it throws before the object is in the document. If you need a handle before that, keep the object itself. .localID exists for tests and debugging, and it is not sent to other peers.

Use @syncing on accessors, not ordinary class fields. The existing Plexus guide also documents @syncing.list, .set, .record, .map, and .child.list. Children participate in ownership. Plain references are a different relationship. Derive local display values with MobX rather than replicating a computed property.

Source contracts: plexus/README.md; plexus/docs/src/guide/fields.md; plexus/docs/src/laws/lifecycle.md; plexus/docs/src/laws/seed.md. The law pages specify the edge cases; this introduction does not replace them.