Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions cedux.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
{ \
Expand All @@ -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; \
} \
\
Expand Down
43 changes: 43 additions & 0 deletions test/test_cedux_subscribers.c
Original file line number Diff line number Diff line change
@@ -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);
}