loro-crdt 1.0.8-alpha.0 → 1.0.8-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.0.8-alpha.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Include the build for web
8
+
3
9
  ## 1.0.8-alpha.0
4
10
 
5
11
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loro-crdt",
3
- "version": "1.0.8-alpha.0",
3
+ "version": "1.0.8-alpha.1",
4
4
  "description": "Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.",
5
5
  "keywords": [
6
6
  "crdt",
package/web/index.d.ts ADDED
@@ -0,0 +1,72 @@
1
+ export * from "./loro_wasm";
2
+ export type * from "./loro_wasm";
3
+ import { AwarenessWasm, PeerID, Container, ContainerID, ContainerType, LoroCounter, LoroDoc, LoroList, LoroMap, LoroText, LoroTree, OpId, Value, AwarenessListener } from "./loro_wasm";
4
+ /**
5
+ * @deprecated Please use LoroDoc
6
+ */
7
+ export declare class Loro extends LoroDoc {
8
+ }
9
+ export declare function isContainerId(s: string): s is ContainerID;
10
+ /** Whether the value is a container.
11
+ *
12
+ * # Example
13
+ *
14
+ * ```ts
15
+ * const doc = new LoroDoc();
16
+ * const map = doc.getMap("map");
17
+ * const list = doc.getList("list");
18
+ * const text = doc.getText("text");
19
+ * isContainer(map); // true
20
+ * isContainer(list); // true
21
+ * isContainer(text); // true
22
+ * isContainer(123); // false
23
+ * isContainer("123"); // false
24
+ * isContainer({}); // false
25
+ * ```
26
+ */
27
+ export declare function isContainer(value: any): value is Container;
28
+ /** Get the type of a value that may be a container.
29
+ *
30
+ * # Example
31
+ *
32
+ * ```ts
33
+ * const doc = new LoroDoc();
34
+ * const map = doc.getMap("map");
35
+ * const list = doc.getList("list");
36
+ * const text = doc.getText("text");
37
+ * getType(map); // "Map"
38
+ * getType(list); // "List"
39
+ * getType(text); // "Text"
40
+ * getType(123); // "Json"
41
+ * getType("123"); // "Json"
42
+ * getType({}); // "Json"
43
+ * ```
44
+ */
45
+ export declare function getType<T>(value: T): T extends LoroText ? "Text" : T extends LoroMap<any> ? "Map" : T extends LoroTree<any> ? "Tree" : T extends LoroList<any> ? "List" : T extends LoroCounter ? "Counter" : "Json";
46
+ export declare function newContainerID(id: OpId, type: ContainerType): ContainerID;
47
+ export declare function newRootContainerID(name: string, type: ContainerType): ContainerID;
48
+ /**
49
+ * Awareness is a structure that allows to track the ephemeral state of the peers.
50
+ *
51
+ * If we don't receive a state update from a peer within the timeout, we will remove their state.
52
+ * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
53
+ */
54
+ export declare class Awareness<T extends Value = Value> {
55
+ inner: AwarenessWasm<T>;
56
+ private peer;
57
+ private timer;
58
+ private timeout;
59
+ private listeners;
60
+ constructor(peer: PeerID, timeout?: number);
61
+ apply(bytes: Uint8Array, origin?: string): void;
62
+ setLocalState(state: T): void;
63
+ getLocalState(): T | undefined;
64
+ getAllStates(): Record<PeerID, T>;
65
+ encode(peers: PeerID[]): Uint8Array;
66
+ encodeAll(): Uint8Array;
67
+ addListener(listener: AwarenessListener): void;
68
+ removeListener(listener: AwarenessListener): void;
69
+ peers(): PeerID[];
70
+ destroy(): void;
71
+ private startTimerIfNotEmpty;
72
+ }
package/web/index.js ADDED
@@ -0,0 +1,156 @@
1
+ import { LoroDoc, AwarenessWasm } from "./loro_wasm";
2
+ export * from "./loro_wasm";
3
+
4
+ /**
5
+ * @deprecated Please use LoroDoc
6
+ */
7
+ class Loro extends LoroDoc {
8
+ }
9
+ const CONTAINER_TYPES = [
10
+ "Map",
11
+ "Text",
12
+ "List",
13
+ "Tree",
14
+ "MovableList",
15
+ "Counter",
16
+ ];
17
+ function isContainerId(s) {
18
+ return s.startsWith("cid:");
19
+ }
20
+ /** Whether the value is a container.
21
+ *
22
+ * # Example
23
+ *
24
+ * ```ts
25
+ * const doc = new LoroDoc();
26
+ * const map = doc.getMap("map");
27
+ * const list = doc.getList("list");
28
+ * const text = doc.getText("text");
29
+ * isContainer(map); // true
30
+ * isContainer(list); // true
31
+ * isContainer(text); // true
32
+ * isContainer(123); // false
33
+ * isContainer("123"); // false
34
+ * isContainer({}); // false
35
+ * ```
36
+ */
37
+ function isContainer(value) {
38
+ if (typeof value !== "object" || value == null) {
39
+ return false;
40
+ }
41
+ const p = Object.getPrototypeOf(value);
42
+ if (p == null || typeof p !== "object" || typeof p["kind"] !== "function") {
43
+ return false;
44
+ }
45
+ return CONTAINER_TYPES.includes(value.kind());
46
+ }
47
+ /** Get the type of a value that may be a container.
48
+ *
49
+ * # Example
50
+ *
51
+ * ```ts
52
+ * const doc = new LoroDoc();
53
+ * const map = doc.getMap("map");
54
+ * const list = doc.getList("list");
55
+ * const text = doc.getText("text");
56
+ * getType(map); // "Map"
57
+ * getType(list); // "List"
58
+ * getType(text); // "Text"
59
+ * getType(123); // "Json"
60
+ * getType("123"); // "Json"
61
+ * getType({}); // "Json"
62
+ * ```
63
+ */
64
+ function getType(value) {
65
+ if (isContainer(value)) {
66
+ return value.kind();
67
+ }
68
+ return "Json";
69
+ }
70
+ function newContainerID(id, type) {
71
+ return `cid:${id.counter}@${id.peer}:${type}`;
72
+ }
73
+ function newRootContainerID(name, type) {
74
+ return `cid:root-${name}:${type}`;
75
+ }
76
+ /**
77
+ * Awareness is a structure that allows to track the ephemeral state of the peers.
78
+ *
79
+ * If we don't receive a state update from a peer within the timeout, we will remove their state.
80
+ * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
81
+ */
82
+ class Awareness {
83
+ constructor(peer, timeout = 30000) {
84
+ this.listeners = new Set();
85
+ this.inner = new AwarenessWasm(peer, timeout);
86
+ this.peer = peer;
87
+ this.timeout = timeout;
88
+ }
89
+ apply(bytes, origin = "remote") {
90
+ const { updated, added } = this.inner.apply(bytes);
91
+ this.listeners.forEach((listener) => {
92
+ listener({ updated, added, removed: [] }, origin);
93
+ });
94
+ this.startTimerIfNotEmpty();
95
+ }
96
+ setLocalState(state) {
97
+ const wasEmpty = this.inner.getState(this.peer) == null;
98
+ this.inner.setLocalState(state);
99
+ if (wasEmpty) {
100
+ this.listeners.forEach((listener) => {
101
+ listener({ updated: [], added: [this.inner.peer()], removed: [] }, "local");
102
+ });
103
+ }
104
+ else {
105
+ this.listeners.forEach((listener) => {
106
+ listener({ updated: [this.inner.peer()], added: [], removed: [] }, "local");
107
+ });
108
+ }
109
+ this.startTimerIfNotEmpty();
110
+ }
111
+ getLocalState() {
112
+ return this.inner.getState(this.peer);
113
+ }
114
+ getAllStates() {
115
+ return this.inner.getAllStates();
116
+ }
117
+ encode(peers) {
118
+ return this.inner.encode(peers);
119
+ }
120
+ encodeAll() {
121
+ return this.inner.encodeAll();
122
+ }
123
+ addListener(listener) {
124
+ this.listeners.add(listener);
125
+ }
126
+ removeListener(listener) {
127
+ this.listeners.delete(listener);
128
+ }
129
+ peers() {
130
+ return this.inner.peers();
131
+ }
132
+ destroy() {
133
+ clearInterval(this.timer);
134
+ this.listeners.clear();
135
+ }
136
+ startTimerIfNotEmpty() {
137
+ if (this.inner.isEmpty() || this.timer != null) {
138
+ return;
139
+ }
140
+ this.timer = setInterval(() => {
141
+ const removed = this.inner.removeOutdated();
142
+ if (removed.length > 0) {
143
+ this.listeners.forEach((listener) => {
144
+ listener({ updated: [], added: [], removed }, "timeout");
145
+ });
146
+ }
147
+ if (this.inner.isEmpty()) {
148
+ clearInterval(this.timer);
149
+ this.timer = undefined;
150
+ }
151
+ }, this.timeout / 2);
152
+ }
153
+ }
154
+
155
+ export { Awareness, Loro, getType, isContainer, isContainerId, newContainerID, newRootContainerID };
156
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../ts/index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAoBA;;AAEG;AACG,MAAO,I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