Skip to content

Instantly share code, notes, and snippets.

@zaferayan
Created September 6, 2021 12:15
Show Gist options
  • Select an option

  • Save zaferayan/e4939085cfaf58d3c2b7fe1c889ab8e3 to your computer and use it in GitHub Desktop.

Select an option

Save zaferayan/e4939085cfaf58d3c2b7fe1c889ab8e3 to your computer and use it in GitHub Desktop.
MMKV library install method
void install(jsi::Runtime& jsiRuntime) {
// MMKV.set(key: string, value: string | number | bool)
auto mmkvSet = jsi::Function::createFromHostFunction(jsiRuntime,
jsi::PropNameID::forAsci(jsiRuntime, "mmkvSet"),
2, // key, value
[](
jsi::Runtime& runtime,
const jsi::Value& thisValue,
const jsi::Value* arguments,
size_t count) -> jsi::Value
{
if (!arguments[0].isString()) throw jsi::JSError(runtime, "MMKV::set: First argument ('key') has to be of type string!");
auto keyName = arguments[0].getString(runtime).utf8(runtime);
if (arguments[1].isBool()) {
MMKV::defaultMMKV()->set(arguments[1].getBool(), keyName);
} else if (arguments[1].isNumber()) {
MMKV::defaultMMKV()->set(arguments[1].getNumber(), keyName);
} else if (arguments[1].isString()) {
auto stringValue = arguments[1].getString(runtime).utf8(runtime);
MMKV::defaultMMKV()->set(stringValue, keyName);
} else {
throw jsi::JSError(runtime, "MMKV::set: 'value' argument is not of type bool, number or string!");
}
return jsi::Value::undefined();
}
);
jsiRuntime.global().setProperty(jsiRuntime, "mmkvSet", std::move(mmkvSet));
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment