@axpecter/lync 1.4.1 → 1.5.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/README.md +41 -2
- package/package.json +3 -2
- package/src/Types.luau +13 -1
- package/src/api/Group.luau +35 -28
- package/src/api/Namespace.luau +107 -89
- package/src/api/Packet.luau +69 -51
- package/src/api/Query.luau +101 -95
- package/src/api/Scope.luau +32 -18
- package/src/api/Signal.luau +78 -56
- package/src/codec/Base.luau +17 -17
- package/src/codec/composite/Array.luau +74 -74
- package/src/codec/composite/Map.luau +80 -80
- package/src/codec/composite/Optional.luau +14 -14
- package/src/codec/composite/Shared.luau +35 -35
- package/src/codec/composite/Struct.luau +108 -108
- package/src/codec/composite/Tagged.luau +71 -71
- package/src/codec/composite/Tuple.luau +35 -35
- package/src/codec/datatype/Buffer.luau +18 -18
- package/src/codec/datatype/CFrame.luau +24 -24
- package/src/codec/datatype/Color.luau +8 -12
- package/src/codec/datatype/Instance.luau +13 -13
- package/src/codec/datatype/IntVector.luau +17 -17
- package/src/codec/datatype/NumberRange.luau +7 -7
- package/src/codec/datatype/Ray.luau +16 -16
- package/src/codec/datatype/Rect.luau +13 -13
- package/src/codec/datatype/Region.luau +32 -36
- package/src/codec/datatype/Sequence.luau +41 -41
- package/src/codec/datatype/String.luau +28 -28
- package/src/codec/datatype/UDim.luau +17 -17
- package/src/codec/datatype/Vector.luau +14 -18
- package/src/codec/meta/Auto.luau +75 -73
- package/src/codec/meta/Bitfield.luau +46 -46
- package/src/codec/meta/Custom.luau +7 -7
- package/src/codec/meta/Enum.luau +25 -25
- package/src/codec/meta/Nothing.luau +3 -3
- package/src/codec/meta/Quantized.luau +63 -60
- package/src/codec/meta/Unknown.luau +11 -11
- package/src/codec/primitive/Bool.luau +12 -12
- package/src/codec/primitive/Float16.luau +28 -28
- package/src/codec/primitive/Number.luau +41 -41
- package/src/codec/primitive/Varint.luau +19 -19
- package/src/init.luau +81 -61
- package/src/internal/Baseline.luau +7 -7
- package/src/internal/Channel.luau +56 -56
- package/src/internal/Middleware.luau +29 -29
- package/src/internal/Pool.luau +15 -15
- package/src/internal/Registry.luau +19 -19
- package/src/transport/Bridge.luau +17 -17
- package/src/transport/Client.luau +46 -46
- package/src/transport/Gate.luau +56 -64
- package/src/transport/Reader.luau +34 -34
- package/src/transport/Server.luau +89 -89
package/src/api/Scope.luau
CHANGED
|
@@ -2,12 +2,23 @@
|
|
|
2
2
|
--!optimize 2
|
|
3
3
|
-- Batched connection lifecycle management.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
local Types = require(script.Parent.Parent.Types)
|
|
6
|
+
|
|
7
|
+
type Connection = Types.Connection
|
|
8
|
+
|
|
9
|
+
-- Private -------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
type ScopeFields = {
|
|
12
|
+
_entries: { any }, -- Connection | RBXScriptConnection
|
|
13
|
+
_count: number,
|
|
14
|
+
}
|
|
6
15
|
|
|
7
16
|
local ScopeImpl = {}
|
|
8
17
|
ScopeImpl.__index = ScopeImpl
|
|
9
18
|
|
|
10
|
-
|
|
19
|
+
export type Scope = typeof(setmetatable({} :: ScopeFields, ScopeImpl))
|
|
20
|
+
|
|
21
|
+
function ScopeImpl.add(self: Scope, conn: any): ()
|
|
11
22
|
if not conn then
|
|
12
23
|
return
|
|
13
24
|
end
|
|
@@ -18,31 +29,31 @@ function ScopeImpl.add (self: any, conn: any): ()
|
|
|
18
29
|
entries[count] = conn
|
|
19
30
|
end
|
|
20
31
|
|
|
21
|
-
function ScopeImpl.listen
|
|
22
|
-
local conn = source:listen
|
|
32
|
+
function ScopeImpl.listen(self: Scope, source: any, callback: (...any) -> ()): ()
|
|
33
|
+
local conn = source:listen(callback)
|
|
23
34
|
local entries = self._entries
|
|
24
35
|
local count = self._count + 1
|
|
25
36
|
self._count = count
|
|
26
37
|
entries[count] = conn
|
|
27
38
|
end
|
|
28
39
|
|
|
29
|
-
function ScopeImpl.once
|
|
30
|
-
local conn = source:once
|
|
40
|
+
function ScopeImpl.once(self: Scope, source: any, callback: (...any) -> ()): ()
|
|
41
|
+
local conn = source:once(callback)
|
|
31
42
|
local entries = self._entries
|
|
32
43
|
local count = self._count + 1
|
|
33
44
|
self._count = count
|
|
34
45
|
entries[count] = conn
|
|
35
46
|
end
|
|
36
47
|
|
|
37
|
-
function ScopeImpl.listenAll
|
|
38
|
-
local conn = namespace:listenAll
|
|
48
|
+
function ScopeImpl.listenAll(self: Scope, namespace: any, callback: (...any) -> ()): ()
|
|
49
|
+
local conn = namespace:listenAll(callback)
|
|
39
50
|
local entries = self._entries
|
|
40
51
|
local count = self._count + 1
|
|
41
52
|
self._count = count
|
|
42
53
|
entries[count] = conn
|
|
43
54
|
end
|
|
44
55
|
|
|
45
|
-
function ScopeImpl.destroy
|
|
56
|
+
function ScopeImpl.destroy(self: Scope): ()
|
|
46
57
|
local entries = self._entries
|
|
47
58
|
local count = self._count
|
|
48
59
|
|
|
@@ -50,24 +61,27 @@ function ScopeImpl.destroy (self: any): ()
|
|
|
50
61
|
local conn = entries[i]
|
|
51
62
|
entries[i] = nil
|
|
52
63
|
|
|
53
|
-
if typeof
|
|
54
|
-
conn:Disconnect
|
|
64
|
+
if typeof(conn) == "RBXScriptConnection" then
|
|
65
|
+
conn:Disconnect()
|
|
55
66
|
elseif conn.disconnect then
|
|
56
|
-
conn:disconnect
|
|
67
|
+
conn:disconnect()
|
|
57
68
|
end
|
|
58
69
|
end
|
|
59
70
|
|
|
60
71
|
self._count = 0
|
|
61
72
|
end
|
|
62
73
|
|
|
63
|
-
table.freeze
|
|
74
|
+
table.freeze(ScopeImpl)
|
|
64
75
|
|
|
65
|
-
-- Public
|
|
76
|
+
-- Public --------------------------------------------------------------
|
|
66
77
|
|
|
67
|
-
local
|
|
78
|
+
local ScopeModule = {}
|
|
68
79
|
|
|
69
|
-
function
|
|
70
|
-
return setmetatable
|
|
80
|
+
function ScopeModule.create(): Scope
|
|
81
|
+
return setmetatable({
|
|
82
|
+
_entries = {},
|
|
83
|
+
_count = 0,
|
|
84
|
+
}, ScopeImpl)
|
|
71
85
|
end
|
|
72
86
|
|
|
73
|
-
return table.freeze
|
|
87
|
+
return table.freeze(ScopeModule)
|
package/src/api/Signal.luau
CHANGED
|
@@ -2,48 +2,64 @@
|
|
|
2
2
|
--!optimize 2
|
|
3
3
|
-- O(1) disconnect listener system with thread reuse.
|
|
4
4
|
|
|
5
|
-
local Types = require
|
|
5
|
+
local Types = require(script.Parent.Parent.Types)
|
|
6
6
|
|
|
7
7
|
type Connection = Types.Connection
|
|
8
8
|
|
|
9
|
-
-- Constants
|
|
9
|
+
-- Constants -----------------------------------------------------------
|
|
10
10
|
|
|
11
11
|
local STATE_DISCONNECTED = 0
|
|
12
12
|
local STATE_CONNECTED = 1
|
|
13
13
|
local STATE_ONCE = 2
|
|
14
14
|
|
|
15
|
-
-- Private
|
|
15
|
+
-- Private -------------------------------------------------------------
|
|
16
16
|
|
|
17
17
|
local _freeThread: thread? = nil
|
|
18
18
|
|
|
19
|
-
local function passer
|
|
19
|
+
local function passer(fn: (...any) -> (), ...: any): ()
|
|
20
20
|
local acquired = _freeThread
|
|
21
21
|
_freeThread = nil
|
|
22
|
-
fn
|
|
22
|
+
fn(...)
|
|
23
23
|
_freeThread = acquired
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
-
local function yielder
|
|
26
|
+
local function yielder(): ()
|
|
27
27
|
while true do
|
|
28
|
-
passer
|
|
28
|
+
passer(coroutine.yield())
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
-
local function spawn
|
|
32
|
+
local function spawn(fn: (...any) -> (), ...: any): ()
|
|
33
33
|
if not _freeThread then
|
|
34
|
-
_freeThread = coroutine.create
|
|
35
|
-
coroutine.resume
|
|
34
|
+
_freeThread = coroutine.create(yielder)
|
|
35
|
+
coroutine.resume(_freeThread :: thread)
|
|
36
36
|
end
|
|
37
|
-
task.spawn
|
|
37
|
+
task.spawn(_freeThread :: thread, fn, ...)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
--
|
|
41
|
-
--
|
|
42
|
-
--
|
|
40
|
+
-- Mutually recursive field types for Entry and Signal.
|
|
41
|
+
-- Entry._signal references the signal's fields; Signal._entries holds entries.
|
|
42
|
+
-- Luau resolves the cycle at type-check time.
|
|
43
|
+
type EntryFields = {
|
|
44
|
+
fn: (...any) -> (),
|
|
45
|
+
_state: number,
|
|
46
|
+
connected: boolean,
|
|
47
|
+
_signal: SignalFields,
|
|
48
|
+
_index: number,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type SignalFields = {
|
|
52
|
+
_entries: { EntryFields },
|
|
53
|
+
_count: number,
|
|
54
|
+
_snapshot: { EntryFields },
|
|
55
|
+
}
|
|
56
|
+
|
|
43
57
|
local EntryImpl = {}
|
|
44
58
|
EntryImpl.__index = EntryImpl
|
|
45
59
|
|
|
46
|
-
|
|
60
|
+
type EntrySelf = typeof(setmetatable({} :: EntryFields, EntryImpl))
|
|
61
|
+
|
|
62
|
+
function EntryImpl.disconnect(self: EntrySelf): ()
|
|
47
63
|
if self._state == STATE_DISCONNECTED then
|
|
48
64
|
return
|
|
49
65
|
end
|
|
@@ -65,51 +81,53 @@ function EntryImpl.disconnect (self: any): ()
|
|
|
65
81
|
last._index = index
|
|
66
82
|
end
|
|
67
83
|
|
|
68
|
-
entries[count] = nil
|
|
84
|
+
entries[count] = nil :: any
|
|
69
85
|
self._signal._count = count - 1
|
|
70
86
|
end
|
|
71
87
|
|
|
72
88
|
local SignalImpl = {}
|
|
73
89
|
SignalImpl.__index = SignalImpl
|
|
74
90
|
|
|
75
|
-
|
|
91
|
+
export type Signal = typeof(setmetatable({} :: SignalFields, SignalImpl))
|
|
92
|
+
|
|
93
|
+
local function addEntry(self: Signal, callback: (...any) -> (), isOnce: boolean): Connection
|
|
76
94
|
local count = self._count + 1
|
|
77
95
|
self._count = count
|
|
78
96
|
|
|
79
|
-
local entry = setmetatable
|
|
97
|
+
local entry = setmetatable({
|
|
80
98
|
fn = callback,
|
|
81
99
|
_state = if isOnce then STATE_ONCE else STATE_CONNECTED,
|
|
82
100
|
connected = true,
|
|
83
|
-
_signal = self,
|
|
101
|
+
_signal = self :: any,
|
|
84
102
|
_index = count,
|
|
85
103
|
}, EntryImpl)
|
|
86
104
|
|
|
87
|
-
self._entries[count] = entry
|
|
105
|
+
self._entries[count] = entry :: any
|
|
88
106
|
return entry :: any
|
|
89
107
|
end
|
|
90
108
|
|
|
91
|
-
function SignalImpl.connect
|
|
92
|
-
return addEntry
|
|
109
|
+
function SignalImpl.connect(self: Signal, callback: (...any) -> ()): Connection
|
|
110
|
+
return addEntry(self, callback, false)
|
|
93
111
|
end
|
|
94
112
|
|
|
95
|
-
function SignalImpl.once
|
|
96
|
-
return addEntry
|
|
113
|
+
function SignalImpl.once(self: Signal, callback: (...any) -> ()): Connection
|
|
114
|
+
return addEntry(self, callback, true)
|
|
97
115
|
end
|
|
98
116
|
|
|
99
|
-
function SignalImpl.wait
|
|
100
|
-
local thread = coroutine.running
|
|
117
|
+
function SignalImpl.wait(self: Signal): ...any
|
|
118
|
+
local thread = coroutine.running()
|
|
101
119
|
local entry: any
|
|
102
120
|
|
|
103
|
-
entry = addEntry
|
|
104
|
-
entry:disconnect
|
|
105
|
-
task.spawn
|
|
121
|
+
entry = addEntry(self, function(...: any): ()
|
|
122
|
+
entry:disconnect()
|
|
123
|
+
task.spawn(thread, ...)
|
|
106
124
|
end, true)
|
|
107
125
|
|
|
108
|
-
return coroutine.yield
|
|
126
|
+
return coroutine.yield()
|
|
109
127
|
end
|
|
110
128
|
|
|
111
|
-
-- Dispatches to all listeners via task.spawn
|
|
112
|
-
function SignalImpl.fire
|
|
129
|
+
-- Dispatches to all listeners via task.spawn(safe for yielding handlers).
|
|
130
|
+
function SignalImpl.fire(self: Signal, ...: any): ()
|
|
113
131
|
local entries = self._entries
|
|
114
132
|
local count = self._count
|
|
115
133
|
if count == 0 then
|
|
@@ -123,41 +141,41 @@ function SignalImpl.fire (self: any, ...: any): ()
|
|
|
123
141
|
return
|
|
124
142
|
end
|
|
125
143
|
|
|
126
|
-
spawn
|
|
144
|
+
spawn(entry.fn, ...)
|
|
127
145
|
|
|
128
146
|
if state == STATE_ONCE then
|
|
129
|
-
entry:disconnect
|
|
147
|
+
(entry :: any):disconnect()
|
|
130
148
|
end
|
|
131
149
|
return
|
|
132
150
|
end
|
|
133
151
|
|
|
134
152
|
local snapshot = self._snapshot
|
|
135
|
-
table.move
|
|
153
|
+
table.move(entries, 1, count, 1, snapshot)
|
|
136
154
|
|
|
137
155
|
local tail = count + 1
|
|
138
156
|
if snapshot[tail] ~= nil then
|
|
139
|
-
snapshot[tail] = nil
|
|
157
|
+
snapshot[tail] = nil :: any
|
|
140
158
|
end
|
|
141
159
|
|
|
142
160
|
for i = 1, count do
|
|
143
161
|
local entry = snapshot[i]
|
|
144
|
-
snapshot[i] = nil
|
|
162
|
+
snapshot[i] = nil :: any
|
|
145
163
|
|
|
146
164
|
local state = entry._state
|
|
147
165
|
if state == STATE_DISCONNECTED then
|
|
148
166
|
continue
|
|
149
167
|
end
|
|
150
168
|
|
|
151
|
-
spawn
|
|
169
|
+
spawn(entry.fn, ...)
|
|
152
170
|
|
|
153
171
|
if state == STATE_ONCE then
|
|
154
|
-
entry:disconnect
|
|
172
|
+
(entry :: any):disconnect()
|
|
155
173
|
end
|
|
156
174
|
end
|
|
157
175
|
end
|
|
158
176
|
|
|
159
177
|
-- Synchronous dispatch. Only safe when all listeners are non-yielding.
|
|
160
|
-
function SignalImpl.fireSync
|
|
178
|
+
function SignalImpl.fireSync(self: Signal, ...: any): ()
|
|
161
179
|
local entries = self._entries
|
|
162
180
|
local count = self._count
|
|
163
181
|
if count == 0 then
|
|
@@ -171,60 +189,64 @@ function SignalImpl.fireSync (self: any, ...: any): ()
|
|
|
171
189
|
return
|
|
172
190
|
end
|
|
173
191
|
|
|
174
|
-
entry.fn
|
|
192
|
+
entry.fn(...)
|
|
175
193
|
|
|
176
194
|
if state == STATE_ONCE then
|
|
177
|
-
entry:disconnect
|
|
195
|
+
(entry :: any):disconnect()
|
|
178
196
|
end
|
|
179
197
|
return
|
|
180
198
|
end
|
|
181
199
|
|
|
182
200
|
local snapshot = self._snapshot
|
|
183
|
-
table.move
|
|
201
|
+
table.move(entries, 1, count, 1, snapshot)
|
|
184
202
|
|
|
185
203
|
local tail = count + 1
|
|
186
204
|
if snapshot[tail] ~= nil then
|
|
187
|
-
snapshot[tail] = nil
|
|
205
|
+
snapshot[tail] = nil :: any
|
|
188
206
|
end
|
|
189
207
|
|
|
190
208
|
for i = 1, count do
|
|
191
209
|
local entry = snapshot[i]
|
|
192
|
-
snapshot[i] = nil
|
|
210
|
+
snapshot[i] = nil :: any
|
|
193
211
|
|
|
194
212
|
local state = entry._state
|
|
195
213
|
if state == STATE_DISCONNECTED then
|
|
196
214
|
continue
|
|
197
215
|
end
|
|
198
216
|
|
|
199
|
-
entry.fn
|
|
217
|
+
entry.fn(...)
|
|
200
218
|
|
|
201
219
|
if state == STATE_ONCE then
|
|
202
|
-
entry:disconnect
|
|
220
|
+
(entry :: any):disconnect()
|
|
203
221
|
end
|
|
204
222
|
end
|
|
205
223
|
end
|
|
206
224
|
|
|
207
|
-
function SignalImpl.disconnectAll
|
|
225
|
+
function SignalImpl.disconnectAll(self: Signal): ()
|
|
208
226
|
local entries = self._entries
|
|
209
227
|
|
|
210
228
|
for i = 1, self._count do
|
|
211
229
|
entries[i]._state = STATE_DISCONNECTED
|
|
212
230
|
entries[i].connected = false
|
|
213
|
-
entries[i] = nil
|
|
231
|
+
entries[i] = nil :: any
|
|
214
232
|
end
|
|
215
233
|
|
|
216
234
|
self._count = 0
|
|
217
235
|
end
|
|
218
236
|
|
|
219
|
-
table.freeze
|
|
220
|
-
table.freeze
|
|
237
|
+
table.freeze(EntryImpl)
|
|
238
|
+
table.freeze(SignalImpl)
|
|
221
239
|
|
|
222
|
-
-- Public
|
|
240
|
+
-- Public --------------------------------------------------------------
|
|
223
241
|
|
|
224
|
-
local
|
|
242
|
+
local SignalModule = {}
|
|
225
243
|
|
|
226
|
-
function
|
|
227
|
-
return setmetatable
|
|
244
|
+
function SignalModule.create(): Signal
|
|
245
|
+
return setmetatable({
|
|
246
|
+
_entries = {} :: { EntryFields },
|
|
247
|
+
_count = 0,
|
|
248
|
+
_snapshot = {} :: { EntryFields },
|
|
249
|
+
}, SignalImpl)
|
|
228
250
|
end
|
|
229
251
|
|
|
230
|
-
return table.freeze
|
|
252
|
+
return table.freeze(SignalModule)
|
package/src/codec/Base.luau
CHANGED
|
@@ -2,52 +2,52 @@
|
|
|
2
2
|
--!native
|
|
3
3
|
-- Fixed-size codec factory. Every fixed-size codec calls Base.define once.
|
|
4
4
|
|
|
5
|
-
local Channel = require
|
|
6
|
-
local Types = require
|
|
5
|
+
local Channel = require(script.Parent.Parent.internal.Channel)
|
|
6
|
+
local Types = require(script.Parent.Parent.Types)
|
|
7
7
|
|
|
8
8
|
type ChannelState = Types.ChannelState
|
|
9
9
|
|
|
10
10
|
local alloc = Channel.alloc
|
|
11
11
|
|
|
12
|
-
-- Public
|
|
12
|
+
-- Public --------------------------------------------------------------
|
|
13
13
|
|
|
14
14
|
local Base = {}
|
|
15
15
|
|
|
16
|
-
function Base.define
|
|
16
|
+
function Base.define<T>(
|
|
17
17
|
size: number,
|
|
18
18
|
directWrite: (b: buffer, offset: number, value: T) -> (),
|
|
19
19
|
directRead: (b: buffer, offset: number) -> T
|
|
20
20
|
): any
|
|
21
|
-
return table.freeze
|
|
21
|
+
return table.freeze({
|
|
22
22
|
_size = size,
|
|
23
23
|
_directWrite = directWrite,
|
|
24
24
|
_directRead = directRead,
|
|
25
|
-
write = function
|
|
25
|
+
write = function(ch: ChannelState, value: T): ()
|
|
26
26
|
local c = ch.cursor
|
|
27
27
|
if c + size > ch.size then
|
|
28
|
-
alloc
|
|
28
|
+
alloc(ch, size)
|
|
29
29
|
end
|
|
30
|
-
directWrite
|
|
30
|
+
directWrite(ch.buff, c, value)
|
|
31
31
|
ch.cursor = c + size
|
|
32
32
|
end,
|
|
33
|
-
read = function
|
|
34
|
-
return directRead
|
|
33
|
+
read = function(src: buffer, pos: number, _refs: { Instance }?): (T, number)
|
|
34
|
+
return directRead(src, pos), size
|
|
35
35
|
end,
|
|
36
36
|
})
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
function Base.zero
|
|
40
|
-
return table.freeze
|
|
39
|
+
function Base.zero(): any
|
|
40
|
+
return table.freeze({
|
|
41
41
|
_size = 0,
|
|
42
|
-
_directWrite = function
|
|
43
|
-
_directRead = function
|
|
42
|
+
_directWrite = function(_b: buffer, _offset: number, _value: any): () end,
|
|
43
|
+
_directRead = function(_b: buffer, _offset: number): any
|
|
44
44
|
return nil
|
|
45
45
|
end,
|
|
46
|
-
write = function
|
|
47
|
-
read = function
|
|
46
|
+
write = function(_ch: ChannelState, _value: any): () end,
|
|
47
|
+
read = function(_src: buffer, _pos: number, _refs: { Instance }?): (any, number)
|
|
48
48
|
return nil, 0
|
|
49
49
|
end,
|
|
50
50
|
})
|
|
51
51
|
end
|
|
52
52
|
|
|
53
|
-
return table.freeze
|
|
53
|
+
return table.freeze(Base)
|