@pluv/crdt-yjs 0.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/.eslintrc.js +4 -0
- package/.turbo/turbo-build.log +17 -0
- package/dist/index.d.ts +43 -0
- package/dist/index.js +139 -0
- package/dist/index.mjs +110 -0
- package/package.json +30 -0
- package/src/array.ts +11 -0
- package/src/doc.ts +90 -0
- package/src/index.ts +14 -0
- package/src/map.ts +7 -0
- package/src/object.ts +18 -0
- package/src/text.ts +5 -0
- package/src/types.ts +40 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
> @pluv/crdt-yjs@0.0.0 build /home/runner/work/pluv/pluv/packages/crdt-yjs
|
|
3
|
+
> tsup src/index.ts --format esm,cjs --dts
|
|
4
|
+
|
|
5
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
+
[34mCLI[39m tsup v6.5.0
|
|
8
|
+
[34mCLI[39m Target: es6
|
|
9
|
+
[34mESM[39m Build start
|
|
10
|
+
[34mCJS[39m Build start
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m4.01 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 54ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m2.88 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 55ms
|
|
15
|
+
[34mDTS[39m Build start
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 1412ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m2.61 KB[39m
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { Array, AbstractType, Doc, Transaction, Map, Text } from 'yjs';
|
|
2
|
+
export { AbstractType, Array, Map, Text } from 'yjs';
|
|
3
|
+
import { JsonObject, JsonPrimitive } from '@pluv/types';
|
|
4
|
+
|
|
5
|
+
declare const array: <T extends unknown>(value?: readonly T[]) => Array<T>;
|
|
6
|
+
|
|
7
|
+
declare class YjsDoc<T extends Record<string, AbstractType<any>> = {}> {
|
|
8
|
+
value: Doc;
|
|
9
|
+
private get _storage();
|
|
10
|
+
constructor(value?: T);
|
|
11
|
+
applyUpdate(update: string | Uint8Array, origin?: string): this;
|
|
12
|
+
destroy(): void;
|
|
13
|
+
encodeStateAsUpdate(): string;
|
|
14
|
+
get<TKey extends keyof T>(key: TKey): T[TKey];
|
|
15
|
+
getSharedTypes(): T;
|
|
16
|
+
subscribe(fn: (update: string, origin: string, doc: Doc, transaction: Transaction) => void): () => void;
|
|
17
|
+
toJSON(): JsonObject;
|
|
18
|
+
static fromUint8Array(u8a: Uint8Array): string;
|
|
19
|
+
static toUint8Array(str: string): Uint8Array;
|
|
20
|
+
}
|
|
21
|
+
declare const doc: <T extends Record<string, AbstractType<any>> = {}>(value?: T | undefined) => YjsDoc<T>;
|
|
22
|
+
|
|
23
|
+
declare const map: <T extends unknown>(value?: readonly (readonly [key: string, value: T])[]) => Map<T>;
|
|
24
|
+
|
|
25
|
+
type unstable__YObjectValue = Record<string, NonNullable<JsonPrimitive> | AbstractType<any>>;
|
|
26
|
+
type unstable__YObject<T extends unstable__YObjectValue> = Map<T[keyof T]> & {
|
|
27
|
+
__$type: T;
|
|
28
|
+
};
|
|
29
|
+
declare const unstable__object: <T extends unstable__YObjectValue = {}>(value: T) => unstable__YObject<T>;
|
|
30
|
+
|
|
31
|
+
declare const text: (value?: string) => Text;
|
|
32
|
+
|
|
33
|
+
type InferYjsDocJson<TDoc extends YjsDoc<any>> = TDoc extends YjsDoc<infer ITypes> ? {
|
|
34
|
+
[P in keyof ITypes]: InferYjsSharedTypeJson<ITypes[P]>;
|
|
35
|
+
} : never;
|
|
36
|
+
type InferYjsDocSharedType<TDoc extends YjsDoc<any>, TKey extends keyof InferYjsDocSharedTypes<TDoc> = keyof InferYjsDocSharedTypes<TDoc>> = InferYjsDocSharedTypes<TDoc>[TKey];
|
|
37
|
+
type InferYjsDocSharedTypeJson<TDoc extends YjsDoc<any>, TKey extends keyof InferYjsDocSharedTypes<TDoc> = keyof InferYjsDocSharedTypes<TDoc>> = InferYjsSharedTypeJson<InferYjsDocSharedTypes<TDoc>[TKey]>;
|
|
38
|
+
type InferYjsDocSharedTypes<TDoc extends YjsDoc<any>> = TDoc extends YjsDoc<infer ITypes> ? ITypes : never;
|
|
39
|
+
type InferYjsSharedTypeJson<TShared extends unknown> = TShared extends AbstractType<any> ? TShared extends Array<infer IItem> ? readonly InferYjsSharedTypeJson<IItem>[] : TShared extends unstable__YObject<infer IObject> ? {
|
|
40
|
+
[P in keyof IObject]: InferYjsSharedTypeJson<IObject[P]>;
|
|
41
|
+
} : TShared extends Map<infer IItem> ? Record<string, InferYjsSharedTypeJson<IItem>> : TShared extends Text ? string : never : TShared;
|
|
42
|
+
|
|
43
|
+
export { InferYjsDocJson, InferYjsDocSharedType, InferYjsDocSharedTypeJson, InferYjsDocSharedTypes, InferYjsSharedTypeJson, YjsDoc, array, doc, map, text, unstable__YObject, unstable__object };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defProps = Object.defineProperties;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
10
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
11
|
+
var __spreadValues = (a, b) => {
|
|
12
|
+
for (var prop in b || (b = {}))
|
|
13
|
+
if (__hasOwnProp.call(b, prop))
|
|
14
|
+
__defNormalProp(a, prop, b[prop]);
|
|
15
|
+
if (__getOwnPropSymbols)
|
|
16
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
17
|
+
if (__propIsEnum.call(b, prop))
|
|
18
|
+
__defNormalProp(a, prop, b[prop]);
|
|
19
|
+
}
|
|
20
|
+
return a;
|
|
21
|
+
};
|
|
22
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
23
|
+
var __export = (target, all) => {
|
|
24
|
+
for (var name in all)
|
|
25
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
26
|
+
};
|
|
27
|
+
var __copyProps = (to, from, except, desc) => {
|
|
28
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
29
|
+
for (let key of __getOwnPropNames(from))
|
|
30
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
31
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
32
|
+
}
|
|
33
|
+
return to;
|
|
34
|
+
};
|
|
35
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
36
|
+
|
|
37
|
+
// src/index.ts
|
|
38
|
+
var src_exports = {};
|
|
39
|
+
__export(src_exports, {
|
|
40
|
+
YjsDoc: () => YjsDoc,
|
|
41
|
+
array: () => array,
|
|
42
|
+
doc: () => doc,
|
|
43
|
+
map: () => map,
|
|
44
|
+
text: () => text,
|
|
45
|
+
unstable__object: () => unstable__object
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(src_exports);
|
|
48
|
+
|
|
49
|
+
// src/array.ts
|
|
50
|
+
var import_yjs = require("yjs");
|
|
51
|
+
var array = (value = []) => {
|
|
52
|
+
const yarray = new import_yjs.Array();
|
|
53
|
+
yarray.push(value.slice());
|
|
54
|
+
return yarray;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// src/doc.ts
|
|
58
|
+
var import_js_base64 = require("js-base64");
|
|
59
|
+
var import_yjs2 = require("yjs");
|
|
60
|
+
var YjsDoc = class {
|
|
61
|
+
constructor(value) {
|
|
62
|
+
this.value = new import_yjs2.Doc();
|
|
63
|
+
if (!value)
|
|
64
|
+
return;
|
|
65
|
+
const storage = this.value.getMap("storage");
|
|
66
|
+
Object.entries(value).forEach(([key, node]) => {
|
|
67
|
+
storage.set(key, node);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
get _storage() {
|
|
71
|
+
return this.value.getMap("storage");
|
|
72
|
+
}
|
|
73
|
+
applyUpdate(update, origin) {
|
|
74
|
+
const uint8Update = typeof update === "string" ? (0, import_js_base64.toUint8Array)(update) : update;
|
|
75
|
+
(0, import_yjs2.applyUpdate)(this.value, uint8Update, origin);
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
destroy() {
|
|
79
|
+
this.value.destroy();
|
|
80
|
+
}
|
|
81
|
+
encodeStateAsUpdate() {
|
|
82
|
+
return (0, import_js_base64.fromUint8Array)((0, import_yjs2.encodeStateAsUpdate)(this.value));
|
|
83
|
+
}
|
|
84
|
+
get(key) {
|
|
85
|
+
return this._storage.get(key.toString());
|
|
86
|
+
}
|
|
87
|
+
getSharedTypes() {
|
|
88
|
+
return Array.from(this._storage.keys()).reduce(
|
|
89
|
+
(acc, key) => __spreadProps(__spreadValues({}, acc), { [key]: this._storage.get(key) }),
|
|
90
|
+
{}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
subscribe(fn) {
|
|
94
|
+
const _fn = (update, origin, doc2, transaction) => fn(YjsDoc.fromUint8Array(update), origin, doc2, transaction);
|
|
95
|
+
this.value.on("update", _fn);
|
|
96
|
+
return () => {
|
|
97
|
+
this.value.off("update", _fn);
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
toJSON() {
|
|
101
|
+
return this._storage.toJSON();
|
|
102
|
+
}
|
|
103
|
+
static fromUint8Array(u8a) {
|
|
104
|
+
return (0, import_js_base64.fromUint8Array)(u8a);
|
|
105
|
+
}
|
|
106
|
+
static toUint8Array(str) {
|
|
107
|
+
return (0, import_js_base64.toUint8Array)(str);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var doc = (value) => {
|
|
111
|
+
return new YjsDoc(value);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/map.ts
|
|
115
|
+
var import_yjs3 = require("yjs");
|
|
116
|
+
var map = (value = []) => {
|
|
117
|
+
return new import_yjs3.Map(value);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
// src/object.ts
|
|
121
|
+
var import_yjs4 = require("yjs");
|
|
122
|
+
var unstable__object = (value) => {
|
|
123
|
+
return new import_yjs4.Map(Object.entries(value));
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
// src/text.ts
|
|
127
|
+
var import_yjs5 = require("yjs");
|
|
128
|
+
var text = (value = "") => {
|
|
129
|
+
return new import_yjs5.Text(value);
|
|
130
|
+
};
|
|
131
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
132
|
+
0 && (module.exports = {
|
|
133
|
+
YjsDoc,
|
|
134
|
+
array,
|
|
135
|
+
doc,
|
|
136
|
+
map,
|
|
137
|
+
text,
|
|
138
|
+
unstable__object
|
|
139
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
|
|
21
|
+
// src/array.ts
|
|
22
|
+
import { Array as YArray } from "yjs";
|
|
23
|
+
var array = (value = []) => {
|
|
24
|
+
const yarray = new YArray();
|
|
25
|
+
yarray.push(value.slice());
|
|
26
|
+
return yarray;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// src/doc.ts
|
|
30
|
+
import { fromUint8Array, toUint8Array } from "js-base64";
|
|
31
|
+
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate } from "yjs";
|
|
32
|
+
var YjsDoc = class {
|
|
33
|
+
constructor(value) {
|
|
34
|
+
this.value = new YDoc();
|
|
35
|
+
if (!value)
|
|
36
|
+
return;
|
|
37
|
+
const storage = this.value.getMap("storage");
|
|
38
|
+
Object.entries(value).forEach(([key, node]) => {
|
|
39
|
+
storage.set(key, node);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
get _storage() {
|
|
43
|
+
return this.value.getMap("storage");
|
|
44
|
+
}
|
|
45
|
+
applyUpdate(update, origin) {
|
|
46
|
+
const uint8Update = typeof update === "string" ? toUint8Array(update) : update;
|
|
47
|
+
applyUpdate(this.value, uint8Update, origin);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
destroy() {
|
|
51
|
+
this.value.destroy();
|
|
52
|
+
}
|
|
53
|
+
encodeStateAsUpdate() {
|
|
54
|
+
return fromUint8Array(encodeStateAsUpdate(this.value));
|
|
55
|
+
}
|
|
56
|
+
get(key) {
|
|
57
|
+
return this._storage.get(key.toString());
|
|
58
|
+
}
|
|
59
|
+
getSharedTypes() {
|
|
60
|
+
return Array.from(this._storage.keys()).reduce(
|
|
61
|
+
(acc, key) => __spreadProps(__spreadValues({}, acc), { [key]: this._storage.get(key) }),
|
|
62
|
+
{}
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
subscribe(fn) {
|
|
66
|
+
const _fn = (update, origin, doc2, transaction) => fn(YjsDoc.fromUint8Array(update), origin, doc2, transaction);
|
|
67
|
+
this.value.on("update", _fn);
|
|
68
|
+
return () => {
|
|
69
|
+
this.value.off("update", _fn);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
toJSON() {
|
|
73
|
+
return this._storage.toJSON();
|
|
74
|
+
}
|
|
75
|
+
static fromUint8Array(u8a) {
|
|
76
|
+
return fromUint8Array(u8a);
|
|
77
|
+
}
|
|
78
|
+
static toUint8Array(str) {
|
|
79
|
+
return toUint8Array(str);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
var doc = (value) => {
|
|
83
|
+
return new YjsDoc(value);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/map.ts
|
|
87
|
+
import { Map as YMap } from "yjs";
|
|
88
|
+
var map = (value = []) => {
|
|
89
|
+
return new YMap(value);
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// src/object.ts
|
|
93
|
+
import { Map as YMap2 } from "yjs";
|
|
94
|
+
var unstable__object = (value) => {
|
|
95
|
+
return new YMap2(Object.entries(value));
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/text.ts
|
|
99
|
+
import { Text as YText } from "yjs";
|
|
100
|
+
var text = (value = "") => {
|
|
101
|
+
return new YText(value);
|
|
102
|
+
};
|
|
103
|
+
export {
|
|
104
|
+
YjsDoc,
|
|
105
|
+
array,
|
|
106
|
+
doc,
|
|
107
|
+
map,
|
|
108
|
+
text,
|
|
109
|
+
unstable__object
|
|
110
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pluv/crdt-yjs",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"publishConfig": {
|
|
8
|
+
"access": "public"
|
|
9
|
+
},
|
|
10
|
+
"dependencies": {
|
|
11
|
+
"@pluv/types": "^0.0.0",
|
|
12
|
+
"js-base64": "^3.7.3"
|
|
13
|
+
},
|
|
14
|
+
"peerDependencies": {
|
|
15
|
+
"yjs": "^13.0.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@pluv/tsconfig": "^0.0.0",
|
|
19
|
+
"eslint-config-pluv": "^0.0.0",
|
|
20
|
+
"tsup": "^6.5.0",
|
|
21
|
+
"typescript": "^4.9.4",
|
|
22
|
+
"yjs": "^13.5.44"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
26
|
+
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
27
|
+
"lint": "eslint src/**/*.ts* --fix",
|
|
28
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/src/array.ts
ADDED
package/src/doc.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { JsonObject } from "@pluv/types";
|
|
2
|
+
import { fromUint8Array, toUint8Array } from "js-base64";
|
|
3
|
+
import type { AbstractType, Map as YMap, Transaction } from "yjs";
|
|
4
|
+
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate } from "yjs";
|
|
5
|
+
|
|
6
|
+
export class YjsDoc<T extends Record<string, AbstractType<any>> = {}> {
|
|
7
|
+
public value: YDoc = new YDoc();
|
|
8
|
+
|
|
9
|
+
private get _storage(): YMap<any> {
|
|
10
|
+
return this.value.getMap("storage");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
constructor(value?: T) {
|
|
14
|
+
if (!value) return;
|
|
15
|
+
|
|
16
|
+
const storage = this.value.getMap("storage");
|
|
17
|
+
|
|
18
|
+
Object.entries(value).forEach(([key, node]) => {
|
|
19
|
+
storage.set(key, node);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public applyUpdate(update: string | Uint8Array, origin?: string): this {
|
|
24
|
+
const uint8Update =
|
|
25
|
+
typeof update === "string" ? toUint8Array(update) : update;
|
|
26
|
+
|
|
27
|
+
applyUpdate(this.value, uint8Update, origin);
|
|
28
|
+
|
|
29
|
+
return this;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
public destroy(): void {
|
|
33
|
+
this.value.destroy();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
public encodeStateAsUpdate(): string {
|
|
37
|
+
return fromUint8Array(encodeStateAsUpdate(this.value));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
public get<TKey extends keyof T>(key: TKey): T[TKey] {
|
|
41
|
+
return this._storage.get(key.toString());
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public getSharedTypes(): T {
|
|
45
|
+
return Array.from(this._storage.keys()).reduce(
|
|
46
|
+
(acc, key) => ({ ...acc, [key]: this._storage.get(key) }),
|
|
47
|
+
{} as T
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public subscribe(
|
|
52
|
+
fn: (
|
|
53
|
+
update: string,
|
|
54
|
+
origin: string,
|
|
55
|
+
doc: YDoc,
|
|
56
|
+
transaction: Transaction
|
|
57
|
+
) => void
|
|
58
|
+
): () => void {
|
|
59
|
+
const _fn = (
|
|
60
|
+
update: Uint8Array,
|
|
61
|
+
origin: string,
|
|
62
|
+
doc: YDoc,
|
|
63
|
+
transaction: Transaction
|
|
64
|
+
) => fn(YjsDoc.fromUint8Array(update), origin, doc, transaction);
|
|
65
|
+
|
|
66
|
+
this.value.on("update", _fn);
|
|
67
|
+
|
|
68
|
+
return () => {
|
|
69
|
+
this.value.off("update", _fn);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
public toJSON(): JsonObject {
|
|
74
|
+
return this._storage.toJSON();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
public static fromUint8Array(u8a: Uint8Array): string {
|
|
78
|
+
return fromUint8Array(u8a);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public static toUint8Array(str: string): Uint8Array {
|
|
82
|
+
return toUint8Array(str);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export const doc = <T extends Record<string, AbstractType<any>> = {}>(
|
|
87
|
+
value?: T
|
|
88
|
+
): YjsDoc<T> => {
|
|
89
|
+
return new YjsDoc(value);
|
|
90
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type { AbstractType, Array, Map, Text } from "yjs";
|
|
2
|
+
export { array } from "./array";
|
|
3
|
+
export { doc, YjsDoc } from "./doc";
|
|
4
|
+
export { map } from "./map";
|
|
5
|
+
export type { unstable__YObject } from "./object";
|
|
6
|
+
export { unstable__object } from "./object";
|
|
7
|
+
export { text } from "./text";
|
|
8
|
+
export type {
|
|
9
|
+
InferYjsDocJson,
|
|
10
|
+
InferYjsDocSharedType,
|
|
11
|
+
InferYjsDocSharedTypeJson,
|
|
12
|
+
InferYjsDocSharedTypes,
|
|
13
|
+
InferYjsSharedTypeJson,
|
|
14
|
+
} from "./types";
|
package/src/map.ts
ADDED
package/src/object.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { JsonPrimitive } from "@pluv/types";
|
|
2
|
+
import type { AbstractType } from "yjs";
|
|
3
|
+
import { Map as YMap } from "yjs";
|
|
4
|
+
|
|
5
|
+
export type unstable__YObjectValue = Record<
|
|
6
|
+
string,
|
|
7
|
+
NonNullable<JsonPrimitive> | AbstractType<any>
|
|
8
|
+
>;
|
|
9
|
+
|
|
10
|
+
export type unstable__YObject<T extends unstable__YObjectValue> = YMap<
|
|
11
|
+
T[keyof T]
|
|
12
|
+
> & { __$type: T };
|
|
13
|
+
|
|
14
|
+
export const unstable__object = <T extends unstable__YObjectValue = {}>(
|
|
15
|
+
value: T
|
|
16
|
+
): unstable__YObject<T> => {
|
|
17
|
+
return new YMap(Object.entries(value)) as unstable__YObject<T>;
|
|
18
|
+
};
|
package/src/text.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AbstractType,
|
|
3
|
+
Array as YArray,
|
|
4
|
+
Map as YMap,
|
|
5
|
+
Text as YText,
|
|
6
|
+
} from "yjs";
|
|
7
|
+
import type { YjsDoc } from "./doc";
|
|
8
|
+
import type { unstable__YObject } from "./object";
|
|
9
|
+
|
|
10
|
+
export type InferYjsDocJson<TDoc extends YjsDoc<any>> = TDoc extends YjsDoc<
|
|
11
|
+
infer ITypes
|
|
12
|
+
>
|
|
13
|
+
? { [P in keyof ITypes]: InferYjsSharedTypeJson<ITypes[P]> }
|
|
14
|
+
: never;
|
|
15
|
+
|
|
16
|
+
export type InferYjsDocSharedType<
|
|
17
|
+
TDoc extends YjsDoc<any>,
|
|
18
|
+
TKey extends keyof InferYjsDocSharedTypes<TDoc> = keyof InferYjsDocSharedTypes<TDoc>
|
|
19
|
+
> = InferYjsDocSharedTypes<TDoc>[TKey];
|
|
20
|
+
|
|
21
|
+
export type InferYjsDocSharedTypeJson<
|
|
22
|
+
TDoc extends YjsDoc<any>,
|
|
23
|
+
TKey extends keyof InferYjsDocSharedTypes<TDoc> = keyof InferYjsDocSharedTypes<TDoc>
|
|
24
|
+
> = InferYjsSharedTypeJson<InferYjsDocSharedTypes<TDoc>[TKey]>;
|
|
25
|
+
|
|
26
|
+
export type InferYjsDocSharedTypes<TDoc extends YjsDoc<any>> =
|
|
27
|
+
TDoc extends YjsDoc<infer ITypes> ? ITypes : never;
|
|
28
|
+
|
|
29
|
+
export type InferYjsSharedTypeJson<TShared extends unknown> =
|
|
30
|
+
TShared extends AbstractType<any>
|
|
31
|
+
? TShared extends YArray<infer IItem>
|
|
32
|
+
? readonly InferYjsSharedTypeJson<IItem>[]
|
|
33
|
+
: TShared extends unstable__YObject<infer IObject>
|
|
34
|
+
? { [P in keyof IObject]: InferYjsSharedTypeJson<IObject[P]> }
|
|
35
|
+
: TShared extends YMap<infer IItem>
|
|
36
|
+
? Record<string, InferYjsSharedTypeJson<IItem>>
|
|
37
|
+
: TShared extends YText
|
|
38
|
+
? string
|
|
39
|
+
: never
|
|
40
|
+
: TShared;
|