@dabble/patches 0.4.5 → 0.4.6

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.
Files changed (181) hide show
  1. package/dist/algorithms/client/applyCommittedChanges.d.ts +8 -2
  2. package/dist/algorithms/client/applyCommittedChanges.js +30 -38
  3. package/dist/algorithms/client/batching.d.ts +8 -2
  4. package/dist/algorithms/client/batching.js +38 -37
  5. package/dist/algorithms/client/breakChange.d.ts +8 -2
  6. package/dist/algorithms/client/breakChange.js +191 -240
  7. package/dist/algorithms/client/createStateFromSnapshot.d.ts +8 -2
  8. package/dist/algorithms/client/createStateFromSnapshot.js +7 -8
  9. package/dist/algorithms/client/getJSONByteSize.d.ts +3 -1
  10. package/dist/algorithms/client/getJSONByteSize.js +12 -11
  11. package/dist/algorithms/client/makeChange.d.ts +8 -2
  12. package/dist/algorithms/client/makeChange.js +28 -36
  13. package/dist/algorithms/server/commitChanges.d.ts +9 -3
  14. package/dist/algorithms/server/commitChanges.js +69 -78
  15. package/dist/algorithms/server/createVersion.d.ts +9 -3
  16. package/dist/algorithms/server/createVersion.js +21 -27
  17. package/dist/algorithms/server/getSnapshotAtRevision.d.ts +9 -3
  18. package/dist/algorithms/server/getSnapshotAtRevision.js +27 -28
  19. package/dist/algorithms/server/getStateAtRevision.d.ts +9 -3
  20. package/dist/algorithms/server/getStateAtRevision.js +13 -17
  21. package/dist/algorithms/server/handleOfflineSessionsAndBatches.d.ts +9 -3
  22. package/dist/algorithms/server/handleOfflineSessionsAndBatches.js +60 -77
  23. package/dist/algorithms/server/transformIncomingChanges.d.ts +8 -2
  24. package/dist/algorithms/server/transformIncomingChanges.js +27 -39
  25. package/dist/algorithms/shared/applyChanges.d.ts +8 -2
  26. package/dist/algorithms/shared/applyChanges.js +11 -16
  27. package/dist/algorithms/shared/rebaseChanges.d.ts +8 -2
  28. package/dist/algorithms/shared/rebaseChanges.js +30 -49
  29. package/dist/chunk-IZ2YBCUP.js +56 -0
  30. package/dist/client/InMemoryStore.d.ts +9 -3
  31. package/dist/client/InMemoryStore.js +92 -101
  32. package/dist/client/IndexedDBStore.d.ts +9 -3
  33. package/dist/client/IndexedDBStore.js +378 -491
  34. package/dist/client/Patches.d.ts +18 -13
  35. package/dist/client/Patches.js +152 -207
  36. package/dist/client/PatchesDoc.d.ts +14 -8
  37. package/dist/client/PatchesDoc.js +147 -154
  38. package/dist/client/PatchesHistoryClient.d.ts +12 -5
  39. package/dist/client/PatchesHistoryClient.js +110 -117
  40. package/dist/client/PatchesStore.d.ts +9 -3
  41. package/dist/client/PatchesStore.js +0 -1
  42. package/dist/client/index.d.ts +12 -6
  43. package/dist/client/index.js +5 -5
  44. package/dist/data/change.d.ts +9 -3
  45. package/dist/data/change.js +23 -15
  46. package/dist/data/version.d.ts +9 -3
  47. package/dist/data/version.js +11 -15
  48. package/dist/event-signal.d.ts +7 -6
  49. package/dist/event-signal.js +24 -39
  50. package/dist/index-CvQws3AB.d.ts +36 -0
  51. package/dist/index.d.ts +27 -5
  52. package/dist/index.js +10 -4
  53. package/dist/json-patch/JSONPatch.d.ts +9 -5
  54. package/dist/json-patch/JSONPatch.js +175 -183
  55. package/dist/json-patch/applyPatch.d.ts +5 -2
  56. package/dist/json-patch/applyPatch.js +27 -35
  57. package/dist/json-patch/composePatch.d.ts +5 -2
  58. package/dist/json-patch/composePatch.js +34 -34
  59. package/dist/json-patch/createJSONPatch.d.ts +7 -2
  60. package/dist/json-patch/createJSONPatch.js +11 -38
  61. package/dist/json-patch/index.d.ts +14 -6
  62. package/dist/json-patch/index.js +20 -9
  63. package/dist/json-patch/invertPatch.d.ts +5 -2
  64. package/dist/json-patch/invertPatch.js +31 -30
  65. package/dist/json-patch/ops/add.d.ts +5 -2
  66. package/dist/json-patch/ops/add.js +53 -51
  67. package/dist/json-patch/ops/bitmask.d.ts +8 -5
  68. package/dist/json-patch/ops/bitmask.js +41 -44
  69. package/dist/json-patch/ops/copy.d.ts +5 -2
  70. package/dist/json-patch/ops/copy.js +32 -33
  71. package/dist/json-patch/ops/increment.d.ts +5 -2
  72. package/dist/json-patch/ops/increment.js +21 -20
  73. package/dist/json-patch/ops/index.d.ts +10 -21
  74. package/dist/json-patch/ops/index.js +34 -24
  75. package/dist/json-patch/ops/move.d.ts +5 -2
  76. package/dist/json-patch/ops/move.js +132 -198
  77. package/dist/json-patch/ops/remove.d.ts +5 -2
  78. package/dist/json-patch/ops/remove.js +33 -30
  79. package/dist/json-patch/ops/replace.d.ts +5 -2
  80. package/dist/json-patch/ops/replace.js +45 -43
  81. package/dist/json-patch/ops/test.d.ts +5 -2
  82. package/dist/json-patch/ops/test.js +25 -21
  83. package/dist/json-patch/ops/text.d.ts +5 -2
  84. package/dist/json-patch/ops/text.js +54 -54
  85. package/dist/json-patch/pathProxy.d.ts +9 -3
  86. package/dist/json-patch/pathProxy.js +27 -48
  87. package/dist/json-patch/state.d.ts +5 -2
  88. package/dist/json-patch/state.js +11 -7
  89. package/dist/json-patch/transformPatch.d.ts +6 -2
  90. package/dist/json-patch/transformPatch.js +21 -24
  91. package/dist/json-patch/types.d.ts +9 -7
  92. package/dist/json-patch/types.js +0 -1
  93. package/dist/json-patch/utils/deepEqual.d.ts +3 -1
  94. package/dist/json-patch/utils/deepEqual.js +32 -28
  95. package/dist/json-patch/utils/exit.d.ts +5 -2
  96. package/dist/json-patch/utils/exit.js +7 -3
  97. package/dist/json-patch/utils/get.d.ts +5 -2
  98. package/dist/json-patch/utils/get.js +8 -4
  99. package/dist/json-patch/utils/getOpData.d.ts +5 -2
  100. package/dist/json-patch/utils/getOpData.js +12 -9
  101. package/dist/json-patch/utils/getType.d.ts +6 -3
  102. package/dist/json-patch/utils/getType.js +9 -4
  103. package/dist/json-patch/utils/index.d.ts +15 -14
  104. package/dist/json-patch/utils/index.js +14 -14
  105. package/dist/json-patch/utils/log.d.ts +4 -2
  106. package/dist/json-patch/utils/log.js +8 -3
  107. package/dist/json-patch/utils/ops.d.ts +8 -5
  108. package/dist/json-patch/utils/ops.js +83 -100
  109. package/dist/json-patch/utils/paths.d.ts +12 -9
  110. package/dist/json-patch/utils/paths.js +54 -51
  111. package/dist/json-patch/utils/pluck.d.ts +8 -5
  112. package/dist/json-patch/utils/pluck.js +32 -26
  113. package/dist/json-patch/utils/shallowCopy.d.ts +3 -1
  114. package/dist/json-patch/utils/shallowCopy.js +22 -18
  115. package/dist/json-patch/utils/softWrites.d.ts +6 -3
  116. package/dist/json-patch/utils/softWrites.js +17 -16
  117. package/dist/json-patch/utils/toArrayIndex.d.ts +3 -1
  118. package/dist/json-patch/utils/toArrayIndex.js +14 -10
  119. package/dist/json-patch/utils/toKeys.d.ts +3 -1
  120. package/dist/json-patch/utils/toKeys.js +15 -11
  121. package/dist/json-patch/utils/updateArrayIndexes.d.ts +5 -2
  122. package/dist/json-patch/utils/updateArrayIndexes.js +33 -37
  123. package/dist/json-patch/utils/updateArrayPath.d.ts +5 -2
  124. package/dist/json-patch/utils/updateArrayPath.js +29 -42
  125. package/dist/net/PatchesClient.d.ts +128 -0
  126. package/dist/net/PatchesClient.js +161 -0
  127. package/dist/net/PatchesSync.d.ts +19 -9
  128. package/dist/net/PatchesSync.js +291 -386
  129. package/dist/net/error.d.ts +3 -1
  130. package/dist/net/error.js +9 -6
  131. package/dist/net/http/FetchTransport.d.ts +21 -0
  132. package/dist/net/http/FetchTransport.js +34 -0
  133. package/dist/net/index.d.ts +26 -12
  134. package/dist/net/index.js +12 -10
  135. package/dist/net/protocol/JSONRPCClient.d.ts +11 -4
  136. package/dist/net/protocol/JSONRPCClient.js +95 -103
  137. package/dist/net/protocol/JSONRPCServer.d.ts +15 -8
  138. package/dist/net/protocol/JSONRPCServer.js +101 -123
  139. package/dist/net/protocol/types.d.ts +21 -15
  140. package/dist/net/protocol/types.js +0 -1
  141. package/dist/net/protocol/utils.d.ts +12 -0
  142. package/dist/net/protocol/utils.js +15 -0
  143. package/dist/net/types.d.ts +4 -2
  144. package/dist/net/types.js +0 -1
  145. package/dist/net/webrtc/WebRTCAwareness.d.ts +14 -4
  146. package/dist/net/webrtc/WebRTCAwareness.js +111 -120
  147. package/dist/net/webrtc/WebRTCTransport.d.ts +16 -8
  148. package/dist/net/webrtc/WebRTCTransport.js +149 -157
  149. package/dist/net/webrtc/index.d.ts +10 -2
  150. package/dist/net/webrtc/index.js +2 -2
  151. package/dist/net/websocket/AuthorizationProvider.d.ts +7 -5
  152. package/dist/net/websocket/AuthorizationProvider.js +12 -17
  153. package/dist/net/websocket/PatchesWebSocket.d.ts +14 -109
  154. package/dist/net/websocket/PatchesWebSocket.js +37 -184
  155. package/dist/net/websocket/RPCServer.d.ts +19 -10
  156. package/dist/net/websocket/RPCServer.js +190 -192
  157. package/dist/net/websocket/SignalingService.d.ts +12 -32
  158. package/dist/net/websocket/SignalingService.js +126 -133
  159. package/dist/net/websocket/WebSocketServer.d.ts +17 -4
  160. package/dist/net/websocket/WebSocketServer.js +64 -72
  161. package/dist/net/websocket/WebSocketTransport.d.ts +13 -5
  162. package/dist/net/websocket/WebSocketTransport.js +178 -207
  163. package/dist/net/websocket/onlineState.d.ts +6 -3
  164. package/dist/net/websocket/onlineState.js +25 -21
  165. package/dist/server/PatchesBranchManager.d.ts +12 -5
  166. package/dist/server/PatchesBranchManager.js +132 -142
  167. package/dist/server/PatchesHistoryManager.d.ts +11 -3
  168. package/dist/server/PatchesHistoryManager.js +81 -84
  169. package/dist/server/PatchesServer.d.ts +16 -10
  170. package/dist/server/PatchesServer.js +131 -137
  171. package/dist/server/index.d.ts +7 -2
  172. package/dist/server/index.js +9 -3
  173. package/dist/server/types.d.ts +9 -3
  174. package/dist/server/types.js +0 -1
  175. package/dist/types.d.ts +38 -19
  176. package/dist/types.js +1 -1
  177. package/dist/utils/concurrency.d.ts +7 -5
  178. package/dist/utils/concurrency.js +43 -53
  179. package/dist/utils/deferred.d.ts +4 -2
  180. package/dist/utils/deferred.js +25 -21
  181. package/package.json +5 -7
@@ -1,38 +1,34 @@
1
- import { getTypeLike } from './getType.js';
2
- import { log } from './log.js';
3
- import { isAdd, mapAndFilterOps, transformRemove } from './ops.js';
4
- import { getPrefixAndProp } from './paths.js';
5
- import { updateArrayPath } from './updateArrayPath.js';
6
- /**
7
- * Update array indexes to account for values being added or removed from an array.
8
- */
9
- export function updateArrayIndexes(state, thisPath, otherOps, modifier, isRemove) {
10
- const [arrayPrefix, indexStr] = getPrefixAndProp(thisPath);
11
- const index = parseInt(indexStr);
12
- log('Shifting array indexes', thisPath, modifier);
13
- // Check ops for any that need to be replaced
14
- return mapAndFilterOps(otherOps, (op, i, breakAfter) => {
15
- if (isRemove && thisPath === op.from) {
16
- const opLike = getTypeLike(state, op);
17
- if (opLike === 'move') {
18
- // We need the rest of the otherOps to be adjusted against this "move"
19
- breakAfter();
20
- return transformRemove(state, op.path, otherOps.slice(i + 1));
21
- }
22
- else if (opLike === 'copy') {
23
- // We need future ops on the copied object to be removed
24
- breakAfter();
25
- let rest = transformRemove(state, thisPath, otherOps.slice(i + 1));
26
- rest = transformRemove(state, op.path, rest);
27
- return rest;
28
- }
29
- }
30
- if (op.soft && isAdd(state, op, 'path') && op.path === thisPath) {
31
- breakAfter(true);
32
- return null;
33
- }
34
- // check for items from the same array that will be affected
35
- op = updateArrayPath(state, op, 'from', arrayPrefix, index, modifier);
36
- return op && updateArrayPath(state, op, 'path', arrayPrefix, index, modifier);
37
- });
1
+ import "../../chunk-IZ2YBCUP.js";
2
+ import { getTypeLike } from "./getType.js";
3
+ import { log } from "./log.js";
4
+ import { isAdd, mapAndFilterOps, transformRemove } from "./ops.js";
5
+ import { getPrefixAndProp } from "./paths.js";
6
+ import { updateArrayPath } from "./updateArrayPath.js";
7
+ function updateArrayIndexes(state, thisPath, otherOps, modifier, isRemove) {
8
+ const [arrayPrefix, indexStr] = getPrefixAndProp(thisPath);
9
+ const index = parseInt(indexStr);
10
+ log("Shifting array indexes", thisPath, modifier);
11
+ return mapAndFilterOps(otherOps, (op, i, breakAfter) => {
12
+ if (isRemove && thisPath === op.from) {
13
+ const opLike = getTypeLike(state, op);
14
+ if (opLike === "move") {
15
+ breakAfter();
16
+ return transformRemove(state, op.path, otherOps.slice(i + 1));
17
+ } else if (opLike === "copy") {
18
+ breakAfter();
19
+ let rest = transformRemove(state, thisPath, otherOps.slice(i + 1));
20
+ rest = transformRemove(state, op.path, rest);
21
+ return rest;
22
+ }
23
+ }
24
+ if (op.soft && isAdd(state, op, "path") && op.path === thisPath) {
25
+ breakAfter(true);
26
+ return null;
27
+ }
28
+ op = updateArrayPath(state, op, "from", arrayPrefix, index, modifier);
29
+ return op && updateArrayPath(state, op, "path", arrayPrefix, index, modifier);
30
+ });
38
31
  }
32
+ export {
33
+ updateArrayIndexes
34
+ };
@@ -1,5 +1,8 @@
1
- import type { JSONPatchOp, State } from '../types.js';
1
+ import { State, JSONPatchOp } from '../types.js';
2
+
2
3
  /**
3
4
  * Adjust ops within an array
4
5
  */
5
- export declare function updateArrayPath(state: State, otherOp: JSONPatchOp, pathName: 'from' | 'path', thisPrefix: string, thisIndex: number, modifier: 1 | -1): JSONPatchOp | [JSONPatchOp, JSONPatchOp] | null;
6
+ declare function updateArrayPath(state: State, otherOp: JSONPatchOp, pathName: 'from' | 'path', thisPrefix: string, thisIndex: number, modifier: 1 | -1): JSONPatchOp | [JSONPatchOp, JSONPatchOp] | null;
7
+
8
+ export { updateArrayPath };
@@ -1,45 +1,32 @@
1
- import { getTypeLike } from './getType.js';
2
- import { isAdd } from './ops.js';
3
- import { getIndexAndEnd } from './paths.js';
4
- import { getValue } from './pluck.js';
5
- /**
6
- * Adjust ops within an array
7
- */
8
- export function updateArrayPath(state, otherOp, pathName, thisPrefix, thisIndex, modifier) {
9
- const path = otherOp[pathName];
10
- if (!path || !path.startsWith(thisPrefix))
11
- return otherOp;
12
- const [otherIndex, end] = getIndexAndEnd(state, path, thisPrefix.length);
13
- const opLike = getTypeLike(state, otherOp);
14
- // A bit of complex logic to handle moves upwards in an array. Since an item is removed earier in the array and added later, the other index is like it was one less (or this index was one more), so we correct it
15
- if (opLike === 'move' &&
16
- pathName === 'path' &&
17
- otherOp.from?.startsWith(thisPrefix) &&
18
- getIndexAndEnd(state, otherOp.from, thisPrefix.length)[0] < otherIndex) {
19
- thisIndex -= 1;
1
+ import "../../chunk-IZ2YBCUP.js";
2
+ import { getTypeLike } from "./getType.js";
3
+ import { isAdd } from "./ops.js";
4
+ import { getIndexAndEnd } from "./paths.js";
5
+ import { getValue } from "./pluck.js";
6
+ function updateArrayPath(state, otherOp, pathName, thisPrefix, thisIndex, modifier) {
7
+ const path = otherOp[pathName];
8
+ if (!path || !path.startsWith(thisPrefix)) return otherOp;
9
+ const [otherIndex, end] = getIndexAndEnd(state, path, thisPrefix.length);
10
+ const opLike = getTypeLike(state, otherOp);
11
+ if (opLike === "move" && pathName === "path" && otherOp.from?.startsWith(thisPrefix) && getIndexAndEnd(state, otherOp.from, thisPrefix.length)[0] < otherIndex) {
12
+ thisIndex -= 1;
13
+ }
14
+ if (otherIndex < thisIndex) return otherOp;
15
+ if (otherIndex === thisIndex && modifier === -1) {
16
+ if (end === path.length) {
17
+ if (isAdd(state, otherOp, pathName)) return otherOp;
18
+ if (otherOp.op === "replace") return getValue(state, otherOp, "op", "add");
19
+ if (opLike === "replace") return [{ op: "add", path: otherOp.path, value: null }, otherOp];
20
20
  }
21
- if (otherIndex < thisIndex)
22
- return otherOp;
23
- // When this is a removed item and the op is a subpath or a non-add, remove it.
24
- if (otherIndex === thisIndex && modifier === -1) {
25
- if (end === path.length) {
26
- // If we are adding to the location something got removed, continue adding it.
27
- if (isAdd(state, otherOp, pathName))
28
- return otherOp;
29
- if (otherOp.op === 'replace')
30
- return getValue(state, otherOp, 'op', 'add');
31
- // If we are replacing an item which was removed, add it (don't replace something else in the array)
32
- if (opLike === 'replace')
33
- return [{ op: 'add', path: otherOp.path, value: null }, otherOp];
34
- }
35
- return null;
36
- }
37
- else if (isAdd(state, otherOp, pathName) && otherIndex === thisIndex && end === path.length) {
38
- if (otherOp.soft)
39
- return null;
40
- return otherOp;
41
- }
42
- const newPath = thisPrefix + (otherIndex + modifier) + path.slice(end);
43
- otherOp = getValue(state, otherOp, pathName, newPath);
21
+ return null;
22
+ } else if (isAdd(state, otherOp, pathName) && otherIndex === thisIndex && end === path.length) {
23
+ if (otherOp.soft) return null;
44
24
  return otherOp;
25
+ }
26
+ const newPath = thisPrefix + (otherIndex + modifier) + path.slice(end);
27
+ otherOp = getValue(state, otherOp, pathName, newPath);
28
+ return otherOp;
45
29
  }
30
+ export {
31
+ updateArrayPath
32
+ };
@@ -0,0 +1,128 @@
1
+ import { Signal } from '../event-signal.js';
2
+ import { Change, PatchesState, ChangeInput, EditableVersionMetadata, ListVersionsOptions, VersionMetadata, PatchesSnapshot } from '../types.js';
3
+ import { JSONRPCClient } from './protocol/JSONRPCClient.js';
4
+ import { PatchesAPI, ClientTransport } from './protocol/types.js';
5
+ import '../json-patch/JSONPatch.js';
6
+ import '@dabble/delta';
7
+ import '../json-patch/types.js';
8
+
9
+ /**
10
+ * High-level client for the Patches real-time collaboration service.
11
+ * This class provides document subscription, patch notification handling,
12
+ * versioning, and other OT-specific functionality
13
+ * over a WebSocket connection.
14
+ */
15
+ declare class PatchesClient implements PatchesAPI {
16
+ rpc: JSONRPCClient;
17
+ transport: ClientTransport;
18
+ /** Signal emitted when the server pushes document changes. */
19
+ readonly onChangesCommitted: Signal<(docId: string, changes: Change[]) => void>;
20
+ /**
21
+ * Creates a new Patches WebSocket client instance.
22
+ * @param url - The WebSocket server URL to connect to
23
+ * @param wsOptions - Optional configuration for the underlying WebSocket connection
24
+ */
25
+ constructor(transport: ClientTransport);
26
+ /**
27
+ * Subscribes the client to one or more documents to receive real-time updates.
28
+ * @param ids - Document ID or IDs to subscribe to.
29
+ * @returns A promise resolving with the list of successfully subscribed document IDs.
30
+ */
31
+ subscribe(ids: string | string[]): Promise<string[]>;
32
+ /**
33
+ * Unsubscribes the client from one or more documents.
34
+ * @param ids - Document ID or IDs to unsubscribe from.
35
+ * @returns A promise resolving when the unsubscription is confirmed.
36
+ */
37
+ unsubscribe(ids: string | string[]): Promise<void>;
38
+ /**
39
+ * Gets the latest state (content and revision) of a document.
40
+ * @param docId - The ID of the document.
41
+ * @returns A promise resolving with the document snapshot.
42
+ */
43
+ getDoc<T = any>(docId: string, atRev?: number): Promise<PatchesState<T>>;
44
+ /**
45
+ * Gets changes that occurred for a document after a specific revision number.
46
+ * @param docId - The ID of the document.
47
+ * @param rev - The revision number after which to fetch changes.
48
+ * @returns A promise resolving with an array of changes.
49
+ */
50
+ getChangesSince(docId: string, rev: number): Promise<Change[]>;
51
+ /**
52
+ * Applies a set of client-generated changes to a document on the server.
53
+ * @param docId - The ID of the document.
54
+ * @param changes - An array of changes to apply.
55
+ * @returns A promise resolving with the changes as committed by the server (potentially transformed).
56
+ */
57
+ commitChanges(docId: string, changes: ChangeInput[]): Promise<Change[]>;
58
+ /**
59
+ * Deletes a document on the server.
60
+ * @param docId - The ID of the document to delete.
61
+ * @returns A promise resolving when the deletion is confirmed.
62
+ */
63
+ deleteDoc(docId: string): Promise<void>;
64
+ /**
65
+ * Creates a named version snapshot of a document's current state on the server.
66
+ * @param docId - The ID of the document.
67
+ * @param name - A descriptive name for the version.
68
+ * @returns A promise resolving with the unique ID of the newly created version.
69
+ */
70
+ createVersion(docId: string, metadata: EditableVersionMetadata): Promise<string>;
71
+ /**
72
+ * Lists metadata for saved versions of a document.
73
+ * @param docId - The ID of the document.
74
+ * @param options - Options for filtering or pagination (e.g., limit, offset).
75
+ * @returns A promise resolving with an array of version metadata objects.
76
+ */
77
+ listVersions(docId: string, options?: ListVersionsOptions): Promise<VersionMetadata[]>;
78
+ /**
79
+ * Gets the document state snapshot corresponding to a specific version ID.
80
+ * @param docId - The ID of the document.
81
+ * @param versionId - The ID of the version to retrieve.
82
+ * @returns A promise resolving with the document snapshot for that version.
83
+ */
84
+ getVersionState(docId: string, versionId: string): Promise<PatchesSnapshot>;
85
+ /**
86
+ * Gets the original changes associated with a specific version ID.
87
+ * @param docId - The ID of the document.
88
+ * @param versionId - The ID of the version.
89
+ * @returns A promise resolving with an array of changes that constitute that version.
90
+ */
91
+ getVersionChanges(docId: string, versionId: string): Promise<Change[]>;
92
+ /**
93
+ * Updates the name of a specific version.
94
+ * @param docId - The ID of the document.
95
+ * @param versionId - The ID of the version to update.
96
+ * @param name - The new name for the version.
97
+ * @returns A promise resolving when the update is confirmed.
98
+ */
99
+ updateVersion(docId: string, versionId: string, metadata: EditableVersionMetadata): Promise<void>;
100
+ /**
101
+ * Lists all branches for a document.
102
+ * @param docId - The ID of the document.
103
+ * @returns A promise resolving with an array of branch metadata objects.
104
+ */
105
+ listBranches(docId: string): Promise<VersionMetadata[]>;
106
+ /**
107
+ * Creates a new branch for a document.
108
+ * @param docId - The ID of the document.
109
+ * @param rev - The revision number to base the new branch on.
110
+ * @param metadata - Optional metadata for the new branch.
111
+ * @returns A promise resolving with the unique ID of the newly created branch.
112
+ */
113
+ createBranch(docId: string, rev: number, metadata?: EditableVersionMetadata): Promise<string>;
114
+ /**
115
+ * Closes a branch on the server.
116
+ * @param branchId - The ID of the branch to close.
117
+ * @returns A promise resolving when the branch is closed.
118
+ */
119
+ closeBranch(branchId: string): Promise<void>;
120
+ /**
121
+ * Merges a branch on the server.
122
+ * @param branchId - The ID of the branch to merge.
123
+ * @returns A promise resolving when the merge is confirmed.
124
+ */
125
+ mergeBranch(branchId: string): Promise<void>;
126
+ }
127
+
128
+ export { PatchesClient };
@@ -0,0 +1,161 @@
1
+ import "../chunk-IZ2YBCUP.js";
2
+ import { signal } from "../event-signal.js";
3
+ import { JSONRPCClient } from "./protocol/JSONRPCClient.js";
4
+ class PatchesClient {
5
+ rpc;
6
+ transport;
7
+ // --- Public Signals ---
8
+ /** Signal emitted when the server pushes document changes. */
9
+ onChangesCommitted = signal();
10
+ /**
11
+ * Creates a new Patches WebSocket client instance.
12
+ * @param url - The WebSocket server URL to connect to
13
+ * @param wsOptions - Optional configuration for the underlying WebSocket connection
14
+ */
15
+ constructor(transport) {
16
+ this.transport = transport;
17
+ this.rpc = new JSONRPCClient(transport);
18
+ this.rpc.on("changesCommitted", (params) => {
19
+ const { docId, changes } = params;
20
+ this.onChangesCommitted.emit(docId, changes);
21
+ });
22
+ }
23
+ // --- Patches API Methods ---
24
+ // === Subscription Operations ===
25
+ /**
26
+ * Subscribes the client to one or more documents to receive real-time updates.
27
+ * @param ids - Document ID or IDs to subscribe to.
28
+ * @returns A promise resolving with the list of successfully subscribed document IDs.
29
+ */
30
+ async subscribe(ids) {
31
+ return this.rpc.call("subscribe", { ids });
32
+ }
33
+ /**
34
+ * Unsubscribes the client from one or more documents.
35
+ * @param ids - Document ID or IDs to unsubscribe from.
36
+ * @returns A promise resolving when the unsubscription is confirmed.
37
+ */
38
+ async unsubscribe(ids) {
39
+ return this.rpc.call("unsubscribe", { ids });
40
+ }
41
+ // === Document Operations ===
42
+ /**
43
+ * Gets the latest state (content and revision) of a document.
44
+ * @param docId - The ID of the document.
45
+ * @returns A promise resolving with the document snapshot.
46
+ */
47
+ async getDoc(docId, atRev) {
48
+ return this.rpc.call("getDoc", { docId, atRev });
49
+ }
50
+ /**
51
+ * Gets changes that occurred for a document after a specific revision number.
52
+ * @param docId - The ID of the document.
53
+ * @param rev - The revision number after which to fetch changes.
54
+ * @returns A promise resolving with an array of changes.
55
+ */
56
+ async getChangesSince(docId, rev) {
57
+ return this.rpc.call("getChangesSince", { docId, rev });
58
+ }
59
+ /**
60
+ * Applies a set of client-generated changes to a document on the server.
61
+ * @param docId - The ID of the document.
62
+ * @param changes - An array of changes to apply.
63
+ * @returns A promise resolving with the changes as committed by the server (potentially transformed).
64
+ */
65
+ async commitChanges(docId, changes) {
66
+ return this.rpc.call("commitChanges", { docId, changes });
67
+ }
68
+ /**
69
+ * Deletes a document on the server.
70
+ * @param docId - The ID of the document to delete.
71
+ * @returns A promise resolving when the deletion is confirmed.
72
+ */
73
+ async deleteDoc(docId) {
74
+ return this.rpc.call("deleteDoc", { docId });
75
+ }
76
+ // === Version Operations ===
77
+ /**
78
+ * Creates a named version snapshot of a document's current state on the server.
79
+ * @param docId - The ID of the document.
80
+ * @param name - A descriptive name for the version.
81
+ * @returns A promise resolving with the unique ID of the newly created version.
82
+ */
83
+ async createVersion(docId, metadata) {
84
+ return this.rpc.call("createVersion", { docId, metadata });
85
+ }
86
+ /**
87
+ * Lists metadata for saved versions of a document.
88
+ * @param docId - The ID of the document.
89
+ * @param options - Options for filtering or pagination (e.g., limit, offset).
90
+ * @returns A promise resolving with an array of version metadata objects.
91
+ */
92
+ async listVersions(docId, options) {
93
+ return this.rpc.call("listVersions", { docId, options });
94
+ }
95
+ /**
96
+ * Gets the document state snapshot corresponding to a specific version ID.
97
+ * @param docId - The ID of the document.
98
+ * @param versionId - The ID of the version to retrieve.
99
+ * @returns A promise resolving with the document snapshot for that version.
100
+ */
101
+ async getVersionState(docId, versionId) {
102
+ return this.rpc.call("getVersionState", { docId, versionId });
103
+ }
104
+ /**
105
+ * Gets the original changes associated with a specific version ID.
106
+ * @param docId - The ID of the document.
107
+ * @param versionId - The ID of the version.
108
+ * @returns A promise resolving with an array of changes that constitute that version.
109
+ */
110
+ async getVersionChanges(docId, versionId) {
111
+ return this.rpc.call("getVersionChanges", { docId, versionId });
112
+ }
113
+ /**
114
+ * Updates the name of a specific version.
115
+ * @param docId - The ID of the document.
116
+ * @param versionId - The ID of the version to update.
117
+ * @param name - The new name for the version.
118
+ * @returns A promise resolving when the update is confirmed.
119
+ */
120
+ async updateVersion(docId, versionId, metadata) {
121
+ return this.rpc.call("updateVersion", { docId, versionId, metadata });
122
+ }
123
+ // === Branch Operations ===
124
+ /**
125
+ * Lists all branches for a document.
126
+ * @param docId - The ID of the document.
127
+ * @returns A promise resolving with an array of branch metadata objects.
128
+ */
129
+ async listBranches(docId) {
130
+ return this.rpc.call("listBranches", { docId });
131
+ }
132
+ /**
133
+ * Creates a new branch for a document.
134
+ * @param docId - The ID of the document.
135
+ * @param rev - The revision number to base the new branch on.
136
+ * @param metadata - Optional metadata for the new branch.
137
+ * @returns A promise resolving with the unique ID of the newly created branch.
138
+ */
139
+ async createBranch(docId, rev, metadata) {
140
+ return this.rpc.call("createBranch", { docId, rev, metadata });
141
+ }
142
+ /**
143
+ * Closes a branch on the server.
144
+ * @param branchId - The ID of the branch to close.
145
+ * @returns A promise resolving when the branch is closed.
146
+ */
147
+ async closeBranch(branchId) {
148
+ return this.rpc.call("closeBranch", { branchId });
149
+ }
150
+ /**
151
+ * Merges a branch on the server.
152
+ * @param branchId - The ID of the branch to merge.
153
+ * @returns A promise resolving when the merge is confirmed.
154
+ */
155
+ async mergeBranch(branchId) {
156
+ return this.rpc.call("mergeBranch", { branchId });
157
+ }
158
+ }
159
+ export {
160
+ PatchesClient
161
+ };
@@ -1,15 +1,23 @@
1
+ import { Signal } from '../event-signal.js';
1
2
  import { Patches } from '../client/Patches.js';
2
- import type { PatchesStore } from '../client/PatchesStore.js';
3
- import type { Change, SyncingState } from '../types.js';
4
- import type { ConnectionState } from './protocol/types.js';
3
+ import { PatchesStore } from '../client/PatchesStore.js';
4
+ import { SyncingState, Change } from '../types.js';
5
+ import { ConnectionState } from './protocol/types.js';
5
6
  import { PatchesWebSocket } from './websocket/PatchesWebSocket.js';
6
- import type { WebSocketOptions } from './websocket/WebSocketTransport.js';
7
- export interface PatchesSyncState {
7
+ import { WebSocketOptions } from './websocket/WebSocketTransport.js';
8
+ import '../client/PatchesDoc.js';
9
+ import '../json-patch/JSONPatch.js';
10
+ import '@dabble/delta';
11
+ import '../json-patch/types.js';
12
+ import './PatchesClient.js';
13
+ import './protocol/JSONRPCClient.js';
14
+
15
+ interface PatchesSyncState {
8
16
  online: boolean;
9
17
  connected: boolean;
10
18
  syncing: SyncingState;
11
19
  }
12
- export interface PatchesSyncOptions {
20
+ interface PatchesSyncOptions {
13
21
  subscribeFilter?: (docIds: string[]) => string[];
14
22
  websocket?: WebSocketOptions;
15
23
  }
@@ -17,7 +25,7 @@ export interface PatchesSyncOptions {
17
25
  * Handles WebSocket connection, document subscriptions, and syncing logic between
18
26
  * the Patches instance and the server.
19
27
  */
20
- export declare class PatchesSync {
28
+ declare class PatchesSync {
21
29
  protected options?: PatchesSyncOptions | undefined;
22
30
  protected ws: PatchesWebSocket;
23
31
  protected patches: Patches;
@@ -28,11 +36,11 @@ export declare class PatchesSync {
28
36
  /**
29
37
  * Signal emitted when the sync state changes.
30
38
  */
31
- readonly onStateChange: import("../event-signal.js").Signal<(state: PatchesSyncState) => void>;
39
+ readonly onStateChange: Signal<(state: PatchesSyncState) => void>;
32
40
  /**
33
41
  * Signal emitted when an error occurs.
34
42
  */
35
- readonly onError: import("../event-signal.js").Signal<(error: Error, context?: {
43
+ readonly onError: Signal<(error: Error, context?: {
36
44
  docId?: string;
37
45
  }) => void>;
38
46
  constructor(patches: Patches, url: string, options?: PatchesSyncOptions | undefined);
@@ -88,3 +96,5 @@ export declare class PatchesSync {
88
96
  protected _handleDocsTracked(docIds: string[]): Promise<void>;
89
97
  protected _handleDocsUntracked(docIds: string[]): Promise<void>;
90
98
  }
99
+
100
+ export { PatchesSync, type PatchesSyncOptions, type PatchesSyncState };