mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-23 14:15:24 +00:00
Refactor plugins, port message dropping bugfix from 0.5.1
This commit is contained in:
@@ -92,6 +92,10 @@ function ApiContext:disconnect()
|
|||||||
self.__connected = false
|
self.__connected = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function ApiContext:setMessageCursor(index)
|
||||||
|
self.__messageCursor = index
|
||||||
|
end
|
||||||
|
|
||||||
function ApiContext:connect()
|
function ApiContext:connect()
|
||||||
local url = ("%s/api/rojo"):format(self.__baseUrl)
|
local url = ("%s/api/rojo"):format(self.__baseUrl)
|
||||||
|
|
||||||
@@ -125,8 +129,6 @@ function ApiContext:read(ids)
|
|||||||
|
|
||||||
assert(validateApiRead(body))
|
assert(validateApiRead(body))
|
||||||
|
|
||||||
self.__messageCursor = body.messageCursor
|
|
||||||
|
|
||||||
return body
|
return body
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
@@ -155,7 +157,7 @@ function ApiContext:retrieveMessages()
|
|||||||
|
|
||||||
assert(validateApiSubscribe(body))
|
assert(validateApiSubscribe(body))
|
||||||
|
|
||||||
self.__messageCursor = body.messageCursor
|
self:setMessageCursor(body.messageCursor)
|
||||||
|
|
||||||
return body.messages
|
return body.messages
|
||||||
end)
|
end)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ local Status = strict("Session.Status", {
|
|||||||
local function DEBUG_printPatch(patch)
|
local function DEBUG_printPatch(patch)
|
||||||
local HttpService = game:GetService("HttpService")
|
local HttpService = game:GetService("HttpService")
|
||||||
|
|
||||||
|
|
||||||
for removed in ipairs(patch.removed) do
|
for removed in ipairs(patch.removed) do
|
||||||
print("Remove:", removed)
|
print("Remove:", removed)
|
||||||
end
|
end
|
||||||
@@ -64,48 +63,9 @@ function ServeSession:start()
|
|||||||
|
|
||||||
local rootInstanceId = serverInfo.rootInstanceId
|
local rootInstanceId = serverInfo.rootInstanceId
|
||||||
|
|
||||||
return self.__apiContext:read({ rootInstanceId })
|
return self:__initialSync(rootInstanceId)
|
||||||
:andThen(function(readResponseBody)
|
:andThen(function()
|
||||||
local hydratePatch = self.__reconciler:hydrate(
|
return self:__mainSyncLoop()
|
||||||
readResponseBody.instances,
|
|
||||||
rootInstanceId,
|
|
||||||
game
|
|
||||||
)
|
|
||||||
|
|
||||||
-- TODO: Prompt user to notify them of this patch, since
|
|
||||||
-- it's effectively a conflict between the Rojo server and
|
|
||||||
-- the client.
|
|
||||||
|
|
||||||
self.__reconciler:applyPatch(hydratePatch)
|
|
||||||
|
|
||||||
-- TODO: Applying a patch may eventually only apply part of
|
|
||||||
-- the patch and start a content negotiation process with
|
|
||||||
-- the Rojo server. We should handle that!
|
|
||||||
|
|
||||||
local function mainLoop()
|
|
||||||
return self.__apiContext:retrieveMessages()
|
|
||||||
:andThen(function(messages)
|
|
||||||
for _, message in ipairs(messages) do
|
|
||||||
-- TODO: Update server to return patches in
|
|
||||||
-- correct format so that we don't have to
|
|
||||||
-- transform them for the reconciler.
|
|
||||||
|
|
||||||
local asPatch = {
|
|
||||||
removed = message.removedInstances,
|
|
||||||
updated = message.updatedInstances,
|
|
||||||
added = message.addedInstances,
|
|
||||||
}
|
|
||||||
|
|
||||||
self.__reconciler:applyPatch(asPatch)
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.__status ~= Status.Disconnected then
|
|
||||||
return mainLoop()
|
|
||||||
end
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
return mainLoop()
|
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
:catch(function(err)
|
:catch(function(err)
|
||||||
@@ -117,6 +77,49 @@ function ServeSession:stop()
|
|||||||
self:__stopInternal()
|
self:__stopInternal()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function ServeSession:__initialSync(rootInstanceId)
|
||||||
|
return self.__apiContext:read({ rootInstanceId })
|
||||||
|
:andThen(function(readResponseBody)
|
||||||
|
-- Tell the API Context that we're up-to-date with the version of
|
||||||
|
-- the tree defined in this response.
|
||||||
|
self.__apiContext:setMessageCursor(readResponseBody.messageCursor)
|
||||||
|
|
||||||
|
-- Calculate the initial patch to apply to the DataModel to catch us
|
||||||
|
-- up to what Rojo thinks the place should look like.
|
||||||
|
local hydratePatch = self.__reconciler:hydrate(
|
||||||
|
readResponseBody.instances,
|
||||||
|
rootInstanceId,
|
||||||
|
game
|
||||||
|
)
|
||||||
|
|
||||||
|
-- TODO: Prompt user to notify them of this patch, since it's
|
||||||
|
-- effectively a conflict between the Rojo server and the client.
|
||||||
|
|
||||||
|
self.__reconciler:applyPatch(hydratePatch)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
function ServeSession:__mainSyncLoop()
|
||||||
|
return self.__apiContext:retrieveMessages()
|
||||||
|
:andThen(function(messages)
|
||||||
|
for _, message in ipairs(messages) do
|
||||||
|
-- TODO: Update server to return patches in correct format so
|
||||||
|
-- that we don't have to transform them for the reconciler.
|
||||||
|
local asPatch = {
|
||||||
|
removed = message.removedInstances,
|
||||||
|
updated = message.updatedInstances,
|
||||||
|
added = message.addedInstances,
|
||||||
|
}
|
||||||
|
|
||||||
|
self.__reconciler:applyPatch(asPatch)
|
||||||
|
end
|
||||||
|
|
||||||
|
if self.__status ~= Status.Disconnected then
|
||||||
|
return self:__mainSyncLoop()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
function ServeSession:__stopInternal(err)
|
function ServeSession:__stopInternal(err)
|
||||||
self:__setStatus(Status.Disconnected, err)
|
self:__setStatus(Status.Disconnected, err)
|
||||||
self.__apiContext:disconnect()
|
self.__apiContext:disconnect()
|
||||||
|
|||||||
Reference in New Issue
Block a user