add gs
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"language": {
|
||||
"mode": "nonstrict"
|
||||
},
|
||||
"lint": {
|
||||
"LocalShadow": "fatal",
|
||||
"LocalUnused": "fatal",
|
||||
"ImportUnused": "fatal",
|
||||
"ImplicitReturn": "fatal",
|
||||
"DeprecatedGlobal": "fatal"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
--[[
|
||||
A component that establishes a connection to a Roblox event when it is rendered.
|
||||
]]
|
||||
|
||||
local CorePackages = game:GetService("CorePackages")
|
||||
local Roact = require(CorePackages.Roact)
|
||||
local ExternalEventConnection = Roact.Component:extend("ExternalEventConnection")
|
||||
|
||||
function ExternalEventConnection:init()
|
||||
self.connection = nil
|
||||
end
|
||||
|
||||
--[[
|
||||
Render the child component so that ExternalEventConnections can be nested like so:
|
||||
|
||||
Roact.createElement(ExternalEventConnection, {
|
||||
event = UserInputService.InputBegan,
|
||||
callback = inputBeganCallback,
|
||||
}, {
|
||||
Roact.createElement(ExternalEventConnection, {
|
||||
event = UserInputService.InputEnded,
|
||||
callback = inputChangedCallback,
|
||||
})
|
||||
})
|
||||
]]
|
||||
function ExternalEventConnection:render()
|
||||
return Roact.oneChild(self.props[Roact.Children])
|
||||
end
|
||||
|
||||
function ExternalEventConnection:didMount()
|
||||
local event = self.props.event
|
||||
local callback = self.props.callback
|
||||
|
||||
self.connection = event:Connect(callback)
|
||||
end
|
||||
|
||||
function ExternalEventConnection:didUpdate(oldProps)
|
||||
if self.props.event ~= oldProps.event or self.props.callback ~= oldProps.callback then
|
||||
self.connection:Disconnect()
|
||||
|
||||
self.connection = self.props.event:Connect(self.props.callback)
|
||||
end
|
||||
end
|
||||
|
||||
function ExternalEventConnection:willUnmount()
|
||||
self.connection:Disconnect()
|
||||
|
||||
self.connection = nil
|
||||
end
|
||||
|
||||
return ExternalEventConnection
|
||||
+91
@@ -0,0 +1,91 @@
|
||||
return function ()
|
||||
local CorePackages = game:GetService("CorePackages")
|
||||
local Roact = require(CorePackages.Roact)
|
||||
local ExternalEventConnection = require(script.Parent.ExternalEventConnection)
|
||||
|
||||
it("if mounted, should call the callback when the event is triggered", function()
|
||||
local event = Instance.new("BindableEvent")
|
||||
local count = 0
|
||||
|
||||
local element = Roact.createElement(ExternalEventConnection, {
|
||||
event = event.Event,
|
||||
callback = function()
|
||||
count = count + 1
|
||||
end,
|
||||
})
|
||||
|
||||
local RoactInstance = Roact.mount(element)
|
||||
event:Fire()
|
||||
|
||||
expect(count).to.equal(1)
|
||||
|
||||
Roact.unmount(RoactInstance)
|
||||
event:Fire()
|
||||
|
||||
expect(count).to.equal(1)
|
||||
|
||||
event:Destroy()
|
||||
end)
|
||||
|
||||
it("should handle updating the callback or event", function()
|
||||
local firstEvent = Instance.new("BindableEvent")
|
||||
local secondEvent = Instance.new("BindableEvent")
|
||||
local count = 0
|
||||
local changeState
|
||||
|
||||
local EventContainer = Roact.Component:extend("EventContainer")
|
||||
|
||||
function EventContainer:init()
|
||||
self.state = {
|
||||
event = firstEvent.Event,
|
||||
callback = function()
|
||||
count = count + 1
|
||||
end,
|
||||
}
|
||||
end
|
||||
|
||||
function EventContainer:render()
|
||||
return Roact.createElement(ExternalEventConnection, {
|
||||
event = self.state.event,
|
||||
callback = self.state.callback,
|
||||
})
|
||||
end
|
||||
|
||||
function EventContainer:didMount()
|
||||
changeState = function(newState)
|
||||
self:setState(newState)
|
||||
end
|
||||
end
|
||||
|
||||
function EventContainer:willUnmount()
|
||||
changeState = nil
|
||||
end
|
||||
|
||||
Roact.mount(Roact.createElement(EventContainer))
|
||||
firstEvent:Fire()
|
||||
|
||||
expect(count).to.equal(1)
|
||||
|
||||
changeState({
|
||||
event = secondEvent.Event,
|
||||
})
|
||||
firstEvent:Fire()
|
||||
|
||||
expect(count).to.equal(1)
|
||||
|
||||
secondEvent:Fire()
|
||||
|
||||
expect(count).to.equal(2)
|
||||
|
||||
changeState({
|
||||
callback = function()
|
||||
-- this is intentionally blank
|
||||
end,
|
||||
})
|
||||
secondEvent:Fire()
|
||||
|
||||
expect(count).to.equal(2)
|
||||
firstEvent:Destroy()
|
||||
secondEvent:Destroy()
|
||||
end)
|
||||
end
|
||||
Reference in New Issue
Block a user