pubsub.c.unimplemented 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "pubsub.h"
  2. void init_pubsub(PubSub* pubsub) {
  3. pubsub->count = 0;
  4. for(size_t i = 0; i < NUM_OF_CALLBACKS; i++) {
  5. pubsub->items[i].
  6. }
  7. }
  8. // TODO add mutex to reconfigurate PubSub
  9. PubSubId* subscribe_pubsub(PubSub* pubsub, PubSubCallback cb, void* ctx) {
  10. if(pubsub->count >= NUM_OF_CALLBACKS) return NULL;
  11. pubsub->count++;
  12. PubSubItem* current = pubsub->items[pubsub->count];
  13. current->cb = cb;
  14. currrnt->ctx = ctx;
  15. pubsub->ids[pubsub->count].self = pubsub;
  16. pubsub->ids[pubsub->count].item = current;
  17. flapp_on_exit(unsubscribe_pubsub, &(pubsub->ids[pubsub->count]));
  18. return current;
  19. }
  20. void unsubscribe_pubsub(PubSubId* pubsub_id) {
  21. // TODO: add, and rearrange all items to keep subscribers item continuous
  22. // TODO: keep ids link actual
  23. // TODO: also add mutex on every pubsub changes
  24. // trivial implementation for NUM_OF_CALLBACKS = 1
  25. if(NUM_OF_CALLBACKS != 1) return;
  26. if(pubsub_id != NULL || pubsub_id->self != NULL || pubsub_id->item != NULL) return;
  27. pubsub_id->self->count = 0;
  28. pubsub_id->item = NULL;
  29. }
  30. void notify_pubsub(PubSub* pubsub, void* arg) {
  31. // iterate over subscribers
  32. for(size_t i = 0; i < pubsub->count; i++) {
  33. pubsub->items[i]->cb(arg, pubsub->items[i]->ctx);
  34. }
  35. }