Skip to content

Events

StorageAdapter uses @logosdx/observer for reactive events.

Event Names

EventFired
before-setBefore a value is written
after-setAfter a value is written
before-removeBefore a key is removed
after-removeAfter a key is removed
clearWhen clear() is called

Event Payload

typescript
interface StorageEventPayload<V, K extends keyof V> {
    key: K
    value?: V[K] | null
}

Which fields are present depends on the event:

Eventkeyvalue
before-setthe key being writtenthe new value (or null if undefined)
after-setthe key that was writtenthe new value (or null if undefined)
before-removethe key being removednull
after-removethe key that was removednull
clearno payloadno payload

undefined becomes null

If the value passed to set() is undefined, the event payload normalizes it to null.

Subscribing

on() returns a cleanup function. Use off() for manual removal.

typescript
// Subscribe -- returns cleanup function
const cleanup = storage.on('after-set', (event) => {
    console.log('Set:', event.key, event.value)
})

// Remove later
cleanup()

// Or use off() directly
function handleRemove(event) {
    console.log('Removed:', event.key)
}

storage.on('before-remove', handleRemove)
storage.off('before-remove', handleRemove)

Event Ordering

  • before-* fires before the driver operation.
  • after-* fires after the driver operation.
  • Bulk set({ ... }) emits one event pair per key.