Improve plugin accuracy

This commit is contained in:
Lucien Greathouse
2017-12-01 00:18:11 -08:00
parent c7171ef513
commit 5e64773784
4 changed files with 73 additions and 33 deletions

View File

@@ -1,9 +1,17 @@
local HttpService = game:GetService("HttpService") local HttpService = game:GetService("HttpService")
local HTTP_DEBUG = false
local Promise = require(script.Parent.Promise) local Promise = require(script.Parent.Promise)
local HttpError = require(script.Parent.HttpError) local HttpError = require(script.Parent.HttpError)
local HttpResponse = require(script.Parent.HttpResponse) local HttpResponse = require(script.Parent.HttpResponse)
local function dprint(...)
if HTTP_DEBUG then
print(...)
end
end
local Http = {} local Http = {}
Http.__index = Http Http.__index = Http
@@ -20,6 +28,7 @@ function Http.new(baseUrl)
end end
function Http:get(endpoint) function Http:get(endpoint)
dprint("\nGET", endpoint)
return Promise.new(function(resolve, reject) return Promise.new(function(resolve, reject)
spawn(function() spawn(function()
local ok, result = pcall(function() local ok, result = pcall(function()
@@ -27,6 +36,7 @@ function Http:get(endpoint)
end) end)
if ok then if ok then
dprint("\t", result, "\n")
resolve(HttpResponse.new(result)) resolve(HttpResponse.new(result))
else else
reject(HttpError.fromErrorString(result)) reject(HttpError.fromErrorString(result))
@@ -36,6 +46,8 @@ function Http:get(endpoint)
end end
function Http:post(endpoint, body) function Http:post(endpoint, body)
dprint("\nPOST", endpoint)
dprint(body)
return Promise.new(function(resolve, reject) return Promise.new(function(resolve, reject)
spawn(function() spawn(function()
local ok, result = pcall(function() local ok, result = pcall(function()
@@ -43,6 +55,7 @@ function Http:post(endpoint, body)
end) end)
if ok then if ok then
dprint("\t", result, "\n")
resolve(HttpResponse.new(result)) resolve(HttpResponse.new(result))
else else
reject(HttpError.fromErrorString(result)) reject(HttpError.fromErrorString(result))

View File

@@ -12,17 +12,26 @@ local function main()
toolbar:CreateButton("Test Connection", "Connect to Rojo Server", "") toolbar:CreateButton("Test Connection", "Connect to Rojo Server", "")
.Click:Connect(function() .Click:Connect(function()
pluginInstance:connect() pluginInstance:connect()
:catch(function(err)
warn(err)
end)
end) end)
toolbar:CreateButton("Sync In", "Sync into Roblox Studio", "") toolbar:CreateButton("Sync In", "Sync into Roblox Studio", "")
.Click:Connect(function() .Click:Connect(function()
pluginInstance:syncIn() pluginInstance:syncIn()
:catch(function(err)
warn(err)
end)
end) end)
toolbar:CreateButton("Toggle Polling", "Poll server for changes", "") toolbar:CreateButton("Toggle Polling", "Poll server for changes", "")
.Click:Connect(function() .Click:Connect(function()
spawn(function() spawn(function()
pluginInstance:togglePolling() pluginInstance:togglePolling()
:catch(function(err)
warn(err)
end)
end) end)
end) end)
end end

View File

@@ -149,7 +149,7 @@ end
function Plugin:connect() function Plugin:connect()
print("Testing connection...") print("Testing connection...")
self:server() return self:server()
:andThen(function(server) :andThen(function(server)
return server:getInfo() return server:getInfo()
end) end)
@@ -163,8 +163,10 @@ end
function Plugin:togglePolling() function Plugin:togglePolling()
if self._polling then if self._polling then
self:stopPolling() self:stopPolling()
return Promise.resolve(nil)
else else
self:startPolling() return self:startPolling()
end end
end end
@@ -173,12 +175,30 @@ function Plugin:stopPolling()
return return
end end
print("Stopping polling...") print("Stopped polling.")
self._polling = false self._polling = false
self._label.Enabled = false self._label.Enabled = false
end end
function Plugin:_pull(server, project, routes)
local items = server:read(routes):await()
for index = 1, #routes do
local route = routes[index]
local partitionName = route[1]
local partition = project.partitions[partitionName]
local data = items[index]
local fullRoute = collectMatch(partition.target, "[^.]+")
for i = 2, #route do
table.insert(fullRoute, routes[index][i])
end
write(game, fullRoute, data)
end
end
function Plugin:startPolling() function Plugin:startPolling()
if self._polling then if self._polling then
return return
@@ -204,17 +224,7 @@ function Plugin:startPolling()
table.insert(routes, change.route) table.insert(routes, change.route)
end end
local items = server:read(routes):await() self:_pull(server, project, routes)
for index = 1, #routes do
local partitionName = routes[index][1]
local partition = project.partitions[partitionName]
local data = items[index]
local fullRoute = collectMatch(partition.target, "[^.]+")
write(game, fullRoute, data)
end
wait(Config.pollingRate) wait(Config.pollingRate)
end end
@@ -231,23 +241,13 @@ function Plugin:syncIn()
:andThen(function(server) :andThen(function(server)
local project = server:getInfo():await().project local project = server:getInfo():await().project
local readRoutes = {} local routes = {}
for name in pairs(project.partitions) do for name in pairs(project.partitions) do
table.insert(readRoutes, {name}) table.insert(routes, {name})
end end
local items = server:read(readRoutes):await() self:_pull(server, project, routes)
for index = 1, #readRoutes do
local partitionName = readRoutes[index][1]
local partition = project.partitions[partitionName]
local data = items[index]
local fullRoute = collectMatch(partition.target, "[^.]+")
write(game, fullRoute, data)
end
print("Sync successful!") print("Sync successful!")
end) end)

View File

@@ -2,7 +2,7 @@
An implementation of Promises similar to Promise/A+. An implementation of Promises similar to Promise/A+.
]] ]]
local PROMISE_DEBUG = true local PROMISE_DEBUG = false
-- If promise debugging is on, use a version of pcall that warns on failure. -- If promise debugging is on, use a version of pcall that warns on failure.
-- This is useful for finding errors that happen within Promise itself. -- This is useful for finding errors that happen within Promise itself.
@@ -89,6 +89,9 @@ function Promise.new(callback)
-- Only valid if _status is set to something besides Started -- Only valid if _status is set to something besides Started
_value = nil, _value = nil,
-- If an error occurs with no observers, this will be set.
_unhandledRejection = false,
-- Queues representing functions we should invoke when we update! -- Queues representing functions we should invoke when we update!
_queuedResolve = {}, _queuedResolve = {},
_queuedReject = {}, _queuedReject = {},
@@ -157,6 +160,8 @@ end
The given callbacks are invoked depending on that result. The given callbacks are invoked depending on that result.
]] ]]
function Promise:andThen(successHandler, failureHandler) function Promise:andThen(successHandler, failureHandler)
self._unhandledRejection = false
-- Create a new promise to follow this part of the chain -- Create a new promise to follow this part of the chain
return Promise.new(function(resolve, reject) return Promise.new(function(resolve, reject)
-- Our default callbacks just pass values onto the next promise. -- Our default callbacks just pass values onto the next promise.
@@ -199,6 +204,8 @@ end
This matches the execution model of normal Roblox functions. This matches the execution model of normal Roblox functions.
]] ]]
function Promise:await() function Promise:await()
self._unhandledRejection = false
if self._status == Promise.Status.Started then if self._status == Promise.Status.Started then
local result local result
local bindable = Instance.new("BindableEvent") local bindable = Instance.new("BindableEvent")
@@ -279,11 +286,22 @@ function Promise:_reject(...)
-- synchronously. We'll wait one tick, and if there are still no -- synchronously. We'll wait one tick, and if there are still no
-- observers, then we should put a message in the console. -- observers, then we should put a message in the console.
local message = ("Unhandled promise rejection:\n\n%s\n\n%s"):format( self._unhandledRejection = true
tostring((...)), local err = tostring((...))
self._source
) spawn(function()
warn(message) -- Someone observed the error, hooray!
if not self._unhandledRejection then
return
end
-- Build a reasonable message
local message = ("Unhandled promise rejection:\n\n%s\n\n%s"):format(
err,
self._source
)
warn(message)
end)
end end
end end