Brio
Brios wrap a value (or tuple of values) and are used to convey the lifetime of that object. The brio is better than a maid, by providing the following constraints:
- Can be in 2 states, dead or alive.
- While alive, can retrieve values.
- While dead, retrieving values is forbidden.
- Died will fire once upon death.
Brios encapsulate the "lifetime" of a valid resource. Unlike a maid, they
- Can only die once, ensuring duplicate calls never occur.
- Have less memory leaks. Memory leaks in maids can occur when use of the maid occurs after the cleanup of the maid has occured, in certain race conditions.
- Cannot be reentered, i.e. cannot retrieve values after death.
info
Calling brio:Destroy()
or brio:Kill()
after death does nothing. Brios cannot
be resurrected.
Brios are useful for downstream events where you want to emit a resource. Typically brios should be killed when their source is killed. Brios are intended to be merged with downstream brios so create a chain of reliable resources.
local brio = Brio.new("a", "b")
print(brio:GetValue()) --> a b
print(brio:IsDead()) --> false
brio:GetDiedSignal():Connect(function()
print("Hello from signal!")
end)
brio:ToMaid():GiveTask(function()
print("Hello from maid cleanup!")
end)
brio:Kill()
--> Hello from signal!
--> Hello from maid cleanup!
print(brio:IsDead()) --> true
print(brio:GetValue()) --> ERROR: Brio is dead
Design philosophy
Brios are designed to solve this issue where we emit an object with a lifetime associated with it from an Observable stream. This resource is only valid for some amount of time (for example, while the object is in the Roblox data model).
In order to know how long we can keep this object/use it, we wrap the object with a Brio, which denotes the lifetime of the object.
Modeling this with pure observables is very tricky because the subscriber will have to also monitor/emit a similar object with less clear conventions. For example an observable that emits the object, and then nil on death.
Properties
DEAD
Brio.DEAD:
Brio
An already dead brio which may be used for identity purposes.
print(Brio.DEAD:IsDead()) --> true
Functions
isBrio
Brio.
isBrio
(
value:
any
) →
boolean
Returns whether a value is a Brio.
print(Brio.isBrio("yolo")) --> false
new
Constructs a new Brio.
local brio = Brio.new("a", "b")
print(brio:GetValue()) --> a b
delayed
Constructs a new brio that will cleanup afer the set amount of time
GetDiedSignal
Gets a signal that will fire when the Brio dies. If the brio is already dead calling this method will error.
info
Calling this while the brio is already dead will throw a error.
local brio = Brio.new("a", "b")
brio:GetDiedSignal():Connect(function()
print("Brio died")
end)
brio:Kill() --> Brio died
brio:Kill() -- no output
IsDead
Brio:
IsDead
(
) →
boolean
Returns true is the brio is dead.
local brio = Brio.new("a", "b")
print(brio:IsDead()) --> false
brio:Kill()
print(brio:IsDead()) --> true
ErrorIfDead
Brio:
ErrorIfDead
(
) →
(
)
Throws an error if the Brio is dead.
brio.DEAD:ErrorIfDead() --> ERROR: [Brio.ErrorIfDead] - Brio is dead
ToMaid
Constructs a new Maid which will clean up when the brio dies. Will error if the Brio is dead.
info
Calling this while the brio is already dead will throw a error.
local brio = Brio.new("a")
local maid = brio:ToMaid()
maid:GiveTask(function()
print("Cleaning up!")
end)
brio:Kill() --> Cleaning up!
GetValue
Brio:
GetValue
(
) →
any
If the brio is not dead, will return the values unpacked from the brio.
info
Calling this while the brio is already dead will throw a error. Values should not be used past the lifetime of the brio, and can be considered invalid.
local brio = Brio.new("a", 1, 2)
print(brio:GetValue()) --> "a" 1 2
brio:Kill()
print(brio:GetValue()) --> ERROR: Brio is dead
GetPackedValues
Brio:
GetPackedValues
(
) →
{
n:
number
,
...
T
}
Returns the packed values from table.pack() format
Destroy
Brio:
Destroy
(
) →
(
)
Kills the Brio.
info
You can call this multiple times and it will not error if the brio is dead.
local brio = Brio.new("hi")
print(brio:GetValue()) --> "hi"
brio:Kill()
print(brio:GetValue()) --> ERROR: Brio is dead
Kill
Brio:
Kill
(
) →
(
)
Alias for Destroy.