Expand description
Nuts is a library that offers a simple publish-subscribe API, featuring decoupled creation of the publisher and the subscriber.
§Quick first example
struct Activity;
let activity = nuts::new_activity(Activity);
activity.subscribe(
|_activity, n: &usize|
println!("Subscriber received {}", n)
);
nuts::publish(17usize);
// "Subscriber received 17" is printed
nuts::publish(289usize);
// "Subscriber received 289" is printed
As you can see in the example above, no explicit channel between publisher and subscriber is necessary.
The call to publish
is a static method that requires no state from the user.
The connection between them is implicit because both use usize
as message type.
Nuts enables this simple API by managing all necessary state in thread-local storage. This is particularly useful when targeting the web. However, Nuts can be used on other platforms, too. In fact, Nuts has no dependencies aside from std.
Macros§
- domain_
enum - Implements DomainEnumeration for an enum.
Structs§
- Activity
Id - Handle to an
Activity
that has been registered, with a type parameter to track the activity’s type. Can be used to add type-checked closures to the activity, which will be used as event listeners. - Default
Domain - If only one domain is required, this can be used.
But if you have your own domain type defined with
domain_enum!
or implementingDomainEnumeration
manually, do not use this. Different domain types should never be mixed! - Domain
State - Stores passive data that can be accessed in event handlers of multiple activities.
- Subscription
Filter - Defines under which circumstances a subscribing activity should be called. At the moment, the only filter option is to check the activity lifecycle state. The default filter will ignore events when the activity is inactive.
- Unchecked
Activity Id - Pointer to an activity that has been registered. Can be used to set the lifecycle stats of activities.
Enums§
- Lifecycle
Status - Each activity has a lifecycle status that can be changed using
set_status
. It starts withLifecycleStatus::Active
. In the current version of Nuts, the only other status isLifecycleStatus::Inactive
.
Traits§
- Activity
- Activities are at the core of Nuts.
From the globally managed data, they represent the active part, i.e. they can have event listeners.
The passive counter-part is defined by
DomainState
. - Domain
Enumeration - Used for mapping domain identifiers to unique integers.
Can be derived with
domain_enum!(TYPE)
;
Functions§
- new_
activity - Consumes a struct and registers it as an Activity.
- new_
domained_ activity - Consumes a struct that is registered as an Activity that has access to the specified domain.
Use the returned
ActivityId
to register callbacks on the activity. - panic_
info - Read some information about currently processing activities. This should be called inside a panic hook.
- publish
- Send the message to all subscribed activities
- publish_
awaiting_ response - Returns a future of type
NutsResponse
which will resolve after the message has been published and all subscribers have finished processing it. - send_to
- Publish a message to a specific activity. The same as
id.private_message()
but works without anActivityId
. - store_
to_ domain - Puts the data object to the domain, which can be accessed by all associated activities.