ssb: Pass around reasons for failing to connect. This will help get that information to the ui when I finish hooking it up.

This commit is contained in:
2024-11-11 22:12:41 -05:00
parent e7979fe9db
commit b1f6ad17e1
5 changed files with 110 additions and 32 deletions

View File

@ -1633,17 +1633,41 @@ static JSValue _tf_ssb_getBroadcasts(JSContext* context, JSValueConst this_val,
return result;
}
typedef struct _connect_t
{
JSContext* context;
JSValue promise[2];
} connect_t;
static void _tf_ssb_connect_callback(tf_ssb_connection_t* connection, const char* reason, void* user_data)
{
connect_t* connect = user_data;
JSContext* context = connect->context;
JSValue arg = connection ? JS_UNDEFINED : JS_NewString(context, reason);
JSValue result = JS_Call(context, connection ? connect->promise[0] : connect->promise[1], JS_UNDEFINED, connection ? 0 : 1, &arg);
tf_util_report_error(context, result);
JS_FreeValue(context, result);
JS_FreeValue(context, connect->promise[0]);
JS_FreeValue(context, connect->promise[1]);
JS_FreeValue(context, arg);
tf_free(connect);
}
static JSValue _tf_ssb_connect(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)
{
connect_t* connect = tf_malloc(sizeof(connect_t));
*connect = (connect_t) { .context = context };
result = JS_NewPromiseCapability(context, connect->promise);
if (JS_IsString(args))
{
const char* address_str = JS_ToCString(context, args);
tf_printf("Connecting to %s\n", address_str);
tf_ssb_connect_str(ssb, address_str, 0);
tf_ssb_connect_str(ssb, address_str, 0, _tf_ssb_connect_callback, connect);
JS_FreeCString(context, address_str);
}
else
@ -1660,11 +1684,11 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
tf_printf("Connecting to %s:%d\n", address_str, port_int);
uint8_t pubkey_bin[k_id_bin_len];
tf_ssb_id_str_to_bin(pubkey_bin, pubkey_str);
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin, 0);
tf_ssb_connect(ssb, address_str, port_int, pubkey_bin, 0, _tf_ssb_connect_callback, connect);
}
else
{
tf_printf("Not connecting to null.\n");
_tf_ssb_connect_callback(NULL, "Not connecting to null.", connect);
}
JS_FreeCString(context, pubkey_str);
JS_FreeCString(context, address_str);
@ -1673,7 +1697,7 @@ static JSValue _tf_ssb_connect(JSContext* context, JSValueConst this_val, int ar
JS_FreeValue(context, pubkey);
}
}
return JS_UNDEFINED;
return result;
}
typedef struct _forget_stored_connection_t