ssb: Add support for registering for blob added notifications similarly to messages. I want to use this to load images on the fly.
This commit is contained in:
67
src/ssb.c
67
src/ssb.c
@@ -133,6 +133,15 @@ typedef struct _tf_ssb_message_added_callback_node_t
|
||||
tf_ssb_message_added_callback_node_t* next;
|
||||
} tf_ssb_message_added_callback_node_t;
|
||||
|
||||
typedef struct _tf_ssb_blob_stored_callback_node_t tf_ssb_blob_stored_callback_node_t;
|
||||
typedef struct _tf_ssb_blob_stored_callback_node_t
|
||||
{
|
||||
tf_ssb_blob_stored_callback_t* callback;
|
||||
tf_ssb_callback_cleanup_t* cleanup;
|
||||
void* user_data;
|
||||
tf_ssb_blob_stored_callback_node_t* next;
|
||||
} tf_ssb_blob_stored_callback_node_t;
|
||||
|
||||
typedef struct _tf_ssb_blob_want_added_callback_node_t tf_ssb_blob_want_added_callback_node_t;
|
||||
typedef struct _tf_ssb_blob_want_added_callback_node_t
|
||||
{
|
||||
@@ -235,6 +244,9 @@ typedef struct _tf_ssb_t
|
||||
tf_ssb_message_added_callback_node_t* message_added;
|
||||
int message_added_count;
|
||||
|
||||
tf_ssb_blob_stored_callback_node_t* blob_stored;
|
||||
int blob_stored_count;
|
||||
|
||||
tf_ssb_blob_want_added_callback_node_t* blob_want_added;
|
||||
int blob_want_added_count;
|
||||
|
||||
@@ -2741,6 +2753,17 @@ void tf_ssb_destroy(tf_ssb_t* ssb)
|
||||
}
|
||||
tf_free(node);
|
||||
}
|
||||
while (ssb->blob_stored)
|
||||
{
|
||||
tf_ssb_blob_stored_callback_node_t* node = ssb->blob_stored;
|
||||
ssb->blob_stored = node->next;
|
||||
ssb->blob_stored_count--;
|
||||
if (node->cleanup)
|
||||
{
|
||||
node->cleanup(ssb, node->user_data);
|
||||
}
|
||||
tf_free(node);
|
||||
}
|
||||
while (ssb->blob_want_added)
|
||||
{
|
||||
tf_ssb_blob_want_added_callback_node_t* node = ssb->blob_want_added;
|
||||
@@ -3960,9 +3983,53 @@ void tf_ssb_remove_message_added_callback(tf_ssb_t* ssb, tf_ssb_message_added_ca
|
||||
}
|
||||
}
|
||||
|
||||
void tf_ssb_add_blob_stored_callback(tf_ssb_t* ssb, tf_ssb_blob_stored_callback_t* callback, void (*cleanup)(tf_ssb_t* ssb, void* user_data), void* user_data)
|
||||
{
|
||||
tf_ssb_blob_stored_callback_node_t* node = tf_malloc(sizeof(tf_ssb_blob_stored_callback_node_t));
|
||||
*node = (tf_ssb_blob_stored_callback_node_t) {
|
||||
.callback = callback,
|
||||
.cleanup = cleanup,
|
||||
.user_data = user_data,
|
||||
.next = ssb->blob_stored,
|
||||
};
|
||||
ssb->blob_stored = node;
|
||||
ssb->blob_stored_count++;
|
||||
}
|
||||
|
||||
void tf_ssb_remove_blob_stored_callback(tf_ssb_t* ssb, tf_ssb_blob_stored_callback_t* callback, void* user_data)
|
||||
{
|
||||
tf_ssb_blob_stored_callback_node_t** it = &ssb->blob_stored;
|
||||
while (*it)
|
||||
{
|
||||
if ((*it)->callback == callback && (*it)->user_data == user_data)
|
||||
{
|
||||
tf_ssb_blob_stored_callback_node_t* node = *it;
|
||||
*it = node->next;
|
||||
ssb->blob_stored_count--;
|
||||
if (node->cleanup)
|
||||
{
|
||||
node->cleanup(ssb, node->user_data);
|
||||
}
|
||||
tf_free(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
it = &(*it)->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void tf_ssb_notify_blob_stored(tf_ssb_t* ssb, const char* id)
|
||||
{
|
||||
tf_ssb_blob_stored_callback_node_t* next = NULL;
|
||||
ssb->blobs_stored++;
|
||||
for (tf_ssb_blob_stored_callback_node_t* node = ssb->blob_stored; node; node = next)
|
||||
{
|
||||
next = node->next;
|
||||
tf_trace_begin(ssb->trace, "blob stored callback");
|
||||
node->callback(ssb, id, node->user_data);
|
||||
tf_trace_end(ssb->trace);
|
||||
}
|
||||
}
|
||||
|
||||
void tf_ssb_notify_message_added(tf_ssb_t* ssb, const char* author, int32_t sequence, const char* id, JSValue message_keys)
|
||||
|
Reference in New Issue
Block a user