@dabble/patches 0.2.32 → 0.3.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.
Files changed (83) hide show
  1. package/dist/algorithms/client/applyCommittedChanges.d.ts +8 -0
  2. package/dist/algorithms/client/applyCommittedChanges.js +40 -0
  3. package/dist/{utils → algorithms/client}/batching.d.ts +1 -1
  4. package/dist/{utils → algorithms/client}/batching.js +2 -2
  5. package/dist/{utils → algorithms/client}/breakChange.d.ts +2 -3
  6. package/dist/algorithms/client/breakChange.js +258 -0
  7. package/dist/algorithms/client/createStateFromSnapshot.d.ts +7 -0
  8. package/dist/algorithms/client/createStateFromSnapshot.js +9 -0
  9. package/dist/algorithms/client/getJSONByteSize.js +12 -0
  10. package/dist/algorithms/client/makeChange.d.ts +3 -0
  11. package/dist/algorithms/client/makeChange.js +37 -0
  12. package/dist/algorithms/server/commitChanges.d.ts +12 -0
  13. package/dist/algorithms/server/commitChanges.js +80 -0
  14. package/dist/algorithms/server/createVersion.d.ts +12 -0
  15. package/dist/algorithms/server/createVersion.js +28 -0
  16. package/dist/algorithms/server/getSnapshotAtRevision.d.ts +10 -0
  17. package/dist/algorithms/server/getSnapshotAtRevision.js +29 -0
  18. package/dist/algorithms/server/getStateAtRevision.d.ts +9 -0
  19. package/dist/algorithms/server/getStateAtRevision.js +18 -0
  20. package/dist/algorithms/server/handleOfflineSessionsAndBatches.d.ts +12 -0
  21. package/dist/algorithms/server/handleOfflineSessionsAndBatches.js +80 -0
  22. package/dist/algorithms/server/transformIncomingChanges.d.ts +11 -0
  23. package/dist/algorithms/server/transformIncomingChanges.js +40 -0
  24. package/dist/algorithms/shared/applyChanges.d.ts +10 -0
  25. package/dist/algorithms/shared/applyChanges.js +17 -0
  26. package/dist/{utils.d.ts → algorithms/shared/rebaseChanges.d.ts} +1 -11
  27. package/dist/{utils.js → algorithms/shared/rebaseChanges.js} +3 -43
  28. package/dist/client/InMemoryStore.d.ts +2 -1
  29. package/dist/client/InMemoryStore.js +9 -3
  30. package/dist/client/IndexedDBStore.d.ts +34 -2
  31. package/dist/client/IndexedDBStore.js +399 -282
  32. package/dist/client/Patches.d.ts +11 -41
  33. package/dist/client/Patches.js +197 -208
  34. package/dist/client/PatchesDoc.d.ts +24 -41
  35. package/dist/client/PatchesDoc.js +57 -214
  36. package/dist/client/PatchesHistoryClient.js +1 -1
  37. package/dist/client/PatchesStore.d.ts +186 -9
  38. package/dist/data/change.d.ts +3 -0
  39. package/dist/data/change.js +20 -0
  40. package/dist/data/version.d.ts +12 -0
  41. package/dist/data/version.js +17 -0
  42. package/dist/json-patch/ops/add.js +1 -1
  43. package/dist/json-patch/ops/move.js +1 -1
  44. package/dist/json-patch/ops/remove.js +1 -1
  45. package/dist/json-patch/ops/replace.js +1 -1
  46. package/dist/json-patch/utils/get.js +0 -1
  47. package/dist/json-patch/utils/log.d.ts +4 -1
  48. package/dist/json-patch/utils/log.js +2 -5
  49. package/dist/json-patch/utils/ops.d.ts +1 -1
  50. package/dist/json-patch/utils/ops.js +4 -1
  51. package/dist/json-patch/utils/paths.js +2 -2
  52. package/dist/json-patch/utils/toArrayIndex.js +1 -1
  53. package/dist/net/PatchesSync.d.ts +55 -24
  54. package/dist/net/PatchesSync.js +336 -258
  55. package/dist/net/protocol/types.d.ts +1 -1
  56. package/dist/net/websocket/AuthorizationProvider.d.ts +9 -2
  57. package/dist/net/websocket/AuthorizationProvider.js +14 -2
  58. package/dist/net/websocket/PatchesWebSocket.d.ts +2 -2
  59. package/dist/net/websocket/PatchesWebSocket.js +3 -2
  60. package/dist/net/websocket/RPCServer.d.ts +2 -2
  61. package/dist/net/websocket/RPCServer.js +3 -3
  62. package/dist/net/websocket/SignalingService.js +1 -1
  63. package/dist/net/websocket/WebSocketServer.d.ts +1 -1
  64. package/dist/net/websocket/WebSocketServer.js +2 -2
  65. package/dist/net/websocket/WebSocketTransport.js +1 -1
  66. package/dist/net/websocket/onlineState.d.ts +2 -2
  67. package/dist/net/websocket/onlineState.js +9 -3
  68. package/dist/server/PatchesBranchManager.js +9 -16
  69. package/dist/server/PatchesHistoryManager.js +1 -1
  70. package/dist/server/PatchesServer.d.ts +11 -38
  71. package/dist/server/PatchesServer.js +32 -255
  72. package/dist/server/index.d.ts +4 -4
  73. package/dist/server/index.js +3 -3
  74. package/dist/server/types.d.ts +1 -1
  75. package/dist/types.d.ts +8 -6
  76. package/dist/utils/concurrency.d.ts +26 -0
  77. package/dist/utils/concurrency.js +60 -0
  78. package/dist/utils/deferred.d.ts +7 -0
  79. package/dist/utils/deferred.js +23 -0
  80. package/package.json +11 -5
  81. package/dist/utils/breakChange.js +0 -302
  82. package/dist/utils/getJSONByteSize.js +0 -12
  83. /package/dist/{utils → algorithms/client}/getJSONByteSize.d.ts +0 -0
@@ -1,6 +1,5 @@
1
1
  import { getOpData } from './getOpData.js';
2
2
  export function get(state, path) {
3
- // eslint-disable-next-line no-unused-vars
4
3
  const [, lastKey, target] = getOpData(state, path);
5
4
  return target ? target[lastKey] : undefined;
6
5
  }
@@ -1,2 +1,5 @@
1
+ export declare let log: {
2
+ (...data: any[]): void;
3
+ (message?: any, ...optionalParams: any[]): void;
4
+ };
1
5
  export declare function verbose(value: boolean): void;
2
- export declare function log(...rest: any[]): void;
@@ -1,7 +1,4 @@
1
- let displayLogs = false;
1
+ export let log = console.log;
2
2
  export function verbose(value) {
3
- displayLogs = value;
4
- }
5
- export function log(...rest) {
6
- displayLogs && console.log(...rest);
3
+ log = value ? console.log : () => undefined;
7
4
  }
@@ -6,7 +6,7 @@ export declare function isAdd(state: State, op: JSONPatchOp, pathName: 'from' |
6
6
  /**
7
7
  * Transforms an array of ops, returning the original if there is no change, filtering out ops that are dropped.
8
8
  */
9
- export declare function mapAndFilterOps(ops: JSONPatchOp[], iterator: (op: JSONPatchOp, index: number, breakAfter: (keepRest?: boolean) => {}) => JSONPatchOp | JSONPatchOp[] | null): JSONPatchOp[];
9
+ export declare function mapAndFilterOps(ops: JSONPatchOp[], iterator: (op: JSONPatchOp, index: number, breakAfter: (keepRest?: boolean) => void) => JSONPatchOp | JSONPatchOp[] | null): JSONPatchOp[];
10
10
  /**
11
11
  * Remove operations that apply to a value which was removed.
12
12
  */
@@ -17,7 +17,10 @@ export function mapAndFilterOps(ops, iterator) {
17
17
  const mapped = [];
18
18
  let shouldBreak = false;
19
19
  let keepRest;
20
- const breakAfter = (keep) => (shouldBreak = true) && (keepRest = keep);
20
+ const breakAfter = (keep) => {
21
+ shouldBreak = true;
22
+ keepRest = keep;
23
+ };
21
24
  for (let i = 0; i < ops.length; i++) {
22
25
  const original = ops[i];
23
26
  // If an op was copied or moved to the same path, it is a no-op and should be removed
@@ -23,7 +23,7 @@ export function isArrayPath(path, state) {
23
23
  if (!state || !state.root || !state.root[''])
24
24
  return true;
25
25
  // Double-check if this is an array or not
26
- const [_, __, target] = getOpData(state, path);
26
+ const [, , target] = getOpData(state, path);
27
27
  return Array.isArray(target) || target == null;
28
28
  }
29
29
  export function getArrayPrefixAndIndex(state, path, pathLength) {
@@ -31,7 +31,7 @@ export function getArrayPrefixAndIndex(state, path, pathLength) {
31
31
  path = path.slice(0, path.indexOf('/', pathLength));
32
32
  if (!arrayPathExp.test(path))
33
33
  return EMPTY;
34
- const [_, __, target] = getOpData(state, path);
34
+ const [, , target] = getOpData(state, path);
35
35
  if (!Array.isArray(target))
36
36
  return EMPTY;
37
37
  const [prefix, indexStr] = getPrefixAndProp(path);
@@ -5,7 +5,7 @@ export function toArrayIndex(array, str) {
5
5
  for (let i = 0, imax = str.length; i < imax; i++) {
6
6
  const ch = str.charCodeAt(i);
7
7
  if (57 < ch || ch < 48) {
8
- return Infinity;
8
+ return -1;
9
9
  }
10
10
  }
11
11
  return +str;
@@ -1,18 +1,11 @@
1
1
  import { Patches } from '../client/Patches.js';
2
+ import type { Change, SyncingState } from '../types.js';
3
+ import type { ConnectionState } from './protocol/types.js';
2
4
  import type { WebSocketOptions } from './websocket/WebSocketTransport.js';
3
- export interface PatchesSyncOptions {
4
- /** WebSocket connection options */
5
- wsOptions?: WebSocketOptions;
6
- /**
7
- * Maximum size in bytes for a single payload (network message).
8
- * Changes exceeding this will be automatically split.
9
- */
10
- maxPayloadBytes?: number;
11
- }
12
5
  export interface PatchesSyncState {
13
6
  online: boolean;
14
7
  connected: boolean;
15
- syncing: 'initial' | 'updating' | null | Error;
8
+ syncing: SyncingState;
16
9
  }
17
10
  /**
18
11
  * Handles WebSocket connection, document subscriptions, and syncing logic between
@@ -22,31 +15,69 @@ export declare class PatchesSync {
22
15
  private ws;
23
16
  private patches;
24
17
  private store;
25
- private options;
18
+ private maxPayloadBytes?;
26
19
  private trackedDocs;
27
- private isFlushing;
28
- private globalSyncTimeout;
29
20
  private _state;
21
+ /**
22
+ * Signal emitted when the sync state changes.
23
+ */
30
24
  readonly onStateChange: import("../event-signal.js").Signal<(state: PatchesSyncState) => void>;
25
+ /**
26
+ * Signal emitted when an error occurs.
27
+ */
31
28
  readonly onError: import("../event-signal.js").Signal<(error: Error, context?: {
32
29
  docId?: string;
33
30
  }) => void>;
34
- constructor(url: string, patches: Patches, options?: PatchesSyncOptions);
31
+ constructor(patches: Patches, url: string, wsOptions?: WebSocketOptions);
32
+ /**
33
+ * Gets the current sync state.
34
+ */
35
35
  get state(): PatchesSyncState;
36
- private setState;
36
+ /**
37
+ * Updates the sync state.
38
+ * @param update - The partial state to update.
39
+ */
40
+ protected updateState(update: Partial<PatchesSyncState>): void;
41
+ /**
42
+ * Connects to the WebSocket server and starts syncing if online. If not online, it will wait for online state.
43
+ */
37
44
  connect(): Promise<void>;
45
+ /**
46
+ * Disconnects from the WebSocket server and stops syncing.
47
+ */
38
48
  disconnect(): void;
39
- private scheduleGlobalSync;
40
- syncAllKnownDocs(): Promise<void>;
41
- syncDoc(docId: string): Promise<void>;
42
- flushDoc(docId: string): Promise<void>;
49
+ /**
50
+ * Syncs all known docs when initially connected.
51
+ */
52
+ protected syncAllKnownDocs(): Promise<void>;
53
+ /**
54
+ * Syncs a single document.
55
+ * @param docId The ID of the document to sync.
56
+ */
57
+ protected syncDoc(docId: string): Promise<void>;
58
+ /**
59
+ * Flushes a document to the server.
60
+ * @param docId The ID of the document to flush.
61
+ */
62
+ protected flushDoc(docId: string): Promise<void>;
63
+ /**
64
+ * Receives committed changes from the server and applies them to the document. This is a blockable function, so it
65
+ * is separate from applyServerChangesToDoc, which is called by other blockable functions. Ensuring this is blockable
66
+ * ensures that while a doc is sending changes to the server, it isn't receiving changes from the server which could
67
+ * cause a race condition.
68
+ */
69
+ protected _receiveCommittedChanges(docId: string, serverChanges: Change[]): Promise<void>;
70
+ /**
71
+ * Applies server changes to a document using the centralized sync algorithm.
72
+ * This ensures consistent OT behavior regardless of whether the doc is open in memory.
73
+ */
74
+ protected _applyServerChangesToDoc(docId: string, serverChanges: Change[], sentPendingRange?: [number, number]): Promise<void>;
43
75
  /**
44
76
  * Initiates the deletion process for a document both locally and on the server.
45
77
  * This now delegates the local tombstone marking to Patches.
46
78
  */
47
- _handleDocDeleted(docId: string): Promise<void>;
48
- private _handleOnlineChange;
49
- private _handleConnectionChange;
50
- private _handleDocsTracked;
51
- private _handleDocsUntracked;
79
+ protected _handleDocDeleted(docId: string): Promise<void>;
80
+ protected _handleConnectionChange(connectionState: ConnectionState): void;
81
+ protected _handleDocsTracked(docIds: string[]): Promise<void>;
82
+ protected _handleDocsUntracked(docIds: string[]): Promise<void>;
52
83
  }