@quenty/timesyncservice 5.1.0 → 5.2.1-canary.276.672e37b.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,25 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [5.2.1-canary.276.672e37b.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/timesyncservice@5.2.0...@quenty/timesyncservice@5.2.1-canary.276.672e37b.0) (2022-07-31)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @quenty/timesyncservice
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# [5.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/timesyncservice@5.1.0...@quenty/timesyncservice@5.2.0) (2022-07-02)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* Can clean up services properly ([eb45e03](https://github.com/Quenty/NevermoreEngine/commit/eb45e03ce2897b18f1ae460974bf2bbb9e27cb97))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
6
25
|
# [5.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/timesyncservice@5.0.0...@quenty/timesyncservice@5.1.0) (2022-06-21)
|
|
7
26
|
|
|
8
27
|
**Note:** Version bump only for package @quenty/timesyncservice
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/timesyncservice",
|
|
3
|
-
"version": "5.1.0",
|
|
3
|
+
"version": "5.2.1-canary.276.672e37b.0",
|
|
4
4
|
"description": "Quenty's TimeSyncService keeps time synchronized between all clients and the server",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -26,15 +26,16 @@
|
|
|
26
26
|
"Quenty"
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@quenty/
|
|
30
|
-
"@quenty/
|
|
31
|
-
"@quenty/
|
|
32
|
-
"@quenty/
|
|
33
|
-
"@quenty/
|
|
34
|
-
"@quenty/
|
|
29
|
+
"@quenty/baseobject": "5.0.1-canary.276.672e37b.0",
|
|
30
|
+
"@quenty/loader": "5.0.0",
|
|
31
|
+
"@quenty/maid": "2.3.1-canary.276.672e37b.0",
|
|
32
|
+
"@quenty/promise": "5.0.1-canary.276.672e37b.0",
|
|
33
|
+
"@quenty/remoting": "5.1.1-canary.276.672e37b.0",
|
|
34
|
+
"@quenty/signal": "2.2.0",
|
|
35
|
+
"@quenty/table": "3.1.0"
|
|
35
36
|
},
|
|
36
37
|
"publishConfig": {
|
|
37
38
|
"access": "public"
|
|
38
39
|
},
|
|
39
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "672e37b9bbb8b2d4c4c6c69ed2647335be0bc469"
|
|
40
41
|
}
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
@class MasterClock
|
|
4
4
|
]=]
|
|
5
5
|
|
|
6
|
-
local
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local BaseObject = require("BaseObject")
|
|
9
|
+
|
|
10
|
+
local MasterClock = setmetatable({}, BaseObject)
|
|
7
11
|
MasterClock.__index = MasterClock
|
|
8
12
|
MasterClock.ClassName = "MasterClock"
|
|
9
13
|
|
|
@@ -15,7 +19,7 @@ MasterClock.ClassName = "MasterClock"
|
|
|
15
19
|
@return MasterClock
|
|
16
20
|
]=]
|
|
17
21
|
function MasterClock.new(remoteEvent, remoteFunction)
|
|
18
|
-
local self = setmetatable(
|
|
22
|
+
local self = setmetatable(BaseObject.new(), MasterClock)
|
|
19
23
|
|
|
20
24
|
self._remoteEvent = remoteEvent or error("No remoteEvent")
|
|
21
25
|
self._remoteFunction = remoteFunction or error("No remoteFunction")
|
|
@@ -23,14 +27,19 @@ function MasterClock.new(remoteEvent, remoteFunction)
|
|
|
23
27
|
self._remoteFunction.OnServerInvoke = function(_, timeThree)
|
|
24
28
|
return self:_handleDelayRequest(timeThree)
|
|
25
29
|
end
|
|
26
|
-
self._remoteEvent.OnServerEvent:Connect(function(player)
|
|
30
|
+
self._maid:GiveTask(self._remoteEvent.OnServerEvent:Connect(function(player)
|
|
27
31
|
self._remoteEvent:FireClient(player, self:GetTime())
|
|
32
|
+
end))
|
|
33
|
+
|
|
34
|
+
local alive = true
|
|
35
|
+
self._maid:GiveTask(function()
|
|
36
|
+
alive = false
|
|
28
37
|
end)
|
|
29
38
|
|
|
30
|
-
task.
|
|
31
|
-
while
|
|
32
|
-
task.wait(5)
|
|
39
|
+
task.delay(5, function()
|
|
40
|
+
while alive do
|
|
33
41
|
self:_forceSync()
|
|
42
|
+
task.wait(5)
|
|
34
43
|
end
|
|
35
44
|
end)
|
|
36
45
|
|
|
@@ -3,7 +3,11 @@
|
|
|
3
3
|
@class SlaveClock
|
|
4
4
|
]=]
|
|
5
5
|
|
|
6
|
-
local
|
|
6
|
+
local require = require(script.Parent.loader).load(script)
|
|
7
|
+
|
|
8
|
+
local BaseObject = require("BaseObject")
|
|
9
|
+
|
|
10
|
+
local SlaveClock = setmetatable({}, BaseObject)
|
|
7
11
|
SlaveClock.__index = SlaveClock
|
|
8
12
|
SlaveClock.ClassName = "SlaveClock"
|
|
9
13
|
SlaveClock._offset = -1 -- Set uncalculated values to -1
|
|
@@ -16,14 +20,14 @@ SlaveClock._offset = -1 -- Set uncalculated values to -1
|
|
|
16
20
|
@return SlaveClock
|
|
17
21
|
]=]
|
|
18
22
|
function SlaveClock.new(remoteEvent, remoteFunction)
|
|
19
|
-
local self = setmetatable(
|
|
23
|
+
local self = setmetatable(BaseObject.new(), SlaveClock)
|
|
20
24
|
|
|
21
25
|
self._remoteEvent = remoteEvent or error("No remoteEvent")
|
|
22
26
|
self._remoteFunction = remoteFunction or error("No remoteFunction")
|
|
23
27
|
|
|
24
|
-
self._remoteEvent.OnClientEvent:Connect(function(timeOne)
|
|
28
|
+
self._maid:GiveTask(self._remoteEvent.OnClientEvent:Connect(function(timeOne)
|
|
25
29
|
self:_handleSyncEventAsync(timeOne)
|
|
26
|
-
end)
|
|
30
|
+
end))
|
|
27
31
|
|
|
28
32
|
self._remoteEvent:FireServer() -- Request server to syncronize with us
|
|
29
33
|
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
--[=[
|
|
2
|
-
Syncronizes time
|
|
2
|
+
Syncronizes time between the server and client. This creates a shared timestamp that can be used to reasonably time
|
|
3
|
+
events between the server and client.
|
|
4
|
+
|
|
3
5
|
@class TimeSyncService
|
|
4
6
|
]=]
|
|
5
7
|
|
|
@@ -17,13 +19,20 @@ local PromiseUtils = require("PromiseUtils")
|
|
|
17
19
|
local SlaveClock = require("SlaveClock")
|
|
18
20
|
local TimeSyncConstants = require("TimeSyncConstants")
|
|
19
21
|
local TimeSyncUtils = require("TimeSyncUtils")
|
|
22
|
+
local Maid = require("Maid")
|
|
20
23
|
|
|
21
24
|
local TimeSyncService = {}
|
|
22
25
|
|
|
26
|
+
--[=[
|
|
27
|
+
Initializes the TimeSyncService
|
|
28
|
+
]=]
|
|
23
29
|
function TimeSyncService:Init()
|
|
24
30
|
assert(not self._clockPromise, "TimeSyncService is already initialized!")
|
|
25
31
|
|
|
32
|
+
self._maid = Maid.new()
|
|
33
|
+
|
|
26
34
|
self._clockPromise = Promise.new()
|
|
35
|
+
self._maid:GiveTask(self._clockPromise)
|
|
27
36
|
|
|
28
37
|
if not RunService:IsRunning() then
|
|
29
38
|
error("Cannot initialize in test mode")
|
|
@@ -37,6 +46,12 @@ function TimeSyncService:Init()
|
|
|
37
46
|
end
|
|
38
47
|
end
|
|
39
48
|
|
|
49
|
+
--[=[
|
|
50
|
+
Returns true if the clock is synced. If the clock is synced, then it can
|
|
51
|
+
be retrieved.
|
|
52
|
+
|
|
53
|
+
@return boolean
|
|
54
|
+
]=]
|
|
40
55
|
function TimeSyncService:IsSynced()
|
|
41
56
|
if not RunService:IsRunning() then
|
|
42
57
|
return true
|
|
@@ -46,6 +61,12 @@ function TimeSyncService:IsSynced()
|
|
|
46
61
|
return self._clockPromise:IsFulfilled()
|
|
47
62
|
end
|
|
48
63
|
|
|
64
|
+
--[=[
|
|
65
|
+
Waits for the synced clock, or throws an error.
|
|
66
|
+
|
|
67
|
+
@yields
|
|
68
|
+
@return MasterClock | SlaveClock
|
|
69
|
+
]=]
|
|
49
70
|
function TimeSyncService:WaitForSyncedClock()
|
|
50
71
|
if not RunService:IsRunning() then
|
|
51
72
|
return self:_buildMockClock()
|
|
@@ -55,6 +76,11 @@ function TimeSyncService:WaitForSyncedClock()
|
|
|
55
76
|
return self._clockPromise:Wait()
|
|
56
77
|
end
|
|
57
78
|
|
|
79
|
+
--[=[
|
|
80
|
+
Returns a synced clock if there is one available. Otherwise, returns nil.
|
|
81
|
+
|
|
82
|
+
@return MasterClock | SlaveClock | nil
|
|
83
|
+
]=]
|
|
58
84
|
function TimeSyncService:GetSyncedClock()
|
|
59
85
|
if not RunService:IsRunning() then
|
|
60
86
|
return self:_buildMockClock()
|
|
@@ -68,6 +94,11 @@ function TimeSyncService:GetSyncedClock()
|
|
|
68
94
|
return nil
|
|
69
95
|
end
|
|
70
96
|
|
|
97
|
+
--[=[
|
|
98
|
+
Promises a synced clock
|
|
99
|
+
|
|
100
|
+
@return Promise<MasterClock | SlaveClock>
|
|
101
|
+
]=]
|
|
71
102
|
function TimeSyncService:PromiseSyncedClock()
|
|
72
103
|
if not RunService:IsRunning() then
|
|
73
104
|
return Promise.resolved(self:_buildMockClock())
|
|
@@ -95,16 +126,28 @@ function TimeSyncService:_buildMasterClock()
|
|
|
95
126
|
local remoteEvent = GetRemoteEvent(TimeSyncConstants.REMOTE_EVENT_NAME)
|
|
96
127
|
local remoteFunction = GetRemoteFunction(TimeSyncConstants.REMOTE_FUNCTION_NAME)
|
|
97
128
|
|
|
98
|
-
|
|
129
|
+
local clock = MasterClock.new(remoteEvent, remoteFunction)
|
|
130
|
+
self._maid:GiveTask(clock)
|
|
131
|
+
|
|
132
|
+
return clock
|
|
99
133
|
end
|
|
100
134
|
|
|
101
135
|
function TimeSyncService:_promiseSlaveClock()
|
|
102
|
-
return PromiseUtils.all({
|
|
136
|
+
return self._maid:GivePromise(PromiseUtils.all({
|
|
103
137
|
PromiseGetRemoteEvent(TimeSyncConstants.REMOTE_EVENT_NAME);
|
|
104
138
|
PromiseGetRemoteFunction(TimeSyncConstants.REMOTE_FUNCTION_NAME);
|
|
105
|
-
}):Then(function(remoteEvent, remoteFunction)
|
|
106
|
-
|
|
139
|
+
})):Then(function(remoteEvent, remoteFunction)
|
|
140
|
+
local clock = SlaveClock.new(remoteEvent, remoteFunction)
|
|
141
|
+
self._maid:GiveTask(clock)
|
|
142
|
+
return TimeSyncUtils.promiseClockSynced(clock)
|
|
107
143
|
end)
|
|
108
144
|
end
|
|
109
145
|
|
|
146
|
+
--[=[
|
|
147
|
+
Cleans up the time syncronization service.
|
|
148
|
+
]=]
|
|
149
|
+
function TimeSyncService:Destroy()
|
|
150
|
+
self._maid:DoCleaning()
|
|
151
|
+
end
|
|
152
|
+
|
|
110
153
|
return TimeSyncService
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
--[=[
|
|
2
|
+
Helper functions for the TimeSyncService.
|
|
2
3
|
@class TimeSyncUtils
|
|
3
4
|
]=]
|
|
4
5
|
|
|
@@ -9,6 +10,12 @@ local Maid = require("Maid")
|
|
|
9
10
|
|
|
10
11
|
local TimeSyncUtils = {}
|
|
11
12
|
|
|
13
|
+
--[=[
|
|
14
|
+
Given a clock, returns a promise that resolves when the clock is syncronized.
|
|
15
|
+
|
|
16
|
+
@param clock MasterClock | SlaveClock
|
|
17
|
+
@return Promise<MasterClock | SlaveClock>
|
|
18
|
+
]=]
|
|
12
19
|
function TimeSyncUtils.promiseClockSynced(clock)
|
|
13
20
|
if clock:IsSynced() then
|
|
14
21
|
return Promise.resolved(clock)
|