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