@quenty/immediateutils 1.5.0 → 1.7.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,14 @@
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
+ # [1.7.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/immediateutils@1.6.0...@quenty/immediateutils@1.7.0) (2026-09-19)
7
+
8
+ **Note:** Version bump only for package @quenty/immediateutils
9
+
10
+ # [1.6.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/immediateutils@1.5.0...@quenty/immediateutils@1.6.0) (2026-09-02)
11
+
12
+ **Note:** Version bump only for package @quenty/immediateutils
13
+
6
14
  # [1.5.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/immediateutils@1.4.0...@quenty/immediateutils@1.5.0) (2026-09-01)
7
15
 
8
16
  **Note:** Version bump only for package @quenty/immediateutils
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/immediateutils",
3
- "version": "1.5.0",
3
+ "version": "1.7.0",
4
4
  "description": "Immediate-mode ECS loop and hooks implemented via jecs+iris addons",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -42,12 +42,12 @@
42
42
  "@quenty/servicebag": "11.21.0",
43
43
  "@quenty/signal": "7.13.2",
44
44
  "@quenty/spring": "10.12.1",
45
- "@quenty/tie": "10.49.0",
45
+ "@quenty/tie": "10.50.0",
46
46
  "@quenty/valueobject": "13.38.0",
47
47
  "@quentystudios/jest-lua": "3.10.0-quenty.2"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"
51
51
  },
52
- "gitHead": "22ecce3068464d33ab5aedeafde9d959cdd3d0de"
52
+ "gitHead": "d2a04b5a70959bb97219c8ccb7053e91c9976129"
53
53
  }
@@ -0,0 +1,753 @@
1
+ --!nonstrict
2
+ --[=[
3
+ @class ImmediateHotReloadInstall
4
+
5
+ Studio-only installer that keeps an ImmediateScheduler's systems folder in
6
+ sync with Rojo. Clone-based require busts ModuleScript identity cache;
7
+ [ImmediateScheduler.RegisterSystem] replaces by name and Destroy()s the
8
+ previous system.
9
+
10
+ Shadow publication (Rojo's ServerScriptService copy of a ReplicatedStorage
11
+ tree) is based on the same idea as sayhisam1/Rewire's HotReloader
12
+ (https://github.com/sayhisam1/Rewire/blob/main/src/HotReloader.lua, MIT).
13
+
14
+ ```
15
+ ImmediateInstall.stackN(..., ImmediateHotReloadInstall.install(systemsFolder))
16
+ ImmediateHotReloadInstall.mirrorShadow(liveSystemsFolder)
17
+ ```
18
+ ]=]
19
+
20
+ local rawrequire = require
21
+ local require = require(script.Parent.loader).load(script)
22
+
23
+ local HttpService = game:GetService("HttpService")
24
+ local ReplicatedStorage = game:GetService("ReplicatedStorage")
25
+ local RunService = game:GetService("RunService")
26
+ local ServerScriptService = game:GetService("ServerScriptService")
27
+
28
+ local Brio = require("Brio")
29
+ local ImmediateCoreUtils = require("ImmediateCoreUtils")
30
+ local ImmediateScheduler = require("ImmediateScheduler")
31
+ local Maid = require("Maid")
32
+ local Observable = require("Observable")
33
+ local Rx = require("Rx")
34
+ local RxBrioUtils = require("RxBrioUtils")
35
+ local RxInstanceUtils = require("RxInstanceUtils")
36
+ local Signal = require("Signal")
37
+
38
+ local HR_PREFIX = "_HR_"
39
+ local LOG_PREFIX = "[ImmediateHotReloadInstall]"
40
+
41
+ export type PlayContext = {
42
+ isStudioPlay: boolean?,
43
+ isServer: boolean?,
44
+ }
45
+
46
+ -- Luau infers `local X = {}` as a sealed empty table, so later
47
+ -- `function X.install` is invisible to require() consumers.
48
+ export type ImmediateHotReloadInstall = {
49
+ install: (
50
+ systemsFolder: Instance,
51
+ playContext: PlayContext?
52
+ ) -> <Rt>(rt: Rt, scheduler: ImmediateScheduler.ImmediateScheduler) -> Rt,
53
+ mirrorShadow: (liveSystemsFolder: Instance, playContext: PlayContext?) -> () -> (),
54
+ }
55
+
56
+ type PackageLocation = {
57
+ service: Instance,
58
+ rootName: string,
59
+ segments: { string },
60
+ }
61
+
62
+ type PathRecord = {
63
+ relativePath: string,
64
+ generation: number,
65
+ source: ModuleScript?,
66
+ executionClone: ModuleScript?,
67
+ schedulerName: string?,
68
+ unregister: (() -> ())?,
69
+ waitingForLoader: boolean,
70
+ }
71
+
72
+ type LiveController = {
73
+ disposed: boolean,
74
+ rt: ImmediateCoreUtils.ImmediateRuntime,
75
+ scheduler: ImmediateScheduler.ImmediateScheduler,
76
+ liveFolder: Instance,
77
+ records: { [string]: PathRecord },
78
+ namesToPath: { [string]: string },
79
+ }
80
+
81
+ local ImmediateHotReloadInstall: ImmediateHotReloadInstall = {} :: any
82
+
83
+ local function warnPath(path: string, message: string)
84
+ warn(`{LOG_PREFIX} {path}: {message}`)
85
+ end
86
+
87
+ local function resolvePlayContext(playContext: PlayContext?): (boolean, boolean)
88
+ local isStudioPlay = if playContext and playContext.isStudioPlay ~= nil
89
+ then playContext.isStudioPlay
90
+ else (RunService:IsStudio() and RunService:IsRunning())
91
+ local isServer = if playContext and playContext.isServer ~= nil then playContext.isServer else RunService:IsServer()
92
+ return isStudioPlay, isServer
93
+ end
94
+
95
+ local function isExecutionCloneName(name: string): boolean
96
+ return string.sub(name, 1, #HR_PREFIX) == HR_PREFIX
97
+ end
98
+
99
+ local function findUniqueChild(parent: Instance, name: string): (Instance?, boolean)
100
+ local found: Instance? = nil
101
+ for _, child in parent:GetChildren() do
102
+ if child.Name == name then
103
+ if found ~= nil then
104
+ return nil, true
105
+ end
106
+ found = child
107
+ end
108
+ end
109
+ return found, false
110
+ end
111
+
112
+ local function getPackageLocation(folder: Instance): PackageLocation?
113
+ local names = {}
114
+ local current: Instance? = folder
115
+ while current and current.Parent do
116
+ local parent = current.Parent
117
+ if parent == ReplicatedStorage or parent == ServerScriptService then
118
+ local segments = {}
119
+ for index = #names, 1, -1 do
120
+ table.insert(segments, names[index])
121
+ end
122
+ return {
123
+ service = parent,
124
+ rootName = current.Name,
125
+ segments = segments,
126
+ }
127
+ end
128
+ table.insert(names, current.Name)
129
+ current = parent
130
+ end
131
+ return nil
132
+ end
133
+
134
+ local function splitPath(relativePath: string): { string }
135
+ if relativePath == "" then
136
+ return {}
137
+ end
138
+ return string.split(relativePath, "/")
139
+ end
140
+
141
+ local function parentPathAndName(relativePath: string): ({ string }, string)
142
+ local segments = splitPath(relativePath)
143
+ local name = table.remove(segments) :: string
144
+ return segments, name
145
+ end
146
+
147
+ local function findUniqueNonCloneChild(parent: Instance, name: string): Instance?
148
+ local found: Instance? = nil
149
+ for _, child in parent:GetChildren() do
150
+ if child.Name == name and not isExecutionCloneName(child.Name) then
151
+ if found ~= nil then
152
+ warnPath(name, `ambiguous child under {parent:GetFullName()}`)
153
+ return nil
154
+ end
155
+ found = child
156
+ end
157
+ end
158
+ return found
159
+ end
160
+
161
+ local function ensureFolderPath(root: Instance, dirSegments: { string }): Instance
162
+ local current = root
163
+ for _, name in dirSegments do
164
+ local child = findUniqueNonCloneChild(current, name)
165
+ if child then
166
+ current = child
167
+ else
168
+ local folder = Instance.new("Folder")
169
+ folder.Name = name
170
+ folder.Parent = current
171
+ current = folder
172
+ end
173
+ end
174
+ return current
175
+ end
176
+
177
+ local function findCanonicalAtPath(root: Instance, relativePath: string): ModuleScript?
178
+ local dirSegments, name = parentPathAndName(relativePath)
179
+ local parent = root
180
+ for _, segment in dirSegments do
181
+ local child = findUniqueNonCloneChild(parent, segment)
182
+ if not child then
183
+ return nil
184
+ end
185
+ parent = child
186
+ end
187
+ local inst = findUniqueNonCloneChild(parent, name)
188
+ if inst and inst:IsA("ModuleScript") then
189
+ return inst
190
+ end
191
+ return nil
192
+ end
193
+
194
+ local function collectCanonicalModules(root: Instance): { [string]: ModuleScript }
195
+ local found: { [string]: ModuleScript } = {}
196
+
197
+ local function visit(folder: Instance, prefix: string)
198
+ for _, child in folder:GetChildren() do
199
+ if isExecutionCloneName(child.Name) then
200
+ continue
201
+ end
202
+ local path = if prefix == "" then child.Name else `{prefix}/{child.Name}`
203
+ if child:IsA("ModuleScript") then
204
+ if child.Name ~= "loader" then
205
+ if found[path] then
206
+ warnPath(path, `ambiguous canonical ModuleScript under {folder:GetFullName()}`)
207
+ else
208
+ found[path] = child
209
+ end
210
+ end
211
+ elseif child:IsA("Folder") then
212
+ visit(child, path)
213
+ end
214
+ end
215
+ end
216
+
217
+ visit(root, "")
218
+ return found
219
+ end
220
+
221
+ local function findLoaderSibling(parent: Instance): ModuleScript?
222
+ for _, child in parent:GetChildren() do
223
+ if child:IsA("ModuleScript") and child.Name == "loader" then
224
+ return child
225
+ end
226
+ end
227
+ return nil
228
+ end
229
+
230
+ local function resetErrorLog(rt: ImmediateCoreUtils.ImmediateRuntime)
231
+ local errorlog = rt.errorlog
232
+ if type(errorlog) ~= "table" then
233
+ return
234
+ end
235
+ table.clear(errorlog)
236
+ errorlog.lastErrorShout = 0
237
+ end
238
+
239
+ local function resolveSchedulerName(loaded: any, canonicalName: string, executionCloneName: string): string
240
+ local explicit = loaded.name
241
+ if type(explicit) ~= "string" or explicit == "" then
242
+ return canonicalName
243
+ end
244
+ if explicit == executionCloneName then
245
+ return canonicalName
246
+ end
247
+ return explicit
248
+ end
249
+
250
+ local function isSystemTable(loaded: any): boolean
251
+ return type(loaded) == "table" and type(loaded.system) == "function"
252
+ end
253
+
254
+ local function emptyCleanup(): () -> ()
255
+ return function() end
256
+ end
257
+
258
+ local function bindDeferredFlush(maid: Maid.Maid, flush: () -> ()): () -> ()
259
+ local requested = Signal.new()
260
+ maid:GiveTask(requested)
261
+ maid:GiveTask(Rx.fromSignal(requested)
262
+ :Pipe({
263
+ Rx.throttleDefer(),
264
+ })
265
+ :Subscribe(flush))
266
+
267
+ return function()
268
+ requested:Fire()
269
+ end
270
+ end
271
+
272
+ local function isTreeInstance(descendant: Instance): boolean
273
+ if isExecutionCloneName(descendant.Name) then
274
+ return false
275
+ end
276
+ return descendant:IsA("Folder") or descendant:IsA("ModuleScript")
277
+ end
278
+
279
+ local function watchFolderTree(
280
+ maid: Maid.Maid,
281
+ folder: Instance,
282
+ scheduleFlush: () -> (),
283
+ onModuleSourceChanged: ((ModuleScript) -> ())?
284
+ )
285
+ maid:GiveTask(RxInstanceUtils.observeDescendantsBrio(folder, isTreeInstance):Subscribe(function(brio)
286
+ if brio:IsDead() then
287
+ return
288
+ end
289
+
290
+ local descendant = brio:GetValue()
291
+ local inner = brio:ToMaid()
292
+ inner:GiveTask(function()
293
+ scheduleFlush()
294
+ end)
295
+ inner:GiveTask(RxInstanceUtils.observeProperty(descendant, "Name")
296
+ :Pipe({
297
+ Rx.skip(1),
298
+ })
299
+ :Subscribe(scheduleFlush))
300
+
301
+ if onModuleSourceChanged and descendant:IsA("ModuleScript") and descendant.Name ~= "loader" then
302
+ -- Play scripts cannot touch ModuleScript.Source (read or GetPropertyChangedSignal).
303
+ -- Instance.Changed still reports the property name when Rojo writes it.
304
+ inner:GiveTask(Rx.fromSignal(descendant.Changed):Subscribe(function(propertyName: string)
305
+ if propertyName == "Source" then
306
+ onModuleSourceChanged(descendant :: ModuleScript)
307
+ end
308
+ end))
309
+ end
310
+
311
+ scheduleFlush()
312
+ end))
313
+ end
314
+
315
+ local function observeUniqueNamedChildBrio(parent: Instance, name: string): Observable.Observable<Brio.Brio<Instance>>
316
+ return Observable.new(function(sub)
317
+ local maid = Maid.new()
318
+ local current: Instance? = nil
319
+
320
+ local function pick()
321
+ local found, ambiguous = findUniqueChild(parent, name)
322
+ if ambiguous then
323
+ warnPath(name, `ambiguous child under {parent:GetFullName()}`)
324
+ found = nil
325
+ end
326
+ if current == found then
327
+ return
328
+ end
329
+ current = found
330
+ if found then
331
+ local brio = Brio.new(found)
332
+ maid._current = brio
333
+ sub:Fire(brio)
334
+ else
335
+ maid._current = nil
336
+ end
337
+ end
338
+
339
+ maid:GiveTask(RxInstanceUtils.observeChildrenBrio(parent):Subscribe(function(childBrio)
340
+ if childBrio:IsDead() then
341
+ return
342
+ end
343
+
344
+ local inner = childBrio:ToMaid()
345
+ inner:GiveTask(function()
346
+ pick()
347
+ end)
348
+ inner:GiveTask(RxInstanceUtils.observeProperty(childBrio:GetValue(), "Name"):Subscribe(function()
349
+ pick()
350
+ end))
351
+ end))
352
+
353
+ return maid
354
+ end) :: any
355
+ end
356
+
357
+ local function observePathBrio(
358
+ service: Instance,
359
+ rootName: string,
360
+ segments: { string }
361
+ ): Observable.Observable<Brio.Brio<Instance>>
362
+ local function step(parent: Instance, index: number): Observable.Observable<Brio.Brio<Instance>>
363
+ local name = if index == 0 then rootName else segments[index]
364
+ if index >= #segments then
365
+ return observeUniqueNamedChildBrio(parent, name)
366
+ end
367
+
368
+ return observeUniqueNamedChildBrio(parent, name):Pipe({
369
+ RxBrioUtils.switchMapBrio(function(child: Instance)
370
+ return step(child, index + 1)
371
+ end),
372
+ }) :: any
373
+ end
374
+
375
+ return step(service, 0)
376
+ end
377
+
378
+ local function cloneModule(source: ModuleScript, relativePath: string): ModuleScript?
379
+ local ok, cloneOrErr = pcall(function()
380
+ return source:Clone()
381
+ end)
382
+ if not ok or typeof(cloneOrErr) ~= "Instance" or not (cloneOrErr :: Instance):IsA("ModuleScript") then
383
+ warnPath(relativePath, `Clone() failed: {tostring(cloneOrErr)}`)
384
+ return nil
385
+ end
386
+ return cloneOrErr :: ModuleScript
387
+ end
388
+
389
+ type PublisherState = {
390
+ disposed: boolean,
391
+ maid: Maid.Maid,
392
+ liveFolder: Instance,
393
+ published: { [string]: ModuleScript },
394
+ fromShadow: { [string]: ModuleScript },
395
+ shadowSeen: boolean,
396
+ shadowFolder: Instance?,
397
+ shadowMaid: Maid.Maid,
398
+ }
399
+
400
+ local function publisherFlush(state: PublisherState)
401
+ if state.disposed then
402
+ return
403
+ end
404
+ local shadowFolder = state.shadowFolder
405
+ if not shadowFolder or not state.shadowSeen then
406
+ return
407
+ end
408
+
409
+ local shadowMap = collectCanonicalModules(shadowFolder)
410
+
411
+ for path, liveInst in state.published do
412
+ if shadowMap[path] == nil then
413
+ if liveInst.Parent then
414
+ liveInst:Destroy()
415
+ end
416
+ state.published[path] = nil
417
+ state.fromShadow[path] = nil
418
+ end
419
+ end
420
+
421
+ for path, shadowModule in shadowMap do
422
+ if state.fromShadow[path] == shadowModule and state.published[path] and state.published[path].Parent then
423
+ continue
424
+ end
425
+
426
+ local clone = cloneModule(shadowModule, path)
427
+ if not clone then
428
+ continue
429
+ end
430
+ local dirSegments, canonicalName = parentPathAndName(path)
431
+ clone.Name = canonicalName
432
+
433
+ local parent = ensureFolderPath(state.liveFolder, dirSegments)
434
+ local existing = findCanonicalAtPath(state.liveFolder, path)
435
+ if existing and existing ~= clone then
436
+ existing:Destroy()
437
+ end
438
+ clone.Parent = parent
439
+
440
+ state.published[path] = clone
441
+ state.fromShadow[path] = shadowModule
442
+ end
443
+ end
444
+
445
+ local function bindShadowFolder(state: PublisherState, shadowFolder: Instance?, scheduleFlush: () -> ())
446
+ state.shadowMaid:DoCleaning()
447
+ state.shadowFolder = shadowFolder
448
+
449
+ if not shadowFolder then
450
+ -- Authoritative once seen: do not republish from stale live originals.
451
+ return
452
+ end
453
+
454
+ state.shadowSeen = true
455
+
456
+ watchFolderTree(state.shadowMaid, shadowFolder, scheduleFlush, function(descendant)
457
+ for path, module in state.fromShadow do
458
+ if module == descendant then
459
+ state.fromShadow[path] = nil :: any
460
+ end
461
+ end
462
+ scheduleFlush()
463
+ end)
464
+ end
465
+
466
+ local function startMirrorShadow(liveFolder: Instance, playContext: PlayContext?): () -> ()
467
+ local isStudioPlay, isServer = resolvePlayContext(playContext)
468
+ if not isStudioPlay or not isServer then
469
+ return emptyCleanup()
470
+ end
471
+
472
+ local location = getPackageLocation(liveFolder)
473
+ if not location or location.service ~= ReplicatedStorage then
474
+ return emptyCleanup()
475
+ end
476
+
477
+ local maid = Maid.new()
478
+ local shadowMaid = maid:Add(Maid.new())
479
+ local state: PublisherState = {
480
+ disposed = false,
481
+ maid = maid,
482
+ liveFolder = liveFolder,
483
+ published = {},
484
+ fromShadow = {},
485
+ shadowSeen = false,
486
+ shadowFolder = nil,
487
+ shadowMaid = shadowMaid,
488
+ }
489
+
490
+ local scheduleFlush = bindDeferredFlush(maid, function()
491
+ publisherFlush(state)
492
+ end)
493
+
494
+ maid:GiveTask(function()
495
+ state.disposed = true
496
+ for _, inst in state.published do
497
+ if inst.Parent then
498
+ inst:Destroy()
499
+ end
500
+ end
501
+ table.clear(state.published)
502
+ table.clear(state.fromShadow)
503
+ end)
504
+
505
+ maid:GiveTask(observePathBrio(ServerScriptService, location.rootName, location.segments)
506
+ :Pipe({
507
+ RxBrioUtils.flattenToValueAndNil :: any,
508
+ })
509
+ :Subscribe(function(resolved: Instance?)
510
+ if state.disposed then
511
+ return
512
+ end
513
+ bindShadowFolder(state, resolved, scheduleFlush)
514
+ end))
515
+
516
+ return function()
517
+ maid:DoCleaning()
518
+ end
519
+ end
520
+
521
+ local function retireExecutionClone(record: PathRecord)
522
+ local clone = record.executionClone
523
+ record.executionClone = nil
524
+ if clone and clone.Parent then
525
+ clone:Destroy()
526
+ end
527
+ end
528
+
529
+ local function unregisterRecord(controller: LiveController, record: PathRecord)
530
+ local unregister = record.unregister
531
+ record.unregister = nil
532
+ if record.schedulerName and controller.namesToPath[record.schedulerName] == record.relativePath then
533
+ controller.namesToPath[record.schedulerName] = nil
534
+ end
535
+ record.schedulerName = nil
536
+ if unregister then
537
+ unregister()
538
+ end
539
+ retireExecutionClone(record)
540
+ end
541
+
542
+ local function beginRequire(controller: LiveController, record: PathRecord, source: ModuleScript)
543
+ local parent = source.Parent
544
+ if not parent or not findLoaderSibling(parent) then
545
+ record.waitingForLoader = true
546
+ return
547
+ end
548
+ record.waitingForLoader = false
549
+
550
+ local generation = record.generation + 1
551
+ record.generation = generation
552
+ record.source = source
553
+
554
+ local clone = cloneModule(source, record.relativePath)
555
+ if not clone then
556
+ return
557
+ end
558
+
559
+ local guid = HttpService:GenerateGUID(false)
560
+ local executionName = `{HR_PREFIX}{source.Name}_{guid}`
561
+ clone.Name = executionName
562
+ clone:SetAttribute("HotReloaded", true)
563
+ clone.Parent = parent
564
+
565
+ task.spawn(function()
566
+ local ok, loadedOrErr = pcall(rawrequire, clone)
567
+ if controller.disposed or record.generation ~= generation or record.source ~= source then
568
+ if clone.Parent then
569
+ clone:Destroy()
570
+ end
571
+ return
572
+ end
573
+
574
+ if not ok then
575
+ warnPath(record.relativePath, `require failed: {tostring(loadedOrErr)}`)
576
+ if clone.Parent then
577
+ clone:Destroy()
578
+ end
579
+ return
580
+ end
581
+
582
+ local loaded = loadedOrErr
583
+ if not isSystemTable(loaded) then
584
+ warnPath(record.relativePath, "module did not return an ImmediateSchedulableSystem table")
585
+ if clone.Parent then
586
+ clone:Destroy()
587
+ end
588
+ return
589
+ end
590
+
591
+ if loaded.name == nil then
592
+ loaded.name = source.Name
593
+ end
594
+
595
+ local schedulerName = resolveSchedulerName(loaded, source.Name, executionName)
596
+ loaded.name = schedulerName
597
+
598
+ local ownerPath = controller.namesToPath[schedulerName]
599
+ if ownerPath and ownerPath ~= record.relativePath then
600
+ warnPath(record.relativePath, `scheduler name {schedulerName} already used by {ownerPath}`)
601
+ if clone.Parent then
602
+ clone:Destroy()
603
+ end
604
+ return
605
+ end
606
+
607
+ local previousUnregister = record.unregister
608
+ local previousName = record.schedulerName
609
+ local previousClone = record.executionClone
610
+
611
+ local unregister = controller.scheduler:RegisterSystem(loaded)
612
+ record.unregister = unregister
613
+ record.schedulerName = schedulerName
614
+ record.executionClone = clone
615
+ controller.namesToPath[schedulerName] = record.relativePath
616
+
617
+ if previousName and previousName ~= schedulerName then
618
+ if controller.namesToPath[previousName] == record.relativePath then
619
+ controller.namesToPath[previousName] = nil
620
+ end
621
+ if previousUnregister then
622
+ previousUnregister()
623
+ end
624
+ end
625
+
626
+ if previousClone and previousClone ~= clone then
627
+ previousClone:Destroy()
628
+ end
629
+
630
+ resetErrorLog(controller.rt)
631
+ end)
632
+ end
633
+
634
+ local function liveFlush(controller: LiveController)
635
+ if controller.disposed then
636
+ return
637
+ end
638
+
639
+ local found = collectCanonicalModules(controller.liveFolder)
640
+
641
+ for path, record in controller.records do
642
+ if found[path] == nil then
643
+ unregisterRecord(controller, record)
644
+ controller.records[path] = nil
645
+ end
646
+ end
647
+
648
+ for path, source in found do
649
+ local record = controller.records[path]
650
+ if not record then
651
+ record = {
652
+ relativePath = path,
653
+ generation = 0,
654
+ source = nil,
655
+ executionClone = nil,
656
+ schedulerName = nil,
657
+ unregister = nil,
658
+ waitingForLoader = false,
659
+ }
660
+ controller.records[path] = record
661
+ end
662
+
663
+ if record.source ~= source or record.waitingForLoader then
664
+ beginRequire(controller, record, source)
665
+ end
666
+ end
667
+ end
668
+
669
+ local function watchLiveFolder(
670
+ liveFolder: Instance,
671
+ rt: ImmediateCoreUtils.ImmediateRuntime,
672
+ scheduler: ImmediateScheduler.ImmediateScheduler,
673
+ observeSource: boolean
674
+ ): () -> ()
675
+ local maid = Maid.new()
676
+ local controller: LiveController = {
677
+ disposed = false,
678
+ rt = rt,
679
+ scheduler = scheduler,
680
+ liveFolder = liveFolder,
681
+ records = {},
682
+ namesToPath = {},
683
+ }
684
+
685
+ local scheduleFlush = bindDeferredFlush(maid, function()
686
+ liveFlush(controller)
687
+ end)
688
+
689
+ local onModuleSourceChanged = if observeSource
690
+ then function(descendant: ModuleScript)
691
+ for _, candidate in controller.records do
692
+ if candidate.source == descendant then
693
+ candidate.source = nil
694
+ break
695
+ end
696
+ end
697
+ scheduleFlush()
698
+ end
699
+ else nil
700
+
701
+ watchFolderTree(maid, liveFolder, scheduleFlush, onModuleSourceChanged)
702
+
703
+ maid:GiveTask(function()
704
+ controller.disposed = true
705
+ for path, record in controller.records do
706
+ unregisterRecord(controller, record)
707
+ controller.records[path] = nil
708
+ end
709
+ end)
710
+
711
+ scheduleFlush()
712
+
713
+ return function()
714
+ maid:DoCleaning()
715
+ end
716
+ end
717
+
718
+ local function installInto(
719
+ systemsFolder: Instance,
720
+ rt: ImmediateCoreUtils.ImmediateRuntime,
721
+ scheduler: ImmediateScheduler.ImmediateScheduler,
722
+ playContext: PlayContext?
723
+ )
724
+ assert(typeof(systemsFolder) == "Instance", "Bad systemsFolder")
725
+
726
+ local isStudioPlay, isServer = resolvePlayContext(playContext)
727
+ if not isStudioPlay then
728
+ scheduler:RegisterDescendantModuleScripts(systemsFolder)
729
+ return
730
+ end
731
+
732
+ rt.maid:GiveTask(watchLiveFolder(systemsFolder, rt, scheduler, isServer))
733
+
734
+ if isServer then
735
+ rt.maid:GiveTask(startMirrorShadow(systemsFolder, playContext))
736
+ end
737
+ end
738
+
739
+ function ImmediateHotReloadInstall.install(systemsFolder: Instance, playContext: PlayContext?)
740
+ assert(typeof(systemsFolder) == "Instance", "Bad systemsFolder")
741
+
742
+ return function<Rt>(rt: Rt, scheduler: ImmediateScheduler.ImmediateScheduler): Rt
743
+ installInto(systemsFolder, rt :: any, scheduler, playContext)
744
+ return rt
745
+ end
746
+ end
747
+
748
+ function ImmediateHotReloadInstall.mirrorShadow(liveSystemsFolder: Instance, playContext: PlayContext?): () -> ()
749
+ assert(typeof(liveSystemsFolder) == "Instance", "Bad liveSystemsFolder")
750
+ return startMirrorShadow(liveSystemsFolder, playContext)
751
+ end
752
+
753
+ return ImmediateHotReloadInstall :: ImmediateHotReloadInstall
@@ -0,0 +1,393 @@
1
+ --!nonstrict
2
+ --[[
3
+ @class ImmediateHotReloadInstall.spec.lua
4
+ ]]
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local HttpService = game:GetService("HttpService")
8
+ local ReplicatedStorage = game:GetService("ReplicatedStorage")
9
+ local ServerScriptService = game:GetService("ServerScriptService")
10
+
11
+ local ImmediateCoreUtils = require("ImmediateCoreUtils")
12
+ local ImmediateHotReloadInstall = require("ImmediateHotReloadInstall")
13
+ local ImmediateScheduler = require("ImmediateScheduler")
14
+ local Jest = require("Jest")
15
+ local ServiceBag = require("ServiceBag")
16
+
17
+ local describe = Jest.Globals.describe
18
+ local expect = Jest.Globals.expect
19
+ local it = Jest.Globals.it
20
+
21
+ local fixtures = script.Parent.fixtures
22
+
23
+ local PLAY_SERVER = {
24
+ isStudioPlay = true,
25
+ isServer = true,
26
+ }
27
+
28
+ local PLAY_CLIENT = {
29
+ isStudioPlay = true,
30
+ isServer = false,
31
+ }
32
+
33
+ local function settle()
34
+ task.wait()
35
+ task.wait()
36
+ end
37
+
38
+ local function cloneFixture(fixtureName: string, canonicalName: string, parent: Instance): ModuleScript
39
+ local fixture = fixtures:FindFirstChild(fixtureName)
40
+ assert(fixture and fixture:IsA("ModuleScript"), `missing fixture {fixtureName}`)
41
+ local copy = fixture:Clone()
42
+ copy.Name = canonicalName
43
+ copy.Parent = parent
44
+ return copy
45
+ end
46
+
47
+ local function addLoader(parent: Instance)
48
+ cloneFixture("loader", "loader", parent)
49
+ end
50
+
51
+ local function makeRuntime()
52
+ local scheduler = ImmediateScheduler.new()
53
+ local rt = ImmediateCoreUtils.createImmediateRuntime(ServiceBag.new(), function(_path: string)
54
+ return nil
55
+ end, {})
56
+ return rt, scheduler
57
+ end
58
+
59
+ local function dictionary(scheduler: ImmediateScheduler.ImmediateScheduler): { [string]: any }
60
+ return (scheduler :: any)._systemDictionary
61
+ end
62
+
63
+ local function installLive(systems: Instance, playContext)
64
+ local rt, scheduler = makeRuntime()
65
+ local installer = ImmediateHotReloadInstall.install(systems, playContext :: any)
66
+ installer(rt, scheduler)
67
+ settle()
68
+ return rt, scheduler
69
+ end
70
+
71
+ local function uniqueRootName(): string
72
+ return "ImmediateHR_" .. HttpService:GenerateGUID(false)
73
+ end
74
+
75
+ local function makeLiveSystemsUnderRS(): (Folder, Folder)
76
+ local root = Instance.new("Folder")
77
+ root.Name = uniqueRootName()
78
+ local systems = Instance.new("Folder")
79
+ systems.Name = "systems"
80
+ systems.Parent = root
81
+ addLoader(systems)
82
+ root.Parent = ReplicatedStorage
83
+ return root, systems
84
+ end
85
+
86
+ describe("ImmediateHotReloadInstall outside Studio play", function()
87
+ it("delegates to RegisterDescendantModuleScripts", function()
88
+ local systems = Instance.new("Folder")
89
+ addLoader(systems)
90
+ cloneFixture("sys_ok", "sys_ok", systems)
91
+
92
+ local rt, scheduler = makeRuntime()
93
+ local installer = ImmediateHotReloadInstall.install(systems, {
94
+ isStudioPlay = false,
95
+ isServer = true,
96
+ })
97
+ installer(rt, scheduler)
98
+
99
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
100
+ expect(dictionary(scheduler).sys_ok.system).toEqual(expect.any("function"))
101
+
102
+ scheduler:Destroy()
103
+ rt:Destroy()
104
+ systems:Destroy()
105
+ end)
106
+ end)
107
+
108
+ describe("ImmediateHotReloadInstall live loading", function()
109
+ it("registers via an execution clone on first load", function()
110
+ local root, systems = makeLiveSystemsUnderRS()
111
+ cloneFixture("sys_ok", "sys_ok", systems)
112
+
113
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
114
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
115
+
116
+ local sawExecutionClone = false
117
+ for _, child in systems:GetChildren() do
118
+ if string.sub(child.Name, 1, 4) == "_HR_" then
119
+ sawExecutionClone = true
120
+ expect(child:GetAttribute("HotReloaded")).toEqual(true)
121
+ end
122
+ end
123
+ expect(sawExecutionClone).toEqual(true)
124
+
125
+ rt.blackboard.version = nil
126
+ scheduler:Tick(rt)
127
+ expect(rt.blackboard.version).toEqual(1)
128
+
129
+ rt:Destroy()
130
+ scheduler:Destroy()
131
+ root:Destroy()
132
+ end)
133
+
134
+ it("replaces the same scheduler name and Destroy()s the previous system once", function()
135
+ local root, systems = makeLiveSystemsUnderRS()
136
+ local first = cloneFixture("sys_counted", "sys_counted", systems)
137
+
138
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
139
+ local counted = dictionary(scheduler).sys_counted
140
+ expect(counted).never.toEqual(nil)
141
+
142
+ first:Destroy()
143
+ cloneFixture("sys_counted", "sys_counted", systems)
144
+ settle()
145
+
146
+ expect(counted.getDestroyCount()).toEqual(1)
147
+ expect(dictionary(scheduler).sys_counted).never.toEqual(counted)
148
+
149
+ rt:Destroy()
150
+ scheduler:Destroy()
151
+ root:Destroy()
152
+ end)
153
+
154
+ it("normalizes explicit names and retires the previous name on change", function()
155
+ local root, systems = makeLiveSystemsUnderRS()
156
+ local first = cloneFixture("sys_named", "sys_named", systems)
157
+
158
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
159
+ expect(dictionary(scheduler).custom).never.toEqual(nil)
160
+ expect(dictionary(scheduler).sys_named).toEqual(nil)
161
+
162
+ first:Destroy()
163
+ cloneFixture("sys_named_v2", "sys_named", systems)
164
+ settle()
165
+
166
+ expect(dictionary(scheduler).custom).toEqual(nil)
167
+ expect(dictionary(scheduler).custom2).never.toEqual(nil)
168
+
169
+ rt.blackboard.from = nil
170
+ scheduler:Tick(rt)
171
+ expect(rt.blackboard.from).toEqual("named_v2")
172
+
173
+ rt:Destroy()
174
+ scheduler:Destroy()
175
+ root:Destroy()
176
+ end)
177
+
178
+ it("rejects a duplicate scheduler name and keeps the first registration", function()
179
+ local root, systems = makeLiveSystemsUnderRS()
180
+ cloneFixture("sys_dup_a", "sys_dup_a", systems)
181
+
182
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
183
+ expect(dictionary(scheduler).sharedName).never.toEqual(nil)
184
+
185
+ cloneFixture("sys_dup_b", "sys_dup_b", systems)
186
+ settle()
187
+
188
+ expect(dictionary(scheduler).sharedName).never.toEqual(nil)
189
+ expect(dictionary(scheduler).sys_dup_b).toEqual(nil)
190
+
191
+ rt:Destroy()
192
+ scheduler:Destroy()
193
+ root:Destroy()
194
+ end)
195
+
196
+ it("keeps the previous version when a candidate fails, then recovers", function()
197
+ local root, systems = makeLiveSystemsUnderRS()
198
+ local first = cloneFixture("sys_ok", "sys_ok", systems)
199
+
200
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
201
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
202
+
203
+ first:Destroy()
204
+ cloneFixture("sys_fail", "sys_ok", systems)
205
+ settle()
206
+
207
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
208
+ rt.blackboard.from = nil
209
+ scheduler:Tick(rt)
210
+ expect(rt.blackboard.from).toEqual("ok")
211
+
212
+ systems:FindFirstChild("sys_ok"):Destroy()
213
+ cloneFixture("sys_ok_v2", "sys_ok", systems)
214
+ settle()
215
+
216
+ rt.blackboard.from = nil
217
+ scheduler:Tick(rt)
218
+ expect(rt.blackboard.from).toEqual("ok_v2")
219
+
220
+ rt:Destroy()
221
+ scheduler:Destroy()
222
+ root:Destroy()
223
+ end)
224
+
225
+ it("unregisters when the canonical module is removed", function()
226
+ local root, systems = makeLiveSystemsUnderRS()
227
+ local first = cloneFixture("sys_ok", "sys_ok", systems)
228
+
229
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
230
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
231
+
232
+ first:Destroy()
233
+ settle()
234
+
235
+ expect(dictionary(scheduler).sys_ok).toEqual(nil)
236
+
237
+ rt:Destroy()
238
+ scheduler:Destroy()
239
+ root:Destroy()
240
+ end)
241
+
242
+ it("ignores a stale yielding require after a newer revision", function()
243
+ local root, systems = makeLiveSystemsUnderRS()
244
+ local first = cloneFixture("sys_yield", "sys_ok", systems)
245
+
246
+ local rt, scheduler = makeRuntime()
247
+ local installer = ImmediateHotReloadInstall.install(systems, PLAY_CLIENT)
248
+ installer(rt, scheduler)
249
+
250
+ first:Destroy()
251
+ cloneFixture("sys_ok_v2", "sys_ok", systems)
252
+ task.wait(0.15)
253
+ task.wait()
254
+
255
+ rt.blackboard.from = nil
256
+ scheduler:Tick(rt)
257
+ expect(rt.blackboard.from).toEqual("ok_v2")
258
+
259
+ rt:Destroy()
260
+ scheduler:Destroy()
261
+ root:Destroy()
262
+ end)
263
+
264
+ it("does not treat HotReloaded canonicals as ineligible", function()
265
+ local root, systems = makeLiveSystemsUnderRS()
266
+ local module = cloneFixture("sys_ok", "sys_ok", systems)
267
+ module:SetAttribute("HotReloaded", true)
268
+
269
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
270
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
271
+
272
+ rt:Destroy()
273
+ scheduler:Destroy()
274
+ root:Destroy()
275
+ end)
276
+
277
+ it("does not register execution-clone instances as canonical systems", function()
278
+ local root, systems = makeLiveSystemsUnderRS()
279
+ cloneFixture("sys_ok", "sys_ok", systems)
280
+
281
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
282
+ local names = {}
283
+ for name in dictionary(scheduler) do
284
+ table.insert(names, name)
285
+ end
286
+ expect(#names).toEqual(1)
287
+ expect(names[1]).toEqual("sys_ok")
288
+
289
+ rt:Destroy()
290
+ scheduler:Destroy()
291
+ root:Destroy()
292
+ end)
293
+ end)
294
+
295
+ describe("ImmediateHotReloadInstall shadow publication", function()
296
+ it("discovers a late shadow tree and replaces live canonicals", function()
297
+ local root, systems = makeLiveSystemsUnderRS()
298
+ cloneFixture("sys_ok", "sys_ok", systems)
299
+
300
+ local rt, scheduler = installLive(systems, PLAY_SERVER)
301
+ expect(dictionary(scheduler).sys_ok).never.toEqual(nil)
302
+ scheduler:Tick(rt)
303
+ expect(rt.blackboard.from).toEqual("ok")
304
+
305
+ local shadowRoot = Instance.new("Folder")
306
+ shadowRoot.Name = root.Name
307
+ local shadowSystems = Instance.new("Folder")
308
+ shadowSystems.Name = "systems"
309
+ shadowSystems.Parent = shadowRoot
310
+ addLoader(shadowSystems)
311
+ cloneFixture("sys_ok_v2", "sys_ok", shadowSystems)
312
+ shadowRoot.Parent = ServerScriptService
313
+ settle()
314
+ settle()
315
+
316
+ rt.blackboard.from = nil
317
+ scheduler:Tick(rt)
318
+ expect(rt.blackboard.from).toEqual("ok_v2")
319
+
320
+ shadowRoot:Destroy()
321
+ rt:Destroy()
322
+ scheduler:Destroy()
323
+ root:Destroy()
324
+ end)
325
+
326
+ it("rebinds when the shadow folder is replaced", function()
327
+ local root, systems = makeLiveSystemsUnderRS()
328
+ cloneFixture("sys_ok", "sys_ok", systems)
329
+
330
+ local rt, scheduler = installLive(systems, PLAY_SERVER)
331
+
332
+ local shadowRoot = Instance.new("Folder")
333
+ shadowRoot.Name = root.Name
334
+ local shadowSystems = Instance.new("Folder")
335
+ shadowSystems.Name = "systems"
336
+ shadowSystems.Parent = shadowRoot
337
+ addLoader(shadowSystems)
338
+ cloneFixture("sys_ok", "sys_ok", shadowSystems)
339
+ shadowRoot.Parent = ServerScriptService
340
+ settle()
341
+
342
+ shadowRoot:Destroy()
343
+ local shadowRoot2 = Instance.new("Folder")
344
+ shadowRoot2.Name = root.Name
345
+ local shadowSystems2 = Instance.new("Folder")
346
+ shadowSystems2.Name = "systems"
347
+ shadowSystems2.Parent = shadowRoot2
348
+ addLoader(shadowSystems2)
349
+ cloneFixture("sys_ok_v2", "sys_ok", shadowSystems2)
350
+ shadowRoot2.Parent = ServerScriptService
351
+ settle()
352
+ settle()
353
+
354
+ rt.blackboard.from = nil
355
+ scheduler:Tick(rt)
356
+ expect(rt.blackboard.from).toEqual("ok_v2")
357
+
358
+ shadowRoot2:Destroy()
359
+ rt:Destroy()
360
+ scheduler:Destroy()
361
+ root:Destroy()
362
+ end)
363
+
364
+ it("mirrorShadow is a no-op when the live folder is not under ReplicatedStorage", function()
365
+ local systems = Instance.new("Folder")
366
+ addLoader(systems)
367
+ local cleanup = ImmediateHotReloadInstall.mirrorShadow(systems, PLAY_SERVER)
368
+ cleanup()
369
+ end)
370
+ end)
371
+
372
+ describe("ImmediateHotReloadInstall cleanup", function()
373
+ it("runtime-first cleanup unregisters systems", function()
374
+ local root, systems = makeLiveSystemsUnderRS()
375
+ cloneFixture("sys_ok", "sys_ok", systems)
376
+
377
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
378
+ rt:Destroy()
379
+ expect(dictionary(scheduler).sys_ok).toEqual(nil)
380
+ scheduler:Destroy()
381
+ root:Destroy()
382
+ end)
383
+
384
+ it("scheduler-first cleanup does not error", function()
385
+ local root, systems = makeLiveSystemsUnderRS()
386
+ cloneFixture("sys_ok", "sys_ok", systems)
387
+
388
+ local rt, scheduler = installLive(systems, PLAY_CLIENT)
389
+ scheduler:Destroy()
390
+ rt:Destroy()
391
+ root:Destroy()
392
+ end)
393
+ end)
@@ -0,0 +1,2 @@
1
+ -- Dummy sibling so execution clones can wait for script.Parent.loader.
2
+ return {}
@@ -0,0 +1,15 @@
1
+ local destroyCount = 0
2
+
3
+ local module = {
4
+ system = function() end,
5
+ }
6
+
7
+ function module.Destroy()
8
+ destroyCount += 1
9
+ end
10
+
11
+ function module.getDestroyCount()
12
+ return destroyCount
13
+ end
14
+
15
+ return module
@@ -0,0 +1,4 @@
1
+ return {
2
+ name = "sharedName",
3
+ system = function() end,
4
+ }
@@ -0,0 +1,4 @@
1
+ return {
2
+ name = "sharedName",
3
+ system = function() end,
4
+ }
@@ -0,0 +1 @@
1
+ error("ImmediateHotReloadInstall fixture fail")
@@ -0,0 +1,6 @@
1
+ return {
2
+ name = "custom",
3
+ system = function(rt)
4
+ rt.blackboard.from = "named"
5
+ end,
6
+ }
@@ -0,0 +1,6 @@
1
+ return {
2
+ name = "custom2",
3
+ system = function(rt)
4
+ rt.blackboard.from = "named_v2"
5
+ end,
6
+ }
@@ -0,0 +1,6 @@
1
+ return {
2
+ system = function(rt)
3
+ rt.blackboard.version = 1
4
+ rt.blackboard.from = "ok"
5
+ end,
6
+ }
@@ -0,0 +1,6 @@
1
+ return {
2
+ system = function(rt)
3
+ rt.blackboard.version = 2
4
+ rt.blackboard.from = "ok_v2"
5
+ end,
6
+ }
@@ -0,0 +1,6 @@
1
+ task.wait()
2
+ return {
3
+ system = function(rt)
4
+ rt.blackboard.from = "yielded"
5
+ end,
6
+ }
@@ -11,8 +11,8 @@
11
11
 
12
12
  ordering.
13
13
 
14
- This doesn't handle hot reloading; some hot-reload service can interact with
15
- a scheduler's APIs to add/remove systems.
14
+ This doesn't handle hot reloading. [ImmediateHotReloadInstall] watches
15
+ system ModuleScripts and calls RegisterSystem / UnregisterSystem.
16
16
 
17
17
  You determine when entire "ticks" happen (usually by hooking it up to
18
18
  PreRender, or you can just manually call tick() a bunch in one go for unit
@@ -307,7 +307,12 @@ function ImmediateScheduler.RegisterSystem(self: ImmediateScheduler, systemTable
307
307
  self._profileLabels[systemName] = `sys.{systemName}`
308
308
  self._sortFlag = true
309
309
  return function()
310
- self:UnregisterSystem(systemName)
310
+ if getmetatable(self) == nil then
311
+ return
312
+ end
313
+ if self._systemDictionary and self._systemDictionary[systemName] == systemTable then
314
+ self:UnregisterSystem(systemName)
315
+ end
311
316
  end
312
317
  end
313
318
 
@@ -352,7 +357,7 @@ function ImmediateScheduler.Destroy(self: ImmediateScheduler)
352
357
  table.clear(self._sorted_postSystem)
353
358
  table.clear(self._sorted_preTick)
354
359
  table.clear(self._sorted_postTick)
355
- self:Destroy()
360
+ BaseObject.Destroy(self)
356
361
  end
357
362
 
358
363
  return ImmediateScheduler
@@ -0,0 +1,88 @@
1
+ --!strict
2
+ --[[
3
+ @class ImmediateScheduler.spec.lua
4
+ ]]
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local ImmediateCoreUtils = require("ImmediateCoreUtils")
8
+ local ImmediateScheduler = require("ImmediateScheduler")
9
+ local Jest = require("Jest")
10
+ local ServiceBag = require("ServiceBag")
11
+
12
+ local describe = Jest.Globals.describe
13
+ local expect = Jest.Globals.expect
14
+ local it = Jest.Globals.it
15
+
16
+ local function makeRuntime()
17
+ return ImmediateCoreUtils.createImmediateRuntime(ServiceBag.new(), function(_path: string)
18
+ return nil
19
+ end)
20
+ end
21
+
22
+ describe("ImmediateScheduler.RegisterSystem disposer", function()
23
+ it("unregisters only while the same table is still registered", function()
24
+ local scheduler = ImmediateScheduler.new()
25
+ local firstDestroyed = 0
26
+ local secondDestroyed = 0
27
+ local first = {
28
+ name = "alpha",
29
+ system = function() end,
30
+ Destroy = function()
31
+ firstDestroyed += 1
32
+ end,
33
+ }
34
+ local second = {
35
+ name = "alpha",
36
+ system = function() end,
37
+ Destroy = function()
38
+ secondDestroyed += 1
39
+ end,
40
+ }
41
+
42
+ local disposeFirst = scheduler:RegisterSystem(first)
43
+ scheduler:RegisterSystem(second)
44
+
45
+ expect(firstDestroyed).toEqual(1)
46
+ expect(secondDestroyed).toEqual(0)
47
+
48
+ disposeFirst()
49
+
50
+ expect(firstDestroyed).toEqual(1)
51
+ expect(secondDestroyed).toEqual(0)
52
+
53
+ scheduler:Destroy()
54
+ expect(secondDestroyed).toEqual(1)
55
+ end)
56
+ end)
57
+
58
+ describe("ImmediateScheduler.Destroy", function()
59
+ it("completes without recursion and destroys registered systems once", function()
60
+ local scheduler = ImmediateScheduler.new()
61
+ local destroyed = 0
62
+ scheduler:RegisterSystem({
63
+ name = "alpha",
64
+ system = function() end,
65
+ Destroy = function()
66
+ destroyed += 1
67
+ end,
68
+ })
69
+
70
+ scheduler:Destroy()
71
+ expect(destroyed).toEqual(1)
72
+ end)
73
+
74
+ it("can tick before destroy", function()
75
+ local scheduler = ImmediateScheduler.new()
76
+ local rt = makeRuntime()
77
+ local ran = 0
78
+ scheduler:RegisterSystem({
79
+ name = "alpha",
80
+ system = function()
81
+ ran += 1
82
+ end,
83
+ })
84
+ scheduler:Tick(rt)
85
+ expect(ran).toEqual(1)
86
+ scheduler:Destroy()
87
+ end)
88
+ end)