|
| 1 | +import { ExcalidrawElement } from '../types'; |
| 2 | +import { arrayToMap } from './array.to.map'; |
| 3 | + |
| 4 | +const shouldDiscardRemoteElement = ( |
| 5 | + local: ExcalidrawElement | undefined, |
| 6 | + remote: ExcalidrawElement, |
| 7 | +): boolean => { |
| 8 | + return !!( |
| 9 | + local && |
| 10 | + // local element is newer |
| 11 | + (local.version > remote.version || |
| 12 | + // resolve conflicting edits deterministically by taking the one with |
| 13 | + // the lowest versionNonce |
| 14 | + (local.version === remote.version && |
| 15 | + local.versionNonce < remote.versionNonce)) |
| 16 | + ); |
| 17 | +}; |
| 18 | + |
| 19 | +/*const validateIndicesThrottled = throttle( |
| 20 | + ( |
| 21 | + orderedElements: readonly OrderedExcalidrawElement[], |
| 22 | + localElements: readonly OrderedExcalidrawElement[], |
| 23 | + remoteElements: readonly RemoteExcalidrawElement[], |
| 24 | + ) => { |
| 25 | + if ( |
| 26 | + import.meta.env.DEV || |
| 27 | + import.meta.env.MODE === ENV.TEST || |
| 28 | + window?.DEBUG_FRACTIONAL_INDICES |
| 29 | + ) { |
| 30 | + // create new instances due to the mutation |
| 31 | + const elements = syncInvalidIndices( |
| 32 | + orderedElements.map((x) => ({ ...x })), |
| 33 | + ); |
| 34 | +
|
| 35 | + validateFractionalIndices(elements, { |
| 36 | + // throw in dev & test only, to remain functional on `DEBUG_FRACTIONAL_INDICES` |
| 37 | + shouldThrow: import.meta.env.DEV || import.meta.env.MODE === ENV.TEST, |
| 38 | + includeBoundTextValidation: true, |
| 39 | + reconciliationContext: { |
| 40 | + localElements, |
| 41 | + remoteElements, |
| 42 | + }, |
| 43 | + }); |
| 44 | + } |
| 45 | + }, |
| 46 | + 1000 * 60, |
| 47 | + { leading: true, trailing: false }, |
| 48 | +);*/ |
| 49 | + |
| 50 | +export const reconcileElements = ( |
| 51 | + localElements: readonly ExcalidrawElement[], |
| 52 | + remoteElements: readonly ExcalidrawElement[], |
| 53 | +): ExcalidrawElement[] => { |
| 54 | + const localElementsMap = arrayToMap(localElements); |
| 55 | + const reconciledElements: ExcalidrawElement[] = []; |
| 56 | + const added = new Set<string>(); |
| 57 | + |
| 58 | + // process remote elements |
| 59 | + for (const remoteElement of remoteElements) { |
| 60 | + if (!added.has(remoteElement.id)) { |
| 61 | + const localElement = localElementsMap.get(remoteElement.id); |
| 62 | + const discardRemoteElement = shouldDiscardRemoteElement( |
| 63 | + localElement, |
| 64 | + remoteElement, |
| 65 | + ); |
| 66 | + |
| 67 | + if (localElement && discardRemoteElement) { |
| 68 | + reconciledElements.push(localElement); |
| 69 | + added.add(localElement.id); |
| 70 | + } else { |
| 71 | + reconciledElements.push(remoteElement); |
| 72 | + added.add(remoteElement.id); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // process remaining local elements |
| 78 | + for (const localElement of localElements) { |
| 79 | + if (!added.has(localElement.id)) { |
| 80 | + reconciledElements.push(localElement); |
| 81 | + added.add(localElement.id); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // const orderedElements = orderByFractionalIndex(reconciledElements); |
| 86 | + |
| 87 | + // validateIndicesThrottled(orderedElements, localElements, remoteElements); |
| 88 | + |
| 89 | + // de-duplicate indices |
| 90 | + // syncInvalidIndices(orderedElements); |
| 91 | + |
| 92 | + // return orderedElements as ReconciledExcalidrawElement[]; |
| 93 | + return reconciledElements; |
| 94 | +}; |
0 commit comments