From 2e1b0089aeea3cd1ef77b46a25aaf35a1620f42f Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 27 Dec 2021 21:48:16 +0000 Subject: [PATCH] Clean up failed callbacks. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3706 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- src/ssb.js.c | 25 ++++++++++++++++++++----- src/util.js.c | 6 +++++- src/util.js.h | 2 +- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/ssb.js.c b/src/ssb.js.c index 4788d53c..0a975f6b 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -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); } diff --git a/src/util.js.c b/src/util.js.c index d6c04552..006cee24 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -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 { diff --git a/src/util.js.h b/src/util.js.h index 0038f660..32c354be 100644 --- a/src/util.js.h +++ b/src/util.js.h @@ -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);