Remove DB work from tf_ssb_notify_message_added, which runs on the main thread.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4497 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2023-10-08 13:52:49 +00:00
parent 7fc23dc085
commit 0473eec0a2
3 changed files with 24 additions and 8 deletions

View File

@ -3279,7 +3279,7 @@ void tf_ssb_notify_blob_stored(tf_ssb_t* ssb, const char* id)
ssb->blobs_stored++; ssb->blobs_stored++;
} }
void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id) void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id, JSValue message_keys)
{ {
tf_ssb_message_added_callback_node_t* next = NULL; tf_ssb_message_added_callback_node_t* next = NULL;
ssb->messages_stored++; ssb->messages_stored++;
@ -3292,10 +3292,9 @@ void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id)
} }
JSContext* context = ssb->context; JSContext* context = ssb->context;
JSValue message_keys = tf_ssb_db_get_message_by_id(ssb, id, true); if (!JS_IsUndefined(message_keys))
JSValue message = JS_GetPropertyStr(context, message_keys, "value");
if (!JS_IsUndefined(message))
{ {
JSValue message = JS_GetPropertyStr(context, message_keys, "value");
JSValue author = JS_GetPropertyStr(context, message, "author"); JSValue author = JS_GetPropertyStr(context, message, "author");
const char* author_string = JS_ToCString(context, author); const char* author_string = JS_ToCString(context, author);
@ -3327,9 +3326,8 @@ void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id)
JS_FreeCString(context, author_string); JS_FreeCString(context, author_string);
JS_FreeValue(context, author); JS_FreeValue(context, author);
JS_FreeValue(context, message);
} }
JS_FreeValue(context, message);
JS_FreeValue(context, message_keys);
} }
void tf_ssb_add_blob_want_added_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, const char* id, void* user_data), void (*cleanup)(tf_ssb_t* ssb, void* user_data), void* user_data) void tf_ssb_add_blob_want_added_callback(tf_ssb_t* ssb, void (*callback)(tf_ssb_t* ssb, const char* id, void* user_data), void (*cleanup)(tf_ssb_t* ssb, void* user_data), void* user_data)

View File

@ -464,7 +464,25 @@ static void _tf_ssb_db_store_message_after_work(uv_work_t* work, int status)
if (store->out_stored) if (store->out_stored)
{ {
tf_trace_begin(trace, "notify_message_added"); tf_trace_begin(trace, "notify_message_added");
tf_ssb_notify_message_added(store->ssb, store->id); JSContext* context = tf_ssb_get_context(store->ssb);
JSValue formatted = tf_ssb_format_message(
context,
store->previous,
store->author,
store->sequence,
store->timestamp,
"sha256",
store->content,
store->signature,
store->sequence_before_author);
JSValue message = JS_NewObject(context);
JS_SetPropertyStr(context, message, "key", JS_NewString(context, store->id));
JS_SetPropertyStr(context, message, "value", formatted);
char timestamp_string[256];
snprintf(timestamp_string, sizeof(timestamp_string), "%f", store->timestamp);
JS_SetPropertyStr(context, message, "timestamp", JS_NewString(context, timestamp_string));
tf_ssb_notify_message_added(store->ssb, store->id, message);
JS_FreeValue(context, message);
tf_trace_end(trace); tf_trace_end(trace);
} }
if (store->out_blob_wants) if (store->out_blob_wants)

View File

@ -152,7 +152,7 @@ void tf_ssb_remove_broadcasts_changed_callback(tf_ssb_t* ssb, tf_ssb_broadcasts_
typedef void (tf_ssb_message_added_callback_t)(tf_ssb_t* ssb, const char* id, void* user_data); typedef void (tf_ssb_message_added_callback_t)(tf_ssb_t* ssb, const char* id, void* user_data);
void tf_ssb_add_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data); void tf_ssb_add_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_callback_t* callback, tf_ssb_callback_cleanup_t* cleanup, void* user_data);
void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_callback_t* callback, void* user_data); void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_callback_t* callback, void* user_data);
void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id); void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* id, JSValue message_with_keys);
void tf_ssb_notify_blob_stored(tf_ssb_t* ssb, const char* id); void tf_ssb_notify_blob_stored(tf_ssb_t* ssb, const char* id);
typedef void (tf_ssb_blob_want_added_callback_t)(tf_ssb_t* ssb, const char* id, void* user_data); typedef void (tf_ssb_blob_want_added_callback_t)(tf_ssb_t* ssb, const char* id, void* user_data);