Update API stuff

This commit is contained in:
Lucien Greathouse 2018-08-26 01:02:34 -07:00
parent 71f0060f20
commit f21abe4728
3 changed files with 19 additions and 59 deletions

View file

@ -19,14 +19,15 @@ setmetatable(ApiContext.Error, {
end end
}) })
function ApiContext.new(url, onMessage) function ApiContext.new(baseUrl, onMessage)
assert(type(url) == "string") assert(type(baseUrl) == "string")
assert(type(onMessage) == "function") assert(type(onMessage) == "function")
local context = { local context = {
url = url, baseUrl = baseUrl,
onMessage = onMessage, onMessage = onMessage,
serverId = nil, serverId = nil,
rootInstanceId = nil,
connected = false, connected = false,
messageCursor = -1, messageCursor = -1,
partitionRoutes = nil, partitionRoutes = nil,
@ -38,7 +39,9 @@ function ApiContext.new(url, onMessage)
end end
function ApiContext:connect() function ApiContext:connect()
return Http.get(self.url .. "/api/rojo") local url = ("%s/api/rojo"):format(self.baseUrl)
return Http.get(url)
:andThen(function(response) :andThen(function(response)
local body = response:json() local body = response:json()
@ -61,15 +64,18 @@ function ApiContext:connect()
self.serverId = body.serverId self.serverId = body.serverId
self.connected = true self.connected = true
self.partitionRoutes = body.partitions self.partitionRoutes = body.partitions
self.rootInstanceId = body.rootInstanceId
end) end)
end end
function ApiContext:readAll() function ApiContext:read(ids)
if not self.connected then if not self.connected then
return Promise.reject() return Promise.reject()
end end
return Http.get(self.url .. "/api/read_all") local url = ("%s/api/read/%s"):format(self.baseUrl, table.concat(ids, ","))
return Http.get(url)
:andThen(function(response) :andThen(function(response)
local body = response:json() local body = response:json()
@ -92,7 +98,9 @@ function ApiContext:retrieveMessages()
return Promise.reject() return Promise.reject()
end end
return Http.get(self.url .. "/api/subscribe/" .. self.messageCursor) local url = ("%s/api/subscribe/%s"):format(self.baseUrl, self.messageCursor)
return Http.get(url)
:andThen(function(response) :andThen(function(response)
local body = response:json() local body = response:json()

View file

@ -1,6 +1,6 @@
local HttpService = game:GetService("HttpService") local HttpService = game:GetService("HttpService")
local HTTP_DEBUG = false local HTTP_DEBUG = true
local Promise = require(script.Parent.Parent.Promise) local Promise = require(script.Parent.Parent.Promise)

View file

@ -11,57 +11,7 @@ function Session.new()
setmetatable(self, Session) setmetatable(self, Session)
local function createFoldersUntil(location, route)
for i = 1, #route - 1 do
local piece = route[i]
local child = location:FindFirstChild(piece)
if child == nil then
child = Instance.new("Folder")
child.Name = piece
child.Parent = location
end
location = child
end
return location
end
local function reify(instancesById, id)
local object = instancesById[tostring(id)]
local instance = Instance.new(object.className)
instance.Name = object.name
for key, property in pairs(object.properties) do
instance[key] = property.value
end
for _, childId in ipairs(object.children) do
reify(instancesById, childId).Parent = instance
end
return instance
end
local api local api
local function readAll()
print("Reading all...")
return api:readAll()
:andThen(function(response)
for partitionName, partitionRoute in pairs(api.partitionRoutes) do
local parent = createFoldersUntil(game, partitionRoute)
local rootInstanceId = response.partitionInstances[partitionName]
print("Root for", partitionName, "is", rootInstanceId)
reify(response.instances, rootInstanceId).Parent = parent
end
end)
end
api = ApiContext.new(REMOTE_URL, function(message) api = ApiContext.new(REMOTE_URL, function(message)
if message.type == "InstanceChanged" then if message.type == "InstanceChanged" then
@ -73,7 +23,9 @@ function Session.new()
end) end)
api:connect() api:connect()
:andThen(readAll) :andThen(function()
return api:read({api.rootInstanceId})
end)
:andThen(function() :andThen(function()
return api:retrieveMessages() return api:retrieveMessages()
end) end)