EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Example of dexode::eventbus::perk::PassEverythingPerk wrong

Open geiseri opened this issue 3 years ago • 3 comments

This is more of a concrete example of the desired operation for #6.

https://wandbox.org/permlink/YUaGMcgiwgtdRHE6

The issue is I obviously am misunderstanding something. If I run the example with line 131 as the following

UIWallet wallet{subordinate}; 

I get the output:

Start
Gold:0
Shop button is:1
Gold:0
Shop button is:1
Shop button is:0
0
Finish

I would expect to see:

Start
Gold:0
Shop button is:1
Gold:100
Shop button is:1
Shop button is:0
0
Finish

Am I misunderstanding the operation of dexode::eventbus::perk::PassEverythingPerk in that on line 126:

subordinate->addPerk(std::make_unique<dexode::eventbus::perk::PassEverythingPerk>(eventBus)).registerPrePostpone(&dexode::eventbus::perk::PassEverythingPerk::onPrePostponeEvent);

Should cause subordinate to get all the events from eventBus forwarded to it. Then when subordinate->process(); is called it will process the event listeners on that bus.

NOTE: I am assuming my understanding is flawed, but correcting the misunderstanding to reflect the intent will "close" this issue. I will then make a PR with the corrected example. Feel free to change the issue title to something more appropriate.

Thanks!

geiseri avatar Apr 30 '21 12:04 geiseri

Thanks if something isn't straight forward then there is issue in design (bad design).

If now I understand correctly your case. You want add this 'perk' to eventBus to pass events from eventBus to subordinate

because now in your example all events which you add to subordinate bus will be passed to eventBus so opposite than you want.

-- Weekend is coming so I will try to fix previous issues ;)

gelldur avatar Apr 30 '21 13:04 gelldur

Yes, my intent is that every thread would post events to the eventBus and that each thread would have its own instance of a subordinate event bus that it would listen on. In rxcpp they have a notion of "scheduling" processing of an event as it moves through their system. So I may be abusing the API you have envisioned. My thinking is that each thread would host its own subordinate for thread-local listeners While I dislike having a bus for publishing and the other for listening in a thread, I think it would be more explicit behavior that anything posted to the main bus is "global" while anything posted to the subordinate is "local" Similar to how dbus works with a system bus and a session bus.

I am still not sure I understand how it works though. I changed the buses around as below:

auto eventBus = std::make_shared<dexode::eventbus::perk::PerkEventBus>();
auto subordinate = std::make_shared<EventBus>();
eventBus->addPerk(std::make_unique<dexode::eventbus::perk::PassEverythingPerk>(subordinate)).registerPrePostpone(&dexode::eventbus::perk::PassEverythingPerk::onPrePostponeEvent);

Then it seems that it doesn't pass any events.

geiseri avatar May 01 '21 00:05 geiseri

As you describe. EventBus is just single bus. You can use "perks" to achieve something else like passing events from bus1 to bus2.

You may just create your own wrapper instead which will combine those 2 event buses so when you post event to your wrapper it will be calling bus1->postpone and bus2->postpone.

Now in thread1 you need to call bus1->process() (to process scheduled events) Same on thread2, call bus2->process()


Also you can use perk which you mentioned above then such wrapper could be created using third bus but then you need to "eat"/process scheduled events.

If I understand correctly you intention is that you want to communicate between 2 threads? E.g. event in UI will be post event which should be handled in worker thread and vice-versa ?

Worth to mention: PassEverythingPerk::onPrePostponeEvent return Flag::postpone_cancel so events won't be duplicated. If you want such behavior then please make your own simple perk with different return.

https://wandbox.org/permlink/Fw78WH9lRTqgtDf8

gelldur avatar May 01 '21 14:05 gelldur