react-native-mmkv 4.3.0 → 4.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,13 +7,13 @@ import androidx.annotation.Nullable;
7
7
  import com.facebook.react.bridge.NativeModule;
8
8
  import com.facebook.react.bridge.ReactApplicationContext;
9
9
  import com.facebook.react.module.model.ReactModuleInfoProvider;
10
- import com.facebook.react.TurboReactPackage;
10
+ import com.facebook.react.BaseReactPackage;
11
11
  import com.margelo.nitro.core.HybridObject;
12
12
 
13
13
  import java.util.HashMap;
14
14
  import java.util.function.Supplier;
15
15
 
16
- public class NitroMmkvPackage extends TurboReactPackage {
16
+ public class NitroMmkvPackage extends BaseReactPackage {
17
17
  @Nullable
18
18
  @Override
19
19
  public NativeModule getModule(String name, ReactApplicationContext reactContext) {
@@ -1,3 +1,4 @@
1
+ import { createTextDecoder } from '../web/createTextDecoder';
1
2
  import { createTextEncoder } from '../web/createTextEncoder';
2
3
  import { getLocalStorage, LOCAL_STORAGE_KEY_WILDCARD, } from '../web/getLocalStorage';
3
4
  export function createMMKV(config = { id: 'mmkv.default' }) {
@@ -7,6 +8,7 @@ export function createMMKV(config = { id: 'mmkv.default' }) {
7
8
  if (config.path != null) {
8
9
  throw new Error("MMKV: 'path' is not supported on Web!");
9
10
  }
11
+ const textDecoder = createTextDecoder();
10
12
  const textEncoder = createTextEncoder();
11
13
  const listeners = new Set();
12
14
  if (config.id.includes(LOCAL_STORAGE_KEY_WILDCARD)) {
@@ -58,7 +60,12 @@ export function createMMKV(config = { id: 'mmkv.default' }) {
58
60
  const storage = getLocalStorage();
59
61
  if (key === '')
60
62
  throw new Error('Cannot set a value for an empty key!');
61
- storage.setItem(prefixedKey(key), value.toString());
63
+ if (value instanceof ArrayBuffer) {
64
+ storage.setItem(prefixedKey(key), textDecoder.decode(value));
65
+ }
66
+ else {
67
+ storage.setItem(prefixedKey(key), String(value));
68
+ }
62
69
  callListeners(key);
63
70
  },
64
71
  getString: (key) => {
@@ -0,0 +1 @@
1
+ export declare function createTextDecoder(): TextDecoder;
@@ -0,0 +1,16 @@
1
+ export function createTextDecoder() {
2
+ const g = global ?? globalThis ?? window;
3
+ if (g.TextDecoder != null) {
4
+ return new g.TextDecoder();
5
+ }
6
+ else {
7
+ return {
8
+ decode: () => {
9
+ throw new Error('TextDecoder is not supported in this environment!');
10
+ },
11
+ encoding: 'utf-8',
12
+ fatal: false,
13
+ ignoreBOM: false,
14
+ };
15
+ }
16
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-mmkv",
3
- "version": "4.3.0",
3
+ "version": "4.3.1",
4
4
  "description": "⚡️ The fastest key/value storage for React Native.",
5
5
  "main": "lib/index",
6
6
  "module": "lib/index",
@@ -1,5 +1,6 @@
1
1
  import type { MMKV } from '../specs/MMKV.nitro'
2
2
  import type { Configuration } from '../specs/MMKVFactory.nitro'
3
+ import { createTextDecoder } from '../web/createTextDecoder'
3
4
  import { createTextEncoder } from '../web/createTextEncoder'
4
5
  import {
5
6
  getLocalStorage,
@@ -16,6 +17,7 @@ export function createMMKV(
16
17
  throw new Error("MMKV: 'path' is not supported on Web!")
17
18
  }
18
19
 
20
+ const textDecoder = createTextDecoder()
19
21
  const textEncoder = createTextEncoder()
20
22
  const listeners = new Set<(key: string) => void>()
21
23
 
@@ -71,7 +73,11 @@ export function createMMKV(
71
73
  set: (key, value) => {
72
74
  const storage = getLocalStorage()
73
75
  if (key === '') throw new Error('Cannot set a value for an empty key!')
74
- storage.setItem(prefixedKey(key), value.toString())
76
+ if (value instanceof ArrayBuffer) {
77
+ storage.setItem(prefixedKey(key), textDecoder.decode(value))
78
+ } else {
79
+ storage.setItem(prefixedKey(key), String(value))
80
+ }
75
81
  callListeners(key)
76
82
  },
77
83
  getString: (key) => {
@@ -0,0 +1,15 @@
1
+ export function createTextDecoder(): TextDecoder {
2
+ const g = global ?? globalThis ?? window
3
+ if (g.TextDecoder != null) {
4
+ return new g.TextDecoder()
5
+ } else {
6
+ return {
7
+ decode: () => {
8
+ throw new Error('TextDecoder is not supported in this environment!')
9
+ },
10
+ encoding: 'utf-8',
11
+ fatal: false,
12
+ ignoreBOM: false,
13
+ }
14
+ }
15
+ }
@@ -1,4 +1,4 @@
1
- export function createTextEncoder() {
1
+ export function createTextEncoder(): TextEncoder {
2
2
  const g = global ?? globalThis ?? window
3
3
  if (g.TextEncoder != null) {
4
4
  return new g.TextEncoder()