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 updateAll = require "../State/updateAll"
local xtypeof = require "../Utility/xtypeof" local xtypeof = require "../Utility/xtypeof"
local peek = require "../State/peek" local peek = require "../State/peek"
local typeof = require "../Polyfill/typeof" local typeof = require "../../../Modules/Polyfill/typeof"
local class = {} local class = {}

View File

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

View File

@ -6,7 +6,7 @@
]] ]]
-- local TweenService = game:GetService "TweenService" -- 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 function getTweenRatio(tweenInfo: TweenInfo, currentTime: number): number
local delay = tweenInfo.DelayTime local delay = tweenInfo.DelayTime

View File

@ -10,7 +10,7 @@
]] ]]
local Oklab = require "../Colour/Oklab" 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 function lerpType(from: any, to: any, ratio: number): any
local typeString = typeof(from) local typeString = typeof(from)

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,18 +19,16 @@ local function logErrorNonFatal(messageID: string, errObj: Types.Error?, ...)
local errorString local errorString
if errObj == nil then if errObj == nil then
errorString = errorString = string.format(
string.format(`[Fusion] {formatString}\n(ID: {messageID})`, ...) string.format(`[Fusion] {formatString}\n(ID: {messageID})`, ...)
)
else else
formatString = formatString =
formatString:gsub("ERROR_MESSAGE", tostring(errObj.message)) string.gsub(formatString, "ERROR_MESSAGE", tostring(errObj.message))
errorString = string.format( errorString = string.format(
"[Fusion] " `[Fusion] {formatString}\n(ID: {messageID})\n---- Stack trace ----\n{tostring(
.. formatString errObj.trace
.. "\n(ID: " )}`,
.. messageID
.. ")\n---- Stack trace ----\n"
.. tostring(errObj.trace),
... ...
) )
end 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'", 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", forKeysDestructorError = "ForKeys destructor error: ERROR_MESSAGE",
forPairsDestructorError = "ForPairs 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'", 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", forPairsProcessorError = "ForPairs callback error: ERROR_MESSAGE",
forValuesProcessorError = "ForValues callback error: ERROR_MESSAGE", forValuesProcessorError = "ForValues callback error: ERROR_MESSAGE",
@ -49,7 +50,4 @@ return {
unrecognisedChildType = "'%s' type children aren't accepted by `[Children]`.", unrecognisedChildType = "'%s' type children aren't accepted by `[Children]`.",
unrecognisedPropertyKey = "'%s' keys aren't accepted in property tables.", unrecognisedPropertyKey = "'%s' keys aren't accepted in property tables.",
unrecognisedPropertyStage = "'%s' isn't a valid stage for a special key to be applied at.", 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 --!strict
--[[ --[[
Roblox implementation for Fusion's abstract scheduler layer. Mercury implementation for Fusion's abstract scheduler layer.
]] ]]
local RunService = game:GetService "RunService" local RunService = game:GetService "RunService"
@ -24,7 +24,7 @@ function MercuryExternal.doTaskDeferred(resume: () -> ())
end 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() local function performUpdateStep()
External.performUpdateStep(time()) External.performUpdateStep(time())
@ -38,7 +38,7 @@ function MercuryExternal.startScheduler()
if stopSchedulerFunc ~= nil then if stopSchedulerFunc ~= nil then
return return
end end
-- if RunService:IsClient() then -- if not _SERVER then
-- In cases where multiple Fusion modules are running simultaneously, -- In cases where multiple Fusion modules are running simultaneously,
-- -- this prevents collisions. -- -- this prevents collisions.
-- local id = "FusionUpdateStep_" .. HttpService:GenerateGUID() -- 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. -- An object which can listen for updates on another state object.
export type Observer = Dependent & { export type Observer = Dependent & {
kind: "Observer", 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. -- A state object whose value is derived from other objects using a callback.
export type ForKeys<KI, KO, M> = PubTypes.ForKeys<KO, any> & { export type ForKeys<KI, KO, M> = PubTypes.ForKeys<KO, any> & {
_oldDependencySet: Set<PubTypes.Dependency>, _oldDependencySet: Set<PubTypes.Dependency>,
_processor: (PubTypes.Use, KI) -> (KO), _processor: (PubTypes.Use, KI) -> KO,
_destructor: (KO, M?) -> (), _destructor: (KO, M?) -> (),
_inputIsState: boolean, _inputIsState: boolean,
_inputTable: PubTypes.CanBeState<{ [KI]: KO }>, _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. -- A state object whose value is derived from other objects using a callback.
export type ForValues<VI, VO, M> = PubTypes.ForValues<any, VO> & { export type ForValues<VI, VO, M> = PubTypes.ForValues<any, VO> & {
_oldDependencySet: Set<PubTypes.Dependency>, _oldDependencySet: Set<PubTypes.Dependency>,
_processor: (PubTypes.Use, VI) -> (VO), _processor: (PubTypes.Use, VI) -> VO,
_destructor: (VO, M?) -> (), _destructor: (VO, M?) -> (),
_inputIsState: boolean, _inputIsState: boolean,
_inputTable: PubTypes.CanBeState<{ [VI]: VO }>, _inputTable: PubTypes.CanBeState<{ [VI]: VO }>,

View File

@ -11,7 +11,7 @@
- an array - `cleanup` will be called on each item - 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 function cleanupOne(task: any)
local taskType = typeof(task) local taskType = typeof(task)

View File

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

View File

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

View File

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

View File

@ -1,7 +1,6 @@
-- A basic polyfill for the TweenInfo.new function, -- A basic polyfill for the TweenInfo.new function,
-- allows using Enum.EasingStyle/Direction or strings instead -- allows using Enum.EasingStyle/Direction or strings instead
local logError = require "../Logging/logError"
local TweenInfo = {} local TweenInfo = {}
function TweenInfo.new( function TweenInfo.new(
@ -19,7 +18,7 @@ function TweenInfo.new(
-- convert it to a string -- convert it to a string
if type(easingStyle) ~= "string" then if type(easingStyle) ~= "string" then
if easingStyle then if easingStyle then
easingStyle = tostring(easingStyle):gsub("Enum.%w+.", "") easingStyle = string.gsub(tostring(easingStyle), "Enum.%w+.", "")
end end
else else
local ok local ok
@ -42,13 +41,17 @@ function TweenInfo.new(
end end
end end
if not ok then 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
end end
if type(easingDirection) ~= "string" then if type(easingDirection) ~= "string" then
if easingDirection then if easingDirection then
easingDirection = tostring(easingDirection):gsub("Enum.%w+.", "") easingDirection =
string.gsub(tostring(easingDirection), "Enum.%w+.", "")
end end
else else
local ok local ok
@ -64,7 +67,10 @@ function TweenInfo.new(
end end
end end
if not ok then 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
end end
@ -99,9 +105,9 @@ function TweenInfo.new(
.. " Reverses:" .. " Reverses:"
.. (reverses and "True" or "False") .. (reverses and "True" or "False")
.. " EasingDirection:" .. " EasingDirection:"
.. easingDirection .. tostring(easingDirection)
.. " EasingStyle:" .. " EasingStyle:"
.. easingStyle .. tostring(easingStyle)
end end
mt.__metatable = "The metatable is locked" mt.__metatable = "The metatable is locked"

View File

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