Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Real-time status about sync details #569

Merged
merged 17 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 74 additions & 28 deletions plugin/src/App/StatusPages/Connected.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ local BorderedContainer = require(Plugin.App.Components.BorderedContainer)

local e = Roact.createElement

local AGE_UNITS = { {31556909, "year"}, {2629743, "month"}, {604800, "week"}, {86400, "day"}, {3600, "hour"}, {60, "minute"}, }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"last synced one year ago" would be a pretty funny message to happen in practice

function timeSinceText(elapsed: number): string
if elapsed < 3 then
return "just now"
end

local ageText = string.format("%d seconds ago", elapsed)

for _,UnitData in ipairs(AGE_UNITS) do
local UnitSeconds, UnitName = UnitData[1], UnitData[2]
if elapsed > UnitSeconds then
local c = math.floor(elapsed/UnitSeconds)
ageText = string.format("%d %s%s ago", c, UnitName, c>1 and "s" or "")
break
end
end

return ageText
end

local function ConnectionDetails(props)
return Theme.with(function(theme)
return e(BorderedContainer, {
Expand Down Expand Up @@ -82,33 +102,59 @@ end
local ConnectedPage = Roact.Component:extend("ConnectedPage")

function ConnectedPage:render()
return Roact.createFragment({
Header = e(Header, {
transparency = self.props.transparency,
layoutOrder = 1,
}),

ConnectionDetails = e(ConnectionDetails, {
projectName = self.state.projectName,
address = self.state.address,
transparency = self.props.transparency,
layoutOrder = 2,

onDisconnect = self.props.onDisconnect,
}),

Layout = e("UIListLayout", {
VerticalAlignment = Enum.VerticalAlignment.Center,
FillDirection = Enum.FillDirection.Vertical,
SortOrder = Enum.SortOrder.LayoutOrder,
Padding = UDim.new(0, 10),
}),

Padding = e("UIPadding", {
PaddingLeft = UDim.new(0, 20),
PaddingRight = UDim.new(0, 20),
}),
})
return Theme.with(function(theme)
return Roact.createFragment({
Header = e(Header, {
transparency = self.props.transparency,
layoutOrder = 1,
}),

ConnectionDetails = e(ConnectionDetails, {
projectName = self.state.projectName,
address = self.state.address,
transparency = self.props.transparency,
layoutOrder = 2,

onDisconnect = self.props.onDisconnect,
}),

Info = e("TextLabel", {
Text = self.props.patchInfo:map(function(info)
return string.format(
"<i>Synced %d change%s %s</i>",
info.changes,
info.changes == 1 and "" or "s",
timeSinceText(os.time() - info.timestamp)
)
end),
Font = Enum.Font.Gotham,
TextSize = 14,
TextWrapped = true,
RichText = true,
TextColor3 = theme.Header.VersionColor,
TextXAlignment = Enum.TextXAlignment.Left,
TextYAlignment = Enum.TextYAlignment.Top,
TextTransparency = self.props.transparency,

Size = UDim2.new(1, 0, 0, 28),

LayoutOrder = 3,
BackgroundTransparency = 1,
}),

Layout = e("UIListLayout", {
VerticalAlignment = Enum.VerticalAlignment.Center,
FillDirection = Enum.FillDirection.Vertical,
SortOrder = Enum.SortOrder.LayoutOrder,
Padding = UDim.new(0, 10),
}),

Padding = e("UIPadding", {
PaddingLeft = UDim.new(0, 20),
PaddingRight = UDim.new(0, 20),
}),
})
end)
end

function ConnectedPage.getDerivedStateFromProps(props)
Expand All @@ -122,4 +168,4 @@ function ConnectedPage.getDerivedStateFromProps(props)
}
end

return ConnectedPage
return ConnectedPage
43 changes: 43 additions & 0 deletions plugin/src/App/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ function App:init()

self.host, self.setHost = Roact.createBinding("")
self.port, self.setPort = Roact.createBinding("")
self.patchInfo, self.setPatchInfo = Roact.createBinding({
changes = 0,
timestamp = os.time(),
})

self:setState({
appStatus = AppStatus.NotConnected,
Expand Down Expand Up @@ -104,6 +108,34 @@ function App:startSession()
twoWaySync = sessionOptions.twoWaySync,
})

serveSession:onPatchApplied(function(patch, unapplied)
local now = os.time()
local changes = 0

for _, set in patch do
for _ in set do
changes += 1
end
end
for _, set in unapplied do
for _ in set do
changes -= 1
end
end

if changes == 0 then return end

local old = self.patchInfo:getValue()
if now - old.timestamp < 2 then
changes += old.changes
end

self.setPatchInfo({
changes = changes,
timestamp = now,
})
end)

serveSession:onStatusChanged(function(status, details)
if status == ServeSession.Status.Connecting then
self:setState({
Expand Down Expand Up @@ -147,6 +179,16 @@ function App:startSession()
serveSession:start()

self.serveSession = serveSession

task.defer(function()
while self.serveSession == serveSession do
-- Trigger rerender to update timestamp text
local patchInfo = table.clone(self.patchInfo:getValue())
self.setPatchInfo(patchInfo)
local elapsed = os.time() - patchInfo.timestamp
task.wait(elapsed < 60 and 1 or elapsed/5)
end
end)
end

function App:endSession()
Expand Down Expand Up @@ -230,6 +272,7 @@ function App:render()
Connected = createPageElement(AppStatus.Connected, {
projectName = self.state.projectName,
address = self.state.address,
patchInfo = self.patchInfo,

onDisconnect = function()
self:endSession()
Expand Down
13 changes: 13 additions & 0 deletions plugin/src/ServeSession.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function ServeSession.new(options)
__instanceMap = instanceMap,
__changeBatcher = changeBatcher,
__statusChangedCallback = nil,
__patchAppliedCallback = nil,
__connections = connections,
}

Expand Down Expand Up @@ -121,6 +122,10 @@ function ServeSession:onStatusChanged(callback)
self.__statusChangedCallback = callback
end

function ServeSession:onPatchApplied(callback)
self.__patchAppliedCallback = callback
end

function ServeSession:start()
self:__setStatus(Status.Connecting)

Expand Down Expand Up @@ -231,6 +236,10 @@ function ServeSession:__initialSync(rootInstanceId)
Log.warn("Could not apply all changes requested by the Rojo server:\n{}",
PatchSet.humanSummary(self.__instanceMap, unappliedPatch))
end

if self.__patchAppliedCallback then
pcall(self.__patchAppliedCallback, catchUpPatch, unappliedPatch)
end
end)
end

Expand All @@ -244,6 +253,10 @@ function ServeSession:__mainSyncLoop()
Log.warn("Could not apply all changes requested by the Rojo server:\n{}",
PatchSet.humanSummary(self.__instanceMap, unappliedPatch))
end

if self.__patchAppliedCallback then
pcall(self.__patchAppliedCallback, message, unappliedPatch)
end
end

if self.__status ~= Status.Disconnected then
Expand Down