@canvist/headless 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/calls.d.ts +15 -0
- package/dist/calls.js +20 -0
- package/dist/create.d.ts +17 -0
- package/dist/create.js +29 -0
- package/dist/engine.d.ts +125 -0
- package/dist/engine.js +176 -0
- package/dist/errors.d.ts +13 -0
- package/dist/errors.js +5 -0
- package/dist/extensions.generated.d.ts +1022 -0
- package/dist/extensions.generated.js +744 -0
- package/dist/extras.generated.d.ts +5803 -0
- package/dist/extras.generated.js +2600 -0
- package/dist/full.d.ts +21 -0
- package/dist/full.js +63 -0
- package/dist/gemoji.generated.d.ts +29 -0
- package/dist/gemoji.generated.js +30 -0
- package/dist/load.d.ts +21 -0
- package/dist/load.js +41 -0
- package/dist/mod.d.ts +25 -0
- package/dist/mod.js +24 -0
- package/dist/native.d.ts +115 -0
- package/dist/native.js +235 -0
- package/dist/scene-types.d.ts +223 -0
- package/dist/scene-types.js +1 -0
- package/dist/scene.d.ts +105 -0
- package/dist/scene.js +153 -0
- package/dist/ssr.d.ts +66 -0
- package/dist/ssr.js +98 -0
- package/dist/types.d.ts +232 -0
- package/dist/types.js +1 -0
- package/package.json +89 -5
- package/readme.md +96 -0
- package/wasm/canvist_headless_wasm.d.ts +216 -0
- package/wasm/canvist_headless_wasm.js +910 -0
- package/wasm/canvist_headless_wasm_bg.wasm +0 -0
- package/wasm/canvist_headless_wasm_bg.wasm.d.ts +44 -0
- package/wasm-full/canvist_headless_full.d.ts +223 -0
- package/wasm-full/canvist_headless_full.js +923 -0
- package/wasm-full/canvist_headless_full_bg.wasm +0 -0
- package/wasm-full/canvist_headless_full_bg.wasm.d.ts +45 -0
- package/wasm-scene/canvist_headless_scene.d.ts +332 -0
- package/wasm-scene/canvist_headless_scene.js +1231 -0
- package/wasm-scene/canvist_headless_scene_bg.wasm +0 -0
- package/wasm-scene/canvist_headless_scene_bg.wasm.d.ts +66 -0
- package/wasm-ssr/canvist_headless_ssr.d.ts +250 -0
- package/wasm-ssr/canvist_headless_ssr.js +1025 -0
- package/wasm-ssr/canvist_headless_ssr_bg.wasm +0 -0
- package/wasm-ssr/canvist_headless_ssr_bg.wasm.d.ts +50 -0
- package/README.md +0 -3
package/dist/calls.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How typed commands and queries reach an engine: one method per
|
|
3
|
+
* registered name, with arguments sent as JSON. The WebAssembly documents
|
|
4
|
+
* and the native editors share it.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* An object with one method per name, each calling `invoke`. The names come
|
|
10
|
+
* from the same metadata as the type `Methods`, which is what makes the
|
|
11
|
+
* object that type.
|
|
12
|
+
*/
|
|
13
|
+
export declare function calls<Methods>(names: readonly string[], invoke: (name: string, args: unknown) => unknown): Methods;
|
|
14
|
+
/** A command's or query's arguments as the engine reads them: JSON, or `null` for none. */
|
|
15
|
+
export declare function encode(args: unknown): string;
|
package/dist/calls.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How typed commands and queries reach an engine: one method per
|
|
3
|
+
* registered name, with arguments sent as JSON. The WebAssembly documents
|
|
4
|
+
* and the native editors share it.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* An object with one method per name, each calling `invoke`. The names come
|
|
10
|
+
* from the same metadata as the type `Methods`, which is what makes the
|
|
11
|
+
* object that type.
|
|
12
|
+
*/
|
|
13
|
+
export function calls(names, invoke) {
|
|
14
|
+
const methods = Object.fromEntries(names.map((name) => [name, (args) => invoke(name, args)]));
|
|
15
|
+
return methods;
|
|
16
|
+
}
|
|
17
|
+
/** A command's or query's arguments as the engine reads them: JSON, or `null` for none. */
|
|
18
|
+
export function encode(args) {
|
|
19
|
+
return args === undefined ? "null" : JSON.stringify(args);
|
|
20
|
+
}
|
package/dist/create.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Engine, type EngineOptions } from "./engine.ts";
|
|
2
|
+
import type { StarterKit } from "./types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Load canvist and create an engine with the given StarterKit extensions,
|
|
5
|
+
* all of them by default. The engine's documents only offer the registered
|
|
6
|
+
* extensions' commands, queries, blocks, and marks, so using another one
|
|
7
|
+
* is a type error. Every other extension is in `@canvist/headless/full`.
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* const engine = await createEngine([bold, heading]);
|
|
11
|
+
* const doc = engine.parse("markdown", "Hello");
|
|
12
|
+
* doc.selectAll();
|
|
13
|
+
* doc.commands.toggleBold();
|
|
14
|
+
* doc.commands.toggleItalic(); // type error: italic is not registered
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function createEngine<const E extends readonly StarterKit[number][] = StarterKit>(extensions?: E, options?: EngineOptions): Promise<Engine<E>>;
|
package/dist/create.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creating engines from the StarterKit WebAssembly module.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { WasmEngine } from "../wasm/canvist_headless_wasm.js";
|
|
7
|
+
import { Engine } from "./engine.js";
|
|
8
|
+
import { starterKit } from "./extensions.generated.js";
|
|
9
|
+
import { load } from "./load.js";
|
|
10
|
+
/**
|
|
11
|
+
* Load canvist and create an engine with the given StarterKit extensions,
|
|
12
|
+
* all of them by default. The engine's documents only offer the registered
|
|
13
|
+
* extensions' commands, queries, blocks, and marks, so using another one
|
|
14
|
+
* is a type error. Every other extension is in `@canvist/headless/full`.
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* const engine = await createEngine([bold, heading]);
|
|
18
|
+
* const doc = engine.parse("markdown", "Hello");
|
|
19
|
+
* doc.selectAll();
|
|
20
|
+
* doc.commands.toggleBold();
|
|
21
|
+
* doc.commands.toggleItalic(); // type error: italic is not registered
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export async function createEngine(extensions, options = {}) {
|
|
25
|
+
await load(options.wasm);
|
|
26
|
+
const registered = (extensions ?? starterKit);
|
|
27
|
+
const names = registered.map((extension) => extension.name);
|
|
28
|
+
return new Engine(new WasmEngine(names, options.client ?? 1), registered);
|
|
29
|
+
}
|
package/dist/engine.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The typed engine and document, over either WebAssembly module: the
|
|
3
|
+
* StarterKit one `@canvist/headless` loads, or the one with every extension
|
|
4
|
+
* `@canvist/headless/full` loads. Both export the same classes.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import type { WasmDoc, WasmEngine } from "../wasm/canvist_headless_wasm.js";
|
|
9
|
+
export { type CanvistError, isCanvistError } from "./errors.ts";
|
|
10
|
+
import type { WasmSource } from "./load.ts";
|
|
11
|
+
import type { CommandName, CommandParams, Commands, DocJson, ExtensionDescriptor, Extensions, Format, Position, Queries, QueryName, QueryParams, QueryResult, Range, Registered } from "./types.ts";
|
|
12
|
+
/** Options for `createEngine`. */
|
|
13
|
+
export interface EngineOptions {
|
|
14
|
+
/** The client block ids are allocated under; collaborating peers need distinct ones. Default 1. */
|
|
15
|
+
readonly client?: number;
|
|
16
|
+
/** Where to load the WebAssembly module from, for runtimes without a file system. */
|
|
17
|
+
readonly wasm?: WasmSource;
|
|
18
|
+
}
|
|
19
|
+
/** What {@link Doc.receiveSync} says to send. */
|
|
20
|
+
export interface SyncReply {
|
|
21
|
+
/** Messages for the connection the message came from. */
|
|
22
|
+
readonly reply: readonly Uint8Array[];
|
|
23
|
+
/** Messages for every other connection to the document. */
|
|
24
|
+
readonly broadcast: readonly Uint8Array[];
|
|
25
|
+
/** Whether the document changed. */
|
|
26
|
+
readonly changed: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** How well a shared document's state fits the schema. */
|
|
29
|
+
export interface Validation {
|
|
30
|
+
/** Shared items the schema rejects; they are hidden from the document but kept. */
|
|
31
|
+
readonly rejectedBlocks: number;
|
|
32
|
+
/** Whether updates wait for earlier updates that have not arrived. */
|
|
33
|
+
readonly waiting: boolean;
|
|
34
|
+
}
|
|
35
|
+
/** A set of extensions that creates documents. Create one with `createEngine`. */
|
|
36
|
+
export declare class Engine<E extends Extensions> {
|
|
37
|
+
#private;
|
|
38
|
+
/** @internal Use `createEngine`, which loads the WebAssembly module first. */
|
|
39
|
+
constructor(wasm: WasmEngine, extensions: E);
|
|
40
|
+
/** The registered extensions, including the ones every engine has. */
|
|
41
|
+
get extensions(): Registered<E>;
|
|
42
|
+
/**
|
|
43
|
+
* The registered extensions' metadata as Rust reports it: their
|
|
44
|
+
* descriptors, with every command, query, and field's `doc`.
|
|
45
|
+
*/
|
|
46
|
+
metadata(): ExtensionDescriptor[];
|
|
47
|
+
/** A document with one empty paragraph. */
|
|
48
|
+
create(): Doc<E>;
|
|
49
|
+
/** Read `input` as `format`. */
|
|
50
|
+
parse(format: Format, input: string): Doc<E>;
|
|
51
|
+
/** Read a ProseMirror or TipTap document that uses only the registered extensions. */
|
|
52
|
+
fromJson(json: DocJson<Registered<E>>): Doc<E>;
|
|
53
|
+
/** Join a shared document from v1 Yjs state as yrs client `clientId`. */
|
|
54
|
+
fromYjs(state: Uint8Array, clientId: number): Doc<E>;
|
|
55
|
+
/** Recreate a document from {@link Doc.snapshot} text. */
|
|
56
|
+
restore(snapshot: string): Doc<E>;
|
|
57
|
+
/** Release the engine's memory now rather than when it is collected. */
|
|
58
|
+
[Symbol.dispose](): void;
|
|
59
|
+
}
|
|
60
|
+
/** One document and the editor that changes it. Every call is one undo step. */
|
|
61
|
+
export declare class Doc<E extends Extensions> {
|
|
62
|
+
#private;
|
|
63
|
+
/** Run a registered command against the selection; returns whether it applied. */
|
|
64
|
+
readonly commands: Commands<Registered<E>>;
|
|
65
|
+
/** Whether a registered command would apply. Nothing changes. */
|
|
66
|
+
readonly can: Commands<Registered<E>>;
|
|
67
|
+
/** Answer a registered query. */
|
|
68
|
+
readonly query: Queries<Registered<E>>;
|
|
69
|
+
/** @internal Documents come from an {@link Engine}. */
|
|
70
|
+
constructor(wasm: WasmDoc, extensions: Extensions);
|
|
71
|
+
/** Run the command named `name`, for callers that pick commands at runtime. */
|
|
72
|
+
run<N extends CommandName<Registered<E>>>(name: N, ...args: CommandParams<Registered<E>, N>): boolean;
|
|
73
|
+
/** Whether the command named `name` would apply. Nothing changes. */
|
|
74
|
+
canRun<N extends CommandName<Registered<E>>>(name: N, ...args: CommandParams<Registered<E>, N>): boolean;
|
|
75
|
+
/** Answer the query named `name`, for callers that pick queries at runtime. */
|
|
76
|
+
ask<N extends QueryName<Registered<E>>>(name: N, ...args: QueryParams<Registered<E>, N>): QueryResult<Registered<E>, N>;
|
|
77
|
+
/** The document as `format`. */
|
|
78
|
+
export(format: Format): string;
|
|
79
|
+
/** The document as ProseMirror JSON, with block ids in `attrs.id`. */
|
|
80
|
+
toJson(): DocJson<Registered<E>>;
|
|
81
|
+
/** The text selection, or `null` for a block selection. */
|
|
82
|
+
get selection(): Range | null;
|
|
83
|
+
/** Select from `anchor` to `head`, or place a caret at `anchor`. Offsets count UTF-16 code units. */
|
|
84
|
+
select(anchor: Position, head?: Position): void;
|
|
85
|
+
/** Select the whole document. */
|
|
86
|
+
selectAll(): void;
|
|
87
|
+
/** Replace the selection with `text`, without input rules. */
|
|
88
|
+
insertText(text: string): boolean;
|
|
89
|
+
/** Type `text` one character at a time, so input rules such as `## ` apply. */
|
|
90
|
+
typeText(text: string): boolean;
|
|
91
|
+
/** Delete the selection. */
|
|
92
|
+
deleteSelection(): boolean;
|
|
93
|
+
/** Press a key binding such as `Enter`, `Tab`, or `Mod-b` (`Mod` is Ctrl). */
|
|
94
|
+
press(key: string): boolean;
|
|
95
|
+
/** Paste `content` read as `format` over the selection. */
|
|
96
|
+
paste(format: Format, content: string): boolean;
|
|
97
|
+
/** Undo the last call that changed the document. */
|
|
98
|
+
undo(): boolean;
|
|
99
|
+
/** Redo the last undone call. */
|
|
100
|
+
redo(): boolean;
|
|
101
|
+
/** The content and selection as snapshot JSON text, for {@link Engine.restore}. */
|
|
102
|
+
snapshot(): string;
|
|
103
|
+
/** Start sharing the document as yrs client `clientId`. */
|
|
104
|
+
share(clientId: number): void;
|
|
105
|
+
/** Apply a v1 Yjs update from another peer; returns whether the document changed. */
|
|
106
|
+
applyUpdate(update: Uint8Array): boolean;
|
|
107
|
+
/** The whole shared state as one v1 update. */
|
|
108
|
+
encodeState(): Uint8Array;
|
|
109
|
+
/** The v1 state vector. */
|
|
110
|
+
stateVector(): Uint8Array;
|
|
111
|
+
/** Everything a peer at `stateVector` is missing, as one v1 update. */
|
|
112
|
+
encodeDiff(stateVector: Uint8Array): Uint8Array;
|
|
113
|
+
/** Drain this peer's own v1 updates for broadcasting. */
|
|
114
|
+
takeUpdates(): Uint8Array[];
|
|
115
|
+
/** Check the shared state against the schema. */
|
|
116
|
+
validate(): Validation;
|
|
117
|
+
/** The y-sync message to greet a connecting client with. */
|
|
118
|
+
syncStart(): Uint8Array;
|
|
119
|
+
/** Handle one y-sync message from a client connection. */
|
|
120
|
+
receiveSync(message: Uint8Array): SyncReply;
|
|
121
|
+
/** This peer's changes since the last call, as y-sync messages for every connection. */
|
|
122
|
+
flushSync(): Uint8Array[];
|
|
123
|
+
/** Release the document's memory now rather than when it is collected. */
|
|
124
|
+
[Symbol.dispose](): void;
|
|
125
|
+
}
|
package/dist/engine.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { calls, encode } from "./calls.js";
|
|
2
|
+
export { isCanvistError } from "./errors.js";
|
|
3
|
+
import { core } from "./extensions.generated.js";
|
|
4
|
+
/** A set of extensions that creates documents. Create one with `createEngine`. */
|
|
5
|
+
export class Engine {
|
|
6
|
+
#wasm;
|
|
7
|
+
#extensions;
|
|
8
|
+
/** @internal Use `createEngine`, which loads the WebAssembly module first. */
|
|
9
|
+
constructor(wasm, extensions) {
|
|
10
|
+
this.#extensions = [...core, ...extensions];
|
|
11
|
+
this.#wasm = wasm;
|
|
12
|
+
}
|
|
13
|
+
/** The registered extensions, including the ones every engine has. */
|
|
14
|
+
get extensions() {
|
|
15
|
+
return this.#extensions;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The registered extensions' metadata as Rust reports it: their
|
|
19
|
+
* descriptors, with every command, query, and field's `doc`.
|
|
20
|
+
*/
|
|
21
|
+
metadata() {
|
|
22
|
+
return JSON.parse(this.#wasm.metadata());
|
|
23
|
+
}
|
|
24
|
+
/** A document with one empty paragraph. */
|
|
25
|
+
create() {
|
|
26
|
+
return new Doc(this.#wasm.create(), this.#extensions);
|
|
27
|
+
}
|
|
28
|
+
/** Read `input` as `format`. */
|
|
29
|
+
parse(format, input) {
|
|
30
|
+
return new Doc(this.#wasm.parse(format, input), this.#extensions);
|
|
31
|
+
}
|
|
32
|
+
/** Read a ProseMirror or TipTap document that uses only the registered extensions. */
|
|
33
|
+
fromJson(json) {
|
|
34
|
+
return this.parse("json", JSON.stringify(json));
|
|
35
|
+
}
|
|
36
|
+
/** Join a shared document from v1 Yjs state as yrs client `clientId`. */
|
|
37
|
+
fromYjs(state, clientId) {
|
|
38
|
+
return new Doc(this.#wasm.fromYjs(state, clientId), this.#extensions);
|
|
39
|
+
}
|
|
40
|
+
/** Recreate a document from {@link Doc.snapshot} text. */
|
|
41
|
+
restore(snapshot) {
|
|
42
|
+
return new Doc(this.#wasm.restore(snapshot), this.#extensions);
|
|
43
|
+
}
|
|
44
|
+
/** Release the engine's memory now rather than when it is collected. */
|
|
45
|
+
[Symbol.dispose]() {
|
|
46
|
+
this.#wasm.free();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** One document and the editor that changes it. Every call is one undo step. */
|
|
50
|
+
export class Doc {
|
|
51
|
+
#wasm;
|
|
52
|
+
/** Run a registered command against the selection; returns whether it applied. */
|
|
53
|
+
commands;
|
|
54
|
+
/** Whether a registered command would apply. Nothing changes. */
|
|
55
|
+
can;
|
|
56
|
+
/** Answer a registered query. */
|
|
57
|
+
query;
|
|
58
|
+
/** @internal Documents come from an {@link Engine}. */
|
|
59
|
+
constructor(wasm, extensions) {
|
|
60
|
+
this.#wasm = wasm;
|
|
61
|
+
const commands = extensions.flatMap((extension) => extension.commands.map((command) => command.name));
|
|
62
|
+
const queries = extensions.flatMap((extension) => extension.queries.map((query) => query.name));
|
|
63
|
+
this.commands = calls(commands, (name, args) => this.#wasm.run(name, encode(args)));
|
|
64
|
+
this.can = calls(commands, (name, args) => this.#wasm.can(name, encode(args)));
|
|
65
|
+
this.query = calls(queries, (name, args) => JSON.parse(this.#wasm.query(name, encode(args))));
|
|
66
|
+
}
|
|
67
|
+
/** Run the command named `name`, for callers that pick commands at runtime. */
|
|
68
|
+
run(name, ...args) {
|
|
69
|
+
return this.#wasm.run(name, encode(args[0]));
|
|
70
|
+
}
|
|
71
|
+
/** Whether the command named `name` would apply. Nothing changes. */
|
|
72
|
+
canRun(name, ...args) {
|
|
73
|
+
return this.#wasm.can(name, encode(args[0]));
|
|
74
|
+
}
|
|
75
|
+
/** Answer the query named `name`, for callers that pick queries at runtime. */
|
|
76
|
+
ask(name, ...args) {
|
|
77
|
+
return JSON.parse(this.#wasm.query(name, encode(args[0])));
|
|
78
|
+
}
|
|
79
|
+
/** The document as `format`. */
|
|
80
|
+
export(format) {
|
|
81
|
+
return this.#wasm.export(format);
|
|
82
|
+
}
|
|
83
|
+
/** The document as ProseMirror JSON, with block ids in `attrs.id`. */
|
|
84
|
+
toJson() {
|
|
85
|
+
return JSON.parse(this.#wasm.export("json"));
|
|
86
|
+
}
|
|
87
|
+
/** The text selection, or `null` for a block selection. */
|
|
88
|
+
get selection() {
|
|
89
|
+
return JSON.parse(this.#wasm.selection());
|
|
90
|
+
}
|
|
91
|
+
/** Select from `anchor` to `head`, or place a caret at `anchor`. Offsets count UTF-16 code units. */
|
|
92
|
+
select(anchor, head = anchor) {
|
|
93
|
+
this.#wasm.select(Uint32Array.from(anchor.path), anchor.offset, Uint32Array.from(head.path), head.offset);
|
|
94
|
+
}
|
|
95
|
+
/** Select the whole document. */
|
|
96
|
+
selectAll() {
|
|
97
|
+
this.#wasm.selectAll();
|
|
98
|
+
}
|
|
99
|
+
/** Replace the selection with `text`, without input rules. */
|
|
100
|
+
insertText(text) {
|
|
101
|
+
return this.#wasm.insertText(text);
|
|
102
|
+
}
|
|
103
|
+
/** Type `text` one character at a time, so input rules such as `## ` apply. */
|
|
104
|
+
typeText(text) {
|
|
105
|
+
return this.#wasm.typeText(text);
|
|
106
|
+
}
|
|
107
|
+
/** Delete the selection. */
|
|
108
|
+
deleteSelection() {
|
|
109
|
+
return this.#wasm.deleteSelection();
|
|
110
|
+
}
|
|
111
|
+
/** Press a key binding such as `Enter`, `Tab`, or `Mod-b` (`Mod` is Ctrl). */
|
|
112
|
+
press(key) {
|
|
113
|
+
return this.#wasm.press(key);
|
|
114
|
+
}
|
|
115
|
+
/** Paste `content` read as `format` over the selection. */
|
|
116
|
+
paste(format, content) {
|
|
117
|
+
return this.#wasm.paste(format, content);
|
|
118
|
+
}
|
|
119
|
+
/** Undo the last call that changed the document. */
|
|
120
|
+
undo() {
|
|
121
|
+
return this.#wasm.undo();
|
|
122
|
+
}
|
|
123
|
+
/** Redo the last undone call. */
|
|
124
|
+
redo() {
|
|
125
|
+
return this.#wasm.redo();
|
|
126
|
+
}
|
|
127
|
+
/** The content and selection as snapshot JSON text, for {@link Engine.restore}. */
|
|
128
|
+
snapshot() {
|
|
129
|
+
return this.#wasm.snapshot();
|
|
130
|
+
}
|
|
131
|
+
/** Start sharing the document as yrs client `clientId`. */
|
|
132
|
+
share(clientId) {
|
|
133
|
+
this.#wasm.share(clientId);
|
|
134
|
+
}
|
|
135
|
+
/** Apply a v1 Yjs update from another peer; returns whether the document changed. */
|
|
136
|
+
applyUpdate(update) {
|
|
137
|
+
return this.#wasm.applyUpdate(update);
|
|
138
|
+
}
|
|
139
|
+
/** The whole shared state as one v1 update. */
|
|
140
|
+
encodeState() {
|
|
141
|
+
return this.#wasm.encodeState();
|
|
142
|
+
}
|
|
143
|
+
/** The v1 state vector. */
|
|
144
|
+
stateVector() {
|
|
145
|
+
return this.#wasm.stateVector();
|
|
146
|
+
}
|
|
147
|
+
/** Everything a peer at `stateVector` is missing, as one v1 update. */
|
|
148
|
+
encodeDiff(stateVector) {
|
|
149
|
+
return this.#wasm.encodeDiff(stateVector);
|
|
150
|
+
}
|
|
151
|
+
/** Drain this peer's own v1 updates for broadcasting. */
|
|
152
|
+
takeUpdates() {
|
|
153
|
+
return this.#wasm.takeUpdates();
|
|
154
|
+
}
|
|
155
|
+
/** Check the shared state against the schema. */
|
|
156
|
+
validate() {
|
|
157
|
+
return JSON.parse(this.#wasm.validate());
|
|
158
|
+
}
|
|
159
|
+
/** The y-sync message to greet a connecting client with. */
|
|
160
|
+
syncStart() {
|
|
161
|
+
return this.#wasm.syncStart();
|
|
162
|
+
}
|
|
163
|
+
/** Handle one y-sync message from a client connection. */
|
|
164
|
+
receiveSync(message) {
|
|
165
|
+
const [reply, broadcast, changed] = this.#wasm.receiveSync(message);
|
|
166
|
+
return { reply, broadcast, changed };
|
|
167
|
+
}
|
|
168
|
+
/** This peer's changes since the last call, as y-sync messages for every connection. */
|
|
169
|
+
flushSync() {
|
|
170
|
+
return this.#wasm.flushSync();
|
|
171
|
+
}
|
|
172
|
+
/** Release the document's memory now rather than when it is collected. */
|
|
173
|
+
[Symbol.dispose]() {
|
|
174
|
+
this.#wasm.free();
|
|
175
|
+
}
|
|
176
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The error every canvist call throws, from WebAssembly or a native editor.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import type { ErrorCode } from "./types.ts";
|
|
7
|
+
/** A failure inside canvist, with its stable code. */
|
|
8
|
+
export interface CanvistError extends Error {
|
|
9
|
+
readonly name: "CanvistError";
|
|
10
|
+
readonly code: ErrorCode;
|
|
11
|
+
}
|
|
12
|
+
/** Whether `error` came from canvist, narrowing it to read its `code`. */
|
|
13
|
+
export declare function isCanvistError(error: unknown): error is CanvistError;
|