Sitelet https://github.com/unjs/unstorage/issues/749
Skip to content

Support React Native(MMKV) #749

Description

@hairyf

Describe the feature

import type { Configuration } from 'react-native-mmkv'
import { createMMKV } from 'react-native-mmkv'
import { defineDriver } from 'unstorage'

export const mmkvStorageDriver = defineDriver<Configuration, never>((options) => {
  const mmkv = createMMKV(options)
  return {
    name: 'mmkv-storage',
    options,
    async hasItem(key) {
      return mmkv.contains(key)
    },
    async getItem(key) {
      const value = mmkv.getString(key)
      return value
    },
    async setItem(key, value) {
      mmkv.set(key, value)
    },
    async removeItem(key) {
      mmkv.remove(key)
    },
    async getKeys() {
      return mmkv.getAllKeys()
    },
    async clear() {
      mmkv.clearAll()
    },
    async watch(callback) {
      const unsub = mmkv.addOnValueChangedListener((key) => {
        callback('update', key)
      })
      return () => unsub.remove()
    },
  }
})

Additional information

  • Would you be willing to help implement this feature?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions