Move Luau polyfills into separate modules folder for better organisation

This commit is contained in:
Lewin Kelly 2024-01-28 17:09:33 +00:00
parent 4f0ebaa9fc
commit d0c2b9d636
21 changed files with 48 additions and 49 deletions

View File

@ -14,7 +14,7 @@ local SpringScheduler = require "../Animation/SpringScheduler"
local updateAll = require "../State/updateAll"
local xtypeof = require "../Utility/xtypeof"
local peek = require "../State/peek"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local class = {}

View File

@ -13,7 +13,7 @@ local logError = require "../Logging/logError"
local logErrorNonFatal = require "../Logging/logErrorNonFatal"
local xtypeof = require "../Utility/xtypeof"
local peek = require "../State/peek"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local class = {}

View File

@ -6,7 +6,7 @@
]]
-- local TweenService = game:GetService "TweenService"
local easing = require "../Polyfill/easing"
local easing = require "../../../Modules/Polyfill/easing"
local function getTweenRatio(tweenInfo: TweenInfo, currentTime: number): number
local delay = tweenInfo.DelayTime

View File

@ -10,7 +10,7 @@
]]
local Oklab = require "../Colour/Oklab"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function lerpType(from: any, to: any, ratio: number): any
local typeString = typeof(from)

View File

@ -11,7 +11,7 @@ local logWarn = require "../Logging/logWarn"
local Observer = require "../State/Observer"
local peek = require "../State/peek"
local isState = require "../State/isState"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
type Set<T> = { [T]: boolean }

View File

@ -7,7 +7,7 @@
local PubTypes = require "../PubTypes"
local logError = require "../Logging/logError"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function OnChange(propertyName: string): PubTypes.SpecialKey
local changeKey = {}

View File

@ -7,7 +7,7 @@
local PubTypes = require "../PubTypes"
local logError = require "../Logging/logError"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function getProperty_unsafe(instance: Instance, property: string)
return (instance :: any)[property]

View File

@ -20,7 +20,7 @@ local xtypeof = require "../Utility/xtypeof"
local logError = require "../Logging/logError"
local Observer = require "../State/Observer"
local peek = require "../State/peek"
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function setProperty_unsafe(
instance: Instance,

View File

@ -20,24 +20,20 @@ local function logError(messageID: string, errObj: Types.Error?, ...)
local errorString
if errObj == nil then
errorString = string.format(
"[Fusion] " .. formatString .. "\n(ID: " .. messageID .. ")",
...
string.format(`[Fusion] {formatString}\n(ID: {messageID})`, ...)
)
else
formatString =
formatString:gsub("ERROR_MESSAGE", tostring(errObj.message))
string.gsub(formatString, "ERROR_MESSAGE", tostring(errObj.message))
errorString = string.format(
"[Fusion] "
.. formatString
.. "\n(ID: "
.. messageID
.. ")\n---- Stack trace ----\n"
.. tostring(errObj.trace),
`[Fusion] {formatString}\n(ID: {messageID})\n---- Stack trace ----\n{tostring(
errObj.trace
)}`,
...
)
end
error(errorString:gsub("\n", "\n "), 0)
error(string.gsub(errorString, "\n", "\n "), 0)
end
return logError

View File

@ -19,18 +19,16 @@ local function logErrorNonFatal(messageID: string, errObj: Types.Error?, ...)
local errorString
if errObj == nil then
errorString =
errorString = string.format(
string.format(`[Fusion] {formatString}\n(ID: {messageID})`, ...)
)
else
formatString =
formatString:gsub("ERROR_MESSAGE", tostring(errObj.message))
string.gsub(formatString, "ERROR_MESSAGE", tostring(errObj.message))
errorString = string.format(
"[Fusion] "
.. formatString
.. "\n(ID: "
.. messageID
.. ")\n---- Stack trace ----\n"
.. tostring(errObj.trace),
`[Fusion] {formatString}\n(ID: {messageID})\n---- Stack trace ----\n{tostring(
errObj.trace
)}`,
...
)
end

View File

@ -23,6 +23,7 @@ return {
forKeysKeyCollision = "ForKeys should only write to output key '%s' once when processing key changes, but it wrote to it twice. Previously input key: '%s'; New input key: '%s'",
forKeysDestructorError = "ForKeys destructor error: ERROR_MESSAGE",
forPairsDestructorError = "ForPairs destructor error: ERROR_MESSAGE",
-- logError(invalidEasingStyle, easingStyle)
forPairsKeyCollision = "ForPairs should only write to output key '%s' once when processing key changes, but it wrote to it twice. Previous input pair: '[%s] = %s'; New input pair: '[%s] = %s'",
forPairsProcessorError = "ForPairs callback error: ERROR_MESSAGE",
forValuesProcessorError = "ForValues callback error: ERROR_MESSAGE",
@ -49,7 +50,4 @@ return {
unrecognisedChildType = "'%s' type children aren't accepted by `[Children]`.",
unrecognisedPropertyKey = "'%s' keys aren't accepted in property tables.",
unrecognisedPropertyStage = "'%s' isn't a valid stage for a special key to be applied at.",
invalidEasingStyle = "The easing style must be a valid Enum.EasingStyle or a string of 'Linear', 'Quad', 'Cubic', 'Quart', 'Quint', 'Sine', 'Exponential', 'Circular', 'Elastic', 'Back', 'Bounce'. (got %s)",
invalidEasingDirection = "The easing direction must be a valid Enum.EasingDirection or a string of 'In', 'Out', 'InOut', 'OutIn'. (got %s)",
}

View File

@ -1,6 +1,6 @@
--!strict
--[[
Roblox implementation for Fusion's abstract scheduler layer.
Mercury implementation for Fusion's abstract scheduler layer.
]]
local RunService = game:GetService "RunService"
@ -24,7 +24,7 @@ function MercuryExternal.doTaskDeferred(resume: () -> ())
end
--[[
Sends an update step to Fusion using the Roblox clock time.
Sends an update step to Fusion using the Mercury clock time.
]]
local function performUpdateStep()
External.performUpdateStep(time())
@ -38,7 +38,7 @@ function MercuryExternal.startScheduler()
if stopSchedulerFunc ~= nil then
return
end
-- if RunService:IsClient() then
-- if not _SERVER then
-- In cases where multiple Fusion modules are running simultaneously,
-- -- this prevents collisions.
-- local id = "FusionUpdateStep_" .. HttpService:GenerateGUID()

View File

@ -133,7 +133,7 @@ export type Spring<T> = StateObject<T> & Dependent & {
-- An object which can listen for updates on another state object.
export type Observer = Dependent & {
kind: "Observer",
onChange: (Observer, callback: () -> ()) -> (() -> ()),
onChange: (Observer, callback: () -> ()) -> () -> (),
}
--[[

View File

@ -83,7 +83,7 @@ export type ForPairs<KI, VI, KO, VO, M> = PubTypes.ForPairs<KO, VO> & {
-- A state object whose value is derived from other objects using a callback.
export type ForKeys<KI, KO, M> = PubTypes.ForKeys<KO, any> & {
_oldDependencySet: Set<PubTypes.Dependency>,
_processor: (PubTypes.Use, KI) -> (KO),
_processor: (PubTypes.Use, KI) -> KO,
_destructor: (KO, M?) -> (),
_inputIsState: boolean,
_inputTable: PubTypes.CanBeState<{ [KI]: KO }>,
@ -103,7 +103,7 @@ export type ForKeys<KI, KO, M> = PubTypes.ForKeys<KO, any> & {
-- A state object whose value is derived from other objects using a callback.
export type ForValues<VI, VO, M> = PubTypes.ForValues<any, VO> & {
_oldDependencySet: Set<PubTypes.Dependency>,
_processor: (PubTypes.Use, VI) -> (VO),
_processor: (PubTypes.Use, VI) -> VO,
_destructor: (VO, M?) -> (),
_inputIsState: boolean,
_inputTable: PubTypes.CanBeState<{ [VI]: VO }>,

View File

@ -11,7 +11,7 @@
- an array - `cleanup` will be called on each item
]]
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function cleanupOne(task: any)
local taskType = typeof(task)

View File

@ -5,7 +5,7 @@
requires manual cleanup.
]]
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function needsDestruction(x: any): boolean
return typeof(x) == "Instance"

View File

@ -6,7 +6,7 @@
Otherwise, returns `typeof()` the argument.
]]
local typeof = require "../Polyfill/typeof"
local typeof = require "../../../Modules/Polyfill/typeof"
local function xtypeof(x: any)
local typeString = typeof(x)

View File

@ -42,8 +42,8 @@ local Fusion = restrictRead("Fusion", {
doNothing = require "./Utility/doNothing",
peek = require "./State/peek",
typeof = require "./Polyfill/typeof",
TweenInfo = require "./Polyfill/TweenInfo",
typeof = require "../../Modules/Polyfill/typeof",
TweenInfo = require "../../Modules/Polyfill/TweenInfo",
Help = function()
return "See https://elttob.uk/Fusion/0.3/ for more information."
@ -69,10 +69,10 @@ type Fusion = {
New: (
className: string
) -> ((propertyTable: PubTypes.PropertyTable) -> Instance),
) -> (propertyTable: PubTypes.PropertyTable) -> Instance,
Hydrate: (
target: Instance
) -> ((propertyTable: PubTypes.PropertyTable) -> Instance),
) -> (propertyTable: PubTypes.PropertyTable) -> Instance,
Ref: PubTypes.SpecialKey,
Cleanup: PubTypes.SpecialKey,
Children: PubTypes.SpecialKey,

View File

@ -1,7 +1,6 @@
-- A basic polyfill for the TweenInfo.new function,
-- allows using Enum.EasingStyle/Direction or strings instead
local logError = require "../Logging/logError"
local TweenInfo = {}
function TweenInfo.new(
@ -19,7 +18,7 @@ function TweenInfo.new(
-- convert it to a string
if type(easingStyle) ~= "string" then
if easingStyle then
easingStyle = tostring(easingStyle):gsub("Enum.%w+.", "")
easingStyle = string.gsub(tostring(easingStyle), "Enum.%w+.", "")
end
else
local ok
@ -42,13 +41,17 @@ function TweenInfo.new(
end
end
if not ok then
logError("invalidEasingStyle", nil, easingStyle)
error(
`The easing style must be a valid Enum.EasingStyle or a string of 'Linear', 'Quad', 'Cubic', 'Quart', 'Quint', 'Sine', 'Exponential', 'Circular', 'Elastic', 'Back', 'Bounce'. (got {easingStyle})`,
0
)
end
end
if type(easingDirection) ~= "string" then
if easingDirection then
easingDirection = tostring(easingDirection):gsub("Enum.%w+.", "")
easingDirection =
string.gsub(tostring(easingDirection), "Enum.%w+.", "")
end
else
local ok
@ -64,7 +67,10 @@ function TweenInfo.new(
end
end
if not ok then
logError("invalidEasingDirection", nil, easingDirection)
error(
`The easing direction must be a valid Enum.EasingDirection or a string of 'In', 'Out', 'InOut', 'OutIn'. (got {easingDirection})`,
0
)
end
end
@ -99,9 +105,9 @@ function TweenInfo.new(
.. " Reverses:"
.. (reverses and "True" or "False")
.. " EasingDirection:"
.. easingDirection
.. tostring(easingDirection)
.. " EasingStyle:"
.. easingStyle
.. tostring(easingStyle)
end
mt.__metatable = "The metatable is locked"

View File

@ -178,4 +178,5 @@ return function(value)
return t
end
end
return basicType
end