Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Tagging #184

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/luacov.*
/site
/site
*.rbxlx
45 changes: 45 additions & 0 deletions default.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"name": "roact",
"tree": {
"$className": "DataModel",
"ReplicatedStorage": {
"$className": "ReplicatedStorage",
"Roact": {
"$path": "lib"
},
"TestEZ": {
"$path": "modules/testez/lib"
}
},
"ServerScriptService": {
"$className": "ServerScriptService",
"RoactBenchmark": {
"$path": "benchmarks"
},
"RoactTests": {
"$path": "bin/run-tests.server.lua"
}
},
"StarterPlayer": {
"$className": "StarterPlayer",
"StarterPlayerScripts": {
"$className": "StarterPlayerScripts",
"RoactTestClient": {
"$path": "client"
},
"RoactExamples": {
"$path": "examples"
}
}
},
"HttpService": {
"$className": "HttpService",
"$properties": {
"HttpEnabled": {
"Type": "Bool",
"Value": true
}
}
}
}
}
5 changes: 5 additions & 0 deletions lib/PropMarkers/Tag.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
local Symbol = require(script.Parent.Parent.Symbol)

local Tag = Symbol.named("Tag")

return Tag
21 changes: 19 additions & 2 deletions lib/RobloxRenderer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
renderer that does anything.
]]

local CollectionService = game:GetService("CollectionService")

local Binding = require(script.Parent.Binding)
local Children = require(script.Parent.PropMarkers.Children)
local ElementKind = require(script.Parent.ElementKind)
local SingleEventManager = require(script.Parent.SingleEventManager)
local getDefaultInstanceProperty = require(script.Parent.getDefaultInstanceProperty)
local Ref = require(script.Parent.PropMarkers.Ref)
local Tag = require(script.Parent.PropMarkers.Tag)
local Type = require(script.Parent.Type)

local applyPropsError = [[
Expand Down Expand Up @@ -90,8 +93,8 @@ local function applyProp(virtualNode, key, newValue, oldValue)
return
end

if key == Ref or key == Children then
-- Refs and children are handled in a separate pass
if key == Ref or key == Children or key == Tag then
-- Refs, children, and tags are handled in a separate pass
return
end

Expand Down Expand Up @@ -194,6 +197,10 @@ function RobloxRenderer.mountHostNode(reconciler, virtualNode)
virtualNode.hostObject = instance

applyRef(element.props[Ref], instance)

if element.props[Tag] ~= nil then
CollectionService:AddTag(instance, element.props[Tag])
end
end

function RobloxRenderer.unmountHostNode(reconciler, virtualNode)
Expand All @@ -220,6 +227,16 @@ function RobloxRenderer.updateHostNode(reconciler, virtualNode, newElement)
applyRef(newProps[Ref], virtualNode.hostObject)
end

if oldProps[Tag] ~= newProps[Tag] then
if oldProps[Tag] ~= nil then
CollectionService:RemoveTag(virtualNode.hostObject, oldProps[Tag])
end

if newProps[Tag] ~= nil then
CollectionService:AddTag(virtualNode.hostObject, newProps[Tag])
end
end

local success, errorMessage = pcall(updateProps, virtualNode, oldProps, newProps)

if not success then
Expand Down
48 changes: 48 additions & 0 deletions lib/RobloxRenderer.spec.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
return function()
local CollectionService = game:GetService("CollectionService")

local assertDeepEqual = require(script.Parent.assertDeepEqual)
local Binding = require(script.Parent.Binding)
local Children = require(script.Parent.PropMarkers.Children)
Expand All @@ -11,6 +13,7 @@ return function()
local Logging = require(script.Parent.Logging)
local Portal = require(script.Parent.Portal)
local Ref = require(script.Parent.PropMarkers.Ref)
local Tag = require(script.Parent.PropMarkers.Tag)

local RobloxRenderer = require(script.Parent.RobloxRenderer)

Expand Down Expand Up @@ -736,4 +739,49 @@ return function()
})
end)
end)

describe("Tags", function()
it("should be assigned on mount", function()
local parent = Instance.new("Folder")
local ref = createRef()
local tag = "TestTag"

local element = createElement("Frame", {
[Tag] = tag,
[Ref] = ref,
})

local node = reconciler.createVirtualNode(element, parent, "Test")
RobloxRenderer.mountHostNode(reconciler, node)

expect(CollectionService:HasTag(ref.current, tag)).to.equal(true)
end)

it("should update properly", function()
local parent = Instance.new("Folder")
local ref = createRef()
local tagA = "TestTag"
local tagB = "OtherTag"

local element = createElement("Frame", {
[Tag] = tagA,
[Ref] = ref,
})

local node = reconciler.createVirtualNode(element, parent, "Test")
RobloxRenderer.mountHostNode(reconciler, node)

expect(CollectionService:HasTag(ref.current, tagA)).to.equal(true)

local newElement = createElement("Frame", {
[Tag] = tagB,
[Ref] = ref,
})

RobloxRenderer.updateHostNode(reconciler, node, newElement)

expect(CollectionService:HasTag(ref.current, tagA)).to.equal(false)
expect(CollectionService:HasTag(ref.current, tagB)).to.equal(true)
end)
end)
end