loro-crdt 0.2.8-alpha → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/loro.d.ts CHANGED
@@ -24,7 +24,7 @@ type TextDiff = {
24
24
  type: "text";
25
25
  diff: Delta<string>[];
26
26
  };
27
- type MapDIff = {
27
+ type MapDiff = {
28
28
  type: "map";
29
29
  diff: {
30
30
  added: Record<string, Value>;
@@ -35,11 +35,11 @@ type MapDIff = {
35
35
  }>;
36
36
  };
37
37
  };
38
- type Diff = ListDiff | TextDiff | MapDIff;
38
+ type Diff = ListDiff | TextDiff | MapDiff;
39
39
  interface LoroEvent {
40
40
  local: boolean;
41
41
  origin?: string;
42
- diff: Diff[];
42
+ diff: Diff;
43
43
  target: ContainerID;
44
44
  path: Path;
45
45
  }
@@ -53,25 +53,33 @@ declare module "loro-wasm" {
53
53
  subscribe(listener: Listener): number;
54
54
  transact(f: (tx: Transaction) => void, origin?: string): void;
55
55
  }
56
- interface LoroList {
56
+ 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;
59
+ }
60
+ interface LoroList<T extends any[] = any[]> {
57
61
  insertContainer(txn: Transaction | Loro, pos: number, container: "Map"): LoroMap;
58
62
  insertContainer(txn: Transaction | Loro, pos: number, container: "List"): LoroList;
59
63
  insertContainer(txn: Transaction | Loro, pos: number, container: "Text"): LoroText;
60
64
  insertContainer(txn: Transaction | Loro, pos: number, container: string): never;
61
65
  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;
62
68
  insert(txn: Transaction | Loro, pos: number, value: Value | Prelim): void;
63
69
  delete(txn: Transaction | Loro, pos: number, len: number): void;
64
70
  subscribe(txn: Transaction | Loro, listener: Listener): number;
65
71
  subscribeDeep(txn: Transaction | Loro, listener: Listener): number;
66
72
  subscribeOnce(txn: Transaction | Loro, listener: Listener): number;
67
73
  }
68
- interface LoroMap {
74
+ interface LoroMap<T extends Record<string, any> = Record<string, any>> {
69
75
  insertContainer(txn: Transaction | Loro, key: string, container_type: "Map"): LoroMap;
70
76
  insertContainer(txn: Transaction | Loro, key: string, container_type: "List"): LoroList;
71
77
  insertContainer(txn: Transaction | Loro, key: string, container_type: "Text"): LoroText;
72
78
  insertContainer(txn: Transaction | Loro, key: string, container_type: string): never;
73
79
  get(key: string): Value;
80
+ getTyped<Key extends (keyof T) & string>(txn: Loro, key: Key): T[Key];
74
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;
75
83
  delete(txn: Transaction | Loro, key: string): void;
76
84
  subscribe(txn: Transaction | Loro, listener: Listener): number;
77
85
  subscribeDeep(txn: Transaction | Loro, listener: Listener): number;
@@ -86,4 +94,4 @@ declare module "loro-wasm" {
86
94
  }
87
95
  }
88
96
 
89
- export { Delta, Diff, ListDiff, LoroEvent, MapDIff, Path, Prelim, TextDiff, Value, isContainerId };
97
+ export { Delta, Diff, ListDiff, LoroEvent, MapDiff, Path, Prelim, TextDiff, Value, isContainerId };
package/dist/loro.js CHANGED
@@ -12,6 +12,26 @@ loroWasm.Loro.prototype.transact = function(cb, origin) {
12
12
  }
13
13
  });
14
14
  };
15
+ loroWasm.Loro.prototype.getTypedMap = loroWasm.Loro.prototype.getMap;
16
+ loroWasm.Loro.prototype.getTypedList = loroWasm.Loro.prototype.getList;
17
+ loroWasm.LoroList.prototype.getTyped = function(loro, index) {
18
+ const value = this.get(index);
19
+ if (typeof value === "string" && isContainerId(value)) {
20
+ return loro.getContainerById(value);
21
+ } else {
22
+ return value;
23
+ }
24
+ };
25
+ loroWasm.LoroList.prototype.insertTyped = loroWasm.LoroList.prototype.insert;
26
+ loroWasm.LoroMap.prototype.getTyped = function(loro, key) {
27
+ const value = this.get(key);
28
+ if (typeof value === "string" && isContainerId(value)) {
29
+ return loro.getContainerById(value);
30
+ } else {
31
+ return value;
32
+ }
33
+ };
34
+ loroWasm.LoroMap.prototype.setTyped = loroWasm.LoroMap.prototype.set;
15
35
  loroWasm.LoroText.prototype.insert = function(txn, pos, text) {
16
36
  if (txn instanceof loroWasm.Loro) {
17
37
  this.__loro_insert(txn, pos, text);
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\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 LoroList {\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 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 {\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 set(txn: Transaction | Loro, key: string, value: Value | Prelim): 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","LoroText","LoroList","LoroMap"],"mappings":";;;;AAwBAA,aAAA,CAAK,SAAU,CAAA,QAAA,GAAW,SAAU,EAAA,EAAI,MAAQ,EAAA;AAC9C,EAAK,IAAA,CAAA,4BAAA,CAA6B,MAAQ,EAAA,CAAC,GAA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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.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,CA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G,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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
package/dist/loro.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { Loro, LoroText, LoroList, LoroMap } from 'loro-wasm';
1
+ import { Loro, LoroList, LoroMap, LoroText } from 'loro-wasm';
2
2
  export { Loro, LoroList, LoroMap, LoroText, PrelimList, PrelimMap, PrelimText, Transaction, setPanicHook } from 'loro-wasm';
3
3
 
4
4
  Loro.prototype.transact = function(cb, origin) {
@@ -11,6 +11,26 @@ Loro.prototype.transact = function(cb, origin) {
11
11
  }
12
12
  });
13
13
  };
14
+ Loro.prototype.getTypedMap = Loro.prototype.getMap;
15
+ Loro.prototype.getTypedList = Loro.prototype.getList;
16
+ LoroList.prototype.getTyped = function(loro, index) {
17
+ const value = this.get(index);
18
+ if (typeof value === "string" && isContainerId(value)) {
19
+ return loro.getContainerById(value);
20
+ } else {
21
+ return value;
22
+ }
23
+ };
24
+ LoroList.prototype.insertTyped = LoroList.prototype.insert;
25
+ LoroMap.prototype.getTyped = function(loro, key) {
26
+ const value = this.get(key);
27
+ if (typeof value === "string" && isContainerId(value)) {
28
+ return loro.getContainerById(value);
29
+ } else {
30
+ return value;
31
+ }
32
+ };
33
+ LoroMap.prototype.setTyped = LoroMap.prototype.set;
14
34
  LoroText.prototype.insert = function(txn, pos, text) {
15
35
  if (txn instanceof Loro) {
16
36
  this.__loro_insert(txn, pos, text);
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\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 LoroList {\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 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 {\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 set(txn: Transaction | Loro, key: string, value: Value | Prelim): 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":["LoroText","LoroList","LoroMap"],"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;AAEAA,QAAAA,CAAS,SAAU,CAAA,MAAA,GAAS,SAAU,GAAA,EAAK,KAAK,IAAM,EAAA;AACpD,EAAA,IAAI,eAAe,IAAM,EAAA;AACvB,IAAK,IAAA,CAAA,aAAA,CAAc,GAAK,EAAA,GAAA,EAAK,IAAI,CAAA,CAAA;AAAA,GAC5B,MAAA;AACL,IAAK,IAAA,CA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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 {\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,IAAI,OAAO,KAAA,KAAU,QAAY,IAAA,aAAA,CAAc,KAAK,CAAG,EAAA;AACrD,IAAO,OAAA,IAAA,CAAK,iBAAiB,KAAK,CAAA,CAAA;AAAA,GAC7B,MAAA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AA;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;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loro-crdt",
3
- "version": "0.2.8-alpha",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "main": "dist/loro.js",
6
6
  "module": "dist/loro.mjs",
@@ -8,7 +8,7 @@
8
8
  "author": "",
9
9
  "license": "ISC",
10
10
  "dependencies": {
11
- "loro-wasm": "0.2.5"
11
+ "loro-wasm": "0.3.0"
12
12
  },
13
13
  "devDependencies": {
14
14
  "@rollup/plugin-node-resolve": "^15.0.1",
@@ -24,6 +24,7 @@
24
24
  "scripts": {
25
25
  "build": "rollup -c",
26
26
  "watch": "rollup -c -w",
27
- "test": "vitest run"
27
+ "test": "vitest run",
28
+ "prepublish": "pnpm run build"
28
29
  }
29
30
  }