From 192b24b4aaaab0dff90bf4330b72f5e376289250 Mon Sep 17 00:00:00 2001 From: Gage Vander Clay Date: Wed, 28 Apr 2021 11:11:14 -0400 Subject: [PATCH] Allow actions dispatched from non-linked subscribers to be run in cedux_run_store --- cedux.h | 22 ++++++++++-------- test/test_cedux_subscribers.c | 43 +++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 test/test_cedux_subscribers.c diff --git a/cedux.h b/cedux.h index 7396728..0349466 100644 --- a/cedux.h +++ b/cedux.h @@ -117,13 +117,15 @@ bool cedux_run_##STORE_NAME(struct STORE_NAME##_handle * p_store) { struct STORE_NAME##_subscriber_container subscriber_container; \ bool did_work = false; \ if (p_store->lock_get) p_store->lock_get(p_store->lock); \ - while(STORE_NAME##_action_queue_dequeue(&p_store->action_queue, &action) == DEQUEUE_RESULT_SUCCESS) \ + while(STORE_NAME##_action_queue_dequeue(&p_store->action_queue, &action) == DEQUEUE_RESULT_SUCCESS) \ { \ + bool action_did_work = false; \ LIST_FOR_EACH(p_store->reducer_list, reducer) \ { \ bool reducer_did_work = reducer(&p_store->tree, action); \ if (reducer_did_work) \ { \ + action_did_work = true; \ did_work = true; \ LIST_FOR_EACH(p_store->subscriber_list, subscriber_container) \ { \ @@ -134,17 +136,17 @@ bool cedux_run_##STORE_NAME(struct STORE_NAME##_handle * p_store) { } \ } \ } \ - } \ - if (p_store->lock_release) p_store->lock_release(p_store->lock); \ - if (did_work) \ - { \ - LIST_FOR_EACH(p_store->subscriber_list, subscriber_container) \ - { \ - if (subscriber_container.linked_reducer == NULL) { \ - subscriber_container.subscriber(p_store, &p_store->tree, subscriber_container.data); \ - } \ + if(action_did_work) { \ + LIST_FOR_EACH(p_store->subscriber_list, subscriber_container) \ + { \ + if (subscriber_container.linked_reducer == NULL) \ + { \ + subscriber_container.subscriber(p_store, &p_store->tree, subscriber_container.data);\ + } \ + } \ } \ } \ + if (p_store->lock_release) p_store->lock_release(p_store->lock); \ return did_work; \ } \ \ diff --git a/test/test_cedux_subscribers.c b/test/test_cedux_subscribers.c new file mode 100644 index 0000000..fb2468d --- /dev/null +++ b/test/test_cedux_subscribers.c @@ -0,0 +1,43 @@ +#include "unity.h" +#include "cedux.h" +#include "mock_cedux_test_helpers.h" + +CEDUX_DECLARE_STORE(int, int, int_store); +CEDUX_DEFINE_STORE(int, int, int_store); + +bool reducer1(int * p_state, int action) { + if (action == 1) { + *p_state = 1; + return true; + } + return false; +} + +bool reducer2(int * p_state, int action) { + if (action == 2) { + *p_state = 2; + return true; + } + return false; +} + +void subscriber1(struct int_store_handle * p_store, int const * const state, void *data) { + if(*state == 1) { + cedux_dispatch_int_store(p_store, 2); + } +} + +void test_cedux_subscriber_dispatches_actions_that_will_be_run(void) { + struct int_store_handle store = cedux_init_int_store(); + store.tree = 0; + + cedux_register_int_store_reducer(&store, reducer1); + cedux_register_int_store_reducer(&store, reducer2); + cedux_register_int_store_subscriber(&store, subscriber1, NULL); + + + cedux_dispatch_int_store(&store, 1); // Dispatch 1 which will result in reducer1 running + cedux_run_int_store(&store); + + TEST_ASSERT_EQUAL(2, store.tree); +} \ No newline at end of file