@@ -69,7 +69,7 @@ function matchByNameAndType(
6969/**
7070 * Automatically match local and remote sources if they have the same UID
7171 */
72- function matchByUUID (
72+ function matchByUIDandUUID (
7373 local : LocalSource [ ] ,
7474 remote : RemoteSource [ ] ,
7575 ids : IdentifiersExtensions ,
@@ -82,27 +82,29 @@ function matchByUUID(
8282 const matched : IdentifiersExtensions = { }
8383 const pendingLocal : LocalSource [ ] = [ ]
8484
85- // First, match by UID
85+ // First, try to match by UID, then by UUID.
8686 local . forEach ( ( localSource ) => {
87- const possibleMatch = remote . find ( ( remoteSource ) => remoteSource . id === localSource . uid )
88- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
89- if ( possibleMatch ) matched [ localSource . localIdentifier ] = possibleMatch . id !
90- else pendingLocal . push ( localSource )
91- } )
87+ const matchByUID = remote . find ( ( remoteSource ) => remoteSource . id === localSource . uid )
88+ const matchByUUID = remote . find ( ( remoteSource ) => remoteSource . uuid === ids [ localSource . localIdentifier ] )
9289
93- // Then, match by UUID
94- pendingLocal . forEach ( ( localSource ) => {
95- const possibleMatch = remote . find ( ( remoteSource ) => remoteSource . uuid === ids [ localSource . localIdentifier ] )
96- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
97- if ( possibleMatch ) matched [ localSource . localIdentifier ] = possibleMatch . uuid !
90+ if ( matchByUID ) {
91+ matched [ localSource . localIdentifier ] = matchByUID . id
92+ } else if ( matchByUUID ) {
93+ matched [ localSource . localIdentifier ] = matchByUUID . uuid
94+ } else {
95+ pendingLocal . push ( localSource )
96+ }
9897 } )
9998
100- // The rest, are either to be created or deleted.
101- // const {matched, toCreate, toConfirm, toManualMatch} = matchByNameAndType(local, notMigratedRemoteExtensions)
99+ const pendingRemote = remote . filter (
100+ ( remoteSource ) =>
101+ ! Object . values ( matched ) . includes ( remoteSource . uuid ) && ! Object . values ( matched ) . includes ( remoteSource . id ) ,
102+ )
102103
103- const toCreate = local . filter ( ( elem ) => ! matched [ elem . localIdentifier ] )
104+ // Then, try to match by name and type as a last resort.
105+ const { matched : matchedByName , toCreate, toConfirm, toManualMatch} = matchByNameAndType ( pendingLocal , pendingRemote )
104106
105- return { matched, toCreate, toConfirm : [ ] , toManualMatch : { local : [ ] , remote : [ ] } }
107+ return { matched : { ... matched , ... matchedByName } , toCreate, toConfirm, toManualMatch}
106108}
107109
108110function migrateLegacyFunctions (
@@ -241,7 +243,7 @@ export async function automaticMatchmaking(
241243 const { local, remote} = pendingAfterMigratingFunctions
242244
243245 const { matched, toCreate, toConfirm, toManualMatch} = useUuidMatching
244- ? matchByUUID ( localSources , remoteSources , ids )
246+ ? matchByUIDandUUID ( localSources , remoteSources , ids )
245247 : matchByNameAndType ( local , remote )
246248
247249 return {
0 commit comments