1
0
mirror of https://github.com/excalidraw/excalidraw.git synced 2024-11-02 03:25:53 +01:00

feat: load old library if migration fails

This commit is contained in:
dwelle 2024-03-11 09:57:01 +01:00
parent 6a385d6663
commit b7babe554b

@ -765,6 +765,7 @@ export const useHandleLibrary = (
initDataPromise.resolve( initDataPromise.resolve(
promiseTry(migrationAdapter.load) promiseTry(migrationAdapter.load)
.then(async (libraryData) => { .then(async (libraryData) => {
let restoredData: LibraryItems | null = null;
try { try {
// if no library data to migrate, assume no migration needed // if no library data to migrate, assume no migration needed
// and skip persisting to new data store, as well as well // and skip persisting to new data store, as well as well
@ -773,17 +774,16 @@ export const useHandleLibrary = (
return AdapterTransaction.getLibraryItems(adapter, "load"); return AdapterTransaction.getLibraryItems(adapter, "load");
} }
restoredData = restoreLibraryItems(
libraryData.libraryItems || [],
"published",
);
// we don't queue this operation because it's running inside // we don't queue this operation because it's running inside
// a promise that's running inside Library update queue itself // a promise that's running inside Library update queue itself
const nextItems = await persistLibraryUpdate( const nextItems = await persistLibraryUpdate(
adapter, adapter,
createLibraryUpdate( createLibraryUpdate([], restoredData),
[],
restoreLibraryItems(
libraryData.libraryItems || [],
"published",
),
),
); );
try { try {
await migrationAdapter.clear(); await migrationAdapter.clear();
@ -798,8 +798,8 @@ export const useHandleLibrary = (
console.error( console.error(
`couldn't migrate legacy library data: ${error.message}`, `couldn't migrate legacy library data: ${error.message}`,
); );
// migration failed, load empty library // migration failed, load data from previous store, if any
return []; return restoredData;
} }
}) })
// errors caught during `migrationAdapter.load()` // errors caught during `migrationAdapter.load()`