Clean up failed callbacks.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3706 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2021-12-27 21:48:16 +00:00
parent 05b55c849a
commit 2e1b0089ae
3 changed files with 26 additions and 7 deletions

View File

@ -491,7 +491,10 @@ static void _tf_ssb_on_message_added_callback(tf_ssb_t* ssb, const char* id, voi
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
JSValue string = JS_NewString(context, id);
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &string);
tf_util_report_error(context, response);
if (tf_util_report_error(context, response))
{
tf_ssb_remove_message_added_callback(ssb, _tf_ssb_on_message_added_callback, user_data);
}
JS_FreeValue(context, response);
JS_FreeValue(context, string);
}
@ -502,7 +505,10 @@ static void _tf_ssb_on_blob_want_added_callback(tf_ssb_t* ssb, const char* id, v
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
JSValue string = JS_NewString(context, id);
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &string);
tf_util_report_error(context, response);
if (tf_util_report_error(context, response))
{
tf_ssb_remove_blob_want_added_callback(ssb, _tf_ssb_on_blob_want_added_callback, user_data);
}
JS_FreeValue(context, response);
JS_FreeValue(context, string);
}
@ -525,7 +531,10 @@ static void _tf_ssb_on_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change
object,
};
response = JS_Call(context, callback, JS_UNDEFINED, 2, args);
tf_util_report_error(context, response);
if (tf_util_report_error(context, response))
{
tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, user_data);
}
JS_FreeValue(context, args[0]);
JS_FreeValue(context, object);
}
@ -539,7 +548,10 @@ static void _tf_ssb_on_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_change
object,
};
response = JS_Call(context, callback, JS_UNDEFINED, 2, args);
tf_util_report_error(context, response);
if (tf_util_report_error(context, response))
{
tf_ssb_remove_connections_changed_callback(ssb, _tf_ssb_on_connections_changed_callback, user_data);
}
JS_FreeValue(context, args[0]);
JS_FreeValue(context, object);
}
@ -554,7 +566,10 @@ static void _tf_ssb_on_broadcasts_changed_callback(tf_ssb_t* ssb, void* user_dat
JSValue callback = JS_MKPTR(JS_TAG_OBJECT, user_data);
JSValue argv = JS_UNDEFINED;
JSValue response = JS_Call(context, callback, JS_UNDEFINED, 1, &argv);
tf_util_report_error(context, response);
if (tf_util_report_error(context, response))
{
tf_ssb_remove_broadcasts_changed_callback(_tf_ssb_on_connections_changed_callback, user_data);
}
JS_FreeValue(context, response);
}

View File

@ -87,8 +87,9 @@ JSValue _util_print(JSContext* context, JSValueConst this_val, int argc, JSValue
return JS_NULL;
}
void tf_util_report_error(JSContext* context, JSValue value)
bool tf_util_report_error(JSContext* context, JSValue value)
{
bool is_error = false;
if (JS_IsError(context, value))
{
const char* string = JS_ToCString(context, value);
@ -109,6 +110,7 @@ void tf_util_report_error(JSContext* context, JSValue value)
{
tf_task_send_error_to_parent(task, value);
}
is_error = true;
}
else if (JS_IsException(value))
{
@ -123,7 +125,9 @@ void tf_util_report_error(JSContext* context, JSValue value)
tf_task_send_error_to_parent(task, exception);
}
JS_FreeValue(context, exception);
is_error = true;
}
return is_error;
}
typedef struct _timeout_t {

View File

@ -6,4 +6,4 @@ void tf_util_register(JSContext* context);
JSValue tf_util_utf8_decode(JSContext* context, JSValue value);
uint8_t* tf_util_try_get_array_buffer(JSContext* context, size_t* psize, JSValueConst obj);
JSValue tf_util_try_get_typed_array_buffer(JSContext* context, JSValueConst obj, size_t* pbyte_offset, size_t* pbyte_length, size_t* pbytes_per_element);
void tf_util_report_error(JSContext* context, JSValue value);
bool tf_util_report_error(JSContext* context, JSValue value);