### Delphi: Implementing Thread-Safe Event Handling with BeginWork/EndWork Source: https://github.com/dalijap/nx-horizon/blob/master/README.md This Delphi code demonstrates how to use "BeginWork" and "EndWork" methods within an event handler to ensure that background tasks initiated by the handler are properly waited for when "WaitFor" is called on the subscription. It shows a "TTask" being used for asynchronous execution, with "BeginWork" called before the task starts and "EndWork" in a "finally" block to guarantee its execution, preventing deadlocks. ```delphi procedure TSubscriber.OnLongEvent(const aEvent: TIntegerEvent); begin fIntegerSubscription.BeginWork; try TTask.Run( procedure begin try ... finally fIntegerSubscription.EndWork; end; end); except fIntegerSubscription.EndWork; raise; end; end; ``` -------------------------------- ### Subscribe and Unsubscribe to NX Horizon Events in Delphi Source: https://github.com/dalijap/nx-horizon/blob/master/README.md This example illustrates how to subscribe to and unsubscribe from events within a Delphi class. Subscriptions are managed using INxEventSubscription objects, which allow specifying the delivery method (e.g., Async, Sync). It's crucial to unsubscribe and potentially WaitFor pending asynchronous operations in the destructor to ensure proper cleanup and prevent memory leaks. ```Delphi type TSubscriber = class protected // subscriptions fIntegerSubscription: INxEventSubscription; fStringSubscription: INxEventSubscription; // event handlers procedure OnIntegerEvent(const aEvent: TIntegerEvent); procedure OnStringEvent(const aEvent: TStringEvent); public constructor Create; destructor Destroy; override; end; constructor TSubscriber.Create; begin fIntegerSubscription := NxHorizon.Instance.Subscribe(Async, OnIntegerEvent); fStringSubscription := NxHorizon.Instance.Subscribe(Sync, OnStringEvent); end; destructor TSubscriber.Destroy; begin fIntegerSubscription.WaitFor; fStringSubscription.WaitFor; NxHorizon.Instance.Unsubscribe(fIntegerSubscription); NxHorizon.Instance.Unsubscribe(fStringSubscription); inherited; end; procedure TSubscriber.OnIntegerEvent(const aEvent: TIntegerEvent); begin Writeln(aEvent); end; procedure TSubscriber.OnStringEvent(const aEvent: TStringEvent); begin Writeln(aEvent); end; ``` -------------------------------- ### APIDOC: INxHorizon Interface and TNxHorizonContainer Class Introduction Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md Introduced the `INxHorizon` interface and its implementing class `TNxHorizonContainer`. This new structure is designed to hold a single instance of the `TNxHorizon` class, providing a standardized way to manage the core horizon object. ```APIDOC INxHorizon: - Type: Interface - Implemented by: TNxHorizonContainer - Purpose: Holds a single instance of TNxHorizon class ``` -------------------------------- ### APIDOC: Send Method Main Thread Dispatching Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md The `Send` method now correctly dispatches events on the main thread when required by a subscription. If the `aDelivery` parameter specifies a blocking call (`Sync`), dispatching for such subscriptions will be performed synchronously via `TThread.Synchronize`. ```APIDOC Send: - Improvement: Honors dispatching events on main thread if required by subscription - Behavior: If aDelivery specifies Sync, dispatching is synchronous via TThread.Synchronize ``` -------------------------------- ### APIDOC: Managing INxHorizon Event Bus Instance Lifetime Source: https://github.com/dalijap/nx-horizon/blob/master/README.md Describes the INxHorizon interface for safely holding and sharing event bus instances, its role in managing their lifetime, and the proper use of the ShutDown method and IsActive flag for cleanup and state checking. ```APIDOC INxHorizon: Purpose: Safely hold and share event bus instances, simplifying life management and avoiding dangling pointers. Instance: Returns a wrapped TNxHorizon instance, manually managed by a container. Usable as long as subscriber holds a strong reference to its container. ShutDown(): Purpose: Initiates cleanup process for the event bus instance. Effect: Sets IsActive flag to False. Event: Sends TNxHorizonShutDownEvent to subscribers. IsActive: Type: Boolean flag. Purpose: Indicates if the event bus instance is active. Usage: Check this flag before calling Post or Send to ensure no new events are dispatched during cleanup. TNxHorizonShutDownEvent: Purpose: Event sent when an INxHorizon instance is shut down. Content: Contains the wrapped TNxHorizon instance, allowing subscribers to manage multiple subjects with a single handler. ``` -------------------------------- ### APIDOC: Unsubscribe Method Safety Improvement Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md The `Unsubscribe` method can now be safely called even if the subscription is `nil`. This improvement enhances the robustness of event handling by preventing potential null reference errors. ```APIDOC Unsubscribe: - Improvement: Can be safely called for nil subscription ``` -------------------------------- ### Send Messages (Post/Send) with NX Horizon in Delphi Source: https://github.com/dalijap/nx-horizon/blob/master/README.md This snippet demonstrates how to publish events using the Post and Send methods of the NxHorizon.Instance. Post is typically used for fire-and-forget, while Send allows specifying the delivery option. Both direct value passing and variable passing are shown for different event types. ```Delphi NxHorizon.Instance.Post(5); NxHorizon.Instance.Send('abc', Async); // Or using variables: var IntEvent: TIntegerEvent; StrEvent: TStringEvent; IntEvent := 5; StrEvent := 'abc'; NxHorizon.Instance.Post(IntEvent); NxHorizon.Instance.Send(StrEvent, Async); ``` -------------------------------- ### NX Horizon Event Delivery Options (TNxHorizonDelivery) Source: https://github.com/dalijap/nx-horizon/blob/master/README.md The TNxHorizonDelivery type defines four distinct options for event delivery within NX Horizon. These options control whether an event is processed synchronously or asynchronously, and whether it executes on the current thread or the main thread. Blocking operations (Sync, MainSync) should be used cautiously, especially with the default event bus instance. ```APIDOC TNxHorizonDelivery: Sync: synchronous in the current thread Async: asynchronous in a random background thread MainSync: synchronous on the main thread MainAsync: asynchronous on the main thread Notes: - Sync and MainSync are BLOCKING operations. - MainAsync uses TThread.ForceQueue for main thread execution. ``` -------------------------------- ### APIDOC: NxHorizon Event Dispatching Methods (Post, Send) Source: https://github.com/dalijap/nx-horizon/blob/master/README.md This API documentation outlines the "Post" and "Send" methods used for dispatching events in NxHorizon. "Post(const aEvent: T)" dispatches an event where the delivery option is determined by the subscription. "Send(const aEvent: T; aDelivery: TNxHorizonDelivery)" overrides the subscription's delivery option, allowing explicit control over how the event is dispatched, while still respecting main thread requirements if specified by the subscription. ```APIDOC procedure Post(const aEvent: T); procedure Send(const aEvent: T; aDelivery: TNxHorizonDelivery); ``` -------------------------------- ### APIDOC: TNxHorizonContainer.New Signature Change Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md The signature of `TNxHorizonContainer.New` has been updated to return the `INXHorizon` interface instead of `TNXHorizonContainer`. This change ensures proper initialization of reference counting for the object. ```APIDOC TNxHorizonContainer.New: - Old: Returns TNxHorizonContainer - New: Returns INXHorizon interface - Purpose: Proper initialization of reference counting ``` -------------------------------- ### APIDOC: WaitFor Periodically Calls CheckSynchronize Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md The `WaitFor` method now periodically invokes `CheckSynchronize`. This enhancement allows `WaitFor` to be used on the main thread without deadlocking, especially when event handlers utilize `TThread.Synchronize` for thread synchronization. ```APIDOC WaitFor: - Improvement: Periodically calls CheckSynchronize - Benefit: Allows use on main thread with TThread.Synchronize event handlers without deadlocking ``` -------------------------------- ### Delphi: Using Dedicated Thread Pool for Long-Running Event Handlers Source: https://github.com/dalijap/nx-horizon/blob/master/README.md Illustrates how to offload long-running tasks from event handlers to a dedicated thread pool using TTask.Run in Delphi XE7 and newer versions. This prevents contention on the default thread pool and provides more control over specific handler behavior. ```delphi procedure TSubscriber.OnLongEvent(const aEvent: TLongEvent); begin TTask.Run( procedure begin ... end, DedicatedThreadPool); end; ``` -------------------------------- ### APIDOC: TNxEvent Record Removal Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md The `TNxEvent` record has been removed as it provided no significant value. Developers should now directly create new types from the original type instead of using the record wrapper. ```APIDOC TNxEvent: - Status: Removed - Reason: Provided no value - Replacement: Create new type directly from original type ``` -------------------------------- ### Declare Event Types in Delphi for NX Horizon Source: https://github.com/dalijap/nx-horizon/blob/master/README.md Events in NX Horizon are categorized by their type information. This snippet demonstrates how to declare various event types, including simple types, type aliases, and generic interface wrappers like INxEvent for existing classes or records, which facilitate easier event management. ```Delphi type TFoo = class ... end; TOtherFoo = type TFoo; TIntegerEvent = type Integer; TStringEvent = type string; TFooEvent = INxEvent; TOtherFooEvent = INxEvent; ``` -------------------------------- ### APIDOC: TNxEventObject Renamed Source: https://github.com/dalijap/nx-horizon/blob/master/ChangeLog.md The class `TNxEventObject` has been renamed to `TNxEvent`. This change aims to shorten the class name, making code more concise and readable, especially when constructing new event objects within `Post` or `Send` calls. ```APIDOC TNxEventObject: - Renamed to: TNxEvent - Reason: Shorter class name for improved readability in Post/Send calls ``` -------------------------------- ### Delphi Event Handler Method Signature for NX Horizon Source: https://github.com/dalijap/nx-horizon/blob/master/README.md Event handler methods for NX Horizon must conform to a specific declaration. This snippet shows the required signature, where T represents the event type. It's important to note that asynchronous delivery requires types with automatic memory management or value types, and careful consideration is needed for manually managed objects. ```Delphi procedure(const aEvent: T) of object; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.