Skip to content

Commit

Permalink
fix: Fix web support for memory warning listener and types (#762)
Browse files Browse the repository at this point in the history
Starting with 3.x webpack required the addition of react-native-web to work.  Webpack would throw errors even with react-native-web but those errors seemed to be ignored and everything worked as expected.
Vite configuration will not ignore those errors even with react-native-web.  This re-arranges some code and adds some new web entry points.  React-native-web will no longer be necessary to use this library with webpack/vite configurations.

Co-authored-by: Adam Utsch <adam.utsch@gmail.com>
  • Loading branch information
amutsch and Adam Utsch authored Nov 19, 2024
1 parent 2be0a60 commit 02a00ae
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 8 deletions.
8 changes: 6 additions & 2 deletions package/src/MMKV.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createMMKV } from './createMMKV';
import { createMockMMKV } from './createMMKV.mock';
import { isTest } from './PlatformChecker';
import type { Configuration } from './NativeMmkv';
import type { Listener, MMKVInterface, NativeMMKV } from './Types';
import type {
Configuration,
Listener,
MMKVInterface,
NativeMMKV,
} from './Types';
import { addMemoryWarningListener } from './MemoryWarningListener';

const onValueChangedListeners = new Map<string, ((key: string) => void)[]>();
Expand Down
5 changes: 5 additions & 0 deletions package/src/MemoryWarningListener.web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MMKVInterface } from './Types';

export const addMemoryWarningListener = (_mmkv: MMKVInterface): void => {
//no-op function, there is not a web equivalent to memory warning
};
5 changes: 5 additions & 0 deletions package/src/NativeMmkv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import type { UnsafeObject } from 'react-native/Libraries/Types/CodegenTypes';
import { ModuleNotFoundError } from './ModuleNotFoundError';
import { getMMKVPlatformContextTurboModule } from './NativeMmkvPlatformContext';

/**
* IMPORTANT: These types are also in the Types.ts file.
* Due to how react-native-codegen works these are required here as the spec types can not be separated from spec.
* We also need the types separate to allow bypassing importing turbo module registry in web
*/
/**
* Configures the mode of the MMKV instance.
*/
Expand Down
74 changes: 74 additions & 0 deletions package/src/Types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,77 @@
/**
* IMPORTANT: Some of these types are also in the NativeMmkv.ts file.
* Due to how react-native-codegen works these are required here as the spec types can not be separated from spec.
* We also need the types separate to allow bypassing importing turbo module registry in web
*/

/**
* Configures the mode of the MMKV instance.
*/
export enum Mode {
/**
* The MMKV instance is only used from a single process (this app).
*/
SINGLE_PROCESS,
/**
* The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
*/
MULTI_PROCESS,
}

/**
* Used for configuration of a single MMKV instance.
*/
export interface Configuration {
/**
* The MMKV instance's ID. If you want to use multiple instances, make sure to use different IDs!
*
* @example
* ```ts
* const userStorage = new MMKV({ id: `user-${userId}-storage` })
* const globalStorage = new MMKV({ id: 'global-app-storage' })
* ```
*
* @default 'mmkv.default'
*/
id: string;
/**
* The MMKV instance's root path. By default, MMKV stores file inside `$(Documents)/mmkv/`. You can customize MMKV's root directory on MMKV initialization:
* @example
* ```ts
* const temporaryStorage = new MMKV({ path: '/tmp/' })
* ```
*
* @note On iOS, if an `AppGroup` is set in `Info.plist` and `path` is `undefined`, MMKV will use the `AppGroup` directory.
* App Groups allow you to share MMKV storage between apps, widgets and extensions within the same AppGroup bundle.
* For more information, see [the `Configuration` section](/~https://github.com/Tencent/MMKV/wiki/iOS_tutorial#configuration).
*
* @default undefined
*/
path?: string;
/**
* The MMKV instance's encryption/decryption key. By default, MMKV stores all key-values in plain text on file, relying on iOS's sandbox to make sure the file is encrypted. Should you worry about information leaking, you can choose to encrypt MMKV.
*
* Encryption keys can have a maximum length of 16 bytes.
*
* @example
* ```ts
* const secureStorage = new MMKV({ encryptionKey: 'my-encryption-key!' })
* ```
*
* @default undefined
*/
encryptionKey?: string;
/**
* Configure the processing mode for MMKV.
* - `SINGLE_PROCESS`: The MMKV instance is only used from a single process (this app).
* - `MULTI_PROCESS`: The MMKV instance may be used from multiple processes, such as app clips, share extensions or background services.
*
* @default SINGLE_PROCESS
*/
mode?: Mode;
}

/**
* Represents a single MMKV instance.
*/
Expand Down
4 changes: 2 additions & 2 deletions package/src/createMMKV.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Platform } from 'react-native';
import { getMMKVTurboModule, Mode, type Configuration } from './NativeMmkv';
import type { NativeMMKV } from './Types';
import { getMMKVTurboModule } from './NativeMmkv';
import { type Configuration, Mode, type NativeMMKV } from './Types';
import { getMMKVPlatformContextTurboModule } from './NativeMmkvPlatformContext';

export const createMMKV = (config: Configuration): NativeMMKV => {
Expand Down
3 changes: 1 addition & 2 deletions package/src/createMMKV.web.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* global localStorage */
import type { Configuration } from './NativeMmkv';
import type { NativeMMKV } from './Types';
import type { Configuration, NativeMMKV } from './Types';
import { createTextEncoder } from './createTextEncoder';

const canUseDOM =
Expand Down
2 changes: 1 addition & 1 deletion package/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
import { MMKV } from './MMKV';
import type { Configuration } from './NativeMmkv';
import type { Configuration } from './Types';

function isConfigurationEqual(
left?: Configuration,
Expand Down
2 changes: 1 addition & 1 deletion package/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './MMKV';
export * from './hooks';

export { Mode, Configuration } from './NativeMmkv';
export { Mode, type Configuration } from './Types';

0 comments on commit 02a00ae

Please sign in to comment.