Skip to content

Commit

Permalink
Merge pull request #103 from OP-Engineering/fix-reactive-and-hook
Browse files Browse the repository at this point in the history
Fix reactive queries not firing after update hook has been registered
  • Loading branch information
ospfranco authored May 30, 2024
2 parents 68e524b + d4e6ba5 commit 0cd4aff
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 38 deletions.
11 changes: 5 additions & 6 deletions cpp/DBHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#else
#include "bridge.h"
#endif
#include "logs.h"
#include "macros.h"
#include "utils.h"
#include <iostream>
Expand All @@ -26,7 +27,7 @@ void DBHostObject::auto_register_update_hook() {
return;
}

auto hook = [this](std::string dbName, std::string table_name,
auto hook = [this](std::string name, std::string table_name,
std::string operation, int rowId) {
if (update_hook_callback != nullptr) {
std::vector<JSVariant> params;
Expand All @@ -37,17 +38,17 @@ void DBHostObject::auto_register_update_hook() {
if (operation != "DELETE") {
std::string query = "SELECT * FROM " + table_name +
" where rowid = " + std::to_string(rowId) + ";";
opsqlite_execute(dbName, query, &params, &results, metadata);
opsqlite_execute(name, query, &params, &results, metadata);
}

jsCallInvoker->invokeAsync(
[this,
results = std::make_shared<std::vector<DumbHostObject>>(results),
callback = update_hook_callback, tableName = std::move(table_name),
callback = update_hook_callback, table_name,
operation = std::move(operation), &rowId] {
auto res = jsi::Object(rt);
res.setProperty(rt, "table",
jsi::String::createFromUtf8(rt, tableName));
jsi::String::createFromUtf8(rt, table_name));
res.setProperty(rt, "operation",
jsi::String::createFromUtf8(rt, operation));
res.setProperty(rt, "rowId", jsi::Value(rowId));
Expand All @@ -63,7 +64,6 @@ void DBHostObject::auto_register_update_hook() {
}

for (const auto &query_ptr : reactive_queries) {

auto query = query_ptr.get();
if (query->discriminators.size() == 0) {
continue;
Expand All @@ -75,7 +75,6 @@ void DBHostObject::auto_register_update_hook() {
if (discriminator.table != table_name) {
continue;
}

// Table has matched

// If no ids are specified, then we should fire
Expand Down
12 changes: 0 additions & 12 deletions cpp/libsql/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,8 @@ std::string opsqlite_get_db_path(std::string const &db_name,
return location + "/" + db_name;
}

#ifdef OP_SQLITE_USE_SQLCIPHER
BridgeResult opsqlite_open(std::string const &dbName,
std::string const &last_path,
std::string const &crsqlitePath,
std::string const &encryptionKey) {
#else
BridgeResult opsqlite_libsql_open(std::string const &name,
std::string const &last_path) {
#endif
std::string path = opsqlite_get_db_path(name, last_path);

int status = 0;
Expand All @@ -72,11 +65,6 @@ BridgeResult opsqlite_libsql_open(std::string const &name,

db_map[name] = {.db = db, .c = c};

#ifdef OP_SQLITE_USE_SQLCIPHER
opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr,
nullptr, nullptr);
#endif

return {.type = SQLiteOk, .affectedRows = 0};
}

Expand Down
23 changes: 9 additions & 14 deletions example/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ GEM
base64
nkf
rexml
activesupport (7.1.3.2)
base64
bigdecimal
activesupport (7.0.8.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
Expand All @@ -22,12 +17,11 @@ GEM
json (>= 1.5.1)
atomos (0.1.3)
base64 (0.2.0)
bigdecimal (3.1.7)
claide (1.1.0)
cocoapods (1.15.2)
cocoapods (1.14.3)
addressable (~> 2.8)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.15.2)
cocoapods-core (= 1.14.3)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 2.1, < 3.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
Expand All @@ -42,7 +36,7 @@ GEM
nap (~> 1.0)
ruby-macho (>= 2.3.0, < 3.0)
xcodeproj (>= 1.23.0, < 2.0)
cocoapods-core (1.15.2)
cocoapods-core (1.14.3)
activesupport (>= 5.0, < 8)
addressable (~> 2.8)
algoliasearch (~> 1.0)
Expand All @@ -63,8 +57,6 @@ GEM
cocoapods-try (1.2.0)
colored2 (3.1.2)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
drb (2.2.1)
escape (0.0.4)
ethon (0.16.0)
ffi (>= 1.15.0)
Expand All @@ -78,7 +70,6 @@ GEM
json (2.7.2)
minitest (5.22.3)
molinillo (0.8.0)
mutex_m (0.2.0)
nanaimo (0.3.0)
nap (1.1.0)
netrc (0.11.0)
Expand All @@ -102,7 +93,11 @@ PLATFORMS
ruby

DEPENDENCIES
cocoapods
activesupport (>= 6.1.7.5, < 7.1.0)
cocoapods (>= 1.13, < 1.15)

RUBY VERSION
ruby 3.3.1p55

BUNDLED WITH
2.5.9
6 changes: 5 additions & 1 deletion example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ PODS:
- hermes-engine/Pre-built (= 0.74.0)
- hermes-engine/Pre-built (0.74.0)
- op-sqlite (6.0.3):
- OpenSSL-Universal
- React
- React-callinvoker
- React-Core
- OpenSSL-Universal (3.1.5004)
- RCT-Folly (2024.01.01.00):
- boost
- DoubleConversion
Expand Down Expand Up @@ -1234,6 +1236,7 @@ DEPENDENCIES:

SPEC REPOS:
trunk:
- OpenSSL-Universal
- SocketRocket

EXTERNAL SOURCES:
Expand Down Expand Up @@ -1358,7 +1361,8 @@ SPEC CHECKSUMS:
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
glog: c5d68082e772fa1c511173d6b30a9de2c05a69a2
hermes-engine: 6eae7edb2f563ee41d7c1f91f4f2e57c26d8a5c3
op-sqlite: 6a6fb7bd336a2cac366408dd07c7f6c5a9d56e90
op-sqlite: 557247bb21ec97c23ff45a012cc1cbea2c42bcaf
OpenSSL-Universal: 0db2e81615ad95efc90ce13a638986858da38c0d
RCT-Folly: 045d6ecaa59d826c5736dfba0b2f4083ff8d79df
RCTDeprecation: 3ca8b6c36bfb302e1895b72cfe7db0de0c92cd47
RCTRequired: 9fc183af555fd0c89a366c34c1ae70b7e03b1dc5
Expand Down
4 changes: 2 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
"node": ">=18"
},
"op-sqlite": {
"sqlcipher": false,
"sqlcipher": true,
"crsqlite": false,
"performanceMode": "1",
"iosSqlite": false,
"fts5": true,
"libsql": true
"libsql": false
}
}
1 change: 0 additions & 1 deletion example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,6 @@ export function queriesTests() {
await new Promise<void>(done => {
setTimeout(() => done(), 50);
});
console.warn('mark1');
tx.execute('SELECT * FROM User;');
ranCallback = true;
});
Expand Down
57 changes: 55 additions & 2 deletions example/src/tests/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {isLibsql, open, type DB} from '@op-engineering/op-sqlite';
import chai from 'chai';
import {beforeEach, describe, it} from './MochaRNAdapter';
import {sleep} from './utils';
import Chance from 'chance';

const expect = chai.expect;
const chance = new Chance();
let db: DB;

export function reactiveTests() {
Expand Down Expand Up @@ -165,11 +167,11 @@ export function reactiveTests() {
[1, 'John', 30, 1000, 'Johnny'],
);

await sleep(20);
await sleep(0);

db.execute('UPDATE User SET name = ? WHERE id = ?;', ['Foo', 1]);

await sleep(20);
await sleep(0);

expect(firstReactiveRan).to.be.false;
expect(secondReactiveRan).to.be.false;
Expand All @@ -180,5 +182,56 @@ export function reactiveTests() {
unsubscribe2();
unsubscribe3();
});

it('Update hook and reactive queries work at the same time', async () => {
let promiseResolve: any;
let promise = new Promise(resolve => {
promiseResolve = resolve;
});
db.updateHook(({operation}) => {
promiseResolve?.(operation);
});

let emittedUser = null;
const unsubscribe = db.reactiveExecute({
query: 'SELECT * FROM User;',
arguments: [],
fireOn: [
{
table: 'User',
},
],
callback: data => {
emittedUser = data.rows._array[0];
},
});

const id = chance.integer({
min: 1,
max: 100000,
});
const name = chance.name();
const age = chance.integer();
const networth = chance.floating();
db.execute(
'INSERT INTO User (id, name, age, networth, nickname) VALUES (?, ?, ?, ?, ?);',
[id, name, age, networth, 'Johnny'],
);

const operation = await promise;

await sleep(0);

expect(operation).to.equal('INSERT');
expect(emittedUser).to.deep.eq({
id,
name,
age,
networth,
nickname: 'Johnny',
});

unsubscribe();
});
});
}

0 comments on commit 0cd4aff

Please sign in to comment.