MCPcopy Create free account
hub / github.com/anomalyco/opencode / migrateLegacyAsync

Function migrateLegacyAsync

packages/app/src/utils/persist.ts:298–338  ·  view source on GitHub ↗
(input: {
  current: AsyncStorage
  legacyStore?: AsyncStorage
  stores: AsyncStorage[]
  keys: string[]
  key: string
  defaults: unknown
  migrate?: (value: unknown) => unknown
})

Source from the content-addressed store, hash-verified

296}
297
298async function migrateLegacyAsync(input: {
299 current: AsyncStorage
300 legacyStore?: AsyncStorage
301 stores: AsyncStorage[]
302 keys: string[]
303 key: string
304 defaults: unknown
305 migrate?: (value: unknown) => unknown
306}) {
307 for (const store of input.stores) {
308 const raw = await store.getItem(input.key)
309 if (raw === null) continue
310
311 const next = normalize(input.defaults, raw, input.migrate)
312 if (next === undefined) {
313 await removeAsync(store, input.key)
314 continue
315 }
316 await input.current.setItem(input.key, next)
317 await store.removeItem(input.key)
318 return next
319 }
320
321 if (!input.legacyStore) return null
322
323 for (const key of input.keys) {
324 const raw = await input.legacyStore.getItem(key)
325 if (raw === null) continue
326
327 const next = normalize(input.defaults, raw, input.migrate)
328 if (next === undefined) {
329 await removeAsync(input.legacyStore, key)
330 continue
331 }
332 await input.current.setItem(input.key, next)
333 await input.legacyStore.removeItem(key)
334 return next
335 }
336
337 return null
338}
339
340function workspaceStorage(dir: string) {
341 const head = (dir.slice(0, 12) || "workspace").replace(/[^a-zA-Z0-9._-]/g, "-")

Callers 1

persistedFunction · 0.85

Calls 5

removeAsyncFunction · 0.85
getItemMethod · 0.80
setItemMethod · 0.80
removeItemMethod · 0.80
normalizeFunction · 0.70

Tested by

no test coverage detected