Giter Club home page Giter Club logo

Comments (11)

wxinix avatar wxinix commented on June 15, 2024 2

@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.

spinettaro avatar spinettaro commented on June 15, 2024 1

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.

spinettaro avatar spinettaro commented on June 15, 2024 1

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.

wxinix avatar wxinix commented on June 15, 2024

I believe - with the new interface-typed event, you can freely add returned value in user-defined interface-typed event.

from delphi-event-bus.

spinettaro avatar spinettaro commented on June 15, 2024

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.

edwinyzh avatar edwinyzh commented on June 15, 2024

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.

wxinix avatar wxinix commented on June 15, 2024

@edwinyzh

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.

edwinyzh avatar edwinyzh commented on June 15, 2024

@wxinix,

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.

edwinyzh avatar edwinyzh commented on June 15, 2024

@wxinix,

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.

edwinyzh avatar edwinyzh commented on June 15, 2024

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.

dkounal avatar dkounal commented on June 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.