@@ -286,20 +286,52 @@ void osp::install(jsi::Runtime &rt,
286286 return {};
287287 });
288288
289+
289290 auto refreshSchema = HOSTFN (" refreshSchema" , 1 ) {
290- if (count == 0 ) {
291- throw jsi::JSError (rt, " [react-native-quick-sqlite][refreshSchema] database name is required" );
292- }
291+ if (count == 0 ) {
292+ throw jsi::JSError (rt, " [react-native-quick-sqlite][refreshSchema] database name is required" );
293+ }
293294
294- if (!args[0 ].isString ()) {
295- throw jsi::JSError (rt, " [react-native-quick-sqlite][refreshSchema] database name must be a string" );
296- }
295+ if (!args[0 ].isString ()) {
296+ throw jsi::JSError (rt, " [react-native-quick-sqlite][refreshSchema] database name must be a string" );
297+ }
297298
298- string dbName = args[0 ].asString (rt).utf8 (rt);
299+ std:: string dbName = args[0 ].asString (rt).utf8 (rt);
299300
300- sqliteRefreshSchema (dbName);
301+ auto promiseCtr = rt.global ().getPropertyAsFunction (rt, " Promise" );
302+ auto jsPromise = promiseCtr.callAsConstructor (rt, HOSTFN (" executor" , 2 ) {
303+ auto resolve = std::make_shared<jsi::Value>(rt, args[0 ]);
304+ auto reject = std::make_shared<jsi::Value>(rt, args[1 ]);
301305
302- return {};
306+ try {
307+ auto future = sqliteRefreshSchema (dbName);
308+
309+ // Waiting for the future to complete in a separate thread
310+ std::thread ([future = std::move (future), &rt, resolve, reject]() mutable {
311+ try {
312+ future.get ();
313+ invoker->invokeAsync ([&rt, resolve] {
314+ resolve->asObject (rt).asFunction (rt).call (rt);
315+ });
316+ } catch (const std::exception& exc) {
317+ invoker->invokeAsync ([&rt, reject, exc] {
318+ auto errorCtr = rt.global ().getPropertyAsFunction (rt, " Error" );
319+ auto error = errorCtr.callAsConstructor (rt, jsi::String::createFromUtf8 (rt, exc.what ()));
320+ reject->asObject (rt).asFunction (rt).call (rt, error);
321+ });
322+ }
323+ }).detach ();
324+
325+ } catch (const std::exception& exc) {
326+ auto errorCtr = rt.global ().getPropertyAsFunction (rt, " Error" );
327+ auto error = errorCtr.callAsConstructor (rt, jsi::String::createFromUtf8 (rt, exc.what ()));
328+ reject->asObject (rt).asFunction (rt).call (rt, error);
329+ }
330+
331+ return {};
332+ }));
333+
334+ return jsPromise;
303335 });
304336
305337 auto executeInContext = HOSTFN (" executeInContext" , 3 ) {
0 commit comments