Make blob store actually not block the main thread.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4352 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2023-07-18 23:46:15 +00:00
parent 7fe8f66fd3
commit b0cd58f5aa
4 changed files with 123 additions and 44 deletions

View File

@ -174,30 +174,54 @@ static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int ar
return result;
}
typedef struct _blob_store_t
{
JSContext* context;
JSValue promise[2];
uint8_t* buffer;
} blob_store_t;
void _tf_ssb_blob_store_complete(blob_store_t* store, const char* id)
{
JSValue result = JS_Call(store->context, id ? store->promise[0] : store->promise[1], JS_UNDEFINED, 0, NULL);
tf_util_report_error(store->context, result);
JS_FreeValue(store->context, result);
JS_FreeValue(store->context, store->promise[0]);
JS_FreeValue(store->context, store->promise[1]);
tf_free(store->buffer);
tf_free(store);
}
void _tf_ssb_blob_store_callback(const char* id, bool is_new, void* user_data)
{
blob_store_t* store = user_data;
_tf_ssb_blob_store_complete(store, id);
}
static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NULL;
blob_store_t* store = tf_malloc(sizeof(blob_store_t));
*store = (blob_store_t) { 0 };
JSValue result = JS_NewPromiseCapability(context, store->promise);
tf_ssb_t* ssb = JS_GetOpaque(this_val, _tf_ssb_classId);
if (ssb)
{
uint8_t* blob = NULL;
size_t size = 0;
char id[512];
if (JS_IsString(argv[0]))
{
const char* text = JS_ToCStringLen(context, &size, argv[0]);
if (tf_ssb_db_blob_store(ssb, (const uint8_t*)text, size, id, sizeof(id), NULL))
{
result = JS_NewString(context, id);
}
store->buffer = tf_malloc(size);
memcpy(store->buffer, text, size);
tf_ssb_db_blob_store_async(ssb, store->buffer, size, _tf_ssb_blob_store_callback, store);
JS_FreeCString(context, text);
}
else if ((blob = tf_util_try_get_array_buffer(context, &size, argv[0])) != 0)
{
if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id), NULL))
{
result = JS_NewString(context, id);
}
store->buffer = tf_malloc(size);
memcpy(store->buffer, blob, size);
tf_ssb_db_blob_store_async(ssb, store->buffer, size, _tf_ssb_blob_store_callback, store);
}
else
{
@ -209,11 +233,18 @@ static JSValue _tf_ssb_blobStore(JSContext* context, JSValueConst this_val, int
blob = tf_util_try_get_array_buffer(context, &size, buffer);
if (blob)
{
if (tf_ssb_db_blob_store(ssb, blob, size, id, sizeof(id), NULL))
{
result = JS_NewString(context, id);
}
store->buffer = tf_malloc(size);
memcpy(store->buffer, blob, size);
tf_ssb_db_blob_store_async(ssb, store->buffer, size, _tf_ssb_blob_store_callback, store);
}
else
{
_tf_ssb_blob_store_complete(store, NULL);
}
}
else
{
_tf_ssb_blob_store_complete(store, NULL);
}
JS_FreeValue(context, buffer);
}
@ -1270,17 +1301,17 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
/* Requires an identity. */
JS_SetPropertyStr(context, object, "createIdentity", JS_NewCFunction(context, _tf_ssb_createIdentity, "createIdentity", 1));
JS_SetPropertyStr(context, object, "getIdentities", JS_NewCFunction(context, _tf_ssb_getIdentities, "getIdentities", 1));
JS_SetPropertyStr(context, object, "appendMessageWithIdentity", JS_NewCFunction(context, _tf_ssb_appendMessageWithIdentity, "appendMessageWithIdentity", 3));
JS_SetPropertyStr(context, object, "hmacsha256sign", JS_NewCFunction(context, _tf_ssb_hmacsha256_sign, "hmacsha256sign", 3));
JS_SetPropertyStr(context, object, "hmacsha256verify", JS_NewCFunction(context, _tf_ssb_hmacsha256_verify, "hmacsha256verify", 3));
JS_SetPropertyStr(context, object, "privateMessageEncrypt", JS_NewCFunction(context, _tf_ssb_private_message_encrypt, "privateMessageEncrypt", 4));
JS_SetPropertyStr(context, object, "privateMessageDecrypt", JS_NewCFunction(context, _tf_ssb_private_message_decrypt, "privateMessageDecrypt", 3));
/* Write. */
JS_SetPropertyStr(context, object, "appendMessageWithIdentity", JS_NewCFunction(context, _tf_ssb_appendMessageWithIdentity, "appendMessageWithIdentity", 3));
/* Does not require an identity. */
JS_SetPropertyStr(context, object, "getAllIdentities", JS_NewCFunction(context, _tf_ssb_getAllIdentities, "getAllIdentities", 0));
JS_SetPropertyStr(context, object, "getMessage", JS_NewCFunction(context, _tf_ssb_getMessage, "getMessage", 2));
JS_SetPropertyStr(context, object, "blobGet", JS_NewCFunction(context, _tf_ssb_blobGet, "blobGet", 1));
JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2));
JS_SetPropertyStr(context, object, "messageContentGet", JS_NewCFunction(context, _tf_ssb_messageContentGet, "messageContentGet", 1));
JS_SetPropertyStr(context, object, "connections", JS_NewCFunction(context, _tf_ssb_connections, "connections", 0));
JS_SetPropertyStr(context, object, "storedConnections", JS_NewCFunction(context, _tf_ssb_storedConnections, "storedConnections", 0));
@ -1288,10 +1319,12 @@ void tf_ssb_register(JSContext* context, tf_ssb_t* ssb)
JS_SetPropertyStr(context, object, "closeConnection", JS_NewCFunction(context, _tf_ssb_closeConnection, "closeConnection", 1));
JS_SetPropertyStr(context, object, "forgetStoredConnection", JS_NewCFunction(context, _tf_ssb_forgetStoredConnection, "forgetStoredConnection", 1));
JS_SetPropertyStr(context, object, "sqlAsync", JS_NewCFunction(context, _tf_ssb_sqlAsync, "sqlAsync", 3));
JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1));
JS_SetPropertyStr(context, object, "getBroadcasts", JS_NewCFunction(context, _tf_ssb_getBroadcasts, "getBroadcasts", 0));
JS_SetPropertyStr(context, object, "connect", JS_NewCFunction(context, _tf_ssb_connect, "connect", 1));
JS_SetPropertyStr(context, object, "createTunnel", JS_NewCFunction(context, _tf_ssb_createTunnel, "createTunnel", 3));
/* Write. */
JS_SetPropertyStr(context, object, "storeMessage", JS_NewCFunction(context, _tf_ssb_storeMessage, "storeMessage", 1));
JS_SetPropertyStr(context, object, "blobStore", JS_NewCFunction(context, _tf_ssb_blobStore, "blobStore", 2));
/* Should be trusted only. */
JS_SetPropertyStr(context, object, "addEventListener", JS_NewCFunction(context, _tf_ssb_add_event_listener, "addEventListener", 2));