ReactiveStream
The ReactiveStream object provides a mechanism for subscribing to a stream of values. This can
be useful when the values are more temporal as opposed to state properties. Most usefully, the same
value can be sent consecutively without the deduplication which ReactiveState
provides.
Example
Below is a simple example which shows how a stream can be used to handle character events.
local eventStream = Reactive.GetStream(function() return nil end)
eventStream:Publisher()
:IgnoreIfNotEquals("JUMPED")
:CallFunction(function() print("Jumped!") end)
:Stored()
eventStream:Publisher()
:IgnoreIfNotEquals("ATTACKED")
:CallFunction(function() print("Attacked!") end)
:Stored()
eventStream:Send("JUMPED") -- "Jumped!"
eventStream:Send("JUMPED") -- "Jumped!"
eventStream:Send("ATTACKED") -- "Attacked!"
Memory Management
The lifecycle of stream objects is owned by LibTSMReactive, but they may be long-living. They are
acquired via the Reactive.GetStream() function and the application may call the :Release()
method in order to release them back to LibTSMReactive for recycling.
API
-
class ReactiveStream:
Class -
staticmethod Get(initialValueFunc: fun():
any):ReactiveStream Gets a stream object.
- Parameters:
initialValueFunc (fun():
any) – A function to get the initial value to send to new publishers
-
Release(self:
ReactiveStream) Releases the stream.
-
Publisher(self:
ReactiveStream):ReactivePublisherSchema Creates a new publisher for the stream.
-
Send(self:
ReactiveStream, data:any) Sends a new data value the stream’s publishers.
- Parameters:
data (
any) – The data to send
-
SetNoPublishersCallback(
self:ReactiveStream,
handler: fun(stream:ReactiveStream)
):ReactiveStream Sets a callback for when there are no remaining publishers.
- Parameters:
handler (fun(stream:
ReactiveStream)) – The handler function
-
GetNumPublishers(self:
ReactiveStream):number Gets the number of publishers on the stream.
-
staticmethod Get(initialValueFunc: fun():