mirror of
https://github.com/XFox111/TabsAsideExtension.git
synced 2026-04-22 07:58:01 +03:00
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { CollectionItem } from "@/models/CollectionModels";
|
|
import getLogger from "@/utils/getLogger";
|
|
import { collectionStorage } from "./collectionStorage";
|
|
import getCollectionsFromCloud from "./getCollectionsFromCloud";
|
|
import getCollectionsFromLocal from "./getCollectionsFromLocal";
|
|
import saveCollectionsToCloud from "./saveCollectionsToCloud";
|
|
import saveCollectionsToLocal from "./saveCollectionsToLocal";
|
|
|
|
const logger = getLogger("resolveConflict");
|
|
|
|
export default function resolveConflict(acceptSource: "local" | "sync"): Promise<void>
|
|
{
|
|
if (acceptSource === "local")
|
|
return replaceCloudWithLocal();
|
|
|
|
return replaceLocalWithCloud();
|
|
}
|
|
|
|
async function replaceCloudWithLocal(): Promise<void>
|
|
{
|
|
const collections: CollectionItem[] = await getCollectionsFromLocal();
|
|
const lastUpdated: number = await collectionStorage.localLastUpdated.getValue();
|
|
|
|
await saveCollectionsToCloud(collections, lastUpdated);
|
|
}
|
|
|
|
async function replaceLocalWithCloud(): Promise<void>
|
|
{
|
|
try
|
|
{
|
|
const collections: CollectionItem[] = await getCollectionsFromCloud();
|
|
const lastUpdated: number = await collectionStorage.syncLastUpdated.getValue();
|
|
|
|
await saveCollectionsToLocal(collections, lastUpdated);
|
|
}
|
|
catch (ex)
|
|
{
|
|
logger("Failed to get cloud storage");
|
|
console.error(ex);
|
|
}
|
|
}
|