Skip to content

Commit

Permalink
Merge pull request #120 from Elttob/pr-820hydrate
Browse files Browse the repository at this point in the history
Implement instance hydration
  • Loading branch information
dphfox authored Feb 4, 2022
2 parents 4cb50cf + bf6692d commit ddf0fff
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/Instances/Hydrate.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--!strict

--[[
Processes and returns an existing instance, with options for setting
properties, event handlers and other attributes on the instance.
]]

local Package = script.Parent.Parent
local PubTypes = require(Package.PubTypes)
local semiWeakRef = require(Package.Instances.semiWeakRef)
local applyInstanceProps = require(Package.Instances.applyInstanceProps)

local function Hydrate(target: Instance)
return function(props: PubTypes.PropertyTable): Instance
applyInstanceProps(props, semiWeakRef(target))
return target
end
end

return Hydrate
7 changes: 4 additions & 3 deletions src/Logging/messages.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ return {
cannotConnectEvent = "The %s class doesn't have an event called '%s'.",
cannotCreateClass = "Can't create a new instance of class '%s'.",
computedCallbackError = "Computed callback error: ERROR_MESSAGE",
duplicatePropertyKey = "",
invalidChangeHandler = "The change handler for the '%s' property must be a function.",
invalidEventHandler = "The handler for the '%s' event must be a function.",
invalidPropertyType = "'%s.%s' expected a '%s' type, but got a '%s' type.",
Expand All @@ -26,7 +27,7 @@ return {
springTypeMismatch = "The type '%s' doesn't match the spring's type '%s'.",
strictReadError = "'%s' is not a valid member of '%s'.",
unknownMessage = "Unknown error: ERROR_MESSAGE",
unrecognisedChildType = "'%s' type children aren't accepted as children in `New`.",
unrecognisedPropertyKey = "'%s' keys aren't accepted in the property table of `New`.",
unrecognisedPropertyStage = "The special key stage '%s' isn't recognised by `New`."
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."
}
2 changes: 2 additions & 0 deletions src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Fusion = {
version: PubTypes.Version,

New: (className: string) -> ((propertyTable: PubTypes.PropertyTable) -> Instance),
Hydrate: (target: Instance) -> ((propertyTable: PubTypes.PropertyTable) -> Instance),
Ref: PubTypes.SpecialKey,
Children: PubTypes.SpecialKey,
OnEvent: (eventName: string) -> PubTypes.SpecialKey,
Expand All @@ -39,6 +40,7 @@ return restrictRead("Fusion", {
version = {major = 0, minor = 2, isRelease = false},

New = require(script.Instances.New),
Hydrate = require(script.Instances.Hydrate),
Ref = require(script.Instances.Ref),
Children = require(script.Instances.Children),
OnEvent = require(script.Instances.OnEvent),
Expand Down
33 changes: 33 additions & 0 deletions test/Instances/Hydrate.spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
local Package = game:GetService("ReplicatedStorage").Fusion
local Hydrate = require(Package.Instances.Hydrate)

return function()
it("should return the instance it was passed", function()
local ins = Instance.new("Folder")

expect(Hydrate(ins) {}).to.equal(ins)
end)

it("should apply properties to the instance", function()
local ins = Instance.new("Folder")

Hydrate(ins) {
Name = "Jeremy"
}

expect(ins.Name).to.equal("Jeremy")
end)

it("should not inhibit garbage collection", function()
local ref = setmetatable({}, {__mode = "v"})
do
ref[1] = Hydrate(Instance.new("Folder")) {}
end

local startTime = os.clock()
repeat
task.wait()
until ref[1] == nil or os.clock() > startTime + 5
expect(ref[1]).to.equal(nil)
end)
end
1 change: 1 addition & 0 deletions test/init.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ return function()
version = "table",

New = "function",
Hydrate = "function",
Ref = "table",
Children = "table",
OnEvent = "function",
Expand Down

0 comments on commit ddf0fff

Please sign in to comment.