Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/persist-plugins/mmkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ export class ObservablePersistMMKV implements ObservablePersistPlugin {
const v = this.data[table];
if (v !== undefined) {
try {
storage.set(table, safeStringify(v));
if (v !== null) {
storage.set(table, safeStringify(v));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I think the root issue is how safeStringify() is implemented:

return value ? JSON.stringify(value, replacer) : value;

It means that if the value is false (false, 0, "" or null), it won't be serialized as a string. If this called JSON.stringify() directly, this would work properly as null would be serialised "null" which can be saved and restored with MMKV.

I'm not sure I understand the intent of safeStringify(), I would expect it to always return either a string or undefined.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably also means that persisting 0 (or false) is broken too, as it will be persisted as a number but we will attempt to restore it with storage.getString()

} else {
storage.delete(table);
}
} catch (err) {
console.error(err);
}
Expand Down