From f1d0f1c1c92e12f509d75f4fe2b102fa5b040bf5 Mon Sep 17 00:00:00 2001 From: boatbomber Date: Sun, 22 May 2022 15:47:11 -0700 Subject: [PATCH] Bugfix: PluginAction spam causing errors (#541) * Use session's state instead of existence to determine action * Retain host/port text * Use bindings instead of text/ref tunneling Co-authored-by: Lucien Greathouse --- plugin/src/App/StatusPages/NotConnected.lua | 28 ++++++++----- plugin/src/App/init.lua | 45 ++++++++++++--------- plugin/src/ServeSession.lua | 4 ++ 3 files changed, 47 insertions(+), 30 deletions(-) diff --git a/plugin/src/App/StatusPages/NotConnected.lua b/plugin/src/App/StatusPages/NotConnected.lua index fb9434fc..7cc7abfc 100644 --- a/plugin/src/App/StatusPages/NotConnected.lua +++ b/plugin/src/App/StatusPages/NotConnected.lua @@ -14,8 +14,6 @@ local PORT_WIDTH = 74 local DIVIDER_WIDTH = 1 local HOST_OFFSET = 12 -local lastHost, lastPort - local e = Roact.createElement local function AddressEntry(props) @@ -26,7 +24,7 @@ local function AddressEntry(props) layoutOrder = props.layoutOrder, }, { Host = e("TextBox", { - Text = lastHost or "", + Text = props.host or "", Font = Enum.Font.Code, TextSize = 18, TextColor3 = theme.AddressEntry.TextColor, @@ -34,6 +32,7 @@ local function AddressEntry(props) TextTransparency = props.transparency, PlaceholderText = Config.defaultHost, PlaceholderColor3 = theme.AddressEntry.PlaceholderColor, + ClearTextOnFocus = false, Size = UDim2.new(1, -(HOST_OFFSET + DIVIDER_WIDTH + PORT_WIDTH), 1, 0), Position = UDim2.new(0, HOST_OFFSET, 0, 0), @@ -41,17 +40,22 @@ local function AddressEntry(props) ClipsDescendants = true, BackgroundTransparency = 1, - [Roact.Ref] = props.hostRef, + [Roact.Change.Text] = function(object) + if props.onHostChange ~= nil then + props.onHostChange(object.Text) + end + end }), Port = e("TextBox", { - Text = lastPort or "", + Text = props.port or "", Font = Enum.Font.Code, TextSize = 18, TextColor3 = theme.AddressEntry.TextColor, TextTransparency = props.transparency, PlaceholderText = Config.defaultPort, PlaceholderColor3 = theme.AddressEntry.PlaceholderColor, + ClearTextOnFocus = false, Size = UDim2.new(0, PORT_WIDTH, 1, 0), Position = UDim2.new(1, 0, 0, 0), @@ -60,12 +64,14 @@ local function AddressEntry(props) ClipsDescendants = true, BackgroundTransparency = 1, - [Roact.Ref] = props.portRef, - [Roact.Change.Text] = function(object) local text = object.Text text = text:gsub("%D", "") object.Text = text + + if props.onPortChange ~= nil then + props.onPortChange(text) + end end, }, { Divider = e("Frame", { @@ -90,8 +96,10 @@ function NotConnectedPage:render() }), AddressEntry = e(AddressEntry, { - hostRef = self.props.hostRef, - portRef = self.props.portRef, + host = self.props.host, + port = self.props.port, + onHostChange = self.props.onHostChange, + onPortChange = self.props.onPortChange, transparency = self.props.transparency, layoutOrder = 2, }), @@ -140,4 +148,4 @@ function NotConnectedPage:render() }) end -return NotConnectedPage +return NotConnectedPage \ No newline at end of file diff --git a/plugin/src/App/init.lua b/plugin/src/App/init.lua index e6dd50c7..a6669f81 100644 --- a/plugin/src/App/init.lua +++ b/plugin/src/App/init.lua @@ -38,8 +38,8 @@ local App = Roact.Component:extend("App") function App:init() preloadAssets() - self.hostRef = Roact.createRef() - self.portRef = Roact.createRef() + self.host, self.setHost = Roact.createBinding("") + self.port, self.setPort = Roact.createBinding("") self:setState({ appStatus = AppStatus.NotConnected, @@ -48,12 +48,19 @@ function App:init() }) end -function App:startSession() - local hostText = self.hostRef.current.Text - local portText = self.portRef.current.Text +function App:getHostAndPort() + local host = self.host:getValue() + local port = self.port:getValue() + + local host = if #host > 0 then host else Config.defaultHost + local port = if #port > 0 then port else Config.defaultPort + + return host, port +end + +function App:startSession() + local host, port = self:getHostAndPort() - local host = if #hostText > 0 then hostText else Config.defaultHost - local port = if #portText > 0 then portText else Config.defaultPort local sessionOptions = { openScriptsExternally = self.props.settings:get("openScriptsExternally"), twoWaySync = self.props.settings:get("twoWaySync"), @@ -176,8 +183,10 @@ function App:render() end, }, { NotConnectedPage = createPageElement(AppStatus.NotConnected, { - hostRef = self.hostRef, - portRef = self.portRef, + host = self.host, + onHostChange = self.setHost, + port = self.port, + onPortChange = self.setPort, onConnect = function() self:startSession() @@ -237,10 +246,10 @@ function App:render() icon = Assets.Images.PluginButton, bindable = true, onTriggered = function() - if self.serveSession then - self:endSession() - else + if self.serveSession == nil or self.serveSession:getStatus() == ServeSession.Status.NotStarted then self:startSession() + elseif self.serveSession ~= nil and self.serveSession:getStatus() == ServeSession.Status.Connected then + self:endSession() end end, }), @@ -252,11 +261,9 @@ function App:render() icon = Assets.Images.PluginButton, bindable = true, onTriggered = function() - if self.serveSession then - return + if self.serveSession == nil or self.serveSession:getStatus() == ServeSession.Status.NotStarted then + self:startSession() end - - self:startSession() end, }), @@ -267,11 +274,9 @@ function App:render() icon = Assets.Images.PluginButton, bindable = true, onTriggered = function() - if not self.serveSession then - return + if self.serveSession ~= nil and self.serveSession:getStatus() == ServeSession.Status.Connected then + self:endSession() end - - self:endSession() end, }), diff --git a/plugin/src/ServeSession.lua b/plugin/src/ServeSession.lua index d0bff1f3..f9a3bd9b 100644 --- a/plugin/src/ServeSession.lua +++ b/plugin/src/ServeSession.lua @@ -113,6 +113,10 @@ function ServeSession:__fmtDebug(output) output:write("}") end +function ServeSession:getStatus() + return self.__status +end + function ServeSession:onStatusChanged(callback) self.__statusChangedCallback = callback end