Make ssb.forgetStoredConnection not use the DB on the main thread. Four remaining?

This commit is contained in:
Cory McWilliams 2024-06-16 15:57:19 -04:00
parent 548febfb22
commit 3eab5a5f70

View File

@ -1507,8 +1507,40 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
return JS_UNDEFINED;
}
typedef struct _forget_stored_connection_t
{
const char* address;
int32_t port;
const char* pubkey;
JSValue promise[2];
} forget_stored_connection_t;
static void _tf_ssb_forget_stored_connection_work(tf_ssb_t* ssb, void* user_data)
{
forget_stored_connection_t* work = user_data;
if (work->pubkey)
{
tf_ssb_db_forget_stored_connection(ssb, work->address, work->port, work->pubkey);
}
}
static void _tf_ssb_forget_stored_connection_after_work(tf_ssb_t* ssb, int status, void* user_data)
{
forget_stored_connection_t* work = user_data;
JSContext* context = tf_ssb_get_context(ssb);
JS_FreeCString(context, work->pubkey);
JS_FreeCString(context, work->address);
JSValue result = JS_Call(context, work->promise[0], JS_UNDEFINED, 0, NULL);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
JS_FreeValue(context, work->promise[0]);
JS_FreeValue(context, work->promise[1]);
tf_free(work);
}
static JSValue _tf_ssb_forgetStoredConnection(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_UNDEFINED;
JSValue args = argv[0];
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb)
@ -1520,17 +1552,20 @@ static JSValue _tf_ssb_forgetStoredConnection(JSContext* context, JSValueConst t
int32_t port_int = 0;
JS_ToInt32(context, &port_int, port);
const char* pubkey_str = JS_ToCString(context, pubkey);
if (pubkey_str)
{
tf_ssb_db_forget_stored_connection(ssb, address_str, port_int, pubkey_str);
}
JS_FreeCString(context, pubkey_str);
JS_FreeCString(context, address_str);
forget_stored_connection_t* work = tf_malloc(sizeof(forget_stored_connection_t));
*work = (forget_stored_connection_t) {
.address = address_str,
.port = port_int,
.pubkey = pubkey_str,
};
result = JS_NewPromiseCapability(context, work->promise);
JS_FreeValue(context, address);
JS_FreeValue(context, port);
JS_FreeValue(context, pubkey);
tf_ssb_run_work(ssb, _tf_ssb_forget_stored_connection_work, _tf_ssb_forget_stored_connection_after_work, work);
}
return JS_UNDEFINED;
return result;
}
static void _tf_ssb_cleanup_value(tf_ssb_t* ssb, void* user_data)