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

Fix de-registration of update hooks #109

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
26 changes: 10 additions & 16 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ namespace react = facebook::react;

#ifndef OP_SQLITE_USE_LIBSQL
void DBHostObject::auto_register_update_hook() {
if (update_hook_callback == nullptr && reactive_queries.size() == 0 &&
has_update_hook_registered) {
if (update_hook_callback == nullptr && reactive_queries.empty() &&
is_update_hook_registered) {
opsqlite_deregister_update_hook(db_name);
is_update_hook_registered = false;
return;
}

if (has_update_hook_registered) {
if (is_update_hook_registered) {
return;
}

Expand Down Expand Up @@ -52,7 +53,7 @@ void DBHostObject::auto_register_update_hook() {
res.setProperty(rt, "operation",
jsi::String::createFromUtf8(rt, operation));
res.setProperty(rt, "rowId", jsi::Value(rowId));
if (results->size() != 0) {
if (!results->empty()) {
res.setProperty(
rt, "row",
jsi::Object::createFromHostObject(
Expand All @@ -65,7 +66,7 @@ void DBHostObject::auto_register_update_hook() {

for (const auto &query_ptr : reactive_queries) {
auto query = query_ptr.get();
if (query->discriminators.size() == 0) {
if (query->discriminators.empty()) {
continue;
}

Expand Down Expand Up @@ -116,23 +117,16 @@ void DBHostObject::auto_register_update_hook() {
[this,
results = std::make_shared<std::vector<DumbHostObject>>(results),
callback = query->callback, metadata, status = std::move(status)] {
if (status.type == SQLiteOk) {
auto jsiResult =
createResult(rt, status, results.get(), metadata);
callback->asObject(rt).asFunction(rt).call(rt, jsiResult);
} else {
auto errorCtr = rt.global().getPropertyAsFunction(rt, "Error");
auto error = errorCtr.callAsConstructor(
rt, jsi::String::createFromUtf8(rt, status.message));
callback->asObject(rt).asFunction(rt).call(rt, error);
}
auto jsiResult =
createResult(rt, status, results.get(), metadata);
callback->asObject(rt).asFunction(rt).call(rt, jsiResult);
});
}
}
};

opsqlite_register_update_hook(db_name, std::move(hook));
has_update_hook_registered = true;
is_update_hook_registered = true;
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion cpp/DBHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class JSI_EXPORT DBHostObject : public jsi::HostObject {
std::shared_ptr<jsi::Value> rollback_hook_callback;
jsi::Runtime &rt;
std::vector<std::shared_ptr<ReactiveQuery>> reactive_queries;
bool has_update_hook_registered = false;
bool is_update_hook_registered = false;
};

} // namespace opsqlite
Loading