From c894b38f06300aca83273a3b10ddc7ef9c046ce8 Mon Sep 17 00:00:00 2001 From: Lucien Greathouse Date: Fri, 1 Jun 2018 23:11:50 -0700 Subject: [PATCH] Improve plugin API robustness --- CHANGES.md | 1 + plugin/src/Api.lua | 5 +++-- plugin/src/Plugin.lua | 47 +++++++++++++++++++++++++------------------ 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index b7076d2d..1788e524 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ ## Current master * Fixed obscure error when syncing into an invalid service +* Fixed multiple sync processes occurring when a server ID mismatch is detected ## 0.4.9 (May 26, 2018) * Fixed warning when renaming or removing files that would sometimes corrupt the instance cache ([#72](https://github.com/LPGhatguy/rojo/pull/72)) diff --git a/plugin/src/Api.lua b/plugin/src/Api.lua index 163c4d1e..317f6282 100644 --- a/plugin/src/Api.lua +++ b/plugin/src/Api.lua @@ -35,6 +35,9 @@ function Api.connect(http) setmetatable(context, Api) return context:_start() + :andThen(function() + return context + end) end function Api:_start() @@ -60,8 +63,6 @@ function Api:_start() self.serverId = response.serverId self.currentTime = response.currentTime - - return self end) end diff --git a/plugin/src/Plugin.lua b/plugin/src/Plugin.lua index eae50920..89c88117 100644 --- a/plugin/src/Plugin.lua +++ b/plugin/src/Plugin.lua @@ -8,6 +8,9 @@ local Api = require(script.Parent.Api) local Reconciler = require(script.Parent.Reconciler) local Version = require(script.Parent.Version) +local MESSAGE_SERVER_CHANGED = "Rojo: The server has changed since the last request, reloading plugin..." +local MESSAGE_PLUGIN_CHANGED = "Rojo: Another instance of Rojo came online, unloading..." + local function collectMatch(source, pattern) local result = {} @@ -80,6 +83,7 @@ function Plugin.new() -- object. screenGui.AncestryChanged:Connect(function(_, parent) if parent == nil then + warn(MESSAGE_PLUGIN_CHANGED) self:restart() end end) @@ -93,8 +97,6 @@ end restarted. ]] function Plugin:restart() - warn("Rojo: The server has changed since the last request, reloading plugin...") - self:stopPolling() self._reconciler:destruct() @@ -105,22 +107,25 @@ function Plugin:restart() self._syncInProgress = false end -function Plugin:api() - if not self._api then - self._api = Api.connect(self._http) - :catch(function(err) - self._api = nil +function Plugin:getApi() + if self._api == nil then + return Api.connect(self._http) + :andThen(function(api) + self._api = api + + return api + end, function(err) return Promise.reject(err) end) end - return self._api + return Promise.resolve(self._api) end function Plugin:connect() print("Rojo: Testing connection...") - return self:api() + return self:getApi() :andThen(function(api) local ok, info = api:getInfo():await() @@ -134,6 +139,7 @@ function Plugin:connect() end) :catch(function(err) if err == Api.Error.ServerIdMismatch then + warn(MESSAGE_SERVER_CHANGED) self:restart() return self:connect() else @@ -204,25 +210,25 @@ function Plugin:startPolling() return end - print("Rojo: Polling server for changes...") + print("Rojo: Starting to poll server for changes...") self._polling = true self._label.Enabled = true - return self:api() + return self:getApi() :andThen(function(api) - local syncOk, result = self:syncIn():await() - - if not syncOk then - return Promise.reject(result) - end - local infoOk, info = api:getInfo():await() if not infoOk then return Promise.reject(info) end + local syncOk, result = self:syncIn():await() + + if not syncOk then + return Promise.reject(result) + end + while self._polling do local changesOk, changes = api:getChanges():await() @@ -248,12 +254,12 @@ function Plugin:startPolling() end end) :catch(function(err) - self:stopPolling() - if err == Api.Error.ServerIdMismatch then + warn(MESSAGE_SERVER_CHANGED) self:restart() return self:startPolling() else + self:stopPolling() return Promise.reject(err) end end) @@ -269,7 +275,7 @@ function Plugin:syncIn() self._syncInProgress = true print("Rojo: Syncing from server...") - return self:api() + return self:getApi() :andThen(function(api) local ok, info = api:getInfo():await() @@ -292,6 +298,7 @@ function Plugin:syncIn() self._syncInProgress = false if err == Api.Error.ServerIdMismatch then + warn(MESSAGE_SERVER_CHANGED) self:restart() return self:syncIn() else