A basic in-application notification library. It queues notifications and uses timer to sequence the distribution, so publisher and subscribers are independent code streams.
Link to the library: https://github.com/slo1958/sl-xj-lib-notifications.git
The ‘Shareable’ folder contains the library itself. Contains an example program opening a few windows to discover and test. Feedback welcomed.
I’ve used something very similar for years, EventBusIJ by Ian Jones. One thing you might consider is making the “message” parameter a variant instead of a string - much more versatile that way.
Thanks for the suggestion. An optional field of type variant is now available. Also added a small demo window where the notification includes a pointer to a listbox.
I think the two libraries (EventBus and this one) are not competing for the same use case(s).
EventBus (as far as I understand) makes synchronous call(s): your code continues once the message is processed by the receiver, including the handling of any subsequent messages generated by the receiver.
lib-notifications handles the messages asynchronously, your code continues when the message is stored in the input queue, irrespective of the time the receiver needs to process it. The sorting and distribution are done when the framework takes over via the action event handler of a timer.
I can probably extend lib-notifications to support synchronous calls, but EventBus is available and already tested for this use-case.
As usual, comments welcomed.
Indeed, synchronous vs asynchronous is a major difference. One might argue that synchronous, being more deterministic, might be less likely to introduce difficult-to-track-down bugs, as well as offering the ability to return a value (which is admittedly not often useful).
Async makes yours inherently thread-safe; I had to use a dispatcher (CallDelegateonMainThreadMBS) in my adaptation of IJ’s module to achieve that (which maybe makes mine “async”, after all, lol).
One rather obscure gotcha I ran into recently: I was using the event bus to publish sensor readings coming in from the outside world from Phidgets devices. It worked fine unless I breakpointed the app, in which case the events would be queued up by the thousands, either in the OS or in the MBS plugin, I’m not sure which. When execution was resumed, all the queued events would start publishing, and since I’d added the thread-safe dispatcher, each event publication added to Windows’ User Handle count, and the OS limit of 10,000 handles was exceeded. I had to decouple the event publishing from the Phidget’s event handler in order to solve the issue.
Interesting because the main reason I ‘revived’ an old library written at the time of Realbasic ( removed tons of ‘dust’, probably introduced as many bugs) is because I’m using TCP sockets to talk to the outside world and I wanted the whole thing easy to test piece by piece (UI, TCP message handling, …)