@tldraw/sync-core 4.2.0-canary.f59b349f83fb → 4.2.0-canary.fa5328cd004a
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-cjs/index.d.ts +339 -5
- package/dist-cjs/index.js +2 -1
- package/dist-cjs/index.js.map +2 -2
- package/dist-cjs/lib/ClientWebSocketAdapter.js.map +2 -2
- package/dist-cjs/lib/RoomSession.js.map +1 -1
- package/dist-cjs/lib/TLSyncClient.js +6 -0
- package/dist-cjs/lib/TLSyncClient.js.map +2 -2
- package/dist-cjs/lib/TLSyncRoom.js +35 -9
- package/dist-cjs/lib/TLSyncRoom.js.map +2 -2
- package/dist-cjs/lib/chunk.js +4 -4
- package/dist-cjs/lib/chunk.js.map +1 -1
- package/dist-cjs/lib/diff.js +29 -29
- package/dist-cjs/lib/diff.js.map +2 -2
- package/dist-cjs/lib/protocol.js +1 -1
- package/dist-cjs/lib/protocol.js.map +1 -1
- package/dist-esm/index.d.mts +339 -5
- package/dist-esm/index.mjs +3 -2
- package/dist-esm/index.mjs.map +2 -2
- package/dist-esm/lib/ClientWebSocketAdapter.mjs.map +2 -2
- package/dist-esm/lib/RoomSession.mjs.map +1 -1
- package/dist-esm/lib/TLSyncClient.mjs +6 -0
- package/dist-esm/lib/TLSyncClient.mjs.map +2 -2
- package/dist-esm/lib/TLSyncRoom.mjs +35 -9
- package/dist-esm/lib/TLSyncRoom.mjs.map +2 -2
- package/dist-esm/lib/chunk.mjs +4 -4
- package/dist-esm/lib/chunk.mjs.map +1 -1
- package/dist-esm/lib/diff.mjs +29 -29
- package/dist-esm/lib/diff.mjs.map +2 -2
- package/dist-esm/lib/protocol.mjs +1 -1
- package/dist-esm/lib/protocol.mjs.map +1 -1
- package/package.json +6 -6
- package/src/index.ts +3 -3
- package/src/lib/ClientWebSocketAdapter.ts +4 -1
- package/src/lib/RoomSession.test.ts +3 -0
- package/src/lib/RoomSession.ts +28 -42
- package/src/lib/TLSyncClient.test.ts +17 -6
- package/src/lib/TLSyncClient.ts +31 -17
- package/src/lib/TLSyncRoom.ts +42 -7
- package/src/lib/chunk.ts +4 -4
- package/src/lib/diff.ts +55 -32
- package/src/lib/protocol.ts +1 -1
- package/src/test/TLSocketRoom.test.ts +2 -2
- package/src/test/TLSyncRoom.test.ts +22 -21
- package/src/test/TestSocketPair.ts +5 -2
- package/src/test/diff.test.ts +200 -0
package/src/lib/TLSyncRoom.ts
CHANGED
|
@@ -156,10 +156,15 @@ export class DocumentState<R extends UnknownRecord> {
|
|
|
156
156
|
*
|
|
157
157
|
* @param state - The new record state
|
|
158
158
|
* @param clock - The new clock value
|
|
159
|
+
* @param legacyAppendMode - If true, string append operations will be converted to Put operations
|
|
159
160
|
* @returns Result containing the diff and new DocumentState, or null if no changes, or validation error
|
|
160
161
|
*/
|
|
161
|
-
replaceState(
|
|
162
|
-
|
|
162
|
+
replaceState(
|
|
163
|
+
state: R,
|
|
164
|
+
clock: number,
|
|
165
|
+
legacyAppendMode = false
|
|
166
|
+
): Result<[ObjectDiff, DocumentState<R>] | null, Error> {
|
|
167
|
+
const diff = diffRecord(this.state, state, legacyAppendMode)
|
|
163
168
|
if (!diff) return Result.ok(null)
|
|
164
169
|
try {
|
|
165
170
|
this.recordType.validate(state)
|
|
@@ -173,11 +178,16 @@ export class DocumentState<R extends UnknownRecord> {
|
|
|
173
178
|
*
|
|
174
179
|
* @param diff - The object diff to apply
|
|
175
180
|
* @param clock - The new clock value
|
|
181
|
+
* @param legacyAppendMode - If true, string append operations will be converted to Put operations
|
|
176
182
|
* @returns Result containing the final diff and new DocumentState, or null if no changes, or validation error
|
|
177
183
|
*/
|
|
178
|
-
mergeDiff(
|
|
184
|
+
mergeDiff(
|
|
185
|
+
diff: ObjectDiff,
|
|
186
|
+
clock: number,
|
|
187
|
+
legacyAppendMode = false
|
|
188
|
+
): Result<[ObjectDiff, DocumentState<R>] | null, Error> {
|
|
179
189
|
const newState = applyObjectDiff(this.state, diff)
|
|
180
|
-
return this.replaceState(newState, clock)
|
|
190
|
+
return this.replaceState(newState, clock, legacyAppendMode)
|
|
181
191
|
}
|
|
182
192
|
}
|
|
183
193
|
|
|
@@ -720,6 +730,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
720
730
|
meta: session.meta,
|
|
721
731
|
isReadonly: session.isReadonly,
|
|
722
732
|
requiresLegacyRejection: session.requiresLegacyRejection,
|
|
733
|
+
supportsStringAppend: session.supportsStringAppend,
|
|
723
734
|
})
|
|
724
735
|
|
|
725
736
|
try {
|
|
@@ -843,10 +854,28 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
843
854
|
isReadonly: isReadonly ?? false,
|
|
844
855
|
// this gets set later during handleConnectMessage
|
|
845
856
|
requiresLegacyRejection: false,
|
|
857
|
+
supportsStringAppend: true,
|
|
846
858
|
})
|
|
847
859
|
return this
|
|
848
860
|
}
|
|
849
861
|
|
|
862
|
+
/**
|
|
863
|
+
* Checks if all connected sessions support string append operations (protocol version 8+).
|
|
864
|
+
* If any client is on an older version, returns false to enable legacy append mode.
|
|
865
|
+
*
|
|
866
|
+
* @returns True if all connected sessions are on protocol version 8 or higher
|
|
867
|
+
*/
|
|
868
|
+
getCanEmitStringAppend(): boolean {
|
|
869
|
+
for (const session of this.sessions.values()) {
|
|
870
|
+
if (session.state === RoomSessionState.Connected) {
|
|
871
|
+
if (!session.supportsStringAppend) {
|
|
872
|
+
return false
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return true
|
|
877
|
+
}
|
|
878
|
+
|
|
850
879
|
/**
|
|
851
880
|
* When we send a diff to a client, if that client is on a lower version than us, we need to make
|
|
852
881
|
* the diff compatible with their version. At the moment this means migrating each affected record
|
|
@@ -1010,6 +1039,10 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
1010
1039
|
if (theirProtocolVersion === 6) {
|
|
1011
1040
|
theirProtocolVersion++
|
|
1012
1041
|
}
|
|
1042
|
+
if (theirProtocolVersion === 7) {
|
|
1043
|
+
theirProtocolVersion++
|
|
1044
|
+
session.supportsStringAppend = false
|
|
1045
|
+
}
|
|
1013
1046
|
|
|
1014
1047
|
if (theirProtocolVersion == null || theirProtocolVersion < getTlsyncProtocolVersion()) {
|
|
1015
1048
|
this.rejectSession(session.sessionId, TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
|
|
@@ -1045,6 +1078,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
1045
1078
|
lastInteractionTime: Date.now(),
|
|
1046
1079
|
debounceTimer: null,
|
|
1047
1080
|
outstandingDataMessages: [],
|
|
1081
|
+
supportsStringAppend: session.supportsStringAppend,
|
|
1048
1082
|
meta: session.meta,
|
|
1049
1083
|
isReadonly: session.isReadonly,
|
|
1050
1084
|
requiresLegacyRejection: session.requiresLegacyRejection,
|
|
@@ -1150,6 +1184,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
1150
1184
|
const initialDocumentClock = this.documentClock
|
|
1151
1185
|
let didPresenceChange = false
|
|
1152
1186
|
transaction((rollback) => {
|
|
1187
|
+
const legacyAppendMode = !this.getCanEmitStringAppend()
|
|
1153
1188
|
// collect actual ops that resulted from the push
|
|
1154
1189
|
// these will be broadcast to other users
|
|
1155
1190
|
interface ActualChanges {
|
|
@@ -1198,7 +1233,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
1198
1233
|
if (doc) {
|
|
1199
1234
|
// If there's an existing document, replace it with the new state
|
|
1200
1235
|
// but propagate a diff rather than the entire value
|
|
1201
|
-
const diff = doc.replaceState(state, this.clock)
|
|
1236
|
+
const diff = doc.replaceState(state, this.clock, legacyAppendMode)
|
|
1202
1237
|
if (!diff.ok) {
|
|
1203
1238
|
return fail(TLSyncErrorCloseEventReason.INVALID_RECORD)
|
|
1204
1239
|
}
|
|
@@ -1238,7 +1273,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
1238
1273
|
|
|
1239
1274
|
if (downgraded.value === doc.state) {
|
|
1240
1275
|
// If the versions are compatible, apply the patch and propagate the patch op
|
|
1241
|
-
const diff = doc.mergeDiff(patch, this.clock)
|
|
1276
|
+
const diff = doc.mergeDiff(patch, this.clock, legacyAppendMode)
|
|
1242
1277
|
if (!diff.ok) {
|
|
1243
1278
|
return fail(TLSyncErrorCloseEventReason.INVALID_RECORD)
|
|
1244
1279
|
}
|
|
@@ -1260,7 +1295,7 @@ export class TLSyncRoom<R extends UnknownRecord, SessionMeta> {
|
|
|
1260
1295
|
return fail(TLSyncErrorCloseEventReason.CLIENT_TOO_OLD)
|
|
1261
1296
|
}
|
|
1262
1297
|
// replace the state with the upgraded version and propagate the patch op
|
|
1263
|
-
const diff = doc.replaceState(upgraded.value, this.clock)
|
|
1298
|
+
const diff = doc.replaceState(upgraded.value, this.clock, legacyAppendMode)
|
|
1264
1299
|
if (!diff.ok) {
|
|
1265
1300
|
return fail(TLSyncErrorCloseEventReason.INVALID_RECORD)
|
|
1266
1301
|
}
|
package/src/lib/chunk.ts
CHANGED
|
@@ -83,13 +83,13 @@ export class JsonChunkAssembler {
|
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* Processes a single message, which can be either a complete JSON object or a chunk.
|
|
86
|
-
* For complete JSON objects (starting with '{'), parses immediately.
|
|
87
|
-
* For chunks (prefixed with "{number}_"), accumulates until all chunks received.
|
|
86
|
+
* For complete JSON objects (starting with '\{'), parses immediately.
|
|
87
|
+
* For chunks (prefixed with "\{number\}_"), accumulates until all chunks received.
|
|
88
88
|
*
|
|
89
89
|
* @param msg - The message to process, either JSON or chunk format
|
|
90
90
|
* @returns Result object with data/stringified on success, error object on failure, or null for incomplete chunks
|
|
91
|
-
* -
|
|
92
|
-
* -
|
|
91
|
+
* - `\{ data: object, stringified: string \}` - Successfully parsed complete message
|
|
92
|
+
* - `\{ error: Error \}` - Parse error or invalid chunk sequence
|
|
93
93
|
* - `null` - Chunk received but more chunks expected
|
|
94
94
|
*
|
|
95
95
|
* @example
|
package/src/lib/diff.ts
CHANGED
|
@@ -123,11 +123,11 @@ export type ValueOpType = (typeof ValueOpType)[keyof typeof ValueOpType]
|
|
|
123
123
|
*/
|
|
124
124
|
export type PutOp = [type: typeof ValueOpType.Put, value: unknown]
|
|
125
125
|
/**
|
|
126
|
-
* Operation that appends new values to the end of an array.
|
|
126
|
+
* Operation that appends new values to the end of an array or string.
|
|
127
127
|
*
|
|
128
128
|
* @internal
|
|
129
129
|
*/
|
|
130
|
-
export type AppendOp = [type: typeof ValueOpType.Append,
|
|
130
|
+
export type AppendOp = [type: typeof ValueOpType.Append, value: unknown[] | string, offset: number]
|
|
131
131
|
/**
|
|
132
132
|
* Operation that applies a nested diff to an object or array.
|
|
133
133
|
*
|
|
@@ -165,6 +165,7 @@ export interface ObjectDiff {
|
|
|
165
165
|
*
|
|
166
166
|
* @param prev - The previous version of the record
|
|
167
167
|
* @param next - The next version of the record
|
|
168
|
+
* @param legacyAppendMode - If true, string append operations will be converted to Put operations
|
|
168
169
|
* @returns An ObjectDiff describing the changes, or null if no changes exist
|
|
169
170
|
*
|
|
170
171
|
* @example
|
|
@@ -181,11 +182,20 @@ export interface ObjectDiff {
|
|
|
181
182
|
*
|
|
182
183
|
* @internal
|
|
183
184
|
*/
|
|
184
|
-
export function diffRecord(
|
|
185
|
-
|
|
185
|
+
export function diffRecord(
|
|
186
|
+
prev: object,
|
|
187
|
+
next: object,
|
|
188
|
+
legacyAppendMode = false
|
|
189
|
+
): ObjectDiff | null {
|
|
190
|
+
return diffObject(prev, next, new Set(['props', 'meta']), legacyAppendMode)
|
|
186
191
|
}
|
|
187
192
|
|
|
188
|
-
function diffObject(
|
|
193
|
+
function diffObject(
|
|
194
|
+
prev: object,
|
|
195
|
+
next: object,
|
|
196
|
+
nestedKeys: Set<string> | undefined,
|
|
197
|
+
legacyAppendMode: boolean
|
|
198
|
+
): ObjectDiff | null {
|
|
189
199
|
if (prev === next) {
|
|
190
200
|
return null
|
|
191
201
|
}
|
|
@@ -197,26 +207,22 @@ function diffObject(prev: object, next: object, nestedKeys?: Set<string>): Objec
|
|
|
197
207
|
result[key] = [ValueOpType.Delete]
|
|
198
208
|
continue
|
|
199
209
|
}
|
|
200
|
-
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
} else if (Array.isArray(nextVal) && Array.isArray(prevVal)) {
|
|
211
|
-
const op = diffArray(prevVal, nextVal)
|
|
212
|
-
if (op) {
|
|
213
|
-
if (!result) result = {}
|
|
214
|
-
result[key] = op
|
|
215
|
-
}
|
|
216
|
-
} else {
|
|
210
|
+
const prevValue = (prev as any)[key]
|
|
211
|
+
const nextValue = (next as any)[key]
|
|
212
|
+
if (
|
|
213
|
+
nestedKeys?.has(key) ||
|
|
214
|
+
(Array.isArray(prevValue) && Array.isArray(nextValue)) ||
|
|
215
|
+
(typeof prevValue === 'string' && typeof nextValue === 'string')
|
|
216
|
+
) {
|
|
217
|
+
// if key is in both places, then compare values
|
|
218
|
+
const diff = diffValue(prevValue, nextValue, legacyAppendMode)
|
|
219
|
+
if (diff) {
|
|
217
220
|
if (!result) result = {}
|
|
218
|
-
result[key] =
|
|
221
|
+
result[key] = diff
|
|
219
222
|
}
|
|
223
|
+
} else if (!isEqual(prevValue, nextValue)) {
|
|
224
|
+
if (!result) result = {}
|
|
225
|
+
result[key] = [ValueOpType.Put, nextValue]
|
|
220
226
|
}
|
|
221
227
|
}
|
|
222
228
|
for (const key of Object.keys(next)) {
|
|
@@ -229,19 +235,29 @@ function diffObject(prev: object, next: object, nestedKeys?: Set<string>): Objec
|
|
|
229
235
|
return result
|
|
230
236
|
}
|
|
231
237
|
|
|
232
|
-
function diffValue(valueA: unknown, valueB: unknown): ValueOp | null {
|
|
238
|
+
function diffValue(valueA: unknown, valueB: unknown, legacyAppendMode: boolean): ValueOp | null {
|
|
233
239
|
if (Object.is(valueA, valueB)) return null
|
|
234
240
|
if (Array.isArray(valueA) && Array.isArray(valueB)) {
|
|
235
|
-
return diffArray(valueA, valueB)
|
|
241
|
+
return diffArray(valueA, valueB, legacyAppendMode)
|
|
242
|
+
} else if (typeof valueA === 'string' && typeof valueB === 'string') {
|
|
243
|
+
if (!legacyAppendMode && valueB.startsWith(valueA)) {
|
|
244
|
+
const appendedText = valueB.slice(valueA.length)
|
|
245
|
+
return [ValueOpType.Append, appendedText, valueA.length]
|
|
246
|
+
}
|
|
247
|
+
return [ValueOpType.Put, valueB]
|
|
236
248
|
} else if (!valueA || !valueB || typeof valueA !== 'object' || typeof valueB !== 'object') {
|
|
237
249
|
return isEqual(valueA, valueB) ? null : [ValueOpType.Put, valueB]
|
|
238
250
|
} else {
|
|
239
|
-
const diff = diffObject(valueA, valueB)
|
|
251
|
+
const diff = diffObject(valueA, valueB, undefined, legacyAppendMode)
|
|
240
252
|
return diff ? [ValueOpType.Patch, diff] : null
|
|
241
253
|
}
|
|
242
254
|
}
|
|
243
255
|
|
|
244
|
-
function diffArray(
|
|
256
|
+
function diffArray(
|
|
257
|
+
prevArray: unknown[],
|
|
258
|
+
nextArray: unknown[],
|
|
259
|
+
legacyAppendMode: boolean
|
|
260
|
+
): PutOp | AppendOp | PatchOp | null {
|
|
245
261
|
if (Object.is(prevArray, nextArray)) return null
|
|
246
262
|
// if lengths are equal, check for patch operation
|
|
247
263
|
if (prevArray.length === nextArray.length) {
|
|
@@ -267,7 +283,7 @@ function diffArray(prevArray: unknown[], nextArray: unknown[]): PutOp | AppendOp
|
|
|
267
283
|
if (!prevItem || !nextItem) {
|
|
268
284
|
diff[i] = [ValueOpType.Put, nextItem]
|
|
269
285
|
} else if (typeof prevItem === 'object' && typeof nextItem === 'object') {
|
|
270
|
-
const op = diffValue(prevItem, nextItem)
|
|
286
|
+
const op = diffValue(prevItem, nextItem, legacyAppendMode)
|
|
271
287
|
if (op) {
|
|
272
288
|
diff[i] = op
|
|
273
289
|
}
|
|
@@ -341,12 +357,19 @@ export function applyObjectDiff<T extends object>(object: T, objectDiff: ObjectD
|
|
|
341
357
|
break
|
|
342
358
|
}
|
|
343
359
|
case ValueOpType.Append: {
|
|
344
|
-
const
|
|
360
|
+
const value = op[1]
|
|
345
361
|
const offset = op[2]
|
|
346
|
-
const
|
|
347
|
-
if (Array.isArray(
|
|
348
|
-
set(key, [...
|
|
362
|
+
const currentValue = object[key as keyof T]
|
|
363
|
+
if (Array.isArray(currentValue) && Array.isArray(value) && currentValue.length === offset) {
|
|
364
|
+
set(key, [...currentValue, ...value])
|
|
365
|
+
} else if (
|
|
366
|
+
typeof currentValue === 'string' &&
|
|
367
|
+
typeof value === 'string' &&
|
|
368
|
+
currentValue.length === offset
|
|
369
|
+
) {
|
|
370
|
+
set(key, currentValue + value)
|
|
349
371
|
}
|
|
372
|
+
// If validation fails (type mismatch or length mismatch), silently ignore
|
|
350
373
|
break
|
|
351
374
|
}
|
|
352
375
|
case ValueOpType.Patch: {
|
package/src/lib/protocol.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SerializedSchema, UnknownRecord } from '@tldraw/store'
|
|
2
2
|
import { NetworkDiff, ObjectDiff, RecordOpType } from './diff'
|
|
3
3
|
|
|
4
|
-
const TLSYNC_PROTOCOL_VERSION =
|
|
4
|
+
const TLSYNC_PROTOCOL_VERSION = 8
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Gets the current tldraw sync protocol version number.
|
|
@@ -159,7 +159,7 @@ describe(TLSocketRoom, () => {
|
|
|
159
159
|
type: 'connect' as const,
|
|
160
160
|
connectRequestId: 'connect-1',
|
|
161
161
|
lastServerClock: 0,
|
|
162
|
-
protocolVersion:
|
|
162
|
+
protocolVersion: 8,
|
|
163
163
|
schema: store.schema.serialize(),
|
|
164
164
|
}
|
|
165
165
|
room.handleSocketMessage(sessionId1, JSON.stringify(connectRequest1))
|
|
@@ -168,7 +168,7 @@ describe(TLSocketRoom, () => {
|
|
|
168
168
|
type: 'connect' as const,
|
|
169
169
|
connectRequestId: 'connect-2',
|
|
170
170
|
lastServerClock: 0,
|
|
171
|
-
protocolVersion:
|
|
171
|
+
protocolVersion: 8,
|
|
172
172
|
schema: store.schema.serialize(),
|
|
173
173
|
}
|
|
174
174
|
room.handleSocketMessage(sessionId2, JSON.stringify(connectRequest2))
|
|
@@ -313,27 +313,28 @@ describe('TLSyncRoom.updateStore', () => {
|
|
|
313
313
|
expect(documentClock).toBeLessThan(room.documentClock)
|
|
314
314
|
|
|
315
315
|
expect(socketA.__lastMessage).toMatchInlineSnapshot(`
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
316
|
+
{
|
|
317
|
+
"data": [
|
|
318
|
+
{
|
|
319
|
+
"diff": {
|
|
320
|
+
"document:document": [
|
|
321
|
+
"patch",
|
|
322
|
+
{
|
|
323
|
+
"name": [
|
|
324
|
+
"append",
|
|
325
|
+
"My lovely document",
|
|
326
|
+
0,
|
|
327
|
+
],
|
|
328
|
+
},
|
|
329
|
+
],
|
|
330
|
+
},
|
|
331
|
+
"serverClock": 1,
|
|
332
|
+
"type": "patch",
|
|
333
|
+
},
|
|
334
|
+
],
|
|
335
|
+
"type": "data",
|
|
336
|
+
}
|
|
337
|
+
`)
|
|
337
338
|
expect(socketB.__lastMessage).toEqual(socketA.__lastMessage)
|
|
338
339
|
})
|
|
339
340
|
|
|
@@ -62,7 +62,7 @@ export class TestSocketPair<R extends UnknownRecord> {
|
|
|
62
62
|
}
|
|
63
63
|
didReceiveFromClient?: (msg: TLSocketClientSentEvent<R>) => void = undefined
|
|
64
64
|
clientDisconnected?: () => void = undefined
|
|
65
|
-
clientSocket: TLPersistentClientSocket<R
|
|
65
|
+
clientSocket: TLPersistentClientSocket<TLSocketClientSentEvent<R>, TLSocketServerSentEvent<R>> = {
|
|
66
66
|
connectionStatus: 'offline',
|
|
67
67
|
onStatusChange: (cb) => {
|
|
68
68
|
this.callbacks.onStatusChange = cb
|
|
@@ -76,7 +76,7 @@ export class TestSocketPair<R extends UnknownRecord> {
|
|
|
76
76
|
this.callbacks.onReceiveMessage = null
|
|
77
77
|
}
|
|
78
78
|
},
|
|
79
|
-
sendMessage: (msg
|
|
79
|
+
sendMessage: (msg) => {
|
|
80
80
|
if (this.clientSocket.connectionStatus !== 'online') {
|
|
81
81
|
throw new Error('trying to send before open')
|
|
82
82
|
}
|
|
@@ -87,6 +87,9 @@ export class TestSocketPair<R extends UnknownRecord> {
|
|
|
87
87
|
this.disconnect()
|
|
88
88
|
this.connect()
|
|
89
89
|
},
|
|
90
|
+
close: () => {
|
|
91
|
+
this.disconnect()
|
|
92
|
+
},
|
|
90
93
|
}
|
|
91
94
|
|
|
92
95
|
callbacks = {
|
package/src/test/diff.test.ts
CHANGED
|
@@ -408,6 +408,155 @@ describe('array diffing comprehensive', () => {
|
|
|
408
408
|
})
|
|
409
409
|
})
|
|
410
410
|
|
|
411
|
+
describe('string appending', () => {
|
|
412
|
+
describe('basic string appending', () => {
|
|
413
|
+
it('should handle string appends', () => {
|
|
414
|
+
const prev = { text: 'Hello' }
|
|
415
|
+
const next = { text: 'Hello world' }
|
|
416
|
+
|
|
417
|
+
expect(diffRecord(prev, next)).toEqual({
|
|
418
|
+
text: [ValueOpType.Append, ' world', 5],
|
|
419
|
+
})
|
|
420
|
+
})
|
|
421
|
+
|
|
422
|
+
it('should handle empty string to non-empty', () => {
|
|
423
|
+
const prev = { text: '' }
|
|
424
|
+
const next = { text: 'Hello' }
|
|
425
|
+
|
|
426
|
+
expect(diffRecord(prev, next)).toEqual({
|
|
427
|
+
text: [ValueOpType.Append, 'Hello', 0],
|
|
428
|
+
})
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
it('should use put when string is replaced (not appended)', () => {
|
|
432
|
+
const prev = { text: 'Hello' }
|
|
433
|
+
const next = { text: 'Goodbye' }
|
|
434
|
+
|
|
435
|
+
expect(diffRecord(prev, next)).toEqual({
|
|
436
|
+
text: [ValueOpType.Put, 'Goodbye'],
|
|
437
|
+
})
|
|
438
|
+
})
|
|
439
|
+
|
|
440
|
+
it('should use put when string is shortened', () => {
|
|
441
|
+
const prev = { text: 'Hello world' }
|
|
442
|
+
const next = { text: 'Hello' }
|
|
443
|
+
|
|
444
|
+
expect(diffRecord(prev, next)).toEqual({
|
|
445
|
+
text: [ValueOpType.Put, 'Hello'],
|
|
446
|
+
})
|
|
447
|
+
})
|
|
448
|
+
|
|
449
|
+
it('should handle identical strings', () => {
|
|
450
|
+
const prev = { text: 'Hello' }
|
|
451
|
+
const next = { text: 'Hello' }
|
|
452
|
+
|
|
453
|
+
expect(diffRecord(prev, next)).toBeNull()
|
|
454
|
+
})
|
|
455
|
+
|
|
456
|
+
it('should handle large text append', () => {
|
|
457
|
+
const prev = { text: 'Start' }
|
|
458
|
+
const longText = ' '.repeat(1000) + 'end'
|
|
459
|
+
const next = { text: 'Start' + longText }
|
|
460
|
+
|
|
461
|
+
const diff = diffRecord(prev, next)
|
|
462
|
+
expect(diff).toEqual({
|
|
463
|
+
text: [ValueOpType.Append, longText, 5],
|
|
464
|
+
})
|
|
465
|
+
})
|
|
466
|
+
})
|
|
467
|
+
|
|
468
|
+
describe('string appending in nested props', () => {
|
|
469
|
+
it('should handle string appending in nested props', () => {
|
|
470
|
+
const prev = { id: 'test:1', props: { label: 'Hello' } }
|
|
471
|
+
const next = { id: 'test:1', props: { label: 'Hello world' } }
|
|
472
|
+
|
|
473
|
+
expect(diffRecord(prev, next)).toEqual({
|
|
474
|
+
props: [ValueOpType.Patch, { label: [ValueOpType.Append, ' world', 5] }],
|
|
475
|
+
})
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
it('should combine string appending with other property changes', () => {
|
|
479
|
+
const prev = { text: 'Hello', x: 100 }
|
|
480
|
+
const next = { text: 'Hello world', x: 200 }
|
|
481
|
+
|
|
482
|
+
expect(diffRecord(prev, next)).toEqual({
|
|
483
|
+
text: [ValueOpType.Append, ' world', 5],
|
|
484
|
+
x: [ValueOpType.Put, 200],
|
|
485
|
+
})
|
|
486
|
+
})
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
describe('apply string appending', () => {
|
|
490
|
+
it('should apply append operations correctly', () => {
|
|
491
|
+
const obj = { text: 'Hello' }
|
|
492
|
+
const diff: ObjectDiff = {
|
|
493
|
+
text: [ValueOpType.Append, ' world', 5],
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
const result = applyObjectDiff(obj, diff)
|
|
497
|
+
expect(result).toEqual({ text: 'Hello world' })
|
|
498
|
+
expect(result).not.toBe(obj)
|
|
499
|
+
})
|
|
500
|
+
|
|
501
|
+
it('should handle append from empty string', () => {
|
|
502
|
+
const obj = { text: '' }
|
|
503
|
+
const diff: ObjectDiff = {
|
|
504
|
+
text: [ValueOpType.Append, 'Hello', 0],
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const result = applyObjectDiff(obj, diff)
|
|
508
|
+
expect(result).toEqual({ text: 'Hello' })
|
|
509
|
+
})
|
|
510
|
+
|
|
511
|
+
it('should ignore append operation with wrong offset', () => {
|
|
512
|
+
const obj = { text: 'Hello' }
|
|
513
|
+
const diff: ObjectDiff = {
|
|
514
|
+
text: [ValueOpType.Append, ' world', 10], // Wrong offset
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const result = applyObjectDiff(obj, diff)
|
|
518
|
+
expect(result).toBe(obj) // No change, same reference
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
it('should ignore append operation on non-string value', () => {
|
|
522
|
+
const obj = { text: 123 }
|
|
523
|
+
const diff: ObjectDiff = {
|
|
524
|
+
text: [ValueOpType.Append, ' world', 3],
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
const result = applyObjectDiff(obj, diff)
|
|
528
|
+
expect(result).toBe(obj) // No change, same reference
|
|
529
|
+
})
|
|
530
|
+
|
|
531
|
+
it('should handle multiple stream operations', () => {
|
|
532
|
+
const obj = { a: 'Hello', b: 'Foo' }
|
|
533
|
+
const diff: ObjectDiff = {
|
|
534
|
+
a: [ValueOpType.Append, ' world', 5],
|
|
535
|
+
b: [ValueOpType.Append, 'bar', 3],
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
const result = applyObjectDiff(obj, diff)
|
|
539
|
+
expect(result).toEqual({ a: 'Hello world', b: 'Foobar' })
|
|
540
|
+
})
|
|
541
|
+
|
|
542
|
+
it('should integrate with network diff workflow', () => {
|
|
543
|
+
const prev = { id: 'shape:1', type: 'text', text: 'Hello' }
|
|
544
|
+
const next = { id: 'shape:1', type: 'text', text: 'Hello world' }
|
|
545
|
+
|
|
546
|
+
const recordsDiff = {
|
|
547
|
+
added: {},
|
|
548
|
+
updated: { 'shape:1': [prev, next] },
|
|
549
|
+
removed: {},
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const networkDiff = getNetworkDiff(recordsDiff)
|
|
553
|
+
expect(networkDiff).toEqual({
|
|
554
|
+
'shape:1': [RecordOpType.Patch, { text: [ValueOpType.Append, ' world', 5] }],
|
|
555
|
+
})
|
|
556
|
+
})
|
|
557
|
+
})
|
|
558
|
+
})
|
|
559
|
+
|
|
411
560
|
describe('applyObjectDiff comprehensive', () => {
|
|
412
561
|
describe('basic operations', () => {
|
|
413
562
|
it('should create new object when changes are needed', () => {
|
|
@@ -582,3 +731,54 @@ describe('complex scenarios', () => {
|
|
|
582
731
|
})
|
|
583
732
|
})
|
|
584
733
|
})
|
|
734
|
+
|
|
735
|
+
describe('nested key primitive value bug', () => {
|
|
736
|
+
it('should handle string changes in nested keys', () => {
|
|
737
|
+
// This tests the bug where nested keys (like 'props') with primitive values
|
|
738
|
+
// are silently dropped instead of being diffed properly
|
|
739
|
+
const prev = { id: 'shape:1', props: 'hello' }
|
|
740
|
+
const next = { id: 'shape:1', props: 'world' }
|
|
741
|
+
|
|
742
|
+
const diff = diffRecord(prev, next)
|
|
743
|
+
|
|
744
|
+
// The diff should contain a 'put' operation for props
|
|
745
|
+
expect(diff).toEqual({
|
|
746
|
+
props: [ValueOpType.Put, 'world'],
|
|
747
|
+
})
|
|
748
|
+
})
|
|
749
|
+
|
|
750
|
+
it('should handle string appending in nested keys', () => {
|
|
751
|
+
const prev = { id: 'shape:1', props: 'hello' }
|
|
752
|
+
const next = { id: 'shape:1', props: 'hello world' }
|
|
753
|
+
|
|
754
|
+
const diff = diffRecord(prev, next)
|
|
755
|
+
|
|
756
|
+
// The diff should contain an 'append' operation for props
|
|
757
|
+
expect(diff).toEqual({
|
|
758
|
+
props: [ValueOpType.Append, ' world', 5],
|
|
759
|
+
})
|
|
760
|
+
})
|
|
761
|
+
|
|
762
|
+
it('should handle number changes in nested keys', () => {
|
|
763
|
+
const prev = { id: 'shape:1', props: 42 }
|
|
764
|
+
const next = { id: 'shape:1', props: 100 }
|
|
765
|
+
|
|
766
|
+
const diff = diffRecord(prev, next)
|
|
767
|
+
|
|
768
|
+
expect(diff).toEqual({
|
|
769
|
+
props: [ValueOpType.Put, 100],
|
|
770
|
+
})
|
|
771
|
+
})
|
|
772
|
+
|
|
773
|
+
it('should still handle object changes in nested keys normally', () => {
|
|
774
|
+
const prev = { id: 'shape:1', props: { color: 'red' } }
|
|
775
|
+
const next = { id: 'shape:1', props: { color: 'blue' } }
|
|
776
|
+
|
|
777
|
+
const diff = diffRecord(prev, next)
|
|
778
|
+
|
|
779
|
+
// Objects in nested keys should still use patch
|
|
780
|
+
expect(diff).toEqual({
|
|
781
|
+
props: [ValueOpType.Patch, { color: [ValueOpType.Put, 'blue'] }],
|
|
782
|
+
})
|
|
783
|
+
})
|
|
784
|
+
})
|