mirror of
https://github.com/gelldur/EventBus.git
synced 2024-12-26 18:51:02 +08:00
Update README
This commit is contained in:
parent
181b89d263
commit
de1852c639
Binary file not shown.
Before Width: | Height: | Size: 46 KiB |
121
README.md
121
README.md
@ -6,12 +6,12 @@ Simple and very fast event bus.
|
||||
The EventBus library is a convenient realization of the observer pattern.
|
||||
It works perfectly to supplement the implementation of MVC logic (model-view-controller) in event-driven UIs
|
||||
|
||||
|
||||
![EventBus Diagram](EventBusDiagram.png)
|
||||
General concept
|
||||
![EventBus Diagram](docs/res/EventBus-concept.png)
|
||||
|
||||
|
||||
EventBus was created because I want something easy to use and faster than [CCNotificationCenter](https://github.com/cocos2d/cocos2d-x/blob/v2/cocos2dx/support/CCNotificationCenter.h)
|
||||
from [cocos2d-x](https://github.com/cocos2d/cocos2d-x) library. Of course C++11 support was mandatory.
|
||||
from [cocos2d-x](https://github.com/cocos2d/cocos2d-x) library. Of course C++11 support was mandatory at that moment.
|
||||
|
||||
|
||||
EventBus main goals:
|
||||
@ -19,29 +19,29 @@ EventBus main goals:
|
||||
- Easy to use
|
||||
- Strongly typed
|
||||
- Free
|
||||
- tiny (~15 KB)
|
||||
- tiny (~37 KB)
|
||||
- Decouples notification senders and receivers
|
||||
- on every platform you need (cross-platform)
|
||||
|
||||
# Brief @ presentation
|
||||
Presentation [google docs](https://docs.google.com/presentation/d/1apAlKcVWo9FcqkPqL8108a1Fy9LGmhgLT56hSVpoI3w/edit?usp=sharing)
|
||||
|
||||
# Sample
|
||||
You can checkout [sample/](sample/)
|
||||
If you want to play with sample online checkout this link: [wandbox.org](https://wandbox.org/permlink/p3xeQaosuxv6w1rv)
|
||||
# Sample / use cases
|
||||
You can checkout [use_case/](use_case/)
|
||||
If you want to play with sample online checkout this link: [wandbox.org](https://wandbox.org/permlink/VWo2acOX6hxUfV1Q)
|
||||
|
||||
# Usage
|
||||
0. Store bus
|
||||
|
||||
```cpp
|
||||
// store it in controller / singleton / std::sharted_ptr whatever you want
|
||||
Dexode::EventBus bus;
|
||||
auto bus = std::make_shared<EventBus>();
|
||||
```
|
||||
|
||||
1. Define events
|
||||
|
||||
```cpp
|
||||
namespace Event // optional namespace
|
||||
namespace event // optional namespace
|
||||
{
|
||||
struct Gold
|
||||
{
|
||||
@ -56,108 +56,29 @@ namespace Event // optional namespace
|
||||
|
||||
```cpp
|
||||
// ...
|
||||
bus.listen<Event::Gold>
|
||||
([](const auto& event) // listen with lambda
|
||||
{
|
||||
std::cout << "I received gold: " << event.goldReceived << "!" << std::endl;
|
||||
});
|
||||
dexode::EventBus::Listener listener{bus};
|
||||
listener.listen([](const event::Gold& event) // listen with lambda
|
||||
{
|
||||
std::cout << "I received gold: " << event.goldReceived << " 💰" << std::endl;
|
||||
});
|
||||
|
||||
HudLayer* hudLayer;
|
||||
// Hud layer will receive info about gold
|
||||
bus.listen<Event::Gold>(std::bind(&HudLayer::onGoldReceived,hudLayer,std::placeholders::_1));
|
||||
hudLayer->listener.listen<event::Gold>(std::bind(&HudLayer::onGoldReceived, hudLayer, std::placeholders::_1));
|
||||
```
|
||||
|
||||
3. Notify
|
||||
3. Spread the news
|
||||
|
||||
```cpp
|
||||
//Inform listeners about event
|
||||
bus.notify(Event::Gold{12}); // 1 way
|
||||
bus->postpone(event::Gold{12}); // 1 way
|
||||
bus->postpone<event::Gold>({12}); // 2 way
|
||||
|
||||
bus.notify<Event::Gold>({12}); // 2 way
|
||||
|
||||
Event::Gold myGold{12};
|
||||
bus.notify(myGold); // 3 way
|
||||
event::Gold myGold{12};
|
||||
bus->postpone(myGold); // 3 way
|
||||
```
|
||||
|
||||
Lambda listener:
|
||||
```cpp
|
||||
struct SampleEvent {};
|
||||
Dexode::EventBus bus;
|
||||
//...
|
||||
int token = bus.listen<SampleEvent>([](const SampleEvent& event) // register listener
|
||||
{
|
||||
});
|
||||
|
||||
//If we want unlisten exact listener we can use token for it
|
||||
bus.unlistenAll(token);
|
||||
```
|
||||
|
||||
Listener is identified by `token`. Token is returned from EventBus::listen methods.
|
||||
We can register multiple listeners on one token:
|
||||
```cpp
|
||||
Dexode::EventBus bus;
|
||||
struct SampleEvent {};
|
||||
//...
|
||||
int token = bus.listen<SimpleEvent>([](const auto& event) // register listener
|
||||
{
|
||||
});
|
||||
|
||||
bus.listen<SimpleEvent>(token, [](const auto& event) // another listener
|
||||
{
|
||||
});
|
||||
|
||||
bus.unlistenAll(token);//Now those two lambdas will be removed from listeners
|
||||
```
|
||||
|
||||
If you don't want to handle manually with `token` you can use `EventCollector` class.
|
||||
It is useful when we want to have multiple listeners in one class. So above example could look like this:
|
||||
|
||||
```cpp
|
||||
Dexode::EventBus bus;
|
||||
struct SampleEvent {};
|
||||
Dexode::EventCollector collector{&bus};
|
||||
//...
|
||||
collector.listen<SampleEvent>([](const SampleEvent& event) // register listener
|
||||
{
|
||||
});
|
||||
|
||||
collector.listen<SampleEvent>([](const SampleEvent& event) // another listener
|
||||
{
|
||||
});
|
||||
|
||||
collector.unlistenAll();//Now those two lambdas will be removed from listeners
|
||||
```
|
||||
|
||||
Or as component of class:
|
||||
```cpp
|
||||
class Example
|
||||
{
|
||||
public:
|
||||
Example(const std::shared_ptr<Dexode::EventBus>& bus)
|
||||
: _collector{bus}
|
||||
{
|
||||
_collector.listen<SimpleEvent>(std::bind(&Example::onEvent1, this, std::placeholders::_1));
|
||||
_collector.listen<OtherEvent>(std::bind(&Example::onEvent2, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void onEvent1(const SimpleEvent& event)
|
||||
{
|
||||
}
|
||||
|
||||
void onEvent2(const OtherEvent& event)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
Dexode::EventCollector _collector;// use RAII
|
||||
};
|
||||
|
||||
//EventCollector sample
|
||||
std::shared_ptr<Dexode::EventBus> bus;
|
||||
Example ex{bus};
|
||||
//...
|
||||
bus.notify<int>("event1", 2);
|
||||
```
|
||||
Checkout [tests](test/) or [use cases](use_case/) for more examples. Or create issue what isn't clear :)
|
||||
|
||||
# Add to your project
|
||||
EventBus can be added as `ADD_SUBDIRECTORY` to your cmake file.
|
||||
|
1
docs/res/EventBus-concept.draw.io.xml
Normal file
1
docs/res/EventBus-concept.draw.io.xml
Normal file
@ -0,0 +1 @@
|
||||
<mxfile host="Chrome" modified="2020-04-12T14:28:37.939Z" agent="5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36" etag="WlXnalW0tExYR9JEFqnc" version="12.9.11" type="device"><diagram id="SfB6skvfT3f-U5eaWSQF" name="Page-1">7Vnbbts4EP0aA92HBLpYivNoO06yWBdrNED7TEmUxJYStRTlS79+hyKpi+3YTmKnAVonSMjD4W3mzAxJD9xptn7gqEg/swjTgWNF64F7N3Dgc3sD/ySyUYhtea5CEk4ijbXAE/mJjaBGKxLhsicoGKOCFH0wZHmOQ9HDEOds1ReLGe3PWqAE7wBPIaK76DcSiVShI89q8UdMktTMbFu6JUNGWANliiK26kDubOBOOWNClbL1FFOpPaMX1e/+mdZmYRzn4pQOq/BxWI0fvH++L4No8beYJl+vrrR5SrExG8YR7F9XGRcpS1iO6KxFJ5xVeYTlqBbUWpk5YwWANoDfsRAbbUxUCQZQKjKqW0vB2Y9GnS4gESrTekTZvLsxvdeSVTzEB3bjaIIgnmBxQE5TUG61M4FW2wNmGRZ8AwIcUyTIsk8FpBmVNHKt0qGg9f4CG4x+SxsMP5QN9KqXiFZ6poHjU1j/pOAYiomolaKgmIFSugbz/6uYabgqa5WPQQAUuG4bzSizJeh0UpVmNFivGrA/CcC9qbco0ifAKiUCPxWotswKQnHf2DGhdMoo43VfN45jJwwbEnRaIj/wPf+Q/ZeYC7w+aDHd6vqe6qJDv+PrQLhq46htgmPaiaFG7uxGdi9hZNvbZ+Q5KQXOMX+bkSFlFLJYcBbisjxu6ACFP5KaGv9WgpIc7ydAhPAo3ksAPxzhID4PAeyR1SOA6/1qAgz/EOA9CeANPxoB/I+Ral+fXb0Ts6vzobKr937ZdVEFlMBB5kyOh9eyOWIwqnGlt2TeyMOjaLjP70ZO4PoXyry3J7rd6FJu57i/wu96B1pZWSAhMM9rxLGGb3JEcz897olnd8W665hztOkIFIzkouyMvJBAywnf7cdib+uyuCXuWofEoaDmbynRbOT1LDEq7YSJB3ld3/HNFckokqmtDgmGRZIlYUpoNEcbVkmTlAKyoalNUsbJT5BHbVxGXGj2OH5P4kn21GNyLGPOwnDE3oI+o3VPcI5KYVbDKEVFSdrYkQFbSD5hQrBMC8kt3KOMUKnpr5hHKEdnSr83fRvaNyfGAc+5UByw7R0LD5yJnCkDZxcqrtc83DK5UHEYUZJI96U4llWpChIiOtawkIFhUkIwJnkyr2Xuhi3yRe9QQgz6xrR+jklJFOG8DjgCCRQ0ZNIuBQvwJvALippa197AgwVNoW63dfiV4lxMWQ6RHZHaZhiYsMKl2HMB8+TP3uNX/dG06ODq8xK6HHax4yRqbm2nccbInT937OOMyuFg19xk8HEuFx1SBKdk5x70WoWik+u7ojvsOpLDzVkgWyfykfM6QCUJr+v09AWHYoooVRGmfva0rWtHlqX+7JHsriKMNHegUto86Ayt3gStXZLcj+XPq0hyhuDhb13eGqN3iWC959ndJNIz396G+06RQVVeQRllkgo1UrBSFCzHn+qUdDMBT7q5++tth8w+x1oiNAcXqxPzQrAlnGp3o14G8as+MG3zVD1/O+q/oaTV8hRKORNhairP8e8MbNp+CrBvnR02OcM9bHIvxqbLvAU4+9jE8gVFG8xniOefQpkiQFQdbTTF1F95SpSPg39YdRqrmnRm3hes3Rj1zqy6yE33OVZJCkEGwnBpOIVWv0e0iikpHgdHvsV4QR48hWP+eTgG1fbrQHWrar9VdWf/Aw==</diagram></mxfile>
|
BIN
docs/res/EventBus-concept.png
Normal file
BIN
docs/res/EventBus-concept.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 31 KiB |
Loading…
x
Reference in New Issue
Block a user