Comments (11)
@edwinyzh
The latest delphi_event_bus framework makes use of interface-typed event, instead of TObject in previous version. This means - when routing an event from the event emitter to subscriber methods, there is no longer internal copying of the event object --- the emitter and subscription methods are now dealing with the same event object.
Thanks to @spinettaro , for this new framework design, which is very neat and greatly improves on previous versions.
This allows us to implement a "bidirectional communication" like below:
TOnEventReceived = reference to procedure(AMessage: string); // You can use anonymous methods, or method ptrs
IMyFunkyEvent = inteface
['{3522E1C5-547F-4AB6-A799-5B3D3574D2FA}']
procedure Set_OnEventReceived(AValue: TOnEventReceived);
function Get_OnEventReceived: TOnEventReceived;
property OnEventReceived: TOnEventReceived read Get_OnEventReceived write Set_OnEventReceived;
end;
Then from the subscriber method (assuming Posting thread mode), you can callback the emitter:
[Subscribe]
procedure OnMyFunkyEvent(AEvent: IMyFunkyEvent);
begin
// manage the event
if Assigned(AEvent.OnEventReceived) then
AEvent.OnEventReceived(); // Calling back the emitter here to send back stuff
end;
Thus you see, a "bidirectional communication" is easily accomplished via this new interface-typed event, which should meet your requirement.
from delphi-event-bus.
thanks @wxinix for the explanation :) . @edwinyzh pay attention with the bidirectional communication as we have seen in other frameworks, ie Flux from Facebook, having a unidirectional flow is much better in term of readability, maintainability and troubleshooting sessions...
from delphi-event-bus.
Now the events are interfaces. So we are relying on the reference counting mechanism for interfaces. MM is not required anymore.
from delphi-event-bus.
I believe - with the new interface-typed event, you can freely add returned value in user-defined interface-typed event.
from delphi-event-bus.
I didn't get it to be honest @edwinyzh . The return type of post would be the same of the passed one? https://github.com/spinettaro/delphi-flux-seed satisfies your request?
from delphi-event-bus.
I didn't get it to be honest @edwinyzh . The return type of post would be the same of the passed one? https://github.com/spinettaro/delphi-flux-seed satisfies your request?
The requirement is simple - turn the eventbus from unidirectional to bi-directional.
from delphi-event-bus.
You can make it "bi-directional" with the latest interface-typed event. You can delegate the callback to the event emitter via a designated user-defined interface event method. Is that what you are looking for?
Overall, bidirectional communication would make the work flow hard to manage. It is just like the infamous "GOTO" of procedural languages.
In a one-to-many publisher/subscriber setting while in thread mode other than TThreadMode.Posting, DIRECTLY returning a value from IEventBus.Post does NOT make any sense. Pointless in my eyes. It unnecessarily complicates the design of delphi_event_bus, which is intended (as I see it) to be a lightweight and flexible Event Bus framework.
from delphi-event-bus.
You explained it very well! I'm convinced ;) You are right, it seems that directly returning a value by in a 1-to-many eventBus system doesn't make much sense and make things complicated as well!
Actually I can/already achieved bidirectional communication by sending back something like TEventReceivedYourReuqest
;)
I'll check the new interface-typed event, but I'm current hesitant to upgrade because I'm using a customized TEventBus
, maybe sometime later.
Thank you for all you guys' contribution to the EventBus lib!
from delphi-event-bus.
Thanks for the demonstration, It's VERY GOOD! GREAT!
In my experience I believe cloning of the event objects with the slow Extended RTTI has been the bottleneck of performance in some situations!
I should have checked the updated readme file for DEB 2.0 ;)
from delphi-event-bus.
One side effect of the 2.0 change - for each event you'll have to define an object and an interface ;)
from delphi-event-bus.
My stupid question is: finally, EventBus is freeing any Event object after replying?
Also, I believe the above example should exist also in the frontpage readme file of EventBus
from delphi-event-bus.
Related Issues (20)
- lock in eventbus class is a class var HOT 2
- consider making arguments const that are of managed types such as interface, string, TValue and so on HOT 1
- You are mistakenly attributing some code to me that is not HOT 1
- Error invoking subscriber method. Subscriber class: <something> . Original exception: EInvalidCast: Invalid class typecast HOT 33
- Error Undeclared TEventBusFactory HOT 4
- Hang on splash screen HOT 8
- Minor improvement of subscriber registration HOT 3
- REQUEST: Set Context on registration HOT 19
- Faster MREW for DEB ? HOT 8
- Remove dependency on DUnitX.Utils HOT 1
- Threading issues with asynchronous messaging to inactive subscription HOT 5
- Request/Reply pattern HOT 2
- [Question] "Active" property in TSubscription - how to use it? HOT 3
- TList inside posted event objects empties on Delphi 11 Alexandria HOT 2
- The project's headed to the wrong direction, unfortunately... HOT 6
- SubscribeAttribute in public, protected and private methods.
- DEB on Android HOT 2
- Crashes galore on theme change HOT 4
- Background / Async tasks deadlock HOT 1
- Question: Explanation/Examples for TThreadMode Subscriptions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from delphi-event-bus.