@quenty/loader 10.8.0 → 10.8.1

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.
@@ -1,3 +1,4 @@
1
+ --!strict
1
2
  --[=[
2
3
  Adds the loader instance so script.Parent.loader works.
3
4
 
@@ -13,7 +14,24 @@ local LoaderLinkCreator = {}
13
14
  LoaderLinkCreator.ClassName = "LoaderLinkCreator"
14
15
  LoaderLinkCreator.__index = LoaderLinkCreator
15
16
 
16
- function LoaderLinkCreator.new(root, references, isRoot)
17
+ export type LoaderLinkCreator = typeof(setmetatable(
18
+ {} :: {
19
+ _maid: Maid.Maid,
20
+ _root: Instance,
21
+ _references: ReplicatorReferences.ReplicatorReferences?,
22
+ _hasLoaderCount: IntValue,
23
+ _childRequiresLoaderCount: IntValue,
24
+ _provideLoader: BoolValue,
25
+ _lastProvidedLoader: Instance?,
26
+ },
27
+ LoaderLinkCreator
28
+ ))
29
+
30
+ function LoaderLinkCreator.new(
31
+ root: Instance,
32
+ references: ReplicatorReferences.ReplicatorReferences?,
33
+ isRoot: boolean?
34
+ ): LoaderLinkCreator
17
35
  assert(typeof(root) == "Instance", "Bad root")
18
36
  assert(ReplicatorReferences.isReplicatorReferences(references) or references == nil, "Bad references")
19
37
 
@@ -36,10 +54,10 @@ function LoaderLinkCreator.new(root, references, isRoot)
36
54
  self:_setupEventTracking()
37
55
  self:_setupRendering()
38
56
 
39
- return self
57
+ return self :: LoaderLinkCreator
40
58
  end
41
59
 
42
- function LoaderLinkCreator:_setupEventTracking()
60
+ function LoaderLinkCreator._setupEventTracking(self: LoaderLinkCreator)
43
61
  self._maid:GiveTask(self._root.ChildAdded:Connect(function(child)
44
62
  self:_handleChildAdded(child)
45
63
  end))
@@ -47,21 +65,21 @@ function LoaderLinkCreator:_setupEventTracking()
47
65
  self:_handleChildRemoved(child)
48
66
  end))
49
67
 
50
- for _, child in pairs(self._root:GetChildren()) do
68
+ for _, child in self._root:GetChildren() do
51
69
  self:_handleChildAdded(child)
52
70
  end
53
71
 
54
72
  -- Need to do this AFTER child added loop
55
73
  if self._references then
56
- self._maid:GiveTask(self._references:ObserveReferenceChanged(loader, function(replicatedLoader)
74
+ self._maid:GiveTask(self._references:ObserveReferenceChanged(loader, function(replicatedLoader: Instance?)
57
75
  if replicatedLoader and replicatedLoader ~= loader then
58
- self._maid._trackFakeLoader = self:_countLoaderReferences(replicatedLoader)
76
+ self._maid._trackFakeLoader = (self :: any):_countLoaderReferences(replicatedLoader)
59
77
  else
60
78
  self._maid._trackFakeLoader = nil
61
79
  end
62
80
  end))
63
81
  else
64
- self._maid:GiveTask(self:_countLoaderReferences(loader))
82
+ self._maid:GiveTask((self :: any):_countLoaderReferences(loader))
65
83
  end
66
84
 
67
85
  -- Update state
@@ -74,11 +92,11 @@ function LoaderLinkCreator:_setupEventTracking()
74
92
  self:_updateProviderLoader()
75
93
  end
76
94
 
77
- function LoaderLinkCreator:_setupRendering()
95
+ function LoaderLinkCreator._setupRendering(self: LoaderLinkCreator)
78
96
  if self._references then
79
97
  local function renderLoader()
80
98
  if self._provideLoader.Value then
81
- self._maid._loader = self:_renderLoaderWithReferences()
99
+ self._maid._loader = self:_renderLoaderWithReferences(self._references)
82
100
  else
83
101
  self._maid._loader = nil
84
102
  end
@@ -101,15 +119,15 @@ function LoaderLinkCreator:_setupRendering()
101
119
  end
102
120
  end
103
121
 
104
- function LoaderLinkCreator:_updateProviderLoader()
122
+ function LoaderLinkCreator._updateProviderLoader(self: LoaderLinkCreator)
105
123
  self._provideLoader.Value = (self._childRequiresLoaderCount.Value > 0) and self._hasLoaderCount.Value <= 0
106
124
  end
107
125
 
108
- function LoaderLinkCreator:_handleChildRemoved(child)
126
+ function LoaderLinkCreator._handleChildRemoved(self: LoaderLinkCreator, child: Instance)
109
127
  self._maid[child] = nil
110
128
  end
111
129
 
112
- function LoaderLinkCreator:_handleChildAdded(child)
130
+ function LoaderLinkCreator._handleChildAdded(self: LoaderLinkCreator, child: Instance)
113
131
  assert(typeof(child) == "Instance", "Bad child")
114
132
 
115
133
  if child:IsA("ModuleScript") then
@@ -126,10 +144,13 @@ function LoaderLinkCreator:_handleChildAdded(child)
126
144
  end
127
145
  end
128
146
 
129
- function LoaderLinkCreator:_renderLoaderWithReferences()
147
+ function LoaderLinkCreator._renderLoaderWithReferences(
148
+ self: LoaderLinkCreator,
149
+ references: ReplicatorReferences.ReplicatorReferences
150
+ ): Maid.Maid
130
151
  local maid = Maid.new()
131
152
 
132
- maid:GiveTask(self._references:ObserveReferenceChanged(loader, function(value)
153
+ maid:GiveTask(references:ObserveReferenceChanged(loader, function(value: Instance?)
133
154
  if value then
134
155
  maid._current = self:_doLoaderRender(value)
135
156
  else
@@ -140,17 +161,16 @@ function LoaderLinkCreator:_renderLoaderWithReferences()
140
161
  return maid
141
162
  end
142
163
 
143
- function LoaderLinkCreator:_doLoaderRender(value)
164
+ function LoaderLinkCreator._doLoaderRender(self: LoaderLinkCreator, value: Instance)
144
165
  local loaderLink = LoaderLinkUtils.create(value, loader.Name)
145
166
  self._lastProvidedLoader = loaderLink
146
167
 
147
168
  loaderLink.Parent = self._root
148
169
 
149
-
150
170
  return loaderLink
151
171
  end
152
172
 
153
- function LoaderLinkCreator:_incrementNeededLoader(amount)
173
+ function LoaderLinkCreator._incrementNeededLoader(self: LoaderLinkCreator, amount: number): () -> ()
154
174
  assert(type(amount) == "number", "Bad amount")
155
175
 
156
176
  self._childRequiresLoaderCount.Value = self._childRequiresLoaderCount.Value + amount
@@ -159,7 +179,7 @@ function LoaderLinkCreator:_incrementNeededLoader(amount)
159
179
  end
160
180
  end
161
181
 
162
- function LoaderLinkCreator:_addToHasLoaderCount(amount)
182
+ function LoaderLinkCreator._addToHasLoaderCount(self: LoaderLinkCreator, amount: number): () -> ()
163
183
  assert(type(amount) == "number", "Bad amount")
164
184
 
165
185
  self._hasLoaderCount.Value = self._hasLoaderCount.Value + amount
@@ -168,7 +188,7 @@ function LoaderLinkCreator:_addToHasLoaderCount(amount)
168
188
  end
169
189
  end
170
190
 
171
- function LoaderLinkCreator:_countLoaderReferences(robloxInst)
191
+ function LoaderLinkCreator._countLoaderReferences(self: LoaderLinkCreator, robloxInst: Instance): Maid.Maid
172
192
  assert(typeof(robloxInst) == "Instance", "Bad robloxInst")
173
193
 
174
194
  local maid = Maid.new()
@@ -193,9 +213,9 @@ end
193
213
  Cleans up the replicator disconnecting all events and cleaning up
194
214
  created instances.
195
215
  ]=]
196
- function LoaderLinkCreator:Destroy()
216
+ function LoaderLinkCreator.Destroy(self: LoaderLinkCreator)
197
217
  self._maid:DoCleaning()
198
- setmetatable(self, nil)
218
+ setmetatable(self :: any, nil)
199
219
  end
200
220
 
201
221
  return LoaderLinkCreator
@@ -1,3 +1,4 @@
1
+ --!strict
1
2
  --[=[
2
3
  @class LoaderLinkUtils
3
4
  @private
@@ -7,7 +8,7 @@ local LoaderLink = script.Parent.LoaderLink
7
8
 
8
9
  local LoaderLinkUtils = {}
9
10
 
10
- function LoaderLinkUtils.create(loader, linkName)
11
+ function LoaderLinkUtils.create(loader: Instance, linkName: string): ModuleScript
11
12
  assert(typeof(loader) == "Instance", "Bad loader")
12
13
  assert(type(linkName) == "string", "Bad linkName")
13
14
 
@@ -1,3 +1,4 @@
1
+ --!strict
1
2
  -- Literally here just so old loading code works
2
3
 
3
4
  error("Not implemented! Please require LoaderUtils.Parent. This file acts as a marker for old loader versions.")
package/src/Maid.lua CHANGED
@@ -1,3 +1,4 @@
1
+ --!strict
1
2
  --[[
2
3
  Manages the cleaning of events and other things. Useful for
3
4
  encapsulating state and make deconstructors easy.
@@ -26,6 +27,21 @@
26
27
  local Maid = {}
27
28
  Maid.ClassName = "Maid"
28
29
 
30
+ export type MaidTask = (() -> ()) | Instance | thread | Maid | any
31
+
32
+ export type Maid = typeof(setmetatable(
33
+ {} :: {
34
+ Add: <T>(self: Maid, task: T) -> T,
35
+ GiveTask: (self: Maid, task: MaidTask) -> number,
36
+ GivePromise: <T>(self: Maid, promise: T) -> T,
37
+ DoCleaning: (self: Maid) -> (),
38
+ Destroy: (self: Maid) -> (),
39
+ _tasks: { [any]: MaidTask },
40
+ [string | number | { [any]: any } | Instance]: MaidTask?,
41
+ },
42
+ Maid
43
+ ))
44
+
29
45
  --[[
30
46
  Constructs a new Maid object
31
47
 
@@ -36,10 +52,10 @@ Maid.ClassName = "Maid"
36
52
  @ignore
37
53
  @return Maid
38
54
  ]]
39
- function Maid.new()
55
+ function Maid.new(): Maid
40
56
  return setmetatable({
41
- _tasks = {}
42
- }, Maid)
57
+ _tasks = {},
58
+ }, Maid) :: Maid
43
59
  end
44
60
 
45
61
  --[[
@@ -54,7 +70,7 @@ end
54
70
  @param value any
55
71
  @return boolean
56
72
  ]]
57
- function Maid.isMaid(value)
73
+ function Maid.isMaid(value: any): boolean
58
74
  return type(value) == "table" and value.ClassName == "Maid"
59
75
  end
60
76
 
@@ -74,7 +90,7 @@ end
74
90
  @param index any
75
91
  @return MaidTask
76
92
  ]]
77
- function Maid:__index(index)
93
+ function Maid.__index(self: Maid, index: any)
78
94
  if Maid[index] then
79
95
  return Maid[index]
80
96
  else
@@ -102,7 +118,7 @@ end
102
118
  @param index any
103
119
  @param newTask MaidTask
104
120
  ]]
105
- function Maid:__newindex(index, newTask)
121
+ function Maid.__newindex(self: Maid, index: any, newTask: MaidTask)
106
122
  if Maid[index] ~= nil then
107
123
  error(string.format("Cannot use '%s' as a Maid key", tostring(index)), 2)
108
124
  end
@@ -117,16 +133,15 @@ function Maid:__newindex(index, newTask)
117
133
  tasks[index] = newTask
118
134
 
119
135
  if job then
120
- local jobType = typeof(job)
121
- if jobType == "function" then
122
- job()
123
- elseif jobType == "table" then
136
+ if typeof(job) == "function" then
137
+ (job :: any)()
138
+ elseif typeof(job) == "table" then
124
139
  if type(job.Destroy) == "function" then
125
140
  job:Destroy()
126
141
  end
127
- elseif jobType == "Instance" then
142
+ elseif typeof(job) == "Instance" then
128
143
  job:Destroy()
129
- elseif jobType == "thread" then
144
+ elseif typeof(job) == "thread" then
130
145
  local cancelled
131
146
  if coroutine.running() ~= job then
132
147
  cancelled = pcall(function()
@@ -139,7 +154,7 @@ function Maid:__newindex(index, newTask)
139
154
  task.cancel(job)
140
155
  end)
141
156
  end
142
- elseif jobType == "RBXScriptConnection" then
157
+ elseif typeof(job) == "RBXScriptConnection" then
143
158
  job:Disconnect()
144
159
  end
145
160
  end
@@ -152,14 +167,14 @@ end
152
167
  @param task MaidTask -- An item to clean
153
168
  @return MaidTask
154
169
  ]]
155
- function Maid:Add(task)
170
+ function Maid.Add<T>(self: Maid, task: T): T
156
171
  if not task then
157
172
  error("Task cannot be false or nil", 2)
158
173
  end
159
174
 
160
- self[#self._tasks+1] = task
175
+ self[#(self._tasks :: any) + 1] = task
161
176
 
162
- if type(task) == "table" and (not task.Destroy) then
177
+ if type(task) == "table" and not task.Destroy then
163
178
  warn("[Maid.Add] - Gave table task without .Destroy\n\n" .. debug.traceback())
164
179
  end
165
180
 
@@ -173,15 +188,15 @@ end
173
188
  @param task MaidTask -- An item to clean
174
189
  @return number -- taskId
175
190
  ]]
176
- function Maid:GiveTask(task)
191
+ function Maid.GiveTask(self: Maid, task: MaidTask): number
177
192
  if not task then
178
193
  error("Task cannot be false or nil", 2)
179
194
  end
180
195
 
181
- local taskId = #self._tasks+1
196
+ local taskId = #(self._tasks :: any) + 1
182
197
  self[taskId] = task
183
198
 
184
- if type(task) == "table" and (not task.Destroy) then
199
+ if type(task) == "table" and not task.Destroy then
185
200
  warn("[Maid.GiveTask] - Gave table task without .Destroy\n\n" .. debug.traceback())
186
201
  end
187
202
 
@@ -195,7 +210,7 @@ end
195
210
  @param promise Promise<T>
196
211
  @return Promise<T>
197
212
  ]]
198
- function Maid:GivePromise(promise)
213
+ function Maid.GivePromise(self: Maid, promise: any): any
199
214
  if not promise:IsPending() then
200
215
  return promise
201
216
  end
@@ -229,11 +244,11 @@ end
229
244
 
230
245
  @ignore
231
246
  ]]
232
- function Maid:DoCleaning()
247
+ function Maid.DoCleaning(self: Maid)
233
248
  local tasks = self._tasks
234
249
 
235
250
  -- Disconnect all events first as we know this is safe
236
- for index, job in pairs(tasks) do
251
+ for index, job in tasks do
237
252
  if typeof(job) == "RBXScriptConnection" then
238
253
  tasks[index] = nil
239
254
  job:Disconnect()
@@ -244,14 +259,13 @@ function Maid:DoCleaning()
244
259
  local index, job = next(tasks)
245
260
  while job ~= nil do
246
261
  tasks[index] = nil
247
- local jobType = typeof(job)
248
- if jobType == "function" then
249
- job()
250
- elseif jobType == "table" and type(job.Destroy) == "function" then
262
+ if typeof(job) == "function" then
263
+ (job :: any)()
264
+ elseif typeof(job) == "table" and type(job.Destroy) == "function" then
251
265
  job:Destroy()
252
- elseif jobType == "Instance" then
266
+ elseif typeof(job) == "Instance" then
253
267
  job:Destroy()
254
- elseif jobType == "thread" then
268
+ elseif typeof(job) == "thread" then
255
269
  local cancelled
256
270
  if coroutine.running() ~= job then
257
271
  cancelled = pcall(function()
@@ -265,7 +279,7 @@ function Maid:DoCleaning()
265
279
  task.cancel(toCancel)
266
280
  end)
267
281
  end
268
- elseif jobType == "RBXScriptConnection" then
282
+ elseif typeof(job) == "RBXScriptConnection" then
269
283
  job:Disconnect()
270
284
  end
271
285
  index, job = next(tasks)
@@ -1,4 +1,6 @@
1
+ --!strict
1
2
  --[=[
3
+
2
4
  Different replication types we can be in.
3
5
 
4
6
  @class ReplicationType
@@ -6,9 +8,18 @@
6
8
 
7
9
  local Utils = require(script.Parent.Parent.Utils)
8
10
 
11
+ export type ReplicationTypeMap = {
12
+ CLIENT: "client",
13
+ SERVER: "server",
14
+ SHARED: "shared",
15
+ PLUGIN: "plugin",
16
+ }
17
+
18
+ export type ReplicationType = "client" | "server" | "shared" | "plugin"
19
+
9
20
  return Utils.readonly({
10
- CLIENT = "client";
11
- SERVER = "server";
12
- SHARED = "shared";
13
- PLUGIN = "plugin";
14
- })
21
+ CLIENT = "client",
22
+ SERVER = "server",
23
+ SHARED = "shared",
24
+ PLUGIN = "plugin",
25
+ } :: ReplicationTypeMap)
@@ -1,3 +1,4 @@
1
+ --!strict
1
2
  --[=[
2
3
  Utility functions involving [ReplicationType]
3
4
  @class ReplicationTypeUtils
@@ -14,14 +15,17 @@ local ReplicationTypeUtils = {}
14
15
  @param replicationType any
15
16
  @return boolean
16
17
  ]=]
17
- function ReplicationTypeUtils.isReplicationType(replicationType)
18
+ function ReplicationTypeUtils.isReplicationType(replicationType: any): boolean
18
19
  return replicationType == ReplicationType.SHARED
19
20
  or replicationType == ReplicationType.CLIENT
20
21
  or replicationType == ReplicationType.SERVER
21
22
  or replicationType == ReplicationType.PLUGIN
22
23
  end
23
24
 
24
- function ReplicationTypeUtils.getFolderReplicationType(folderName, ancestorReplicationType)
25
+ function ReplicationTypeUtils.getFolderReplicationType(
26
+ folderName: string,
27
+ ancestorReplicationType: ReplicationType.ReplicationType
28
+ ): ReplicationType.ReplicationType
25
29
  assert(type(folderName) == "string", "Bad folderName")
26
30
  assert(type(ancestorReplicationType) == "string", "Bad ancestorReplicationType")
27
31
 
@@ -36,7 +40,7 @@ function ReplicationTypeUtils.getFolderReplicationType(folderName, ancestorRepli
36
40
  end
37
41
  end
38
42
 
39
- function ReplicationTypeUtils.inferReplicationType()
43
+ function ReplicationTypeUtils.inferReplicationType(): ReplicationType.ReplicationType
40
44
  if (not RunService:IsRunning()) and RunService:IsStudio() then
41
45
  return ReplicationType.PLUGIN
42
46
  elseif RunService:IsServer() then
@@ -48,7 +52,7 @@ function ReplicationTypeUtils.inferReplicationType()
48
52
  end
49
53
  end
50
54
 
51
- function ReplicationTypeUtils.isAllowed(replicationType, requestedReplicationType)
55
+ function ReplicationTypeUtils.isAllowed(replicationType: ReplicationType.ReplicationType, requestedReplicationType: ReplicationType.ReplicationType): boolean
52
56
  assert(ReplicationTypeUtils.isReplicationType(replicationType), "Bad replicationType")
53
57
  assert(ReplicationTypeUtils.isReplicationType(requestedReplicationType), "Bad requestedReplicationType")
54
58