feat: change LibraryPersistenceAdapter `load()` `source` -> `priority`

to clarify the semantics
This commit is contained in:
dwelle 2024-03-11 09:40:51 +01:00
parent 2382fad4f6
commit 6a385d6663
2 changed files with 14 additions and 14 deletions

View File

@ -318,7 +318,7 @@ const ExcalidrawWrapper = () => {
useHandleLibrary({
excalidrawAPI,
adapter: LibraryIndexedDBAdapter,
// TODO maybe remove this in several months (shipped: 24-02-07)
// TODO maybe remove this in several months (shipped: 24-03-11)
migrationAdapter: LibraryLocalStorageMigrationAdapter,
});

View File

@ -51,6 +51,8 @@ const onLibraryUpdateEmitter = new Emitter<
[update: LibraryUpdate, libraryItems: LibraryItems]
>();
export type LibraryAdatapterSource = "load" | "save";
export interface LibraryPersistenceAdapter {
/**
* Should load data that were previously saved into the database using the
@ -61,12 +63,10 @@ export interface LibraryPersistenceAdapter {
*/
load(metadata: {
/**
* Priority 1 indicates we're loading latest data with intent
* to reconcile with before save.
* Priority 2 indicates we're loading for read-only purposes, so
* host app can implement more aggressive caching strategy.
* Indicates whether we're loading data for save purposes, or reading
* purposes, in which case host app can implement more aggressive caching.
*/
priority: 1 | 2;
source: LibraryAdatapterSource;
}): MaybePromise<{ libraryItems: LibraryItems_anyVersion } | null>;
/** Should persist to the database as is (do no change the data structure). */
save(libraryData: LibraryPersistedData): MaybePromise<void>;
@ -487,13 +487,13 @@ class AdapterTransaction {
static async getLibraryItems(
adapter: LibraryPersistenceAdapter,
priority: 1 | 2,
source: LibraryAdatapterSource,
_queue = true,
): Promise<LibraryItems> {
const task = () =>
new Promise<LibraryItems>(async (resolve, reject) => {
try {
const data = await adapter.load({ priority });
const data = await adapter.load({ source });
resolve(restoreLibraryItems(data?.libraryItems || [], "published"));
} catch (error: any) {
reject(error);
@ -523,8 +523,8 @@ class AdapterTransaction {
this.adapter = adapter;
}
getLibraryItems(priority: 1 | 2) {
return AdapterTransaction.getLibraryItems(this.adapter, priority, false);
getLibraryItems(source: LibraryAdatapterSource) {
return AdapterTransaction.getLibraryItems(this.adapter, source, false);
}
}
@ -551,7 +551,7 @@ const persistLibraryUpdate = async (
return await AdapterTransaction.run(adapter, async (transaction) => {
const nextLibraryItemsMap = arrayToMap(
await transaction.getLibraryItems(1),
await transaction.getLibraryItems("save"),
);
for (const [id] of update.deletedItems) {
@ -770,7 +770,7 @@ export const useHandleLibrary = (
// and skip persisting to new data store, as well as well
// clearing the old store via `migrationAdapter.clear()`
if (!libraryData) {
return AdapterTransaction.getLibraryItems(adapter, 2);
return AdapterTransaction.getLibraryItems(adapter, "load");
}
// we don't queue this operation because it's running inside
@ -806,12 +806,12 @@ export const useHandleLibrary = (
.catch((error: any) => {
console.error(`error during library migration: ${error.message}`);
// as a default, load latest library from current data source
return AdapterTransaction.getLibraryItems(adapter, 2);
return AdapterTransaction.getLibraryItems(adapter, "load");
}),
);
} else {
initDataPromise.resolve(
promiseTry(AdapterTransaction.getLibraryItems, adapter, 2),
promiseTry(AdapterTransaction.getLibraryItems, adapter, "load"),
);
}