Observer
- Wally Package: Observer
A typed observer that notifies subscribers when its value changes. Observers can be created and subscribed to from any module. They are useful for decoupling modules and creating a more modular codebase. The Observer class is a singleton and should not be instantiated. The Observer class provides two methods for creating and getting observers. You can create an observer with a specific type and subscribe to it with a callback. When the observer's value changes, all subscribed callbacks are called with the new value.
One of my favorite explanations of the observer pattern is from Refactoring Guru.
Here's an example of how to use the Observer class:
Firstly, Setup Event Module
local Observer = require(path.to.Observer)
local events = {
["PlayerDamaged"] = Observer.Create("PlayerDamaged") :: Observer.Event<Player, number>,
["MorningTime"] = Observer.Create("MorningTime") :: Observer.Event<string>,
}
return events
Secondly, Connect to an Event in any Module
local events = require(path.to.Events)
events.MorningTime:Connect(function(morningString: string)
if morningString == "Good Morning" then
print(`{morningString}! It's a awesome day!`)
elseif morningString == "Bad Morning" then
print(`{morningString}! It's what we get when it rains!`)
end
end)
events.PlayerDamaged:Connect(function(player: Player, damage: number)
print(`{player.Name} took {damage} damage!`)
playerEffects:ShakeScreen() -- example function
particleSystem:BloodSplatter(player.Position) -- example function
end)
Lastly, Fire the Event Value in any Module
local events = require(path.to.Events)
events.MorningTime:Fire("Good Morning")
while player:IsStandingInFire() do
events.PlayerDamaged:Fire(10)
end
NOTE
Observers are client/server specific and cannot be shared between them.
You would need to create a separate observer for each side.
OR create some sort of networked event system to communicate between them.
Types
Connection
interface
Connection {
Disconnect:
(
self:
Connection
)
→
(
)
--
Disconnects the connection, removing the callback from the event.
}
Functions
Create
Observer.
Create
(
name:
string
) →
Event
<
T...
>
Types
interface
Event<T...> {
Connect:
(
self:
Event
<
T...
>
,
callback:
(
...any
)
→
(
)
)
→
Connection
--
Connects a callback to the event. The callback will be called with the event's parameters when the event is fired.
Fire:
(
self:
Event
<
T...
>
,
...:
T...
)
→
(
)
--
Fires the event with the given parameters. All connected callbacks will be called with these parameters.
}
Creates a new typed observer with the specified name.
local Observer = require(path.to.Observer)
local events = {
["PlayerDamaged"] = Observer.Create("PlayerDamaged") :: Observer.Event<Player, number>,
["MorningTime"] = Observer.Create("MorningTime") :: Observer.Event<string>,
}
return events