forked from cory/tildefriends
Make ssb.storedConnections do its DB work not on the main thread. Five remaining by my new count?
This commit is contained in:
parent
b40f72443a
commit
548febfb22
53
src/ssb.js.c
53
src/ssb.js.c
@ -969,24 +969,53 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in
|
||||
return result;
|
||||
}
|
||||
|
||||
typedef struct _stored_connections_t
|
||||
{
|
||||
int count;
|
||||
tf_ssb_db_stored_connection_t* connections;
|
||||
JSValue promise[2];
|
||||
} stored_connections_t;
|
||||
|
||||
static void _tf_ssb_stored_connections_work(tf_ssb_t* ssb, void* user_data)
|
||||
{
|
||||
stored_connections_t* work = user_data;
|
||||
work->connections = tf_ssb_db_get_stored_connections(ssb, &work->count);
|
||||
}
|
||||
|
||||
static void _tf_ssb_stored_connections_after_work(tf_ssb_t* ssb, int status, void* user_data)
|
||||
{
|
||||
stored_connections_t* work = user_data;
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
JSValue result = JS_NewArray(context);
|
||||
for (int i = 0; i < work->count; i++)
|
||||
{
|
||||
JSValue connection = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, connection, "address", JS_NewString(context, work->connections[i].address));
|
||||
JS_SetPropertyStr(context, connection, "port", JS_NewInt32(context, work->connections[i].port));
|
||||
JS_SetPropertyStr(context, connection, "pubkey", JS_NewString(context, work->connections[i].pubkey));
|
||||
JS_SetPropertyUint32(context, result, i, connection);
|
||||
}
|
||||
tf_free(work->connections);
|
||||
|
||||
JSValue error = JS_Call(context, work->promise[0], JS_UNDEFINED, 1, &result);
|
||||
JS_FreeValue(context, result);
|
||||
JS_FreeValue(context, work->promise[0]);
|
||||
JS_FreeValue(context, work->promise[1]);
|
||||
tf_util_report_error(context, error);
|
||||
JS_FreeValue(context, error);
|
||||
tf_free(work);
|
||||
}
|
||||
|
||||
static JSValue _tf_ssb_storedConnections(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
|
||||
{
|
||||
JSValue result = JS_NULL;
|
||||
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
|
||||
if (ssb)
|
||||
{
|
||||
int count = 0;
|
||||
tf_ssb_db_stored_connection_t* connections = tf_ssb_db_get_stored_connections(ssb, &count);
|
||||
result = JS_NewArray(context);
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
JSValue connection = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, connection, "address", JS_NewString(context, connections[i].address));
|
||||
JS_SetPropertyStr(context, connection, "port", JS_NewInt32(context, connections[i].port));
|
||||
JS_SetPropertyStr(context, connection, "pubkey", JS_NewString(context, connections[i].pubkey));
|
||||
JS_SetPropertyUint32(context, result, i, connection);
|
||||
}
|
||||
tf_free(connections);
|
||||
stored_connections_t* work = tf_malloc(sizeof(stored_connections_t));
|
||||
*work = (stored_connections_t) { 0 };
|
||||
result = JS_NewPromiseCapability(context, work->promise);
|
||||
tf_ssb_run_work(ssb, _tf_ssb_stored_connections_work, _tf_ssb_stored_connections_after_work, work);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user