-
But regardless, I'd love to catch errors with adapters and then use a different adapter. Such as if we can't use OPFS I fallback to localstorage. A catch here is that I like that Still worth creating this issue to discuss further? Happy to move to a discussion... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The Here’s an example how a fallback adapter could look like. function createFallbackPersistenceAdapter(name: string) {
let usedAdapter: PersistenceAdapter = createOPFSAdapter(name)
const initializeFallbackAdapter = () => createLocalStorageAdapter(name)
return createPersistenceAdapter({
register: async (...args) => {
try {
return await usedAdapter.register(...args)
} catch (error) {
usedAdapter = initializeFallbackAdapter()
return usedAdapter.register(...args)
}
},
load: async (...args) => usedAdapter.load(...args),
save: async (...args) => usedAdapter.save(...args),
})
} |
Beta Was this translation helpful? Give feedback.
The
persistence.error
event is triggered when an error occurs during registration. In your case, implementing a fallback to a different persistence adapter is probably only feasible with a custom adapter. However, this custom adapter could simply wrap existing persistence adapters and act as a proxy. I’m using a similar approach in my project.Here’s an example how a fallback adapter could look like.