FusionUtil
A collection of utility functions for Fusion.
DO NOT ACCESS THIS IN MULTIPLE VMs. Studio freaks out when fusion is loaded in multiple VMs for some unknown reason.
Functions
promiseStateChange
FusionUtil.
promiseStateChange
(
state:
State
<
any
>
,
--
The state to observe
callback:
(
(
value:
any
)
→
boolean
)
?
--
An optional condition to check before resolving the promise
) →
Promise
--
The promise that will resolve when the state changes
Creates a promise that resolves when the given state changes. If a callback is given then the callback must return true for the promise to resolve.
local a = Value(10)
FusionUtil.promiseStateChange(a, function(value)
return value > 10
end):andThen(function(value)
print("Value is now greater than 10")
end)
a:set(5) -- Promise does not resolve
a:set(15) -- Promise resolves
defaultProps
FusionUtil.
defaultProps
(
explicitProps:
Props
,
--
The props to use
defaultProps:
Props
--
The default props that to fill in the gaps of explicit
) →
table
--
The reconciled props
Similar to a shallow reconcile, but also moves any children at numeric indices
ensureIsState
FusionUtil.
ensureIsState
(
datatype:
(
string
|
{
string
}
)
?
--
The type or types of the data expected in the state
) →
StateObject
<
T
>
--
The existing or newly created state object
Ensures a passed data is a StateObject. If it is not, it will be converted to one.
ensureIsValue
FusionUtil.
ensureIsValue
(
datatype:
(
string
|
{
string
}
)
?
--
The type or types of the data expected in the value
) →
Value
<
T
>
--
The existing or newly created value
Ensures the given data is a settable Value. Allows for passing of a default value and a datatype to check against.
syncValues
FusionUtil.
syncValues
(
stateToWatch:
State
<
any
>
,
--
The state to watch for changes
valueToSet:
Value
<
any
>
--
The value to set when the state changes
) →
(
)
→
(
)
--
A function that will disconnect the observer
Syncronizes a StateObject to a Value. The Value will be set to the StateObject's value any time it changes.
local a = Value(123)
local b = Value(0)
local disconnect = FusionUtil.syncValues(a, b)
print( peek(b) ) -- 123
a:set(456)
print( peek(b) ) -- 456
disconnect()
ensureAssetId
FusionUtil.
ensureAssetId
(
id:
CanBeState
<
string
|
number
>
,
--
The AssetId to ensure
default:
(
string
|
number
)
?
--
The default AssetId to use if the id is nil
) →
CanBeState
<
string
>
--
The State that is synced with the AssetId
Takes an AssetId and ensures it to a valid State.
local assetId = FusionUtil.ensureAssetId("rbxassetid://1234567890")
print( peek(assetId) ) -- "rbxassetid://1234567890"
local assetId = FusionUtil.ensureAssetId(1234567890)
print( peek(assetId) ) -- "rbxassetid://1234567890"
ratio
FusionUtil.
ratio
(
numerator:
CanBeState
<
number
>
,
--
The numerator of the ratio
denominator:
CanBeState
<
number
>
,
--
The denominator of the ratio
) →
State
<
T
>
--
The ratio (Potentially mutated)
Generates a computed that calculates the ratio of two numbers as a State.
local numerator = Value(100)
local denominator = Value(200)
local ratio = FusionUtil.ratio(numerator, denominator)
print( peek(ratio) ) -- 0.5
ratioUDim2
FusionUtil.
ratioUDim2
(
numerator:
CanBeState
<
number
>
,
--
The numerator of the ratio
denominator:
CanBeState
<
number
>
,
--
The denominator of the ratio
) →
State
<
UDim2
>
--
The scaled UDim2
Wraps FusionUtil.ratio with a handler for UDim2s
local numerator = Value(100)
local denominator = Value(200)
local size = Value(UDim2.new(0.2, 100, 0.2, 100))
local sizeAdjusted = FusionUtil.ratioUDim2(numerator, denominator, size)
print( peek(sizeAdjusted) ) -- UDim2.new(0.1, 50, 0.1, 50)
screenRatio
This item only works when running on the client. ClientFusionUtil.
screenRatio
(
mutator:
(
CanBeState
<
T
>
|
(
ratio:
number
,
use:
Use
)
→
T
)
?
,
--
An optional State to scale by or a function to mutate the ratio
ratioFn:
any
--
An optional function to use for the ratio, defaults to FusionUtil.ratio, but could be given something like FusionUtil.ratioUDim2
) →
(
)
This wraps FusionUtil.ratio with a handler for scaling states/functions with the Screen Height.
local paddingOffset = Value(10)
local paddingAdjusted = FusionUtil.screenRatio(paddingOffset)
local size = Value(UDim2.new(0, 100, 0, 100))
local sizeAdjusted = FusionUtil.screenRatio(size, FusionUtil.ratioUDim2)
local x = Value(10)
local y = Value(20)
local z = FusionUtil.screenRatio(function(ratio, use)
return (use(x) + use(y)) * ratio
end)
lerpNumber
FusionUtil.
lerpNumber
(
n1:
CanBeState
<
number
>
,
--
The first number state
n2:
CanBeState
<
number
>
,
--
The second number state
alpha:
CanBeState
<
number
>
,
--
The alpha state
_use:
(
(
any
)
→
(
any
)
)
?
--
An optional function to use to get the values of the states
) →
State
<
number
>
|
number
--
The resultant lerped number state
Lerps between two number states. If no use function is given then it returns a state
local a = Value(10)
local b = Value(20)
local alpha = Value(0.5)
local z = FusionUtil.lerpNumber(a, b, alpha)
print( peek(z) ) -- 15
ifThenElse
FusionUtil.
ifThenElse
(
stateToCheck:
State
<
boolean
>
,
--
The condition to check
trueOutcome:
CanBeState
<
X
>
,
--
The value to return if the condition is true
falseOutcome:
CanBeState
<
Y
>
--
The value to return if the condition is false
) →
State
<
X
|
Y
>
--
The value that was returned
A simple swap function that returns the first value if the condition is true, otherwise returns the second value. Helps with simplifying lots of bulky computeds.
local a = Value(10)
local b = Value(20)
local y = Value(false)
local z = FusionUtil.ifThenElse(y, a, b)
print( peek(z) ) -- 20
y:set(true)
print( peek(z) ) -- 10
eq
FusionUtil.
eq
(
stateToCheck1:
CanBeState
<
any
>
,
--
The first potential state to check
stateToCheck2:
CanBeState
<
any
>
--
The second potential state to check
) →
State
<
boolean
>
--
A state resolving to the equality of the two given arguments
A simple equality function that returns true if the two states are equal.
local a = Value(10)
local b = Value(10)
local c = FusionUtil.eq(a, b)
print( peek(c) ) -- true
a:set(20)
print( peek(c) ) -- false
ifExists
FusionUtil.
ifExists
(
value:
any?
,
--
Value to check existance.
...:
any
--
Args to be passed to the callback.
) →
U...?
--
The returned value of the callback if 'value' exists.
Checks if a given value exists, if it does then this returns the returned value of the passed function.
ifIsState
FusionUtil.
ifIsState
(
state:
any?
,
--
State to check existence.
...:
any
--
Args to be passed to the callback.
) →
U...?
--
The returned value of the callback if 'value' exists.
Checks if a given value is a state, if it does then this returns the returned value of the passed function.
observeState
FusionUtil.
observeState
(
fusionState:
State
<
any
>
,
--
The state object to observe
callback:
(
stateValue:
any
)
→
(
)
--
The callback to call when the fusionState is updated
) →
(
)
→
(
)
--
A function that will disconnect the observer
Calls the provided callback immediately with the initial state and again anytime the state updates.
watchAttribute
This was deprecated in v1.0.0
Use Fusion's Attribute System instead
FusionUtil.
watchAttribute
(
attribute:
string
,
--
The name of the attribute to watch
defaultValue:
any?
--
The default value to use if the attribute is nil
) →
(
State
,
--
The state object that is synced with the value
(
)
→
(
)
--
A function that will disconnect the observer
)
Syncronizes an instances attribute to a Value. The value will be set to the attribute's value any time it changes. This is superceded by Fusion's Attribute System and should no longer be used.