loro-crdt 0.3.0 → 0.4.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/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # loro-crdt
2
+
3
+ Loro CRDTs is a high-performance CRDT framework.
4
+
5
+ It makes your app state synchronized, collaborative and maintainable effortlessly.
6
+
7
+ Learn more at https://loro.dev
8
+
package/dist/loro.d.ts CHANGED
@@ -1,21 +1,44 @@
1
- import { ContainerID, PrelimList, PrelimMap, PrelimText } from 'loro-wasm';
2
- export { ContainerID, ContainerType, Loro, LoroList, LoroMap, LoroText, PrelimList, PrelimMap, PrelimText, Transaction, setPanicHook } from 'loro-wasm';
1
+ import { ContainerID, PrelimList, PrelimMap, PrelimText, Delta, TreeID } from 'loro-wasm';
2
+ export * from 'loro-wasm';
3
+ export { Loro } from 'loro-wasm';
3
4
 
4
- type Value = ContainerID | string | number | null | {
5
+ /**
6
+ * Data types supported by loro
7
+ */
8
+ type Value = ContainerID | string | number | boolean | null | {
5
9
  [key: string]: Value;
6
- } | Value[];
10
+ } | Uint8Array | Value[];
7
11
  type Prelim = PrelimList | PrelimMap | PrelimText;
12
+ /**
13
+ * Represents a path to identify the exact location of an event's target.
14
+ * The path is composed of numbers (e.g., indices of a list container) and strings
15
+ * (e.g., keys of a map container), indicating the absolute position of the event's source
16
+ * within a loro document.
17
+ */
8
18
  type Path = (number | string)[];
9
- type Delta<T> = {
10
- type: "insert";
11
- value: T;
12
- } | {
13
- type: "delete";
14
- len: number;
15
- } | {
16
- type: "retain";
17
- len: number;
18
- };
19
+ /**
20
+ * The event of Loro.
21
+ * @prop local - Indicates whether the event is local.
22
+ * @prop origin - (Optional) Provides information about the origin of the event.
23
+ * @prop diff - Contains the differential information related to the event.
24
+ * @prop target - Identifies the container ID of the event's target.
25
+ * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.
26
+ */
27
+ interface LoroEvent {
28
+ local: boolean;
29
+ origin?: string;
30
+ /**
31
+ * If true, this event was triggered by a child container.
32
+ */
33
+ fromChildren: boolean;
34
+ /**
35
+ * If true, this event was triggered by a checkout.
36
+ */
37
+ fromCheckout: boolean;
38
+ diff: Diff;
39
+ target: ContainerID;
40
+ path: Path;
41
+ }
19
42
  type ListDiff = {
20
43
  type: "list";
21
44
  diff: Delta<Value[]>[];
@@ -26,23 +49,20 @@ type TextDiff = {
26
49
  };
27
50
  type MapDiff = {
28
51
  type: "map";
52
+ updated: Record<string, Value | undefined>;
53
+ };
54
+ type TreeDiff = {
55
+ type: "tree";
29
56
  diff: {
30
- added: Record<string, Value>;
31
- deleted: Record<string, Value>;
32
- updated: Record<string, {
33
- old: Value;
34
- new: Value;
35
- }>;
57
+ target: TreeID;
58
+ action: "create" | "delete";
59
+ } | {
60
+ target: TreeID;
61
+ action: "move";
62
+ parent: TreeID;
36
63
  };
37
64
  };
38
- type Diff = ListDiff | TextDiff | MapDiff;
39
- interface LoroEvent {
40
- local: boolean;
41
- origin?: string;
42
- diff: Diff;
43
- target: ContainerID;
44
- path: Path;
45
- }
65
+ type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;
46
66
  interface Listener {
47
67
  (event: LoroEvent): void;
48
68
  }
@@ -51,47 +71,42 @@ declare function isContainerId(s: string): s is ContainerID;
51
71
  declare module "loro-wasm" {
52
72
  interface Loro {
53
73
  subscribe(listener: Listener): number;
54
- transact(f: (tx: Transaction) => void, origin?: string): void;
55
74
  }
56
75
  interface Loro<T extends Record<string, any> = Record<string, any>> {
57
- getTypedMap<Key extends (keyof T) & string>(name: Key): T[Key] extends LoroMap ? T[Key] : never;
58
- getTypedList<Key extends (keyof T) & string>(name: Key): T[Key] extends LoroList ? T[Key] : never;
76
+ getTypedMap<Key extends keyof T & string>(name: Key): T[Key] extends LoroMap ? T[Key] : never;
77
+ getTypedList<Key extends keyof T & string>(name: Key): T[Key] extends LoroList ? T[Key] : never;
59
78
  }
60
79
  interface LoroList<T extends any[] = any[]> {
61
- insertContainer(txn: Transaction | Loro, pos: number, container: "Map"): LoroMap;
62
- insertContainer(txn: Transaction | Loro, pos: number, container: "List"): LoroList;
63
- insertContainer(txn: Transaction | Loro, pos: number, container: "Text"): LoroText;
64
- insertContainer(txn: Transaction | Loro, pos: number, container: string): never;
80
+ insertContainer(pos: number, container: "Map"): LoroMap;
81
+ insertContainer(pos: number, container: "List"): LoroList;
82
+ insertContainer(pos: number, container: "Text"): LoroText;
83
+ insertContainer(pos: number, container: "Tree"): LoroTree;
84
+ insertContainer(pos: number, container: string): never;
65
85
  get(index: number): Value;
66
- getTyped<Key extends (keyof T) & number>(loro: Loro, index: Key): T[Key];
67
- insertTyped<Key extends (keyof T) & number>(txn: Transaction | Loro, pos: Key, value: T[Key]): void;
68
- insert(txn: Transaction | Loro, pos: number, value: Value | Prelim): void;
69
- delete(txn: Transaction | Loro, pos: number, len: number): void;
70
- subscribe(txn: Transaction | Loro, listener: Listener): number;
71
- subscribeDeep(txn: Transaction | Loro, listener: Listener): number;
72
- subscribeOnce(txn: Transaction | Loro, listener: Listener): number;
86
+ getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];
87
+ insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;
88
+ insert(pos: number, value: Value | Prelim): void;
89
+ delete(pos: number, len: number): void;
90
+ subscribe(txn: Loro, listener: Listener): number;
73
91
  }
74
92
  interface LoroMap<T extends Record<string, any> = Record<string, any>> {
75
- insertContainer(txn: Transaction | Loro, key: string, container_type: "Map"): LoroMap;
76
- insertContainer(txn: Transaction | Loro, key: string, container_type: "List"): LoroList;
77
- insertContainer(txn: Transaction | Loro, key: string, container_type: "Text"): LoroText;
78
- insertContainer(txn: Transaction | Loro, key: string, container_type: string): never;
93
+ setContainer(key: string, container_type: "Map"): LoroMap;
94
+ setContainer(key: string, container_type: "List"): LoroList;
95
+ setContainer(key: string, container_type: "Text"): LoroText;
96
+ setContainer(key: string, container_type: "Tree"): LoroTree;
97
+ setContainer(key: string, container_type: string): never;
79
98
  get(key: string): Value;
80
- getTyped<Key extends (keyof T) & string>(txn: Loro, key: Key): T[Key];
81
- set(txn: Transaction | Loro, key: string, value: Value | Prelim): void;
82
- setTyped<Key extends (keyof T) & string>(txn: Transaction | Loro, key: Key, value: T[Key]): void;
83
- delete(txn: Transaction | Loro, key: string): void;
84
- subscribe(txn: Transaction | Loro, listener: Listener): number;
85
- subscribeDeep(txn: Transaction | Loro, listener: Listener): number;
86
- subscribeOnce(txn: Transaction | Loro, listener: Listener): number;
99
+ getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];
100
+ set(key: string, value: Value | Prelim): void;
101
+ setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): void;
102
+ delete(key: string): void;
103
+ subscribe(txn: Loro, listener: Listener): number;
87
104
  }
88
105
  interface LoroText {
89
- insert(txn: Transaction | Loro, pos: number, text: string): void;
90
- delete(txn: Transaction | Loro, pos: number, len: number): void;
91
- subscribe(txn: Transaction | Loro, listener: Listener): number;
92
- subscribeDeep(txn: Transaction | Loro, listener: Listener): number;
93
- subscribeOnce(txn: Transaction | Loro, listener: Listener): number;
106
+ insert(pos: number, text: string): void;
107
+ delete(pos: number, len: number): void;
108
+ subscribe(txn: Loro, listener: Listener): number;
94
109
  }
95
110
  }
96
111
 
97
- export { Delta, Diff, ListDiff, LoroEvent, MapDiff, Path, Prelim, TextDiff, Value, isContainerId };
112
+ export { Diff, ListDiff, LoroEvent, MapDiff, Path, Prelim, TextDiff, TreeDiff, Value, isContainerId };
package/dist/loro.js CHANGED
@@ -2,18 +2,12 @@
2
2
 
3
3
  var loroWasm = require('loro-wasm');
4
4
 
5
- loroWasm.Loro.prototype.transact = function(cb, origin) {
6
- this.__raw__transactionWithOrigin(origin, (txn) => {
7
- try {
8
- cb(txn);
9
- } finally {
10
- txn.commit();
11
- txn.free();
12
- }
13
- });
5
+ loroWasm.Loro.prototype.getTypedMap = function(...args) {
6
+ return this.getMap(...args);
7
+ };
8
+ loroWasm.Loro.prototype.getTypedList = function(...args) {
9
+ return this.getList(...args);
14
10
  };
15
- loroWasm.Loro.prototype.getTypedMap = loroWasm.Loro.prototype.getMap;
16
- loroWasm.Loro.prototype.getTypedList = loroWasm.Loro.prototype.getList;
17
11
  loroWasm.LoroList.prototype.getTyped = function(loro, index) {
18
12
  const value = this.get(index);
19
13
  if (typeof value === "string" && isContainerId(value)) {
@@ -22,7 +16,9 @@ loroWasm.LoroList.prototype.getTyped = function(loro, index) {
22
16
  return value;
23
17
  }
24
18
  };
25
- loroWasm.LoroList.prototype.insertTyped = loroWasm.LoroList.prototype.insert;
19
+ loroWasm.LoroList.prototype.insertTyped = function(...args) {
20
+ return this.insert(...args);
21
+ };
26
22
  loroWasm.LoroMap.prototype.getTyped = function(loro, key) {
27
23
  const value = this.get(key);
28
24
  if (typeof value === "string" && isContainerId(value)) {
@@ -31,107 +27,22 @@ loroWasm.LoroMap.prototype.getTyped = function(loro, key) {
31
27
  return value;
32
28
  }
33
29
  };
34
- loroWasm.LoroMap.prototype.setTyped = loroWasm.LoroMap.prototype.set;
35
- loroWasm.LoroText.prototype.insert = function(txn, pos, text) {
36
- if (txn instanceof loroWasm.Loro) {
37
- this.__loro_insert(txn, pos, text);
38
- } else {
39
- this.__txn_insert(txn, pos, text);
40
- }
41
- };
42
- loroWasm.LoroText.prototype.delete = function(txn, pos, len) {
43
- if (txn instanceof loroWasm.Loro) {
44
- this.__loro_delete(txn, pos, len);
45
- } else {
46
- this.__txn_delete(txn, pos, len);
47
- }
48
- };
49
- loroWasm.LoroList.prototype.insert = function(txn, pos, len) {
50
- if (txn instanceof loroWasm.Loro) {
51
- this.__loro_insert(txn, pos, len);
52
- } else {
53
- this.__txn_insert(txn, pos, len);
54
- }
55
- };
56
- loroWasm.LoroList.prototype.delete = function(txn, pos, len) {
57
- if (txn instanceof loroWasm.Loro) {
58
- this.__loro_delete(txn, pos, len);
59
- } else {
60
- this.__txn_delete(txn, pos, len);
61
- }
62
- };
63
- loroWasm.LoroMap.prototype.set = function(txn, key, value) {
64
- if (txn instanceof loroWasm.Loro) {
65
- this.__loro_insert(txn, key, value);
66
- } else {
67
- this.__txn_insert(txn, key, value);
68
- }
69
- };
70
- loroWasm.LoroMap.prototype.delete = function(txn, key) {
71
- if (txn instanceof loroWasm.Loro) {
72
- this.__loro_delete(txn, key);
73
- } else {
74
- this.__txn_delete(txn, key);
75
- }
30
+ loroWasm.LoroMap.prototype.setTyped = function(...args) {
31
+ return this.set(...args);
76
32
  };
77
- const CONTAINER_TYPES = ["Map", "Text", "List"];
78
33
  function isContainerId(s) {
79
- try {
80
- if (s.startsWith("/")) {
81
- const [_, type] = s.slice(1).split(":");
82
- if (!CONTAINER_TYPES.includes(type)) {
83
- return false;
84
- }
85
- } else {
86
- const [id, type] = s.split(":");
87
- if (!CONTAINER_TYPES.includes(type)) {
88
- return false;
89
- }
90
- const [counter, client] = id.split("@");
91
- Number.parseInt(counter);
92
- Number.parseInt(client);
93
- }
94
- return true;
95
- } catch (e) {
96
- return false;
97
- }
34
+ return s.startsWith("cid:");
98
35
  }
99
36
 
100
37
  Object.defineProperty(exports, 'Loro', {
101
38
  enumerable: true,
102
39
  get: function () { return loroWasm.Loro; }
103
40
  });
104
- Object.defineProperty(exports, 'LoroList', {
105
- enumerable: true,
106
- get: function () { return loroWasm.LoroList; }
107
- });
108
- Object.defineProperty(exports, 'LoroMap', {
109
- enumerable: true,
110
- get: function () { return loroWasm.LoroMap; }
111
- });
112
- Object.defineProperty(exports, 'LoroText', {
113
- enumerable: true,
114
- get: function () { return loroWasm.LoroText; }
115
- });
116
- Object.defineProperty(exports, 'PrelimList', {
117
- enumerable: true,
118
- get: function () { return loroWasm.PrelimList; }
119
- });
120
- Object.defineProperty(exports, 'PrelimMap', {
121
- enumerable: true,
122
- get: function () { return loroWasm.PrelimMap; }
123
- });
124
- Object.defineProperty(exports, 'PrelimText', {
125
- enumerable: true,
126
- get: function () { return loroWasm.PrelimText; }
127
- });
128
- Object.defineProperty(exports, 'Transaction', {
129
- enumerable: true,
130
- get: function () { return loroWasm.Transaction; }
131
- });
132
- Object.defineProperty(exports, 'setPanicHook', {
133
- enumerable: true,
134
- get: function () { return loroWasm.setPanicHook; }
135
- });
136
41
  exports.isContainerId = isContainerId;
42
+ Object.keys(loroWasm).forEach(function (k) {
43
+ if (k !== 'default' && !exports.hasOwnProperty(k)) Object.defineProperty(exports, k, {
44
+ enumerable: true,
45
+ get: function () { return loroWasm[k]; }
46
+ });
47
+ });
137
48
  //# sourceMappingURL=loro.js.map
package/dist/loro.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"loro.js","sources":["../src/index.ts"],"sourcesContent":["export {\n LoroList,\n LoroMap,\n LoroText,\n PrelimList,\n PrelimMap,\n PrelimText,\n setPanicHook,\n Transaction,\n} from \"loro-wasm\";\nimport { PrelimMap } from \"loro-wasm\";\nimport { PrelimText } from \"loro-wasm\";\nimport { PrelimList } from \"loro-wasm\";\nimport {\n ContainerID,\n Loro,\n LoroList,\n LoroMap,\n LoroText,\n Transaction,\n} from \"loro-wasm\";\n\nexport type { ContainerID, ContainerType } from \"loro-wasm\";\n\nLoro.prototype.transact = function (cb, origin) {\n this.__raw__transactionWithOrigin(origin, (txn: Transaction) => {\n try {\n cb(txn);\n } finally {\n txn.commit();\n txn.free();\n }\n });\n};\n\nLoro.prototype.getTypedMap = Loro.prototype.getMap;\nLoro.prototype.getTypedList = Loro.prototype.getList;\nLoroList.prototype.getTyped = function (loro, index) {\n const value = this.get(index);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroList.prototype.insertTyped = LoroList.prototype.insert;\nLoroMap.prototype.getTyped = function (loro, key) {\n const value = this.get(key);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroMap.prototype.setTyped = LoroMap.prototype.set;\n\nLoroText.prototype.insert = function (txn, pos, text) {\n if (txn instanceof Loro) {\n this.__loro_insert(txn, pos, text);\n } else {\n this.__txn_insert(txn, pos, text);\n }\n};\n\nLoroText.prototype.delete = function (txn, pos, len) {\n if (txn instanceof Loro) {\n this.__loro_delete(txn, pos, len);\n } else {\n this.__txn_delete(txn, pos, len);\n }\n};\n\nLoroList.prototype.insert = function (txn, pos, len) {\n if (txn instanceof Loro) {\n this.__loro_insert(txn, pos, len);\n } else {\n this.__txn_insert(txn, pos, len);\n }\n};\n\nLoroList.prototype.delete = function (txn, pos, len) {\n if (txn instanceof Loro) {\n this.__loro_delete(txn, pos, len);\n } else {\n this.__txn_delete(txn, pos, len);\n }\n};\n\nLoroMap.prototype.set = function (txn, key, value) {\n if (txn instanceof Loro) {\n this.__loro_insert(txn, key, value);\n } else {\n this.__txn_insert(txn, key, value);\n }\n};\n\nLoroMap.prototype.delete = function (txn, key) {\n if (txn instanceof Loro) {\n this.__loro_delete(txn, key);\n } else {\n this.__txn_delete(txn, key);\n }\n};\n\nexport type Value =\n | ContainerID\n | string\n | number\n | null\n | { [key: string]: Value }\n | Value[];\n\nexport type Prelim = PrelimList | PrelimMap | PrelimText;\n\nexport type Path = (number | string)[];\nexport type Delta<T> = {\n type: \"insert\";\n value: T;\n} | {\n type: \"delete\";\n len: number;\n} | {\n type: \"retain\";\n len: number;\n};\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<Value[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n diff: {\n added: Record<string, Value>;\n deleted: Record<string, Value>;\n updated: Record<string, {\n old: Value;\n new: Value;\n }>;\n };\n};\n\nexport type Diff = ListDiff | TextDiff | MapDiff;\n\nexport interface LoroEvent {\n local: boolean;\n origin?: string;\n diff: Diff;\n target: ContainerID;\n path: Path;\n}\n\ninterface Listener {\n (event: LoroEvent): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n try {\n if (s.startsWith(\"/\")) {\n const [_, type] = s.slice(1).split(\":\");\n if (!CONTAINER_TYPES.includes(type)) {\n return false;\n }\n } else {\n const [id, type] = s.split(\":\");\n if (!CONTAINER_TYPES.includes(type)) {\n return false;\n }\n\n const [counter, client] = id.split(\"@\");\n Number.parseInt(counter);\n Number.parseInt(client);\n }\n\n return true;\n } catch (e) {\n return false;\n }\n}\n\nexport { Loro };\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n transact(f: (tx: Transaction) => void, origin?: string): void;\n }\n\n interface Loro<T extends Record<string, any> = Record<string, any>> {\n getTypedMap<Key extends (keyof T) & string>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : never;\n getTypedList<Key extends (keyof T) & string>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : never;\n }\n\n interface LoroList<T extends any[] = any[]> {\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: \"Map\",\n ): LoroMap;\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: \"List\",\n ): LoroList;\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: \"Text\",\n ): LoroText;\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: string,\n ): never;\n\n get(index: number): Value;\n getTyped<Key extends (keyof T) & number>(loro: Loro, index: Key): T[Key];\n insertTyped<Key extends (keyof T) & number>(\n txn: Transaction | Loro,\n pos: Key,\n value: T[Key],\n ): void;\n insert(txn: Transaction | Loro, pos: number, value: Value | Prelim): void;\n delete(txn: Transaction | Loro, pos: number, len: number): void;\n subscribe(txn: Transaction | Loro, listener: Listener): number;\n subscribeDeep(txn: Transaction | Loro, listener: Listener): number;\n subscribeOnce(txn: Transaction | Loro, listener: Listener): number;\n }\n\n interface LoroMap<T extends Record<string, any> = Record<string, any>> {\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: \"Map\",\n ): LoroMap;\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: \"List\",\n ): LoroList;\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: \"Text\",\n ): LoroText;\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: string,\n ): never;\n\n get(key: string): Value;\n getTyped<Key extends (keyof T) & string>(\n txn: Loro,\n key: Key,\n ): T[Key];\n set(txn: Transaction | Loro, key: string, value: Value | Prelim): void;\n setTyped<Key extends (keyof T) & string>(\n txn: Transaction | Loro,\n key: Key,\n value: T[Key],\n ): void;\n delete(txn: Transaction | Loro, key: string): void;\n subscribe(txn: Transaction | Loro, listener: Listener): number;\n subscribeDeep(txn: Transaction | Loro, listener: Listener): number;\n subscribeOnce(txn: Transaction | Loro, listener: Listener): number;\n }\n\n interface LoroText {\n insert(txn: Transaction | Loro, pos: number, text: string): void;\n delete(txn: Transaction | Loro, pos: number, len: number): void;\n subscribe(txn: Transaction | Loro, listener: Listener): number;\n subscribeDeep(txn: Transaction | Loro, listener: Listener): number;\n subscribeOnce(txn: Transaction | Loro, listener: Listener): number;\n }\n}\n"],"names":["Loro","LoroList","LoroMap","LoroText"],"mappings":";;;;AAwBAA,aAAA,CAAK,SAAU,CAAA,QAAA,GAAW,SAAU,EAAA,EAAI,MAAQ,EAAA;AAC9C,EAAK,IAAA,CAAA,4BAAA,CAA6B,MAAQ,EAAA,CAAC,GAAqB,KAAA;AAC9D,IAAI,IAAA;AACF,MAAA,EAAA,CAAG,GAAG,CAAA,CAAA;AAAA,KACN,SAAA;AACA,MAAA,GAAA,CAAI,MAAO,EAAA,CAAA;AACX,MAAA,GAAA,CAAI,IAAK,EAAA,CAAA;AAAA,KACX;AAAA,GACD,CAAA,CAAA;AACH,CAAA,CAAA;AAEAA,aAAK,CAAA,SAAA,CAAU,WAAc,GAAAA,aAAA,CAAK,SAAU,CAAA,MAAA,CAAA;AAC5CA,aAAK,CAAA,SAAA,CAAU,YAAe,GAAAA,aAAA,CAAK,SAAU,CAAA,OAAA,CAAA;AAC7CC,iBAAAA,CAAS,SAAU,CAAA,Q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;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"loro.js","sources":["../src/index.ts"],"sourcesContent":["export * from \"loro-wasm\";\nimport { Delta } from \"loro-wasm\";\nimport { PrelimText,PrelimList,PrelimMap } from \"loro-wasm\";\nimport { ContainerID, Loro, LoroList, LoroMap, LoroText , LoroTree, TreeID} from \"loro-wasm\";\n\nLoro.prototype.getTypedMap = function (...args) {\n return this.getMap(...args);\n};\nLoro.prototype.getTypedList = function (...args) {\n return this.getList(...args);\n};\nLoroList.prototype.getTyped = function (loro, index) {\n const value = this.get(index);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroList.prototype.insertTyped = function (...args) {\n return this.insert(...args);\n};\nLoroMap.prototype.getTyped = function (loro, key) {\n const value = this.get(key);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroMap.prototype.setTyped = function (...args) {\n return this.set(...args);\n};\n\n/**\n * Data types supported by loro\n */\nexport type Value =\n | ContainerID\n | string\n | number\n | boolean\n | null\n | { [key: string]: Value }\n | Uint8Array\n | Value[];\n\n\nexport type Prelim = PrelimList | PrelimMap | PrelimText;\n\n/**\n * Represents a path to identify the exact location of an event's target.\n * The path is composed of numbers (e.g., indices of a list container) and strings \n * (e.g., keys of a map container), indicating the absolute position of the event's source\n * within a loro document.\n */\nexport type Path = (number | string)[];\n\n/**\n * The event of Loro.\n * @prop local - Indicates whether the event is local.\n * @prop origin - (Optional) Provides information about the origin of the event.\n * @prop diff - Contains the differential information related to the event.\n * @prop target - Identifies the container ID of the event's target.\n * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\nexport interface LoroEvent {\n local: boolean;\n origin?: string;\n /**\n * If true, this event was triggered by a child container.\n */\n fromChildren: boolean;\n /**\n * If true, this event was triggered by a checkout.\n */\n fromCheckout: boolean;\n diff: Diff;\n target: ContainerID;\n path: Path;\n}\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<Value[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n updated: Record<string, Value | undefined>;\n};\n\nexport type TreeDiff = {\n type: \"tree\";\n diff: {target: TreeID, action: \"create\"|\"delete\" } | {target: TreeID; action:\"move\"; parent: TreeID};\n}\n\nexport type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;\n\ninterface Listener {\n (event: LoroEvent): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\", \"Tree\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n return s.startsWith(\"cid:\");\n}\n\nexport { Loro };\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n }\n\n interface Loro<T extends Record<string, any> = Record<string, any>> {\n getTypedMap<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : never;\n getTypedList<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : never;\n }\n\n interface LoroList<T extends any[] = any[]> {\n insertContainer(pos: number, container: \"Map\"): LoroMap;\n insertContainer(pos: number, container: \"List\"): LoroList;\n insertContainer(pos: number, container: \"Text\"): LoroText;\n insertContainer(pos: number, container: \"Tree\"): LoroTree;\n insertContainer(pos: number, container: string): never;\n\n get(index: number): Value;\n getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];\n insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;\n insert(pos: number, value: Value | Prelim): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroMap<T extends Record<string, any> = Record<string, any>> {\n setContainer(key: string, container_type: \"Map\"): LoroMap;\n setContainer(key: string, container_type: \"List\"): LoroList;\n setContainer(key: string, container_type: \"Text\"): LoroText;\n setContainer(key: string, container_type: \"Tree\"): LoroTree;\n setContainer(key: string, container_type: string): never;\n\n get(key: string): Value;\n getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];\n set(key: string, value: Value | Prelim): void;\n setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): void;\n delete(key: string): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroText {\n insert(pos: number, text: string): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n}\n"],"names":["Loro","LoroList","LoroMap"],"mappings":";;;;AAKAA,aAAK,CAAA,SAAA,CAAU,WAAc,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,MAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAC5B,CAAA,CAAA;AACAA,aAAK,CAAA,SAAA,CAAU,YAAe,GAAA,SAAA,GAAa,IAAM,EAAA;AAC/C,EAAO,OAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,IAAI,CAAA,CAAA;AAC7B,CAAA,CAAA;AACAC,iBAAA,CAAS,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,KAAO,EAAA;AACnD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,KAAK,CAAA,CAAA;AAC5B,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA;AACL,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA,CAAA;AACAA,iBAAS,CAAA,SAAA,CAAU,WAAc,GAAA,SAAA,GAAa,IAAM,EAAA;AAClD,EAAO,OAAA,IAAA,CAAK,MAAO,CAAA,GAAG,IAAI,CAAA,CAAA;AAC5B,CAAA,CAAA;AACAC,gBAAA,CAAQ,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,GAAK,EAAA;AAChD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,GAAG,CAAA,CAAA;AAC1B,EAAA,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA;AACL,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF,CAAA,CAAA;AACAA,gBAAQ,CAAA,SAAA,CAAU,QAAW,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,GAAI,CAAA,GAAG,IAAI,CAAA,CAAA;AACzB,CAAA,CAAA;AA8EO,SAAS,cAAc,CAA6B,EAAA;AACzD,EAAO,OAAA,CAAA,CAAE,WAAW,MAAM,CAAA,CAAA;AAC5B;;;;;;;;;;;;;;"}
package/dist/loro.mjs CHANGED
@@ -1,18 +1,13 @@
1
- import { Loro, LoroList, LoroMap, LoroText } from 'loro-wasm';
2
- export { Loro, LoroList, LoroMap, LoroText, PrelimList, PrelimMap, PrelimText, Transaction, setPanicHook } from 'loro-wasm';
1
+ import { Loro, LoroList, LoroMap } from 'loro-wasm';
2
+ export * from 'loro-wasm';
3
+ export { Loro } from 'loro-wasm';
3
4
 
4
- Loro.prototype.transact = function(cb, origin) {
5
- this.__raw__transactionWithOrigin(origin, (txn) => {
6
- try {
7
- cb(txn);
8
- } finally {
9
- txn.commit();
10
- txn.free();
11
- }
12
- });
5
+ Loro.prototype.getTypedMap = function(...args) {
6
+ return this.getMap(...args);
7
+ };
8
+ Loro.prototype.getTypedList = function(...args) {
9
+ return this.getList(...args);
13
10
  };
14
- Loro.prototype.getTypedMap = Loro.prototype.getMap;
15
- Loro.prototype.getTypedList = Loro.prototype.getList;
16
11
  LoroList.prototype.getTyped = function(loro, index) {
17
12
  const value = this.get(index);
18
13
  if (typeof value === "string" && isContainerId(value)) {
@@ -21,7 +16,9 @@ LoroList.prototype.getTyped = function(loro, index) {
21
16
  return value;
22
17
  }
23
18
  };
24
- LoroList.prototype.insertTyped = LoroList.prototype.insert;
19
+ LoroList.prototype.insertTyped = function(...args) {
20
+ return this.insert(...args);
21
+ };
25
22
  LoroMap.prototype.getTyped = function(loro, key) {
26
23
  const value = this.get(key);
27
24
  if (typeof value === "string" && isContainerId(value)) {
@@ -30,70 +27,11 @@ LoroMap.prototype.getTyped = function(loro, key) {
30
27
  return value;
31
28
  }
32
29
  };
33
- LoroMap.prototype.setTyped = LoroMap.prototype.set;
34
- LoroText.prototype.insert = function(txn, pos, text) {
35
- if (txn instanceof Loro) {
36
- this.__loro_insert(txn, pos, text);
37
- } else {
38
- this.__txn_insert(txn, pos, text);
39
- }
40
- };
41
- LoroText.prototype.delete = function(txn, pos, len) {
42
- if (txn instanceof Loro) {
43
- this.__loro_delete(txn, pos, len);
44
- } else {
45
- this.__txn_delete(txn, pos, len);
46
- }
47
- };
48
- LoroList.prototype.insert = function(txn, pos, len) {
49
- if (txn instanceof Loro) {
50
- this.__loro_insert(txn, pos, len);
51
- } else {
52
- this.__txn_insert(txn, pos, len);
53
- }
54
- };
55
- LoroList.prototype.delete = function(txn, pos, len) {
56
- if (txn instanceof Loro) {
57
- this.__loro_delete(txn, pos, len);
58
- } else {
59
- this.__txn_delete(txn, pos, len);
60
- }
61
- };
62
- LoroMap.prototype.set = function(txn, key, value) {
63
- if (txn instanceof Loro) {
64
- this.__loro_insert(txn, key, value);
65
- } else {
66
- this.__txn_insert(txn, key, value);
67
- }
68
- };
69
- LoroMap.prototype.delete = function(txn, key) {
70
- if (txn instanceof Loro) {
71
- this.__loro_delete(txn, key);
72
- } else {
73
- this.__txn_delete(txn, key);
74
- }
30
+ LoroMap.prototype.setTyped = function(...args) {
31
+ return this.set(...args);
75
32
  };
76
- const CONTAINER_TYPES = ["Map", "Text", "List"];
77
33
  function isContainerId(s) {
78
- try {
79
- if (s.startsWith("/")) {
80
- const [_, type] = s.slice(1).split(":");
81
- if (!CONTAINER_TYPES.includes(type)) {
82
- return false;
83
- }
84
- } else {
85
- const [id, type] = s.split(":");
86
- if (!CONTAINER_TYPES.includes(type)) {
87
- return false;
88
- }
89
- const [counter, client] = id.split("@");
90
- Number.parseInt(counter);
91
- Number.parseInt(client);
92
- }
93
- return true;
94
- } catch (e) {
95
- return false;
96
- }
34
+ return s.startsWith("cid:");
97
35
  }
98
36
 
99
37
  export { isContainerId };
package/dist/loro.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"loro.mjs","sources":["../src/index.ts"],"sourcesContent":["export {\n LoroList,\n LoroMap,\n LoroText,\n PrelimList,\n PrelimMap,\n PrelimText,\n setPanicHook,\n Transaction,\n} from \"loro-wasm\";\nimport { PrelimMap } from \"loro-wasm\";\nimport { PrelimText } from \"loro-wasm\";\nimport { PrelimList } from \"loro-wasm\";\nimport {\n ContainerID,\n Loro,\n LoroList,\n LoroMap,\n LoroText,\n Transaction,\n} from \"loro-wasm\";\n\nexport type { ContainerID, ContainerType } from \"loro-wasm\";\n\nLoro.prototype.transact = function (cb, origin) {\n this.__raw__transactionWithOrigin(origin, (txn: Transaction) => {\n try {\n cb(txn);\n } finally {\n txn.commit();\n txn.free();\n }\n });\n};\n\nLoro.prototype.getTypedMap = Loro.prototype.getMap;\nLoro.prototype.getTypedList = Loro.prototype.getList;\nLoroList.prototype.getTyped = function (loro, index) {\n const value = this.get(index);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroList.prototype.insertTyped = LoroList.prototype.insert;\nLoroMap.prototype.getTyped = function (loro, key) {\n const value = this.get(key);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroMap.prototype.setTyped = LoroMap.prototype.set;\n\nLoroText.prototype.insert = function (txn, pos, text) {\n if (txn instanceof Loro) {\n this.__loro_insert(txn, pos, text);\n } else {\n this.__txn_insert(txn, pos, text);\n }\n};\n\nLoroText.prototype.delete = function (txn, pos, len) {\n if (txn instanceof Loro) {\n this.__loro_delete(txn, pos, len);\n } else {\n this.__txn_delete(txn, pos, len);\n }\n};\n\nLoroList.prototype.insert = function (txn, pos, len) {\n if (txn instanceof Loro) {\n this.__loro_insert(txn, pos, len);\n } else {\n this.__txn_insert(txn, pos, len);\n }\n};\n\nLoroList.prototype.delete = function (txn, pos, len) {\n if (txn instanceof Loro) {\n this.__loro_delete(txn, pos, len);\n } else {\n this.__txn_delete(txn, pos, len);\n }\n};\n\nLoroMap.prototype.set = function (txn, key, value) {\n if (txn instanceof Loro) {\n this.__loro_insert(txn, key, value);\n } else {\n this.__txn_insert(txn, key, value);\n }\n};\n\nLoroMap.prototype.delete = function (txn, key) {\n if (txn instanceof Loro) {\n this.__loro_delete(txn, key);\n } else {\n this.__txn_delete(txn, key);\n }\n};\n\nexport type Value =\n | ContainerID\n | string\n | number\n | null\n | { [key: string]: Value }\n | Value[];\n\nexport type Prelim = PrelimList | PrelimMap | PrelimText;\n\nexport type Path = (number | string)[];\nexport type Delta<T> = {\n type: \"insert\";\n value: T;\n} | {\n type: \"delete\";\n len: number;\n} | {\n type: \"retain\";\n len: number;\n};\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<Value[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n diff: {\n added: Record<string, Value>;\n deleted: Record<string, Value>;\n updated: Record<string, {\n old: Value;\n new: Value;\n }>;\n };\n};\n\nexport type Diff = ListDiff | TextDiff | MapDiff;\n\nexport interface LoroEvent {\n local: boolean;\n origin?: string;\n diff: Diff;\n target: ContainerID;\n path: Path;\n}\n\ninterface Listener {\n (event: LoroEvent): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n try {\n if (s.startsWith(\"/\")) {\n const [_, type] = s.slice(1).split(\":\");\n if (!CONTAINER_TYPES.includes(type)) {\n return false;\n }\n } else {\n const [id, type] = s.split(\":\");\n if (!CONTAINER_TYPES.includes(type)) {\n return false;\n }\n\n const [counter, client] = id.split(\"@\");\n Number.parseInt(counter);\n Number.parseInt(client);\n }\n\n return true;\n } catch (e) {\n return false;\n }\n}\n\nexport { Loro };\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n transact(f: (tx: Transaction) => void, origin?: string): void;\n }\n\n interface Loro<T extends Record<string, any> = Record<string, any>> {\n getTypedMap<Key extends (keyof T) & string>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : never;\n getTypedList<Key extends (keyof T) & string>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : never;\n }\n\n interface LoroList<T extends any[] = any[]> {\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: \"Map\",\n ): LoroMap;\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: \"List\",\n ): LoroList;\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: \"Text\",\n ): LoroText;\n insertContainer(\n txn: Transaction | Loro,\n pos: number,\n container: string,\n ): never;\n\n get(index: number): Value;\n getTyped<Key extends (keyof T) & number>(loro: Loro, index: Key): T[Key];\n insertTyped<Key extends (keyof T) & number>(\n txn: Transaction | Loro,\n pos: Key,\n value: T[Key],\n ): void;\n insert(txn: Transaction | Loro, pos: number, value: Value | Prelim): void;\n delete(txn: Transaction | Loro, pos: number, len: number): void;\n subscribe(txn: Transaction | Loro, listener: Listener): number;\n subscribeDeep(txn: Transaction | Loro, listener: Listener): number;\n subscribeOnce(txn: Transaction | Loro, listener: Listener): number;\n }\n\n interface LoroMap<T extends Record<string, any> = Record<string, any>> {\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: \"Map\",\n ): LoroMap;\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: \"List\",\n ): LoroList;\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: \"Text\",\n ): LoroText;\n insertContainer(\n txn: Transaction | Loro,\n key: string,\n container_type: string,\n ): never;\n\n get(key: string): Value;\n getTyped<Key extends (keyof T) & string>(\n txn: Loro,\n key: Key,\n ): T[Key];\n set(txn: Transaction | Loro, key: string, value: Value | Prelim): void;\n setTyped<Key extends (keyof T) & string>(\n txn: Transaction | Loro,\n key: Key,\n value: T[Key],\n ): void;\n delete(txn: Transaction | Loro, key: string): void;\n subscribe(txn: Transaction | Loro, listener: Listener): number;\n subscribeDeep(txn: Transaction | Loro, listener: Listener): number;\n subscribeOnce(txn: Transaction | Loro, listener: Listener): number;\n }\n\n interface LoroText {\n insert(txn: Transaction | Loro, pos: number, text: string): void;\n delete(txn: Transaction | Loro, pos: number, len: number): void;\n subscribe(txn: Transaction | Loro, listener: Listener): number;\n subscribeDeep(txn: Transaction | Loro, listener: Listener): number;\n subscribeOnce(txn: Transaction | Loro, listener: Listener): number;\n }\n}\n"],"names":["LoroList","LoroMap","LoroText"],"mappings":";;;AAwBA,IAAA,CAAK,SAAU,CAAA,QAAA,GAAW,SAAU,EAAA,EAAI,MAAQ,EAAA;AAC9C,EAAK,IAAA,CAAA,4BAAA,CAA6B,MAAQ,EAAA,CAAC,GAAqB,KAAA;AAC9D,IAAI,IAAA;AACF,MAAA,EAAA,CAAG,GAAG,CAAA,CAAA;AAAA,KACN,SAAA;AACA,MAAA,GAAA,CAAI,MAAO,EAAA,CAAA;AACX,MAAA,GAAA,CAAI,IAAK,EAAA,CAAA;AAAA,KACX;AAAA,GACD,CAAA,CAAA;AACH,CAAA,CAAA;AAEA,IAAK,CAAA,SAAA,CAAU,WAAc,GAAA,IAAA,CAAK,SAAU,CAAA,MAAA,CAAA;AAC5C,IAAK,CAAA,SAAA,CAAU,YAAe,GAAA,IAAA,CAAK,SAAU,CAAA,OAAA,CAAA;AAC7CA,QAAAA,CAAS,SAAU,CAAA,QAAA,GAAW,SAAU,IAAA,EAAM,KAAO,EAAA;AACnD,EAAM,MAAA,KAAA,GAAQ,IAAK,CAAA,GAAA,CAAI,KAAK,CAAA,CAAA;AAC5B,EAAA,I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aAAA,CAAc,KAAK,GAAG,CAAA,CAAA;AAAA,GACtB,MAAA;AACL,IAAK,IAAA,CAAA,YAAA,CAAa,KAAK,GAAG,CAAA,CAAA;AAAA,GAC5B;AACF,CAAA,CAAA;AA4DA,MAAM,eAAkB,GAAA,CAAC,KAAO,EAAA,MAAA,EAAQ,MAAM,CAAA,CAAA;AAEvC,SAAS,cAAc,CAA6B,EAAA;AACzD,EAAI,IAAA;AACF,IAAI,IAAA,CAAA,CAAE,UAAW,CAAA,GAAG,CAAG,EAAA;AACrB,MAAM,MAAA,CAAC,GAAG,IAAI,CAAA,GAAI,EAAE,KAAM,CAAA,CAAC,CAAE,CAAA,KAAA,CAAM,GAAG,CAAA,CAAA;AACtC,MAAA,IAAI,CAAC,eAAA,CAAgB,QAAS,CAAA,IAAI,CAAG,EAAA;AACnC,QAAO,OAAA,KAAA,CAAA;AAAA,OACT;AAAA,KACK,MAAA;AACL,MAAA,MAAM,CAAC,EAAI,EAAA,IAAI,CAAI,GAAA,CAAA,CAAE,MAAM,GAAG,CAAA,CAAA;AAC9B,MAAA,IAAI,CAAC,eAAA,CAAgB,QAAS,CAAA,IAAI,CAAG,EAAA;AACnC,QAAO,OAAA,KAAA,CAAA;AAAA,OACT;AAEA,MAAA,MAAM,CAAC,OAAS,EAAA,MAAM,CAAI,GAAA,EAAA,CAAG,MAAM,GAAG,CAAA,CAAA;AACtC,MAAA,MAAA,CAAO,SAAS,OAAO,CAAA,CAAA;AACvB,MAAA,MAAA,CAAO,SAAS,MAAM,CAAA,CAAA;AAAA,KACxB;AAEA,IAAO,OAAA,IAAA,CAAA;AAAA,WACA,CAAP,EAAA;AACA,IAAO,OAAA,KAAA,CAAA;AAAA,GACT;AACF;;;;"}
1
+ {"version":3,"file":"loro.mjs","sources":["../src/index.ts"],"sourcesContent":["export * from \"loro-wasm\";\nimport { Delta } from \"loro-wasm\";\nimport { PrelimText,PrelimList,PrelimMap } from \"loro-wasm\";\nimport { ContainerID, Loro, LoroList, LoroMap, LoroText , LoroTree, TreeID} from \"loro-wasm\";\n\nLoro.prototype.getTypedMap = function (...args) {\n return this.getMap(...args);\n};\nLoro.prototype.getTypedList = function (...args) {\n return this.getList(...args);\n};\nLoroList.prototype.getTyped = function (loro, index) {\n const value = this.get(index);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroList.prototype.insertTyped = function (...args) {\n return this.insert(...args);\n};\nLoroMap.prototype.getTyped = function (loro, key) {\n const value = this.get(key);\n if (typeof value === \"string\" && isContainerId(value)) {\n return loro.getContainerById(value);\n } else {\n return value;\n }\n};\nLoroMap.prototype.setTyped = function (...args) {\n return this.set(...args);\n};\n\n/**\n * Data types supported by loro\n */\nexport type Value =\n | ContainerID\n | string\n | number\n | boolean\n | null\n | { [key: string]: Value }\n | Uint8Array\n | Value[];\n\n\nexport type Prelim = PrelimList | PrelimMap | PrelimText;\n\n/**\n * Represents a path to identify the exact location of an event's target.\n * The path is composed of numbers (e.g., indices of a list container) and strings \n * (e.g., keys of a map container), indicating the absolute position of the event's source\n * within a loro document.\n */\nexport type Path = (number | string)[];\n\n/**\n * The event of Loro.\n * @prop local - Indicates whether the event is local.\n * @prop origin - (Optional) Provides information about the origin of the event.\n * @prop diff - Contains the differential information related to the event.\n * @prop target - Identifies the container ID of the event's target.\n * @prop path - Specifies the absolute path of the event's emitter, which can be an index of a list container or a key of a map container.\n */\nexport interface LoroEvent {\n local: boolean;\n origin?: string;\n /**\n * If true, this event was triggered by a child container.\n */\n fromChildren: boolean;\n /**\n * If true, this event was triggered by a checkout.\n */\n fromCheckout: boolean;\n diff: Diff;\n target: ContainerID;\n path: Path;\n}\n\nexport type ListDiff = {\n type: \"list\";\n diff: Delta<Value[]>[];\n};\n\nexport type TextDiff = {\n type: \"text\";\n diff: Delta<string>[];\n};\n\nexport type MapDiff = {\n type: \"map\";\n updated: Record<string, Value | undefined>;\n};\n\nexport type TreeDiff = {\n type: \"tree\";\n diff: {target: TreeID, action: \"create\"|\"delete\" } | {target: TreeID; action:\"move\"; parent: TreeID};\n}\n\nexport type Diff = ListDiff | TextDiff | MapDiff | TreeDiff;\n\ninterface Listener {\n (event: LoroEvent): void;\n}\n\nconst CONTAINER_TYPES = [\"Map\", \"Text\", \"List\", \"Tree\"];\n\nexport function isContainerId(s: string): s is ContainerID {\n return s.startsWith(\"cid:\");\n}\n\nexport { Loro };\n\ndeclare module \"loro-wasm\" {\n interface Loro {\n subscribe(listener: Listener): number;\n }\n\n interface Loro<T extends Record<string, any> = Record<string, any>> {\n getTypedMap<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroMap ? T[Key] : never;\n getTypedList<Key extends keyof T & string>(\n name: Key,\n ): T[Key] extends LoroList ? T[Key] : never;\n }\n\n interface LoroList<T extends any[] = any[]> {\n insertContainer(pos: number, container: \"Map\"): LoroMap;\n insertContainer(pos: number, container: \"List\"): LoroList;\n insertContainer(pos: number, container: \"Text\"): LoroText;\n insertContainer(pos: number, container: \"Tree\"): LoroTree;\n insertContainer(pos: number, container: string): never;\n\n get(index: number): Value;\n getTyped<Key extends keyof T & number>(loro: Loro, index: Key): T[Key];\n insertTyped<Key extends keyof T & number>(pos: Key, value: T[Key]): void;\n insert(pos: number, value: Value | Prelim): void;\n delete(pos: number, len: number): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroMap<T extends Record<string, any> = Record<string, any>> {\n setContainer(key: string, container_type: \"Map\"): LoroMap;\n setContainer(key: string, container_type: \"List\"): LoroList;\n setContainer(key: string, container_type: \"Text\"): LoroText;\n setContainer(key: string, container_type: \"Tree\"): LoroTree;\n setContainer(key: string, container_type: string): never;\n\n get(key: string): Value;\n getTyped<Key extends keyof T & string>(txn: Loro, key: Key): T[Key];\n set(key: string, value: Value | Prelim): void;\n setTyped<Key extends keyof T & string>(key: Key, value: T[Key]): void;\n delete(key: string): void;\n subscribe(txn: Loro, listener: Listener): number;\n }\n\n interface LoroText {\n insert(pos: number, text: string):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ACF,CAAA,CAAA;AACA,OAAQ,CAAA,SAAA,CAAU,QAAW,GAAA,SAAA,GAAa,IAAM,EAAA;AAC9C,EAAO,OAAA,IAAA,CAAK,GAAI,CAAA,GAAG,IAAI,CAAA,CAAA;AACzB,CAAA,CAAA;AA8EO,SAAS,cAAc,CAA6B,EAAA;AACzD,EAAO,OAAA,CAAA,CAAE,WAAW,MAAM,CAAA,CAAA;AAC5B;;;;"}
package/package.json CHANGED
@@ -1,25 +1,38 @@
1
1
  {
2
2
  "name": "loro-crdt",
3
- "version": "0.3.0",
4
- "description": "",
3
+ "version": "0.4.0",
4
+ "description": "Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.",
5
+ "keywords": [
6
+ "crdt",
7
+ "CRDTs",
8
+ "realtime",
9
+ "collaboration",
10
+ "sync",
11
+ "p2p"
12
+ ],
5
13
  "main": "dist/loro.js",
6
14
  "module": "dist/loro.mjs",
7
15
  "typings": "dist/loro.d.ts",
8
- "author": "",
16
+ "author": "Loro",
17
+ "homepage": "https://loro.dev",
9
18
  "license": "ISC",
10
19
  "dependencies": {
11
- "loro-wasm": "0.3.0"
20
+ "loro-wasm": "0.4.0"
12
21
  },
13
22
  "devDependencies": {
14
23
  "@rollup/plugin-node-resolve": "^15.0.1",
15
- "esbuild": "^0.17.12",
24
+ "@typescript-eslint/parser": "^6.2.0",
25
+ "@vitest/ui": "^0.34.6",
26
+ "esbuild": "^0.18.20",
27
+ "eslint": "^8.46.0",
28
+ "prettier": "^3.0.0",
16
29
  "rollup": "^3.20.1",
17
30
  "rollup-plugin-dts": "^5.3.0",
18
31
  "rollup-plugin-esbuild": "^5.0.0",
19
32
  "typescript": "^5.0.2",
20
33
  "vite": "^4.2.1",
21
34
  "vite-plugin-wasm": "^3.2.2",
22
- "vitest": "^0.29.7"
35
+ "vitest": "^0.34.0"
23
36
  },
24
37
  "scripts": {
25
38
  "build": "rollup -c",