Resuming work to move all DB access off the main thread.

This commit is contained in:
2024-06-10 11:45:20 -04:00
parent 9497d7cf64
commit 9d35b4bdfb
6 changed files with 1191 additions and 23 deletions

View File

@ -581,6 +581,29 @@ static JSValue _tf_ssb_appendMessageWithIdentity(JSContext* context, JSValueCons
return result;
}
typedef struct _blob_get_t
{
JSContext* context;
JSValue promise[2];
} blob_get_t;
static void _tf_ssb_blobGet_callback(bool found, const uint8_t* data, size_t size, void* user_data)
{
blob_get_t* get = user_data;
JSValue result = JS_UNDEFINED;
if (found)
{
result = JS_NewArrayBufferCopy(get->context, data, size);
}
JSValue error = JS_Call(get->context, get->promise[0], JS_UNDEFINED, 1, &result);
JS_FreeValue(get->context, result);
JS_FreeValue(get->context, get->promise[0]);
JS_FreeValue(get->context, get->promise[1]);
tf_util_report_error(get->context, error);
JS_FreeValue(get->context, error);
tf_free(get);
}
static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{
JSValue result = JS_NULL;
@ -588,13 +611,10 @@ static JSValue _tf_ssb_blobGet(JSContext* context, JSValueConst this_val, int ar
if (ssb)
{
const char* id = JS_ToCString(context, argv[0]);
uint8_t* blob = NULL;
size_t size = 0;
if (tf_ssb_db_blob_get(ssb, id, &blob, &size))
{
result = JS_NewArrayBufferCopy(context, blob, size);
tf_free(blob);
}
blob_get_t* get = tf_malloc(sizeof(blob_get_t));
*get = (blob_get_t) { .context = context };
result = JS_NewPromiseCapability(context, get->promise);
tf_ssb_db_blob_get_async(ssb, id, _tf_ssb_blobGet_callback, get);
JS_FreeCString(context, id);
}
return result;