@dovetail-ts/liveblocks 0.0.0-stage → 1.0.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/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/README.md +79 -2
- package/dist/engine-error.d.ts +6 -0
- package/dist/index.d.ts +59 -0
- package/dist/index.js +81 -0
- package/dist/index.js.map +1 -0
- package/dist/yjs.d.ts +4 -0
- package/package.json +54 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# @dovetail-ts/liveblocks
|
|
2
|
+
|
|
3
|
+
## 1.0.0
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 480744b: First release of Dovetail: real-time and offline sync for Ember.
|
|
8
|
+
|
|
9
|
+
- `@dovetail-ts/core`: the connection life cycle, live store subscriptions, the optimistic ledger with rebase and rollback, the offline write queue in IndexedDB with a leader tab and dead letters, `DocSession` and `PresenceRoom`.
|
|
10
|
+
- `@dovetail-ts/warp-drive`: the store feed into WarpDrive.
|
|
11
|
+
- `@dovetail-ts/crdt`, `crdt-yjs`, `crdt-loro` and `crdt-automerge`: one `DocEngine` interface with three engines.
|
|
12
|
+
- `@dovetail-ts/tiptap`: co-editing in Tiptap 3 with carets, per-user undo, versions and comment threads.
|
|
13
|
+
- `@dovetail-ts/ember`, `notifications` and `inspector`: presence and status components, the inbox, toasts and activity feeds, and a dev panel.
|
|
14
|
+
- `@dovetail-ts/server`, the transports (`transport-ws`, `transport-sse`, `transport-cable`) and `conformance`: a Node server and one wire protocol that the Rails gems also pass.
|
|
15
|
+
- Adapters for Hocuspocus, Liveblocks, PartyKit and Supabase, and integrations for Switchyard, Millwork, Benchdog and Lattice. The Neon adapter is not in 1.0: it waits for a stable Neon Realtime client (1.1).
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- Updated dependencies [480744b]
|
|
20
|
+
- @dovetail-ts/crdt@1.0.0
|
|
21
|
+
- @dovetail-ts/protocol@1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Joe Gaudet
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @dovetail-ts/liveblocks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The Liveblocks adapter: doc sync, presence and the store feed.
|
|
4
|
+
|
|
5
|
+
Part of [Dovetail](https://joegaudet.com/dovetail/). Not released yet.
|
|
6
|
+
|
|
7
|
+
| Kind | Supported | How |
|
|
8
|
+
| ----------- | --------- | ---------------------------------------------------------- |
|
|
9
|
+
| `StoreFeed` | Yes | `liveblocksStoreFeed`: feed messages that the server sends |
|
|
10
|
+
| `DocSync` | Yes | `liveblocksSync`: `@liveblocks/yjs` (Yjs only) |
|
|
11
|
+
| `Presence` | Yes | `liveblocksPresence`: the room presence, as the awareness |
|
|
12
|
+
|
|
13
|
+
`kinds` has the same list: `['StoreFeed', 'DocSync', 'Presence']`.
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { createClient } from '@liveblocks/client'
|
|
19
|
+
import { yjsEngine } from '@dovetail-ts/crdt-yjs'
|
|
20
|
+
import { liveblocksPresence, liveblocksStoreFeed, liveblocksSync } from '@dovetail-ts/liveblocks'
|
|
21
|
+
|
|
22
|
+
const client = createClient({ authEndpoint: '/api/liveblocks-auth' })
|
|
23
|
+
const { room, leave } = client.enterRoom('notes-42', { initialPresence: {} })
|
|
24
|
+
|
|
25
|
+
const handle = yjsEngine().create()
|
|
26
|
+
const provider = liveblocksSync(room, handle)
|
|
27
|
+
const stopPresence = liveblocksPresence(room, handle.awareness)
|
|
28
|
+
const stopFeed = liveblocksStoreFeed(room, { apply: op => ledger.receive(op) })
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Doc sync
|
|
32
|
+
|
|
33
|
+
`liveblocksSync(room, handle, options?)` gives the Yjs document of the handle to a
|
|
34
|
+
`LiveblocksYjsProvider`, and gives back the provider. `options` are the provider options of
|
|
35
|
+
`@liveblocks/yjs`. `provider.destroy()` stops the sync.
|
|
36
|
+
|
|
37
|
+
`@liveblocks/yjs` supports Yjs only. A handle of another engine gives an `EngineError` (ADR 0002).
|
|
38
|
+
|
|
39
|
+
## Presence
|
|
40
|
+
|
|
41
|
+
`liveblocksPresence(room, awareness, { key? })` puts the local awareness state in the presence
|
|
42
|
+
field `key` (default `dovetail`) of the room. It applies the same field of each other user to
|
|
43
|
+
the awareness, with the connection id as the client id. A user that leaves the room goes out
|
|
44
|
+
of the awareness. It works with the awareness of each engine. The function gives back a
|
|
45
|
+
function that stops it, removes the remote states and clears the local presence field.
|
|
46
|
+
|
|
47
|
+
Do not use the key `__yjs`. `@liveblocks/yjs` keeps its own awareness in that field.
|
|
48
|
+
If the app declares a `Presence` type for Liveblocks, add the field to it, for example
|
|
49
|
+
`dovetail: JsonObject | null`.
|
|
50
|
+
|
|
51
|
+
## Store feed
|
|
52
|
+
|
|
53
|
+
`liveblocksStoreFeed(room, sink)` applies the ops of each Dovetail `feed` message that the
|
|
54
|
+
server broadcasts to the room. The backend sends the feed message with the "broadcast event"
|
|
55
|
+
endpoint of the Liveblocks REST API, as the event body:
|
|
56
|
+
|
|
57
|
+
```json
|
|
58
|
+
{
|
|
59
|
+
"type": "feed",
|
|
60
|
+
"room": "notes-42",
|
|
61
|
+
"cursor": "17",
|
|
62
|
+
"ops": [{ "op": "delete", "ref": { "type": "note", "id": "1" } }]
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
- Only an event of the REST API (connection id -1) can change the store. The adapter ignores
|
|
67
|
+
the events of other clients, because each client of the room can broadcast.
|
|
68
|
+
- Each event goes through `parseMessage`. The adapter ignores an event that is not a valid
|
|
69
|
+
`feed` message, or that names another room.
|
|
70
|
+
- A broadcast gets only to the clients that are in the room at that time. Load the records
|
|
71
|
+
over HTTP when a client enters the room.
|
|
72
|
+
- `sink` is a `StoreSink` (`apply(op)`), as the one of `@dovetail-ts/core`. With the optimistic
|
|
73
|
+
ledger, give `{ apply: op => ledger.receive(op) }`, so the feed does not overwrite the
|
|
74
|
+
pending local changes (ADR 0011).
|
|
75
|
+
|
|
76
|
+
## Tests
|
|
77
|
+
|
|
78
|
+
The unit tests use a fake room with the parts of a Liveblocks `Room` that the adapter uses. A
|
|
79
|
+
compile-time check makes sure that a real `Room` has these parts. The live test runs only when
|
|
80
|
+
`LIVEBLOCKS_PUBLIC_KEY` has the public key (`pk_...`) of a Liveblocks project.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { LiveblocksYjsProvider } from '@liveblocks/yjs';
|
|
2
|
+
import type { ProviderOptions } from '@liveblocks/yjs';
|
|
3
|
+
import type { DocAwareness, DocHandle, JsonObject } from '@dovetail-ts/crdt';
|
|
4
|
+
import type { StoreOp } from '@dovetail-ts/protocol';
|
|
5
|
+
export { EngineError } from './engine-error';
|
|
6
|
+
/** The name of this package. */
|
|
7
|
+
export declare const PACKAGE_NAME = "@dovetail-ts/liveblocks";
|
|
8
|
+
/** A kind of adapter (ADR 0003). */
|
|
9
|
+
export type AdapterKind = 'StoreFeed' | 'DocSync' | 'Presence';
|
|
10
|
+
/** The kinds that this adapter supports. */
|
|
11
|
+
export declare const kinds: readonly ["StoreFeed", "DocSync", "Presence"];
|
|
12
|
+
/**
|
|
13
|
+
* Where the store feed puts each op. The `StoreSink` of `@dovetail-ts/core` is one. With the
|
|
14
|
+
* optimistic ledger, give `{ apply: op => ledger.receive(op) }` (ADR 0011).
|
|
15
|
+
*/
|
|
16
|
+
export interface StoreSink {
|
|
17
|
+
apply(op: StoreOp): void;
|
|
18
|
+
}
|
|
19
|
+
/** A broadcast event of a room. The REST API of Liveblocks sends it with `connectionId` -1. */
|
|
20
|
+
export interface RoomEvent {
|
|
21
|
+
connectionId: number;
|
|
22
|
+
user: unknown;
|
|
23
|
+
event: unknown;
|
|
24
|
+
}
|
|
25
|
+
/** One other user of a room, with the presence of that user. */
|
|
26
|
+
export interface RoomUser {
|
|
27
|
+
readonly connectionId: number;
|
|
28
|
+
readonly presence: Readonly<Record<string, unknown>>;
|
|
29
|
+
}
|
|
30
|
+
/** The parts of a Liveblocks `Room` that the presence and the store feed use. */
|
|
31
|
+
export interface LiveblocksRoom {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
updatePresence(patch: JsonObject): void;
|
|
34
|
+
getOthers(): readonly RoomUser[];
|
|
35
|
+
subscribe(type: 'event', listener: (message: RoomEvent) => void): () => void;
|
|
36
|
+
subscribe(type: 'others', listener: (others: readonly RoomUser[]) => void): () => void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Syncs the Yjs document of a handle with a Liveblocks room. Throws an `EngineError` for a
|
|
40
|
+
* document of another engine. `provider.destroy()` stops the sync.
|
|
41
|
+
*/
|
|
42
|
+
export declare function liveblocksSync(room: ConstructorParameters<typeof LiveblocksYjsProvider>[0], handle: DocHandle, options?: ProviderOptions): LiveblocksYjsProvider;
|
|
43
|
+
export interface LiveblocksPresenceOptions {
|
|
44
|
+
/** The presence field of the awareness state. Not `__yjs`: `@liveblocks/yjs` uses it. */
|
|
45
|
+
key?: string;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Puts the local awareness state in the Liveblocks presence of the room, and the presence of
|
|
49
|
+
* the others in the awareness. The id of another user is its connection id. Returns a function
|
|
50
|
+
* that stops it.
|
|
51
|
+
*/
|
|
52
|
+
export declare function liveblocksPresence(room: LiveblocksRoom, awareness: DocAwareness, options?: LiveblocksPresenceOptions): () => void;
|
|
53
|
+
/**
|
|
54
|
+
* Applies the ops of each Dovetail `feed` message that the server broadcasts to the room. The
|
|
55
|
+
* backend sends it with the REST API of Liveblocks. The adapter ignores the events of other
|
|
56
|
+
* clients and each event that is not a valid feed message of this room. Returns a function
|
|
57
|
+
* that stops it.
|
|
58
|
+
*/
|
|
59
|
+
export declare function liveblocksStoreFeed(room: LiveblocksRoom, sink: StoreSink): () => void;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { LiveblocksYjsProvider } from '@liveblocks/yjs';
|
|
2
|
+
import { parseMessage } from '@dovetail-ts/protocol';
|
|
3
|
+
import * as Y from 'yjs';
|
|
4
|
+
|
|
5
|
+
// src/index.ts
|
|
6
|
+
|
|
7
|
+
// src/engine-error.ts
|
|
8
|
+
var EngineError = class extends Error {
|
|
9
|
+
constructor(adapter, engine) {
|
|
10
|
+
super(
|
|
11
|
+
`The ${adapter} adapter supports the Yjs engine only. This document uses the "${engine}" engine.`
|
|
12
|
+
);
|
|
13
|
+
this.adapter = adapter;
|
|
14
|
+
this.engine = engine;
|
|
15
|
+
this.name = "EngineError";
|
|
16
|
+
}
|
|
17
|
+
adapter;
|
|
18
|
+
engine;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/yjs.ts
|
|
22
|
+
function requireYjs(handle, adapter) {
|
|
23
|
+
if (isYjs(handle)) return handle;
|
|
24
|
+
throw new EngineError(adapter, handle.engine.name);
|
|
25
|
+
}
|
|
26
|
+
function isYjs(handle) {
|
|
27
|
+
return handle.engine.name === "yjs" && "doc" in handle && handle.doc instanceof Y.Doc && "yAwareness" in handle;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var PACKAGE_NAME = "@dovetail-ts/liveblocks";
|
|
32
|
+
var kinds = ["StoreFeed", "DocSync", "Presence"];
|
|
33
|
+
function liveblocksSync(room, handle, options) {
|
|
34
|
+
return new LiveblocksYjsProvider(room, requireYjs(handle, "Liveblocks").doc, options);
|
|
35
|
+
}
|
|
36
|
+
function liveblocksPresence(room, awareness, options = {}) {
|
|
37
|
+
const key = options.key ?? "dovetail";
|
|
38
|
+
const send = (state) => room.updatePresence({ [key]: state });
|
|
39
|
+
let remote = /* @__PURE__ */ new Set();
|
|
40
|
+
const local = awareness.getLocalState();
|
|
41
|
+
if (local) send(local);
|
|
42
|
+
const stopLocal = awareness.onChange((change) => {
|
|
43
|
+
if (change.local) send(awareness.getLocalState());
|
|
44
|
+
});
|
|
45
|
+
const applyOthers = (others) => {
|
|
46
|
+
const now = /* @__PURE__ */ new Set();
|
|
47
|
+
for (const other of others) {
|
|
48
|
+
const state = other.presence[key];
|
|
49
|
+
if (!isJsonObject(state)) continue;
|
|
50
|
+
const id = String(other.connectionId);
|
|
51
|
+
now.add(id);
|
|
52
|
+
awareness.applyRemoteState(id, state);
|
|
53
|
+
}
|
|
54
|
+
for (const id of remote) if (!now.has(id)) awareness.applyRemoteState(id, null);
|
|
55
|
+
remote = now;
|
|
56
|
+
};
|
|
57
|
+
applyOthers(room.getOthers());
|
|
58
|
+
const stopOthers = room.subscribe("others", applyOthers);
|
|
59
|
+
return () => {
|
|
60
|
+
stopLocal();
|
|
61
|
+
stopOthers();
|
|
62
|
+
for (const id of remote) awareness.applyRemoteState(id, null);
|
|
63
|
+
remote = /* @__PURE__ */ new Set();
|
|
64
|
+
send(null);
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function liveblocksStoreFeed(room, sink) {
|
|
68
|
+
return room.subscribe("event", ({ connectionId, event }) => {
|
|
69
|
+
if (connectionId !== -1) return;
|
|
70
|
+
const result = parseMessage(event, { from: "server" });
|
|
71
|
+
if (!result.ok || result.message.type !== "feed" || result.message.room !== room.id) return;
|
|
72
|
+
for (const op of result.message.ops) sink.apply(op);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
function isJsonObject(value) {
|
|
76
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { EngineError, PACKAGE_NAME, kinds, liveblocksPresence, liveblocksStoreFeed, liveblocksSync };
|
|
80
|
+
//# sourceMappingURL=index.js.map
|
|
81
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/engine-error.ts","../src/yjs.ts","../src/index.ts"],"names":[],"mappings":";;;;;;;AAGO,IAAM,WAAA,GAAN,cAA0B,KAAA,CAAM;AAAA,EACrC,WAAA,CACW,SACA,MAAA,EACT;AACA,IAAA,KAAA;AAAA,MACE,CAAA,IAAA,EAAO,OAAO,CAAA,+DAAA,EACe,MAAM,CAAA,SAAA;AAAA,KACrC;AANS,IAAA,IAAA,CAAA,OAAA,GAAA,OAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAMT,IAAA,IAAA,CAAK,IAAA,GAAO,aAAA;AAA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,KAAA,EAAO,EAAE,IAAA,EAAM,UAAU,CAAA;AACrD,IAAA,IAAI,CAAC,MAAA,CAAO,EAAA,IAAM,MAAA,CAAO,OAAA,CAAQ,IAAA,KAAS,MAAA,IAAU,MAAA,CAAO,OAAA,CAAQ,IAAA,KAAS,IAAA,CAAK,EAAA,EAAI;AACrF,IAAA,KAAA,MAAW,MAAM,MAAA,CAAO,OAAA,CAAQ,GAAA,EAAK,IAAA,CAAK,MAAM,EAAE,CAAA;AAAA,EACpD,CAAC,CAAA;AACH;AAEA,SAAS,aAAa,KAAA,EAAqC;AACzD,EAAA,OAAO,OAAO,UAAU,QAAA,IAAY,KAAA,KAAU,QAAQ,CAAC,KAAA,CAAM,QAAQ,KAAK,CAAA;AAC5E","file":"index.js","sourcesContent":["// The error of an adapter that supports the Yjs engine only (ADR 0002).\n\n/** The error for a document of an engine that the adapter does not support. */\nexport class EngineError extends Error {\n constructor(\n readonly adapter: string,\n readonly engine: string,\n ) {\n super(\n `The ${adapter} adapter supports the Yjs engine only. ` +\n `This document uses the \"${engine}\" engine.`,\n )\n this.name = 'EngineError'\n }\n}\n","// The Yjs check of the adapters that support the Yjs engine only (ADR 0002).\nimport * as Y from 'yjs'\nimport type { DocHandle } from '@dovetail-ts/crdt'\nimport type { YjsDocHandle } from '@dovetail-ts/crdt-yjs'\nimport { EngineError } from './engine-error'\n\n/** Gives the handle as a Yjs handle, or throws an `EngineError`. */\nexport function requireYjs(handle: DocHandle, adapter: string): YjsDocHandle {\n if (isYjs(handle)) return handle\n throw new EngineError(adapter, handle.engine.name)\n}\n\nfunction isYjs(handle: DocHandle): handle is YjsDocHandle {\n return (\n handle.engine.name === 'yjs' &&\n 'doc' in handle &&\n handle.doc instanceof Y.Doc &&\n 'yAwareness' in handle\n )\n}\n","// The Liveblocks adapter (ADR 0003, ADR 0017).\n// - Doc sync: `@liveblocks/yjs` syncs the Yjs document of a handle (Yjs only).\n// - Presence: the Liveblocks presence of the room, as the awareness of a handle.\n// - Store feed: feed messages that the server broadcasts to the room, into a store sink.\nimport { LiveblocksYjsProvider } from '@liveblocks/yjs'\nimport type { ProviderOptions } from '@liveblocks/yjs'\nimport type { DocAwareness, DocHandle, JsonObject } from '@dovetail-ts/crdt'\nimport { parseMessage } from '@dovetail-ts/protocol'\nimport type { StoreOp } from '@dovetail-ts/protocol'\nimport { requireYjs } from './yjs'\n\nexport { EngineError } from './engine-error'\n\n/** The name of this package. */\nexport const PACKAGE_NAME = '@dovetail-ts/liveblocks'\n\n/** A kind of adapter (ADR 0003). */\nexport type AdapterKind = 'StoreFeed' | 'DocSync' | 'Presence'\n\n/** The kinds that this adapter supports. */\nexport const kinds = ['StoreFeed', 'DocSync', 'Presence'] as const satisfies readonly AdapterKind[]\n\n/**\n * Where the store feed puts each op. The `StoreSink` of `@dovetail-ts/core` is one. With the\n * optimistic ledger, give `{ apply: op => ledger.receive(op) }` (ADR 0011).\n */\nexport interface StoreSink {\n apply(op: StoreOp): void\n}\n\n/** A broadcast event of a room. The REST API of Liveblocks sends it with `connectionId` -1. */\nexport interface RoomEvent {\n connectionId: number\n user: unknown\n event: unknown\n}\n\n/** One other user of a room, with the presence of that user. */\nexport interface RoomUser {\n readonly connectionId: number\n readonly presence: Readonly<Record<string, unknown>>\n}\n\n/** The parts of a Liveblocks `Room` that the presence and the store feed use. */\nexport interface LiveblocksRoom {\n readonly id: string\n updatePresence(patch: JsonObject): void\n getOthers(): readonly RoomUser[]\n subscribe(type: 'event', listener: (message: RoomEvent) => void): () => void\n subscribe(type: 'others', listener: (others: readonly RoomUser[]) => void): () => void\n}\n\n/**\n * Syncs the Yjs document of a handle with a Liveblocks room. Throws an `EngineError` for a\n * document of another engine. `provider.destroy()` stops the sync.\n */\nexport function liveblocksSync(\n room: ConstructorParameters<typeof LiveblocksYjsProvider>[0],\n handle: DocHandle,\n options?: ProviderOptions,\n): LiveblocksYjsProvider {\n return new LiveblocksYjsProvider(room, requireYjs(handle, 'Liveblocks').doc, options)\n}\n\nexport interface LiveblocksPresenceOptions {\n /** The presence field of the awareness state. Not `__yjs`: `@liveblocks/yjs` uses it. */\n key?: string\n}\n\n/**\n * Puts the local awareness state in the Liveblocks presence of the room, and the presence of\n * the others in the awareness. The id of another user is its connection id. Returns a function\n * that stops it.\n */\nexport function liveblocksPresence(\n room: LiveblocksRoom,\n awareness: DocAwareness,\n options: LiveblocksPresenceOptions = {},\n): () => void {\n const key = options.key ?? 'dovetail'\n const send = (state: JsonObject | null) => room.updatePresence({ [key]: state })\n let remote = new Set<string>()\n\n const local = awareness.getLocalState()\n if (local) send(local)\n const stopLocal = awareness.onChange(change => {\n if (change.local) send(awareness.getLocalState())\n })\n const applyOthers = (others: readonly RoomUser[]) => {\n const now = new Set<string>()\n for (const other of others) {\n const state = other.presence[key]\n if (!isJsonObject(state)) continue\n const id = String(other.connectionId)\n now.add(id)\n awareness.applyRemoteState(id, state)\n }\n for (const id of remote) if (!now.has(id)) awareness.applyRemoteState(id, null)\n remote = now\n }\n // The `others` event comes only at a change, so read the users that are in the room now.\n applyOthers(room.getOthers())\n const stopOthers = room.subscribe('others', applyOthers)\n\n return () => {\n stopLocal()\n stopOthers()\n for (const id of remote) awareness.applyRemoteState(id, null)\n remote = new Set()\n send(null)\n }\n}\n\n/**\n * Applies the ops of each Dovetail `feed` message that the server broadcasts to the room. The\n * backend sends it with the REST API of Liveblocks. The adapter ignores the events of other\n * clients and each event that is not a valid feed message of this room. Returns a function\n * that stops it.\n */\nexport function liveblocksStoreFeed(room: LiveblocksRoom, sink: StoreSink): () => void {\n return room.subscribe('event', ({ connectionId, event }) => {\n // A client can broadcast too. Only the server (connection id -1) can change the store.\n if (connectionId !== -1) return\n const result = parseMessage(event, { from: 'server' })\n if (!result.ok || result.message.type !== 'feed' || result.message.room !== room.id) return\n for (const op of result.message.ops) sink.apply(op)\n })\n}\n\nfunction isJsonObject(value: unknown): value is JsonObject {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n"]}
|
package/dist/yjs.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dovetail-ts/liveblocks",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "The Liveblocks adapter: doc sync, presence and the store feed.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Joe Gaudet <joe@joegaudet.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/joegaudet/dovetail.git",
|
|
10
|
+
"directory": "packages/liveblocks"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://joegaudet.com/dovetail/",
|
|
13
|
+
"bugs": "https://github.com/joegaudet/dovetail/issues",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"realtime",
|
|
16
|
+
"offline",
|
|
17
|
+
"sync",
|
|
18
|
+
"crdt"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"dist",
|
|
31
|
+
"CHANGELOG.md",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"sideEffects": false,
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@dovetail-ts/crdt": "1.0.0",
|
|
38
|
+
"@dovetail-ts/protocol": "1.0.0",
|
|
39
|
+
"@liveblocks/client": "^3.25.2",
|
|
40
|
+
"@liveblocks/yjs": "^3.25.2",
|
|
41
|
+
"yjs": "^13.6.33"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@dovetail-ts/crdt-loro": "1.0.0",
|
|
45
|
+
"@dovetail-ts/crdt-yjs": "1.0.0",
|
|
46
|
+
"tsup": "^8.5.1"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"engines": {
|
|
52
|
+
"node": ">=22.12.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "tsc --build --force && tsup"
|
|
56
|
+
}
|
|
6
57
|
}
|