Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Batching #535

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
32 changes: 32 additions & 0 deletions ios/MmkvHostObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,38 @@
return jsi::Value::undefined();
});
}

if (propName == "applyBatchedWrites") {
// MMKV.applyBatchedWrites(Map<K, V>, DELETE_SYMBOL)
return jsi::Function::createFromHostFunction(runtime,
jsi::PropNameID::forAscii(runtime, funcName),
1, // encryptionKey
[this](jsi::Runtime& runtime,
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value {
auto run = [this](jsi::Runtime& runtime,
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value {
auto resolve = arguments[0].asObject(runtime).asFunction(runtime);

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// TODO: Read from Map and apply everything here?

// TODO: Resolve on CallInvoker
});

return jsi::Value::undefined();
};

auto newPromise = runtime.global().getPropertyAsFunction(runtime, "Promise");
auto runFunction = jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forAscii(runtime, "Promise::run"), 1, std::move(run));

return newPromise.callAsConstructor(runtime, runFunction, 1);
});

}

return jsi::Value::undefined();
}
39 changes: 38 additions & 1 deletion src/MMKV.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createMMKV } from './createMMKV';
import { createMockMMKV } from './createMMKV.mock';
import { DELETE_SYMBOL, MemoryCache, MemoryCacheMap } from './MemoryCache';
import { isJest } from './PlatformChecker';

interface Listener {
Expand Down Expand Up @@ -125,7 +126,12 @@ export type NativeMMKV = Pick<
| 'getBuffer'
| 'set'
| 'recrypt'
>;
> & {
applyBatchedWrites: (
map: MemoryCacheMap,
deleteFlag: typeof DELETE_SYMBOL
) => Promise<void>;
};

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

Expand Down Expand Up @@ -233,6 +239,37 @@ export class MMKV implements MMKVInterface {
};
}

/**
* Creates a proxy of the MMKV instance that writes all new operations to an
* in-memory `Map<K, V>` instead of writing it to the file.
* The Map can later be used to apply all writes in a single batch.
*/
private createInMemoryCopy(memoryCache: MemoryCache): MMKVInterface {
return {
addOnValueChangedListener: this.addOnValueChangedListener,
clearAll: memoryCache.clear,
contains: memoryCache.has,
delete: memoryCache.delete,
getAllKeys: memoryCache.getAllKeys,
getBoolean: memoryCache.getBoolean,
getBuffer: memoryCache.getBuffer,
getNumber: memoryCache.getNumber,
getString: memoryCache.getString,
recrypt: this.recrypt,
set: memoryCache.set,
};
}

batch(callback: (storage: MMKVInterface) => void): Promise<void> {
const memoryCache = new MemoryCache(this);
const copy = this.createInMemoryCopy(memoryCache);
callback(copy);
return this.nativeInstance.applyBatchedWrites(
memoryCache.map,
DELETE_SYMBOL
);
}

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

Expand Down
103 changes: 103 additions & 0 deletions src/MemoryCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import type { MMKV } from './MMKV';

export const DELETE_SYMBOL = Symbol();
export const NOT_YET_LOADED_SYMBOL = Symbol();

type ValueType = string | boolean | number | Uint8Array;
type NOT_YET_LOADED = typeof NOT_YET_LOADED_SYMBOL;
type WILL_DELETE = typeof DELETE_SYMBOL;

export type MemoryCacheMap = Map<
string,
ValueType | NOT_YET_LOADED | WILL_DELETE
>;

export class MemoryCache {
map: MemoryCacheMap;
private mmkv: MMKV;

constructor(mmkv: MMKV) {
this.map = new Map();
this.mmkv = mmkv;

// init all keys, values are not yet loaded though.
for (const key of mmkv.getAllKeys()) {
this.map.set(key, NOT_YET_LOADED_SYMBOL);
}
}

has(key: string): boolean {
const value = this.map.get(key);
if (value == null) return false;
if (value === DELETE_SYMBOL) return false;
return true;
}

getAllKeys(): string[] {
const keys: string[] = [];
this.map.forEach((value, key) => {
if (value !== DELETE_SYMBOL) keys.push(key);
});
return keys;
}

private get<T extends 'getBoolean' | 'getString' | 'getNumber' | 'getBuffer'>(
key: string,
method: T
): ValueType | undefined {
const value = this.map.get(key);

if (value === NOT_YET_LOADED_SYMBOL) {
const nativeValue = this.mmkv[method](key);
if (nativeValue != null) {
this.map.set(key, nativeValue);
return nativeValue;
} else {
return undefined;
}
}
if (value === DELETE_SYMBOL) {
return undefined;
}

return value;
}

getBoolean(key: string): boolean | undefined {
const value = this.get(key, 'getBoolean');
if (typeof value === 'boolean') return value;
else return undefined;
}

getString(key: string): string | undefined {
const value = this.get(key, 'getString');
if (typeof value === 'string') return value;
else return undefined;
}

getNumber(key: string): number | undefined {
const value = this.get(key, 'getNumber');
if (typeof value === 'number') return value;
else return undefined;
}

getBuffer(key: string): Uint8Array | undefined {
const value = this.get(key, 'getBuffer');
if (value instanceof Uint8Array) return value;
else return undefined;
}

set(key: string, value: ValueType): void {
this.map.set(key, value);
}

delete(key: string): void {
this.map.set(key, DELETE_SYMBOL);
}

clear(): void {
for (const key of this.map.keys()) {
this.delete(key);
}
}
}