Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 13 additions & 12 deletions package/src/MMKV.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import type {
} from './Types';
import { addMemoryWarningListener } from './MemoryWarningListener';

const onValueChangedListeners = new Map<string, ((key: string) => void)[]>();
type ListenerType = (key: string) => void;
const onValueChangedListeners = new Map<string, ListenerType[]>();

/**
* A single MMKV instance.
*/
export class MMKV implements MMKVInterface {
export class MMKV<K extends string> implements MMKVInterface<K> {
private nativeInstance: NativeMMKV;
private functionCache: Partial<NativeMMKV>;
private id: string;
Expand Down Expand Up @@ -65,33 +66,33 @@ export class MMKV implements MMKVInterface {
get isReadOnly(): boolean {
return this.nativeInstance.isReadOnly;
}
set(key: string, value: boolean | string | number | ArrayBuffer): void {
set(key: K, value: boolean | string | number | ArrayBuffer): void {
const func = this.getFunctionFromCache('set');
func(key, value);

this.onValuesChanged([key]);
}
getBoolean(key: string): boolean | undefined {
getBoolean(key: K): boolean | undefined {
const func = this.getFunctionFromCache('getBoolean');
return func(key);
}
getString(key: string): string | undefined {
getString(key: K): string | undefined {
const func = this.getFunctionFromCache('getString');
return func(key);
}
getNumber(key: string): number | undefined {
getNumber(key: K): number | undefined {
const func = this.getFunctionFromCache('getNumber');
return func(key);
}
getBuffer(key: string): ArrayBufferLike | undefined {
getBuffer(key: K): ArrayBufferLike | undefined {
const func = this.getFunctionFromCache('getBuffer');
return func(key);
}
contains(key: string): boolean {
contains(key: K): boolean {
const func = this.getFunctionFromCache('contains');
return func(key);
}
delete(key: string): void {
delete(key: K): void {
const func = this.getFunctionFromCache('delete');
func(key);

Expand Down Expand Up @@ -127,12 +128,12 @@ export class MMKV implements MMKVInterface {
};
}

addOnValueChangedListener(onValueChanged: (key: string) => void): Listener {
this.onValueChangedListeners.push(onValueChanged);
addOnValueChangedListener(onValueChanged: (key: K) => void): Listener {
this.onValueChangedListeners.push(onValueChanged as ListenerType);

return {
remove: () => {
const index = this.onValueChangedListeners.indexOf(onValueChanged);
const index = this.onValueChangedListeners.indexOf(onValueChanged as ListenerType);
if (index !== -1) {
this.onValueChangedListeners.splice(index, 1);
}
Expand Down
4 changes: 3 additions & 1 deletion package/src/MemoryWarningListener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { AppState } from 'react-native';
import type { NativeEventSubscription } from 'react-native';
import { MMKVInterface } from './Types';

export function addMemoryWarningListener(mmkv: MMKVInterface): void {
export function addMemoryWarningListener<K extends string = string>(
mmkv: MMKVInterface<K>
): void {
if (global.WeakRef != null && global.FinalizationRegistry != null) {
// 1. Weakify MMKV so we can safely use it inside the memoryWarning event listener
const weakMmkv = new WeakRef(mmkv);
Expand Down
24 changes: 11 additions & 13 deletions package/src/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,45 +81,45 @@ export interface Configuration {
/**
* Represents a single MMKV instance.
*/
export interface NativeMMKV {
export interface NativeMMKV<K extends string = string> {
/**
* Set a value for the given `key`.
*
* @throws an Error if the value cannot be set.
*/
set: (key: string, value: boolean | string | number | ArrayBuffer) => void;
set: (key: K, value: boolean | string | number | ArrayBuffer) => void;
/**
* Get the boolean value for the given `key`, or `undefined` if it does not exist.
*
* @default undefined
*/
getBoolean: (key: string) => boolean | undefined;
getBoolean: (key: K) => boolean | undefined;
/**
* Get the string value for the given `key`, or `undefined` if it does not exist.
*
* @default undefined
*/
getString: (key: string) => string | undefined;
getString: (key: K) => string | undefined;
/**
* Get the number value for the given `key`, or `undefined` if it does not exist.
*
* @default undefined
*/
getNumber: (key: string) => number | undefined;
getNumber: (key: K) => number | undefined;
/**
* Get a raw buffer of unsigned 8-bit (0-255) data.
*
* @default undefined
*/
getBuffer: (key: string) => ArrayBufferLike | undefined;
getBuffer: (key: K) => ArrayBufferLike | undefined;
/**
* Checks whether the given `key` is being stored in this MMKV instance.
*/
contains: (key: string) => boolean;
contains: (key: K) => boolean;
/**
* Delete the given `key`.
*/
delete: (key: string) => void;
delete: (key: K) => void;
/**
* Get all keys.
*
Expand All @@ -139,7 +139,7 @@ export interface NativeMMKV {
*
* @throws an Error if the instance cannot be recrypted.
*/
recrypt: (key: string | undefined) => void;
recrypt: (key: K | undefined) => void;
/**
* Trims the storage space and clears memory cache.
*
Expand All @@ -165,14 +165,12 @@ export interface Listener {
remove: () => void;
}

export interface MMKVInterface extends NativeMMKV {
export interface MMKVInterface<K extends string = string> extends NativeMMKV<K> {
/**
* Adds a value changed listener. The Listener will be called whenever any value
* in this storage instance changes (set or delete).
*
* To unsubscribe from value changes, call `remove()` on the Listener.
*/
addOnValueChangedListener: (
onValueChanged: (key: string) => void
) => Listener;
addOnValueChangedListener: (onValueChanged: (key: K) => void) => Listener;
}