@pluv/crdt-yjs 5.0.1 → 5.0.3
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/index.d.mts +209 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +551 -0
- package/dist/index.mjs.map +1 -0
- package/dist/rolldown-runtime-D7D4PA-g.mjs +13 -0
- package/package.json +9 -9
- package/CHANGELOG.md +0 -1626
- package/eslint.config.mjs +0 -4
- package/src/array/YjsArray.ts +0 -18
- package/src/array/array.ts +0 -9
- package/src/array/index.ts +0 -2
- package/src/awareness/PluvYjsAwareness.ts +0 -220
- package/src/awareness/awareness.ts +0 -15
- package/src/awareness/index.ts +0 -2
- package/src/constants.ts +0 -2
- package/src/doc/CrdtYjsDoc.ts +0 -241
- package/src/doc/CrdtYjsDocFactory.ts +0 -33
- package/src/doc/builder.ts +0 -71
- package/src/doc/doc.ts +0 -9
- package/src/doc/index.ts +0 -4
- package/src/index.ts +0 -5
- package/src/map/YjsMap.ts +0 -19
- package/src/map/index.ts +0 -2
- package/src/map/map.ts +0 -9
- package/src/provider/PluvYjsProvider.ts +0 -132
- package/src/provider/index.ts +0 -2
- package/src/provider/provider.ts +0 -15
- package/src/text/YjsText.ts +0 -16
- package/src/text/index.ts +0 -2
- package/src/text/text.ts +0 -7
- package/src/types.ts +0 -35
- package/src/utils/index.ts +0 -1
- package/src/utils/oneLine.ts +0 -7
- package/src/xmlElement/YjsXmlElement.ts +0 -25
- package/src/xmlElement/index.ts +0 -2
- package/src/xmlElement/xmlElement.ts +0 -9
- package/src/xmlFragment/YjsXmlFragment.ts +0 -19
- package/src/xmlFragment/index.ts +0 -2
- package/src/xmlFragment/xmlFragment.ts +0 -8
- package/src/xmlText/YjsXmlText.ts +0 -16
- package/src/xmlText/index.ts +0 -2
- package/src/xmlText/xmlText.ts +0 -7
- package/src/yjs.ts +0 -12
- package/tsconfig.json +0 -8
- package/tsdown.config.ts +0 -14
package/eslint.config.mjs
DELETED
package/src/array/YjsArray.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Array as YArray } from "yjs";
|
|
2
|
-
import type { YjsType } from "../types";
|
|
3
|
-
|
|
4
|
-
export class YjsArray<T extends unknown> extends YArray<T> implements YjsType<YArray<T>, T[]> {
|
|
5
|
-
public initialValue?: T[] | readonly T[];
|
|
6
|
-
|
|
7
|
-
constructor(value: T[] | readonly T[] = []) {
|
|
8
|
-
super();
|
|
9
|
-
|
|
10
|
-
this.insert(0, value as T[]);
|
|
11
|
-
|
|
12
|
-
this.initialValue = value;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
public __pluvType() {
|
|
16
|
-
return {} as any;
|
|
17
|
-
}
|
|
18
|
-
}
|
package/src/array/array.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { Array as YArray } from "yjs";
|
|
2
|
-
import type { YjsType } from "../types";
|
|
3
|
-
import { YjsArray } from "./YjsArray";
|
|
4
|
-
|
|
5
|
-
export const array = <T extends unknown>(
|
|
6
|
-
value: T[] | readonly T[] = [],
|
|
7
|
-
): YjsType<YArray<T>, T[]> => {
|
|
8
|
-
return new YjsArray(value);
|
|
9
|
-
};
|
package/src/array/index.ts
DELETED
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
CrdtType,
|
|
3
|
-
IOLike,
|
|
4
|
-
OthersSubscriptionEvent,
|
|
5
|
-
PluvRouterEventConfig,
|
|
6
|
-
RoomLike,
|
|
7
|
-
UserInfo,
|
|
8
|
-
} from "@pluv/types";
|
|
9
|
-
import { ObservableV2 } from "lib0/observable";
|
|
10
|
-
import type { Doc as YDoc } from "yjs";
|
|
11
|
-
import { PLUV_PRESENCE_META_KEY, PLUV_PRESENCE_Y_ID_KEY } from "../constants";
|
|
12
|
-
import type { AwarenessPresence, MetaClientState, YjsAwarenessUpdate } from "../types";
|
|
13
|
-
|
|
14
|
-
export interface PluvYjsAwarenessParams<
|
|
15
|
-
TIO extends IOLike<any, any, any>,
|
|
16
|
-
TPresence extends Record<string, any>,
|
|
17
|
-
TStorage extends Record<string, CrdtType<any, any>>,
|
|
18
|
-
TEvents extends PluvRouterEventConfig,
|
|
19
|
-
TField extends keyof TPresence | null = null,
|
|
20
|
-
> {
|
|
21
|
-
presenceField?: TField;
|
|
22
|
-
room: RoomLike<TIO, YDoc, TPresence, TStorage, TEvents>;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export class PluvYjsAwareness<
|
|
26
|
-
TIO extends IOLike,
|
|
27
|
-
TPresence extends Record<string, any>,
|
|
28
|
-
TStorage extends Record<string, CrdtType<any, any>>,
|
|
29
|
-
TEvents extends PluvRouterEventConfig,
|
|
30
|
-
TField extends keyof TPresence | null = null,
|
|
31
|
-
> extends ObservableV2<{
|
|
32
|
-
change: (updates: YjsAwarenessUpdate, kind: "presence") => void;
|
|
33
|
-
destroy: (awareness: PluvYjsAwareness<TIO, TPresence, TStorage, TEvents, TField>) => void;
|
|
34
|
-
update: (updates: YjsAwarenessUpdate, kind: "local" | "presence") => void;
|
|
35
|
-
}> {
|
|
36
|
-
public readonly doc: YDoc;
|
|
37
|
-
|
|
38
|
-
private readonly _field: TField = null as TField;
|
|
39
|
-
private readonly _idMap = new Map<[connectionId: string][0], [clientID: number][0]>();
|
|
40
|
-
private readonly _room: RoomLike<TIO, YDoc, TPresence, TStorage, TEvents>;
|
|
41
|
-
private readonly _unsubscribe: () => void;
|
|
42
|
-
|
|
43
|
-
public get clientID(): number {
|
|
44
|
-
return this.doc.clientID;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public get states(): Map<number, TPresence> {
|
|
48
|
-
return this.getStates();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
constructor(params: PluvYjsAwarenessParams<TIO, TPresence, TStorage, TEvents, TField>) {
|
|
52
|
-
super();
|
|
53
|
-
|
|
54
|
-
const { presenceField, room } = params;
|
|
55
|
-
|
|
56
|
-
this.doc = room.getDoc().value;
|
|
57
|
-
this._room = room;
|
|
58
|
-
|
|
59
|
-
if (!!presenceField) this._field = presenceField;
|
|
60
|
-
|
|
61
|
-
const myPresence: any = this._room.getMyPresence();
|
|
62
|
-
const myMeta = myPresence?.[PLUV_PRESENCE_META_KEY] || {};
|
|
63
|
-
|
|
64
|
-
this._room.updateMyPresence({
|
|
65
|
-
[PLUV_PRESENCE_META_KEY]: {
|
|
66
|
-
...myMeta,
|
|
67
|
-
[PLUV_PRESENCE_Y_ID_KEY]: this.clientID,
|
|
68
|
-
},
|
|
69
|
-
} as any);
|
|
70
|
-
|
|
71
|
-
this._unsubscribe = this._room.subscribe.others((others, event) => {
|
|
72
|
-
const updates = this._deriveUpdates(others, event);
|
|
73
|
-
|
|
74
|
-
if (!updates) return;
|
|
75
|
-
|
|
76
|
-
this.emit("change", [updates, "presence"]);
|
|
77
|
-
this.emit("update", [updates, "presence"]);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public destroy(): void {
|
|
82
|
-
this.emit("destroy", [this]);
|
|
83
|
-
this._unsubscribe();
|
|
84
|
-
super.destroy();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public getLocalState(): AwarenessPresence<TPresence, TField> {
|
|
88
|
-
return this._adaptPresence(this._room.getMyPresence());
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
public setLocalState(state: AwarenessPresence<TPresence, TField>): void {
|
|
92
|
-
const patch = (!!this._field
|
|
93
|
-
? { [this._field]: state }
|
|
94
|
-
: state) as unknown as Partial<TPresence>;
|
|
95
|
-
|
|
96
|
-
this._room.updateMyPresence(patch);
|
|
97
|
-
|
|
98
|
-
this.emit("update", [{ added: [], updated: [this.clientID], removed: [] }, "local"]);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public setLocalStateField<TLocalField extends keyof AwarenessPresence<TPresence, TField>>(
|
|
102
|
-
field: TLocalField,
|
|
103
|
-
value: AwarenessPresence<TPresence, TField>[TLocalField],
|
|
104
|
-
): void {
|
|
105
|
-
/**
|
|
106
|
-
* !HACK
|
|
107
|
-
* @description Cast to unknown first, because field loses its type of TField when set in
|
|
108
|
-
* an object.
|
|
109
|
-
* @date May 14, 2025
|
|
110
|
-
*/
|
|
111
|
-
const patch = (!!this._field
|
|
112
|
-
? { [this._field]: { ...this.getLocalState(), [field]: value } }
|
|
113
|
-
: { [field]: value }) as unknown as Partial<TPresence>;
|
|
114
|
-
|
|
115
|
-
this._room.updateMyPresence(patch);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* !HACK
|
|
120
|
-
* @description We're building the awareness presences from the states stored on the room's
|
|
121
|
-
* presense instead of using the map defined in the awareness interface.
|
|
122
|
-
* @see https://github.com/yjs/y-protocols/blob/master/awareness.js
|
|
123
|
-
* @date May 13, 2025
|
|
124
|
-
*/
|
|
125
|
-
public getStates(): Map<number, AwarenessPresence<TPresence, TField>> {
|
|
126
|
-
const others = this._room.getOthers();
|
|
127
|
-
|
|
128
|
-
const states = others.reduce((map, other) => {
|
|
129
|
-
const presence = this._adaptPresence(other.presence);
|
|
130
|
-
const meta = other.presence?.[PLUV_PRESENCE_META_KEY] || {};
|
|
131
|
-
const clientID: number | undefined = meta[PLUV_PRESENCE_Y_ID_KEY];
|
|
132
|
-
|
|
133
|
-
return typeof presence !== "undefined" && typeof clientID !== "undefined"
|
|
134
|
-
? map.set(clientID, presence)
|
|
135
|
-
: map;
|
|
136
|
-
}, new Map<number, AwarenessPresence<TPresence, TField>>());
|
|
137
|
-
|
|
138
|
-
const myPresence = this.getLocalState();
|
|
139
|
-
|
|
140
|
-
if (typeof myPresence !== "undefined") states.set(this.clientID, myPresence);
|
|
141
|
-
|
|
142
|
-
return states;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
private _adaptPresence(presence: TPresence): AwarenessPresence<TPresence, TField> {
|
|
146
|
-
return (!!this._field ? presence[this._field] : presence) as AwarenessPresence<
|
|
147
|
-
TPresence,
|
|
148
|
-
TField
|
|
149
|
-
>;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
private _deriveUpdates(
|
|
153
|
-
others: readonly UserInfo<any, any>[],
|
|
154
|
-
event: OthersSubscriptionEvent<any, any>,
|
|
155
|
-
): YjsAwarenessUpdate | null {
|
|
156
|
-
switch (event.kind) {
|
|
157
|
-
case "clear": {
|
|
158
|
-
this._idMap.clear();
|
|
159
|
-
return null;
|
|
160
|
-
}
|
|
161
|
-
case "enter": {
|
|
162
|
-
const clientID = this._resetIdMap(others).get(event.user.connectionId);
|
|
163
|
-
|
|
164
|
-
return {
|
|
165
|
-
added: typeof clientID !== "undefined" ? [clientID] : [],
|
|
166
|
-
updated: [],
|
|
167
|
-
removed: [],
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
case "leave": {
|
|
171
|
-
const clientID = this._idMap.get(event.user.connectionId);
|
|
172
|
-
|
|
173
|
-
this._resetIdMap(others);
|
|
174
|
-
|
|
175
|
-
return {
|
|
176
|
-
added: [],
|
|
177
|
-
updated: [],
|
|
178
|
-
removed: typeof clientID !== "undefined" ? [clientID] : [],
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
case "sync": {
|
|
182
|
-
this._resetIdMap(others);
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
case "update": {
|
|
186
|
-
const clientID = this._resetIdMap(others).get(event.user.connectionId);
|
|
187
|
-
|
|
188
|
-
return {
|
|
189
|
-
added: [],
|
|
190
|
-
updated: typeof clientID !== "undefined" ? [clientID] : [],
|
|
191
|
-
removed: [],
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
default:
|
|
195
|
-
return null;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
private _resetIdMap(others: readonly UserInfo<any, any>[]): Map<string, number> {
|
|
200
|
-
this._idMap.clear();
|
|
201
|
-
|
|
202
|
-
return others.reduce((map, other) => {
|
|
203
|
-
const clientID = other.presence[PLUV_PRESENCE_META_KEY]?.[PLUV_PRESENCE_Y_ID_KEY];
|
|
204
|
-
|
|
205
|
-
if (typeof clientID === "undefined") return map;
|
|
206
|
-
|
|
207
|
-
return map.set(other.connectionId, clientID);
|
|
208
|
-
}, this._idMap);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* !HACK
|
|
213
|
-
* @description We dgaf about these. We're just mirroring the fields that exist in the
|
|
214
|
-
* og awareness to make sure everything aligns.
|
|
215
|
-
* @see https://github.com/yjs/y-protocols/blob/master/awareness.js
|
|
216
|
-
* @date May 13, 2025
|
|
217
|
-
*/
|
|
218
|
-
public readonly meta = new Map<any, MetaClientState>();
|
|
219
|
-
public readonly _checkInterval: number = 0;
|
|
220
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { CrdtType, IOLike, PluvRouterEventConfig } from "@pluv/types";
|
|
2
|
-
import type { PluvYjsAwarenessParams } from "./PluvYjsAwareness";
|
|
3
|
-
import { PluvYjsAwareness } from "./PluvYjsAwareness";
|
|
4
|
-
|
|
5
|
-
export const awareness = <
|
|
6
|
-
TIO extends IOLike<any, any, any>,
|
|
7
|
-
TPresence extends Record<string, any>,
|
|
8
|
-
TStorage extends Record<string, CrdtType<any, any>>,
|
|
9
|
-
TEvents extends PluvRouterEventConfig,
|
|
10
|
-
TField extends keyof TPresence | null = null,
|
|
11
|
-
>(
|
|
12
|
-
params: PluvYjsAwarenessParams<TIO, TPresence, TStorage, TEvents, TField>,
|
|
13
|
-
): PluvYjsAwareness<TIO, TPresence, TStorage, TEvents, TField> => {
|
|
14
|
-
return new PluvYjsAwareness(params);
|
|
15
|
-
};
|
package/src/awareness/index.ts
DELETED
package/src/constants.ts
DELETED
package/src/doc/CrdtYjsDoc.ts
DELETED
|
@@ -1,241 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
DocApplyEncodedStateParams,
|
|
3
|
-
DocBatchApplyEncodedStateParams,
|
|
4
|
-
DocSubscribeCallbackParams,
|
|
5
|
-
InferCrdtJson,
|
|
6
|
-
} from "@pluv/crdt";
|
|
7
|
-
import type { CrdtDocLike } from "@pluv/types";
|
|
8
|
-
import { fromUint8Array, toUint8Array } from "js-base64";
|
|
9
|
-
import {
|
|
10
|
-
AbstractType,
|
|
11
|
-
UndoManager,
|
|
12
|
-
Array as YArray,
|
|
13
|
-
Doc as YDoc,
|
|
14
|
-
Map as YMap,
|
|
15
|
-
Text as YText,
|
|
16
|
-
XmlElement as YXmlElement,
|
|
17
|
-
XmlFragment as YXmlFragment,
|
|
18
|
-
XmlText as YXmlText,
|
|
19
|
-
applyUpdate,
|
|
20
|
-
encodeStateAsUpdate,
|
|
21
|
-
mergeUpdates,
|
|
22
|
-
} from "yjs";
|
|
23
|
-
import type { YjsType } from "../types";
|
|
24
|
-
import type { YjsBuilder } from "./builder";
|
|
25
|
-
import { builder } from "./builder";
|
|
26
|
-
|
|
27
|
-
const MERGE_INTERVAL_MS = 1_000;
|
|
28
|
-
const PLUV_ID_FIELD = "__$pluv";
|
|
29
|
-
|
|
30
|
-
export type CrdtYjsDocParams<TStorage extends Record<string, YjsType<any, any>>> = (
|
|
31
|
-
builder: YjsBuilder,
|
|
32
|
-
) => TStorage;
|
|
33
|
-
|
|
34
|
-
export class CrdtYjsDoc<TStorage extends Record<string, YjsType<any, any>>> implements CrdtDocLike<
|
|
35
|
-
YDoc,
|
|
36
|
-
TStorage
|
|
37
|
-
> {
|
|
38
|
-
public value: YDoc = new YDoc();
|
|
39
|
-
|
|
40
|
-
#_storage: TStorage;
|
|
41
|
-
#_undoManager: UndoManager | null = null;
|
|
42
|
-
|
|
43
|
-
constructor(params: CrdtYjsDocParams<TStorage> = () => ({}) as TStorage) {
|
|
44
|
-
const storage = params(builder(this.value));
|
|
45
|
-
const keys = this.value.share.keys().reduce((set, key) => set.add(key), new Set<string>());
|
|
46
|
-
|
|
47
|
-
this.#_storage = Object.entries(storage).reduce(
|
|
48
|
-
(acc, [key, node]) => (keys.has(key) ? { ...acc, [key]: node } : acc),
|
|
49
|
-
{} as TStorage,
|
|
50
|
-
);
|
|
51
|
-
if (!!Object.keys(storage).length) this.#_setPluvId();
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public applyEncodedState(params: DocApplyEncodedStateParams): this {
|
|
55
|
-
const { origin, update } = params;
|
|
56
|
-
|
|
57
|
-
if (update === null || typeof update === "undefined") return this;
|
|
58
|
-
|
|
59
|
-
const uint8Update = typeof update === "string" ? toUint8Array(update) : update;
|
|
60
|
-
|
|
61
|
-
applyUpdate(this.value, uint8Update, origin);
|
|
62
|
-
|
|
63
|
-
return this;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public batchApplyEncodedState(params: DocBatchApplyEncodedStateParams): this {
|
|
67
|
-
const { origin } = params;
|
|
68
|
-
const updates = params.updates ?? [];
|
|
69
|
-
|
|
70
|
-
const filtered = updates.reduce<Uint8Array[]>((acc, update) => {
|
|
71
|
-
if (!update) return acc;
|
|
72
|
-
|
|
73
|
-
if (typeof update === "string") {
|
|
74
|
-
acc.push(toUint8Array(update));
|
|
75
|
-
return acc;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (typeof update === "object") {
|
|
79
|
-
acc.push(update);
|
|
80
|
-
return acc;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return acc;
|
|
84
|
-
}, []);
|
|
85
|
-
|
|
86
|
-
if (!filtered.length) return this;
|
|
87
|
-
|
|
88
|
-
const merged = mergeUpdates(filtered);
|
|
89
|
-
|
|
90
|
-
return this.applyEncodedState({ origin, update: merged });
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public canRedo(): boolean {
|
|
94
|
-
return !!this.#_undoManager?.canRedo();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public canUndo(): boolean {
|
|
98
|
-
return !!this.#_undoManager?.canUndo();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public destroy(): void {
|
|
102
|
-
this.#_undoManager?.destroy();
|
|
103
|
-
this.value.destroy();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
public get(key?: undefined): TStorage;
|
|
107
|
-
public get<TKey extends keyof TStorage>(type: TKey): TStorage[TKey];
|
|
108
|
-
public get<TKey extends keyof TStorage>(type?: TKey): TStorage | TStorage[TKey] {
|
|
109
|
-
if (typeof type === "undefined") return this.#_storage;
|
|
110
|
-
|
|
111
|
-
return this.#_storage[type as TKey];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public getEncodedState(): string {
|
|
115
|
-
return fromUint8Array(encodeStateAsUpdate(this.value));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public isEmpty(): boolean {
|
|
119
|
-
return !this.value.share.size;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
public rebuildStorage(reference: TStorage): this {
|
|
123
|
-
const isBuilt = !!Object.keys(this.#_storage).length;
|
|
124
|
-
|
|
125
|
-
if (isBuilt) {
|
|
126
|
-
console.warn("Attempted to rebuild storage multiple times");
|
|
127
|
-
return this;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
this.#_storage = Object.entries(reference).reduce((acc, [key, node]) => {
|
|
131
|
-
/**
|
|
132
|
-
* @description It is important that the XML shared-types be checked before the others
|
|
133
|
-
* because they extend off the non-xml types (thereby you can mistakenly identify the
|
|
134
|
-
* wrong type if checked in the reverse order)
|
|
135
|
-
* @date May 9 ,2025
|
|
136
|
-
*/
|
|
137
|
-
if (node instanceof YXmlElement) {
|
|
138
|
-
return { ...acc, [key]: this.value.getXmlElement(key) };
|
|
139
|
-
}
|
|
140
|
-
if (node instanceof YXmlFragment) {
|
|
141
|
-
return { ...acc, [key]: this.value.getXmlFragment(key) };
|
|
142
|
-
}
|
|
143
|
-
if (node instanceof YXmlText) return { ...acc, [key]: this.value.get(key, YXmlText) };
|
|
144
|
-
if (node instanceof YArray) return { ...acc, [key]: this.value.getArray(key) };
|
|
145
|
-
if (node instanceof YMap) return { ...acc, [key]: this.value.getMap(key) };
|
|
146
|
-
if (node instanceof YText) return { ...acc, [key]: this.value.getText(key) };
|
|
147
|
-
|
|
148
|
-
return acc;
|
|
149
|
-
}, {} as TStorage);
|
|
150
|
-
|
|
151
|
-
return this.track();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
public redo(): this {
|
|
155
|
-
this.#_undoManager?.redo();
|
|
156
|
-
|
|
157
|
-
return this;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
public subscribe(
|
|
161
|
-
listener: (params: DocSubscribeCallbackParams<YDoc, TStorage>) => void,
|
|
162
|
-
): () => void {
|
|
163
|
-
const fn = (update: Uint8Array, origin: any, doc: YDoc) => {
|
|
164
|
-
listener({
|
|
165
|
-
doc: this,
|
|
166
|
-
local: origin === null || typeof origin === "undefined",
|
|
167
|
-
origin: origin ? String(origin) : null,
|
|
168
|
-
update: fromUint8Array(update),
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
this.value.on("update", fn);
|
|
173
|
-
|
|
174
|
-
return () => {
|
|
175
|
-
this.value.off("update", fn);
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
public track(): this {
|
|
180
|
-
if (this.#_undoManager) this.#_undoManager.destroy();
|
|
181
|
-
|
|
182
|
-
const sharedTypes = Object.values(this.#_storage).reduce<YjsType<AbstractType<any>, any>[]>(
|
|
183
|
-
(acc, type) => {
|
|
184
|
-
if (
|
|
185
|
-
type instanceof YArray ||
|
|
186
|
-
type instanceof YMap ||
|
|
187
|
-
type instanceof YText ||
|
|
188
|
-
type instanceof YXmlElement ||
|
|
189
|
-
type instanceof YXmlFragment ||
|
|
190
|
-
type instanceof YXmlText
|
|
191
|
-
) {
|
|
192
|
-
acc.push(type as YjsType<AbstractType<any>, any>);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return acc;
|
|
196
|
-
},
|
|
197
|
-
[],
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
if (!sharedTypes.length) {
|
|
201
|
-
this.#_undoManager?.destroy();
|
|
202
|
-
this.#_undoManager = null;
|
|
203
|
-
|
|
204
|
-
return this;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
this.#_undoManager = new UndoManager(sharedTypes, { captureTimeout: MERGE_INTERVAL_MS });
|
|
208
|
-
|
|
209
|
-
return this;
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
public transact(fn: () => void): this {
|
|
213
|
-
this.value.transact(fn);
|
|
214
|
-
|
|
215
|
-
return this;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
public toJson(): InferCrdtJson<TStorage>;
|
|
219
|
-
public toJson<TKey extends keyof TStorage>(type: TKey): InferCrdtJson<TStorage[TKey]>;
|
|
220
|
-
public toJson<TKey extends keyof TStorage>(type?: TKey) {
|
|
221
|
-
if (typeof type === "string") return this.#_storage[type].toJSON();
|
|
222
|
-
|
|
223
|
-
return Object.entries(this.#_storage).reduce(
|
|
224
|
-
(acc, [key, value]) => ({ ...(acc as any), [key]: value.toJSON() }),
|
|
225
|
-
{} as InferCrdtJson<TStorage>,
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
public undo(): this {
|
|
230
|
-
this.#_undoManager?.undo();
|
|
231
|
-
|
|
232
|
-
return this;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
#_setPluvId() {
|
|
236
|
-
const text = this.value.getText(PLUV_ID_FIELD);
|
|
237
|
-
const id = typeof crypto !== "undefined" ? crypto.randomUUID() : Math.random().toString();
|
|
238
|
-
|
|
239
|
-
text.insert(0, id);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { AbstractCrdtDocFactory } from "@pluv/crdt";
|
|
2
|
-
import type { Doc as YDoc } from "yjs";
|
|
3
|
-
import type { YjsType } from "../types";
|
|
4
|
-
import { CrdtYjsDoc } from "./CrdtYjsDoc";
|
|
5
|
-
import type { YjsBuilder } from "./builder";
|
|
6
|
-
|
|
7
|
-
export class CrdtYjsDocFactory<
|
|
8
|
-
TStorage extends Record<string, YjsType<any, any>> = {},
|
|
9
|
-
> extends AbstractCrdtDocFactory<YDoc, TStorage> {
|
|
10
|
-
public readonly _initialStorage: (builder: YjsBuilder) => TStorage;
|
|
11
|
-
|
|
12
|
-
constructor(initialStorage: (builder: YjsBuilder) => TStorage = () => ({}) as TStorage) {
|
|
13
|
-
super(initialStorage);
|
|
14
|
-
|
|
15
|
-
this._initialStorage = initialStorage;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public getEmpty(): CrdtYjsDoc<TStorage> {
|
|
19
|
-
return new CrdtYjsDoc<TStorage>();
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
public getFactory(
|
|
23
|
-
initialStorage: (builder: YjsBuilder) => TStorage,
|
|
24
|
-
): CrdtYjsDocFactory<TStorage> {
|
|
25
|
-
return new CrdtYjsDocFactory<TStorage>(initialStorage ?? this._initialStorage);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public getInitialized(
|
|
29
|
-
initialStorage?: (builder: YjsBuilder) => TStorage,
|
|
30
|
-
): CrdtYjsDoc<TStorage> {
|
|
31
|
-
return new CrdtYjsDoc<TStorage>(initialStorage ?? this._initialStorage);
|
|
32
|
-
}
|
|
33
|
-
}
|
package/src/doc/builder.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Array as YArray,
|
|
3
|
-
Doc as YDoc,
|
|
4
|
-
Map as YMap,
|
|
5
|
-
Text as YText,
|
|
6
|
-
XmlElement as YXmlElement,
|
|
7
|
-
XmlFragment as YXmlFragment,
|
|
8
|
-
XmlText as YXmlText,
|
|
9
|
-
} from "yjs";
|
|
10
|
-
import { YjsType } from "../types";
|
|
11
|
-
|
|
12
|
-
export type YjsBuilder = ReturnType<typeof builder>;
|
|
13
|
-
|
|
14
|
-
export const builder = (doc: YDoc) => {
|
|
15
|
-
return {
|
|
16
|
-
array<T extends unknown>(
|
|
17
|
-
name: string,
|
|
18
|
-
value: T[] | readonly T[] = [],
|
|
19
|
-
): YjsType<YArray<T>, T[]> {
|
|
20
|
-
const array = doc.getArray<T>(name);
|
|
21
|
-
|
|
22
|
-
if (!!value.length) array.insert(0, value as T[]);
|
|
23
|
-
|
|
24
|
-
return array as unknown as YjsType<YArray<T>, T[]>;
|
|
25
|
-
},
|
|
26
|
-
map<T extends unknown>(
|
|
27
|
-
name: string,
|
|
28
|
-
value: readonly (readonly [key: string, value: T])[] = [],
|
|
29
|
-
): YjsType<YMap<T>, Record<string, T>> {
|
|
30
|
-
const map = doc.getMap(name);
|
|
31
|
-
|
|
32
|
-
value.forEach(([k, v]) => {
|
|
33
|
-
map.set(k, v);
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
return map as unknown as YjsType<YMap<T>, Record<string, T>>;
|
|
37
|
-
},
|
|
38
|
-
text(name: string, value: string = ""): YjsType<YText, string> {
|
|
39
|
-
const text = doc.getText(name);
|
|
40
|
-
|
|
41
|
-
if (!!value) text.insert(0, value);
|
|
42
|
-
|
|
43
|
-
return text as unknown as YjsType<YText, string>;
|
|
44
|
-
},
|
|
45
|
-
xmlElement(
|
|
46
|
-
name: string,
|
|
47
|
-
children: (YXmlElement | YXmlText)[] | readonly (YXmlElement | YXmlText)[] = [],
|
|
48
|
-
): YjsType<YXmlElement, string> {
|
|
49
|
-
const xmlElement = doc.getXmlElement(name);
|
|
50
|
-
|
|
51
|
-
if (!!children.length) xmlElement.insert(0, children as (YXmlElement | YXmlText)[]);
|
|
52
|
-
|
|
53
|
-
return xmlElement as unknown as YjsType<YXmlElement, string>;
|
|
54
|
-
},
|
|
55
|
-
xmlFragment(
|
|
56
|
-
name: string,
|
|
57
|
-
children: (YXmlElement | YXmlText)[] | readonly (YXmlElement | YXmlText)[] = [],
|
|
58
|
-
): YjsType<YXmlFragment, string> {
|
|
59
|
-
const xmlFragment = doc.getXmlFragment(name);
|
|
60
|
-
|
|
61
|
-
if (!!children.length) xmlFragment.insert(0, children as (YXmlElement | YXmlText)[]);
|
|
62
|
-
|
|
63
|
-
return xmlFragment as unknown as YjsType<YXmlFragment, string>;
|
|
64
|
-
},
|
|
65
|
-
xmlText(name: string): YjsType<YXmlText, string> {
|
|
66
|
-
const xmlText = doc.get(name, YXmlText);
|
|
67
|
-
|
|
68
|
-
return xmlText as unknown as YjsType<YXmlText, string>;
|
|
69
|
-
},
|
|
70
|
-
};
|
|
71
|
-
};
|
package/src/doc/doc.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { YjsType } from "../types";
|
|
2
|
-
import type { YjsBuilder } from "./builder";
|
|
3
|
-
import { CrdtYjsDocFactory } from "./CrdtYjsDocFactory";
|
|
4
|
-
|
|
5
|
-
export const doc = <TStorage extends Record<string, YjsType<any, any>>>(
|
|
6
|
-
value: (buidler: YjsBuilder) => TStorage = () => ({}) as TStorage,
|
|
7
|
-
): CrdtYjsDocFactory<TStorage> => {
|
|
8
|
-
return new CrdtYjsDocFactory<TStorage>(value);
|
|
9
|
-
};
|
package/src/doc/index.ts
DELETED
package/src/index.ts
DELETED
package/src/map/YjsMap.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Map as YMap } from "yjs";
|
|
2
|
-
import type { YjsType } from "../types";
|
|
3
|
-
|
|
4
|
-
export class YjsMap<T extends unknown>
|
|
5
|
-
extends YMap<T>
|
|
6
|
-
implements YjsType<YMap<T>, Record<string, T>>
|
|
7
|
-
{
|
|
8
|
-
public initialValue?: readonly (readonly [key: string, value: T])[];
|
|
9
|
-
|
|
10
|
-
constructor(value: readonly (readonly [key: string, value: T])[] = []) {
|
|
11
|
-
super(value);
|
|
12
|
-
|
|
13
|
-
this.initialValue = value;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public __pluvType(): any {
|
|
17
|
-
return {} as any;
|
|
18
|
-
}
|
|
19
|
-
}
|
package/src/map/index.ts
DELETED
package/src/map/map.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { Map as YMap } from "yjs";
|
|
2
|
-
import type { YjsType } from "../types";
|
|
3
|
-
import { YjsMap } from "./YjsMap";
|
|
4
|
-
|
|
5
|
-
export const map = <T extends unknown>(
|
|
6
|
-
value: readonly (readonly [key: string, value: T])[] = [],
|
|
7
|
-
): YjsType<YMap<T>, Record<string, T>> => {
|
|
8
|
-
return new YjsMap<T>(value) as YjsType<YMap<T>, Record<string, T>>;
|
|
9
|
-
};
|