Recent

Author Topic: Event Listeners in Lazarus - free library  (Read 2750 times)

strubri

  • Newbie
  • Posts: 6
Event Listeners in Lazarus - free library
« on: March 14, 2024, 01:22:30 pm »
I am a die-hard fan and user of Lazarus. It just boggles my mind that the general conversations about programming languages do not include FPC/Lazarus. I intend to start making videos showing how Lazarus can be used to build sturdy and complex applications to showcase the many powerful features of the language (by design).

This is my first post on this forum. I come in peace and gratitude. I want to contribute. And I want to do my bit to keep the amazing work of the Lazarus team going. Seriously... there is no other contender in this space!

To the main theme of my post: I have uploaded a library to github that may be useful to many folks. It is an event listener - in the style of JavaScript frameworks - with the ability to configure how the listeners are executed - in Application.QueueAsyncCall(), each procedure in a separate thread, or serially, inside the main loop.

This is implemented as a type helper for TControl. So all you need to do is include the unit in your form and you can start using the event listener capability on every control on the form.

I built it for myself... I have a tricky UI to build. I realized I needed a framework to define multiple event listeners (on the same event name) and to be able to trigger events by business logic, rather than from inside the event handlers in LCL.

Please feel free to check this out and let me know.

saph: https://github.com/rubrican-research/saph/tree/main

Thaddy

  • Hero Member
  • *****
  • Posts: 16369
  • Censorship about opinions does not belong here.
Re: Event Listeners in Lazarus - free library
« Reply #1 on: March 14, 2024, 03:17:54 pm »
That is quite neat code, but did you forget that FPC already has such a feature?
Nowhere near as yours, but it is there:
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html
And everything descendant of TPersistent does have it.
There is nothing wrong with being blunt. At a minimum it is also honest.

strubri

  • Newbie
  • Posts: 6
Re: Event Listeners in Lazarus - free library
« Reply #2 on: March 14, 2024, 04:24:54 pm »
Oh wow. I don't recall exploring that at all.

Now that you mentioned it, I remember vaguely having looked at it while debugging something. Thank you, Thaddy for pointing that out. It just makes a stronger case for how robust the FPC/Lazarus ecosystem is!

I just wanted something that "reads" easy.... clear central declaration and easy invocation.

I suppose we should start uploading "mature" tutorials on youtube that showcases the language, FCL, LCL and RTL.

strubri

  • Newbie
  • Posts: 6
Re: Event Listeners in Lazarus - free library
« Reply #3 on: March 15, 2024, 03:47:49 pm »
FYI:
For the case where you need to assign multiple listeners for the same event you can now declare listeners like this:

Code: Pascal  [Select][+][-]
  1.    
  2. with Edit1 do begin
  3.     addListener('change', [@L1Change, @L2Change, @L3Change, @L4Change], qThreads); // run the listeners in independent threads
  4. end;
  5.  

LBoxPO

  • New Member
  • *
  • Posts: 15
Re: Event Listeners in Lazarus - free library
« Reply #4 on: September 21, 2024, 11:58:09 pm »
That is quite neat code, but did you forget that FPC already has such a feature?
Nowhere near as yours, but it is there:
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html
And everything descendant of TPersistent does have it.

Thanks for the link, but for beginners to learn Free Pascal, at least one simple code example is missing.

For example, you can show the interaction between TEdit and TLabel, where TEdit will be observed, and TLabel will be the observer,
and every time TEdit.Text is changed, TLabel will react to this and change its TLabel.Caption value to TEdit.Text value  :)

dsiders

  • Hero Member
  • *****
  • Posts: 1324
Re: Event Listeners in Lazarus - free library
« Reply #5 on: September 22, 2024, 12:13:28 am »
That is quite neat code, but did you forget that FPC already has such a feature?
Nowhere near as yours, but it is there:
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html
And everything descendant of TPersistent does have it.

Thanks for the link, but for beginners to learn Free Pascal, at least one simple code example is missing.

You have the complete source code for RTl, FCL, LCL, and LazUtils. I'm sure you can find a robust (non-contrived) example in those.
« Last Edit: September 22, 2024, 12:18:11 am by dsiders »
Preview the next Lazarus documentation release at: https://dsiders.gitlab.io/lazdocsnext

LBoxPO

  • New Member
  • *
  • Posts: 15
Re: Event Listeners in Lazarus - free library
« Reply #6 on: September 22, 2024, 12:15:02 am »
I suppose we should start uploading "mature" tutorials on youtube that showcases the language, FCL, LCL and RTL.

A very good idea for popularizing Free Pascal  8)👍
Give a link to this YouTube channel, I will be the first to subscribe  :)

...
I built it for myself... I have a tricky UI to build. I realized I needed a framework to define multiple event listeners (on the same event name) and to be able to trigger events by business logic, rather than from inside the event handlers in LCL.

Please feel free to check this out and let me know.

saph: https://github.com/rubrican-research/saph/tree/main

Thanks for your work and for sharing. :)
I downloaded the archive with the files and will study it in the next few days.
As I understand it, there is a project with an example, which is very useful for beginners like me. :D

LBoxPO

  • New Member
  • *
  • Posts: 15
Re: Event Listeners in Lazarus - free library
« Reply #7 on: September 22, 2024, 12:31:57 am »
That is quite neat code, but did you forget that FPC already has such a feature?
Nowhere near as yours, but it is there:
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html
And everything descendant of TPersistent does have it.

Thanks for the link, but for beginners to learn Free Pascal, at least one simple code example is missing.

You have the complete source code for RTl, FCL, LCL, and LazUtils. I'm sure you can find a robust (non-contrived) in those.

Yes, studying existing code is also one of the methods of acquiring knowledge, but for beginners it is not so effective,
because they do not yet have the experience that advanced users have who, based on the knowledge they have already acquired,
understand what and where they need to look for in the existing code.

In general, the presence of simple examples can help beginners quickly understand and love this wonderful Free Pascal,
and this is important for the popularization of Free Pascal.

Curt Carpenter

  • Hero Member
  • *****
  • Posts: 576
Re: Event Listeners in Lazarus - free library
« Reply #8 on: September 22, 2024, 03:01:32 am »
I've downloaded the code and will study it.  As it happens, I have a project that has almost the exact TEdit - TLabel problem LBoxPO mentioned as his simple example.  My case involves a virtual instrument control panel in which some of the controls are inter-linked:  change one control setting may require one or two others to be adjusted.  Appreciate the contribution!

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: Event Listeners in Lazarus - free library
« Reply #9 on: September 25, 2024, 11:33:30 am »
That is quite neat code, but did you forget that FPC already has such a feature?
Nowhere near as yours, but it is there:
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html
And everything descendant of TPersistent does have it.

Can you provide a demo or point to one where this feature is used?

I agree with LBoxPO's response to dsiders on the matter of an example.
Lazarus 3.0/FPC 3.2.2

LBox

  • Jr. Member
  • **
  • Posts: 50
Re: Event Listeners in Lazarus - free library
« Reply #10 on: October 03, 2024, 12:05:36 am »
That is quite neat code, but did you forget that FPC already has such a feature?
Nowhere near as yours, but it is there:
https://www.freepascal.org/docs-html/rtl/classes/ifpobserved.html
And everything descendant of TPersistent does have it.

Can you provide a demo or point to one where this feature is used?

I agree with LBoxPO's response to dsiders on the matter of an example.

Perhaps even a simple example of an observer is not so simple to show it within the framework of one post,
or Lazarus experts simply do not have free time to demonstrate this example.  :D

Let's hope that one of the Free Pascal experts will someday have free time and a desire
to show a simple example of how to declare and use an observer.  :)

By the way, while discussing this topic of an observer, I wondered if this method
is a good practice in terms of using hardware resources?

That is, declaring an observer is essentially starting a cycle that will run in the background
until the program is closed and eat up resources even when the program is idle.

That is, this will not be a single pass through the array inside the procedure launched by the user's click.

In short, if there are 100 or 1000 or 10k objects, then probably starting a cycle with each click or declaring such a number of observers
will equally use hardware resources, but what if the array of checked data-objects consists of 100k, 500k or more?  %)

Wouldn't the announcement of millions of observers lead to an irrational use of hardware resources?  :-\

paweld

  • Hero Member
  • *****
  • Posts: 1278
Re: Event Listeners in Lazarus - free library
« Reply #11 on: October 03, 2024, 06:52:45 am »
Michael Van Canneyt wrote an article about it: https://www.freepascal.org/~michael/articles/cdcover/cdcover.pdf
Sources: https://www.freepascal.org/~michael/articles/cdcover/cdcover.zip

Edit: link fixed, thanks @vfclists
« Last Edit: October 03, 2024, 04:08:41 pm by paweld »
Best regards / Pozdrawiam
paweld

VisualLab

  • Hero Member
  • *****
  • Posts: 614
Re: Event Listeners in Lazarus - free library
« Reply #12 on: October 03, 2024, 12:08:39 pm »
Michael Van Canneyt wrote an article about it: https://www.freepascal.org/~michael/articles/cdcover/cdcover.pdf
Sources: https://www.freepascal.org/~michael/articles/cdcover/cdcover.zip

There is an error in the hyperlink (the word "Source" was added to the end of the URL).

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Lazarus 3.0/FPC 3.2.2

silvercoder70

  • Full Member
  • ***
  • Posts: 120
    • Tim Coates
Re: Event Listeners in Lazarus - free library
« Reply #14 on: October 03, 2024, 12:58:00 pm »
I noticed a reference to wanting tutorials and YouTube channel. Let me know what you are looking for and I can create a video for it next :)
Explore the beauty of modern Pascal programming with Delphi & Free Pascal - https://www.youtube.com/@silvercoder70

 

TinyPortal © 2005-2018