feature: bili-toy cloud fallback local

This commit is contained in:
Vick Scarlet
2026-08-16 19:52:03 +08:00
parent da997b7a8e
commit 2c615523e3
4 changed files with 67 additions and 16 deletions
+48 -8
View File
@@ -1,6 +1,7 @@
const PREFIX = 'bili-toy-remake'
const storage = {
get: async (keys: string[]) => {
var result = await toy.getCloudStorage(keys)
const result = await toy.getCloudStorage(keys)
if (result && result.data && typeof result.data === 'object') {
return result.data
}
@@ -24,19 +25,58 @@ export async function init() {
}
}
export async function get(key: string) {
function getCloudInstance(key: string) {
if (!cloudMap.has(key)) {
const cloud = ToyCloudSaveCore.createCloudSave({ storage, prefix: key })
cloudMap.set(key, cloud)
}
return await cloudMap.get(key)!.load()
return cloudMap.get(key) || null
}
export async function set(key: string, value: string) {
if (!cloudMap.has(key)) {
const cloud = ToyCloudSaveCore.createCloudSave({ storage, prefix: key })
cloudMap.set(key, cloud)
export async function get(key: string): Promise<string | null> {
const localKey = `${PREFIX}-${key}`
const localData = localStorage.getItem(localKey)
const cloud = getCloudInstance(key)
if (!cloud) return localData
try {
const cloudData = await cloud.load()
if (cloudData !== null) {
localStorage.setItem(localKey, cloudData)
return cloudData
}
} catch (error) {
console.error(
`[Bili Storage] 业务 Key '${key}' 云端拉取失败,降级使用本地档:`,
error,
)
}
return localData
}
export async function set(key: string, value: string): Promise<boolean> {
const localKey = `${PREFIX}-${key}`
try {
localStorage.setItem(localKey, value)
} catch (localError) {
console.error(
`[Bili Storage] 本地 LocalStorage 写入崩溃(可能满 5MB 额度):`,
localError,
)
}
const cloud = getCloudInstance(key)
if (!cloud) return true
try {
await cloud.save(value)
} catch (cloudError) {
console.error(
`[Bili Storage] 业务 Key '${key}' 本地已保存,但同步至 B 站云端失败:`,
cloudError,
)
}
await cloudMap.get(key)!.save(value)
return true
}