ammarahm-ed / react-native-mmkv-storage

An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
https://rnmmkv.now.sh
MIT License
1.58k stars 109 forks source link

[Bug] Values set to `null` or `undefined` are not reflected by other observers #244

Closed autoreleasefool closed 2 years ago

autoreleasefool commented 2 years ago

Describe the bug

Any operation that sets a value for a specific key to null or undefined is ignored by any other places the key is being observed.

To Reproduce Steps to reproduce the behavior:

// storage.ts
export const storage = new MMKVStorage.Loader().initialize();
export function useStorage = create(storage)

// Component.tsx
import { storage, useStorage } from './storage.ts'

function Component() {
  const [value] = useStorage('myKey', 'some string');
  useEffect(() => {
    function update(event) {
      console.log(`Value changed for myKey to ${event.value}`)
    }

    storage.ev.subscribe('myKey:onwrite', update)
    return () => storage.ev.unsubscribe('myKey:onwrite', update)
  }, [])

  return <View />
}

// other.ts
import { storage } from './storage.ts'

function clearStorage() {
  storage.setString('some new string')
  storage.clearStore() // or, storage.removeItem('myKey')
}

I would expect, in the above scenario, for my update function, after calling clearStorage() to output:

Value changed for myKey to some new string
Value changed for myKey to undefined

But it is never changed to undefined

Expected behavior

Setting a value for a key to null or undefined should cause all observers to update and adopt the new value accordingly.

Screenshots If applicable, add screenshots to help explain your problem.

Platform Information: