@quenty/templateprovider 11.52.0 → 11.53.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,16 @@
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
+ # [11.53.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/templateprovider@11.52.1...@quenty/templateprovider@11.53.0) (2026-09-21)
7
+
8
+ ### Bug Fixes
9
+
10
+ - Fix unit testing template provider ([08dd007](https://github.com/Quenty/NevermoreEngine/commit/08dd007171bd348dcd1a966ff67b8d597a202cb5))
11
+
12
+ ## [11.52.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/templateprovider@11.52.0...@quenty/templateprovider@11.52.1) (2026-09-03)
13
+
14
+ **Note:** Version bump only for package @quenty/templateprovider
15
+
6
16
  # [11.52.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/templateprovider@11.51.0...@quenty/templateprovider@11.52.0) (2026-09-02)
7
17
 
8
18
  **Note:** Version bump only for package @quenty/templateprovider
@@ -0,0 +1,10 @@
1
+ {
2
+ "targets": {
3
+ "test": {
4
+ "universeId": 9716264427,
5
+ "placeId": 101461088212720,
6
+ "project": "test/default.project.json",
7
+ "scriptTemplate": "test/scripts/Server/ServerMain.server.lua"
8
+ }
9
+ }
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/templateprovider",
3
- "version": "11.52.0",
3
+ "version": "11.53.0",
4
4
  "description": "Base of a template retrieval system",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -35,19 +35,24 @@
35
35
  "@quenty/enums": "1.3.1",
36
36
  "@quenty/insertserviceutils": "10.24.0",
37
37
  "@quenty/instanceutils": "13.37.0",
38
+ "@quenty/jestutils": "1.1.0",
38
39
  "@quenty/loader": "10.11.1",
39
40
  "@quenty/maid": "3.12.0",
40
- "@quenty/observablecollection": "12.51.0",
41
+ "@quenty/nevermore-test-runner": "1.6.0",
42
+ "@quenty/observablecollection": "12.52.0",
43
+ "@quenty/playermock": "1.10.0",
41
44
  "@quenty/promise": "10.24.0",
42
45
  "@quenty/promisemaid": "5.24.0",
43
- "@quenty/remoting": "12.44.0",
46
+ "@quenty/remoting": "12.45.0",
44
47
  "@quenty/rx": "13.34.0",
45
48
  "@quenty/servicebag": "11.21.0",
46
49
  "@quenty/string": "3.5.0",
47
- "@quenty/table": "3.9.2"
50
+ "@quenty/table": "3.9.2",
51
+ "@quenty/tie": "10.51.0",
52
+ "@quentystudios/jest-lua": "3.10.0-quenty.2"
48
53
  },
49
54
  "publishConfig": {
50
55
  "access": "public"
51
56
  },
52
- "gitHead": "278f4ca25486a8ba0601704b27a11da8c77499c9"
57
+ "gitHead": "a9d5da78a9816d6be6c2784bff2e8260e22f94b5"
53
58
  }
@@ -8,9 +8,27 @@ local require = require(script.Parent.loader).load(script)
8
8
  local RunService = game:GetService("RunService")
9
9
 
10
10
  local TemplateReplicationModes = require("TemplateReplicationModes")
11
+ local TieRealms = require("TieRealms")
11
12
 
12
13
  local TemplateReplicationModesUtils = {}
13
14
 
15
+ --[=[
16
+ Returns the replication mode for a [TieRealm]
17
+ ]=]
18
+ function TemplateReplicationModesUtils.fromTieRealm(
19
+ tieRealm: TieRealms.TieRealm
20
+ ): TemplateReplicationModes.TemplateReplicationMode
21
+ if tieRealm == TieRealms.SERVER then
22
+ return TemplateReplicationModes.SERVER
23
+ elseif tieRealm == TieRealms.CLIENT then
24
+ return TemplateReplicationModes.CLIENT
25
+ elseif tieRealm == TieRealms.SHARED then
26
+ return TemplateReplicationModes.SHARED
27
+ else
28
+ error("Bad tieRealm")
29
+ end
30
+ end
31
+
14
32
  --[=[
15
33
  Uses run service to infer the replication mode
16
34
  ]=]
@@ -0,0 +1,115 @@
1
+ --!strict
2
+ --[[
3
+ @class TaggedTemplateProvider.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local CollectionService = game:GetService("CollectionService")
9
+ local HttpService = game:GetService("HttpService")
10
+ local ReplicatedStorage = game:GetService("ReplicatedStorage")
11
+
12
+ local Jest = require("Jest")
13
+ local JestUtils = require("JestUtils")
14
+ local Maid = require("Maid")
15
+ local PromiseTestUtils = require("PromiseTestUtils")
16
+ local ServiceBag = require("ServiceBag")
17
+ local TaggedTemplateProvider = require("TaggedTemplateProvider")
18
+ local TemplateProvider = require("TemplateProvider")
19
+
20
+ local describe = Jest.Globals.describe
21
+ local expect = Jest.Globals.expect
22
+ local it = Jest.Globals.it
23
+
24
+ type Controller = {
25
+ tagName: string,
26
+ newProvider: () -> TemplateProvider.TemplateProvider,
27
+ newTagged: (className: string, name: string) -> Instance,
28
+ Destroy: (self: Controller) -> (),
29
+ }
30
+
31
+ local function setup(): Controller
32
+ local maid = Maid.new()
33
+ local tagName = HttpService:GenerateGUID(false)
34
+
35
+ local controller: Controller = {
36
+ tagName = tagName,
37
+
38
+ newProvider = function()
39
+ local serviceBag = maid:Add(ServiceBag.new())
40
+ local provider: TemplateProvider.TemplateProvider =
41
+ serviceBag:GetService(TaggedTemplateProvider.new(HttpService:GenerateGUID(false), tagName)) :: any
42
+ serviceBag:Init()
43
+ serviceBag:Start()
44
+ return provider
45
+ end,
46
+
47
+ newTagged = function(className, name)
48
+ local instance = maid:Add(Instance.new(className))
49
+ instance.Name = name
50
+ instance:AddTag(tagName)
51
+ instance.Parent = ReplicatedStorage
52
+ return instance
53
+ end,
54
+
55
+ Destroy = function(_self)
56
+ maid:DoCleaning()
57
+ end,
58
+ }
59
+
60
+ maid:GiveTask(JestUtils.afterThis(controller))
61
+
62
+ return controller
63
+ end
64
+
65
+ describe("TaggedTemplateProvider.new", function()
66
+ it("returns a template provider", function()
67
+ expect(TemplateProvider.isTemplateProvider(TaggedTemplateProvider.new("Provider", "Tag"))).toBe(true)
68
+ end)
69
+
70
+ it("rejects a tag name that is not a string", function()
71
+ expect(function()
72
+ TaggedTemplateProvider.new("Provider", nil :: any)
73
+ end).toThrow("Bad tagName")
74
+ end)
75
+
76
+ it("provides tagged instances as templates", function()
77
+ local controller = setup()
78
+ local sword = controller.newTagged("Model", "Sword")
79
+ local provider = controller.newProvider()
80
+
81
+ expect(provider:GetTemplate("Sword")).toBe(sword)
82
+
83
+ controller:Destroy()
84
+ end)
85
+
86
+ it("provides the children of a tagged folder", function()
87
+ local controller = setup()
88
+ local weapons = controller.newTagged("Folder", "Weapons")
89
+ local bow = Instance.new("Model")
90
+ bow.Name = "Bow"
91
+ bow.Parent = weapons
92
+ local provider = controller.newProvider()
93
+
94
+ expect(provider:GetTemplate("Bow")).toBe(bow)
95
+
96
+ controller:Destroy()
97
+ end)
98
+
99
+ it("follows the tag being added and removed", function()
100
+ local controller = setup()
101
+ local provider = controller.newProvider()
102
+
103
+ local sword = controller.newTagged("Model", "Sword")
104
+ expect(PromiseTestUtils.awaitValue(function()
105
+ return provider:GetTemplate("Sword") == sword
106
+ end)).toBe(true)
107
+
108
+ CollectionService:RemoveTag(sword, controller.tagName)
109
+ expect(PromiseTestUtils.awaitValue(function()
110
+ return provider:GetTemplate("Sword") == nil
111
+ end)).toBe(true)
112
+
113
+ controller:Destroy()
114
+ end)
115
+ end)
@@ -52,7 +52,9 @@ local DuckTypeUtils = require("DuckTypeUtils")
52
52
  local Maid = require("Maid")
53
53
  local Observable = require("Observable")
54
54
  local ObservableCountingMap = require("ObservableCountingMap")
55
+ local ObservableMap = require("ObservableMap")
55
56
  local ObservableMapList = require("ObservableMapList")
57
+ local PlayerMock = require("PlayerMock")
56
58
  local Promise = require("Promise")
57
59
  local PromiseMaidUtils = require("PromiseMaidUtils")
58
60
  local Remoting = require("Remoting")
@@ -62,10 +64,12 @@ local ServiceBag = require("ServiceBag")
62
64
  local String = require("String")
63
65
  local TemplateReplicationModes = require("TemplateReplicationModes")
64
66
  local TemplateReplicationModesUtils = require("TemplateReplicationModesUtils")
67
+ local TieRealmService = require("TieRealmService")
65
68
 
66
69
  local TOMBSTONE_ID_ATTRIBUTE = "UnreplicatedTemplateId"
67
70
  local TOMBSTONE_NAME_POSTFIX_UNLOADED = "_Unloaded"
68
71
  local TOMBSTONE_NAME_POSTFIX_LOADED = "_Loaded"
72
+ local PREVENT_REPLICATION_CAMERA_NAME = "PreventReplication"
69
73
 
70
74
  local TemplateProvider = {}
71
75
  TemplateProvider.ClassName = "TemplateProvider"
@@ -87,6 +91,7 @@ export type TemplateProvider = typeof(setmetatable(
87
91
  _templateMapList: any, -- ObservableMapList.ObservableMapList<Instance>,
88
92
  _unreplicatedTemplateMapList: any, -- ObservableMapList.ObservableMapList<Instance>,
89
93
  _containerRootCountingMap: any, -- ObservableCountingMap.ObservableCountingMap<Instance>,
94
+ _childTemplateNameCountingMaps: any, -- ObservableMap.ObservableMap<Instance, ObservableCountingMap.ObservableCountingMap<string>>,
90
95
  _remoting: Remoting.Remoting,
91
96
  _tombstoneLookup: { [string]: Instance },
92
97
  _pendingTemplatePromises: { [string]: Promise.Promise<Instance> },
@@ -136,6 +141,9 @@ end
136
141
  --[=[
137
142
  Initializes the container provider. Should be done via [ServiceBag].
138
143
 
144
+ The replication mode follows the bag's [TieRealmService] realm when one was set, and is otherwise
145
+ inferred from [RunService].
146
+
139
147
  @param serviceBag ServiceBag
140
148
  ]=]
141
149
  function TemplateProvider.Init(self: TemplateProvider, serviceBag: ServiceBag.ServiceBag): ()
@@ -143,7 +151,12 @@ function TemplateProvider.Init(self: TemplateProvider, serviceBag: ServiceBag.Se
143
151
  self._serviceBag = assert(serviceBag, "No serviceBag")
144
152
  self._maid = Maid.new()
145
153
 
146
- self._replicationMode = TemplateReplicationModesUtils.inferReplicationMode()
154
+ local tieRealmService: TieRealmService.TieRealmService = serviceBag:GetService(TieRealmService) :: any
155
+ if tieRealmService:HasExplicitTieRealm() then
156
+ self._replicationMode = TemplateReplicationModesUtils.fromTieRealm(tieRealmService:GetTieRealm())
157
+ else
158
+ self._replicationMode = TemplateReplicationModesUtils.inferReplicationMode()
159
+ end
147
160
 
148
161
  -- There can be multiple templates for a given name
149
162
  self._templateMapList = self._maid:Add(ObservableMapList.new())
@@ -166,7 +179,9 @@ function TemplateProvider._setupTemplateCache(self: TemplateProvider): ()
166
179
  assert(self._tombstoneLookup[tombstoneId], "Not a valid tombstone")
167
180
 
168
181
  -- Stuff doesn't replicate in the PlayerGui
169
- local playerGui = player:FindFirstChildWhichIsA("PlayerGui")
182
+ local playerGui = if PlayerMock.isMock(player)
183
+ then PlayerMock.getPlayerGui(player)
184
+ else player:FindFirstChildWhichIsA("PlayerGui")
170
185
  if not playerGui then
171
186
  return Promise.rejected("No playerGui")
172
187
  end
@@ -209,8 +224,8 @@ function TemplateProvider._handleContainer(self: TemplateProvider, containerMaid
209
224
  then
210
225
  -- Prevent replication to client immediately
211
226
 
212
- local camera = containerMaid:Add(Instance.new("Camera"))
213
- camera.Name = "PreventReplication"
227
+ local camera = Instance.new("Camera")
228
+ camera.Name = PREVENT_REPLICATION_CAMERA_NAME
214
229
  camera.Parent = container
215
230
 
216
231
  local function handleChild(child: Instance)
@@ -224,7 +239,7 @@ function TemplateProvider._handleContainer(self: TemplateProvider, containerMaid
224
239
  child.Parent = camera
225
240
  end
226
241
 
227
- containerMaid:GiveTask(container.ChildAdded:Connect(handleChild))
242
+ local childAddedConnection = container.ChildAdded:Connect(handleChild)
228
243
 
229
244
  for _, child in container:GetChildren() do
230
245
  handleChild(child)
@@ -232,6 +247,16 @@ function TemplateProvider._handleContainer(self: TemplateProvider, containerMaid
232
247
 
233
248
  self:_replicateTombstones(containerMaid, camera, container)
234
249
 
250
+ containerMaid:GiveTask(function()
251
+ childAddedConnection:Disconnect()
252
+
253
+ for _, child in camera:GetChildren() do
254
+ child.Parent = container
255
+ end
256
+
257
+ camera:Destroy()
258
+ end)
259
+
235
260
  return
236
261
  end
237
262
 
@@ -319,6 +344,185 @@ function TemplateProvider.ObserveUnreplicatedTemplateNamesBrio(
319
344
  return self._unreplicatedTemplateMapList:ObserveKeysBrio()
320
345
  end
321
346
 
347
+ --[=[
348
+ Lists the names of the templates directly inside a folder template without
349
+ replicating the folder or any of its contents. On the client this reads the
350
+ tombstones the server left behind, so it works before anything is loaded.
351
+ Use this to pick one template by name and then [TemplateProvider.PromiseTemplate]
352
+ just that one.
353
+
354
+ Returns an empty list if the folder template is not known yet, or if it does not
355
+ hold child templates. Only folders and roots passed to [TemplateProvider.AddTemplates]
356
+ hold child templates.
357
+
358
+ :::info
359
+ The order of the names is not stable. Sort them if you need a deterministic order.
360
+ :::
361
+
362
+ @param folderTemplateName string
363
+ @return { string }
364
+ ]=]
365
+ function TemplateProvider.GetChildTemplateNameList(self: TemplateProvider, folderTemplateName: string): { string }
366
+ assert(type(folderTemplateName) == "string", "Bad folderTemplateName")
367
+
368
+ local container = self:_findTemplateOrTombstone(folderTemplateName)
369
+ if not container then
370
+ return {}
371
+ end
372
+
373
+ local countingMap = self:_ensureChildTemplateNameCache():Get(container)
374
+ if not countingMap then
375
+ return {}
376
+ end
377
+
378
+ return countingMap:GetKeyList()
379
+ end
380
+
381
+ --[=[
382
+ Observes [TemplateProvider.GetChildTemplateNameList]. Emits a new list whenever
383
+ the set of child template names changes, starting with an empty list until the
384
+ folder template is known.
385
+
386
+ Adding or removing a template that leaves the set of names unchanged does not emit,
387
+ and neither does anything deeper than a direct child.
388
+
389
+ @param folderTemplateName string
390
+ @return Observable<{ string }>
391
+ ]=]
392
+ function TemplateProvider.ObserveChildTemplateNameList(
393
+ self: TemplateProvider,
394
+ folderTemplateName: string
395
+ ): Observable.Observable<{ string }>
396
+ assert(type(folderTemplateName) == "string", "Bad folderTemplateName")
397
+
398
+ local countingMaps = self:_ensureChildTemplateNameCache()
399
+
400
+ return self:_observeTemplateOrTombstone(folderTemplateName):Pipe({
401
+ Rx.switchMap(function(container: Instance?)
402
+ if not container then
403
+ return Rx.of({}) :: any
404
+ end
405
+
406
+ return countingMaps:ObserveAtKey(container):Pipe({
407
+ Rx.switchMap(function(countingMap)
408
+ if not countingMap then
409
+ return Rx.of({})
410
+ end
411
+
412
+ return countingMap:ObserveKeysList()
413
+ end :: any),
414
+ } :: any) :: any
415
+ end) :: any,
416
+ } :: any) :: any
417
+ end
418
+
419
+ function TemplateProvider._findTemplateOrTombstone(self: TemplateProvider, templateName: string): Instance?
420
+ return self._templateMapList:GetItemForKeyAtIndex(templateName, -1)
421
+ or self._unreplicatedTemplateMapList:GetItemForKeyAtIndex(templateName, -1)
422
+ end
423
+
424
+ function TemplateProvider._observeTemplateOrTombstone(
425
+ self: TemplateProvider,
426
+ templateName: string
427
+ ): Observable.Observable<Instance?>
428
+ local function observeLast(mapList)
429
+ return mapList:ObserveList(templateName):Pipe({
430
+ Rx.switchMap(function(list)
431
+ if not list then
432
+ return Rx.of(nil)
433
+ end
434
+
435
+ return list:ObserveAtIndex(-1)
436
+ end :: any),
437
+ } :: any)
438
+ end
439
+
440
+ return Rx.combineLatest({
441
+ template = observeLast(self._templateMapList),
442
+ tombstone = observeLast(self._unreplicatedTemplateMapList),
443
+ }):Pipe({
444
+ Rx.map(function(state)
445
+ return state.template or state.tombstone
446
+ end) :: any,
447
+ Rx.distinct() :: any,
448
+ } :: any) :: any
449
+ end
450
+
451
+ function TemplateProvider._ensureChildTemplateNameCache(self: TemplateProvider)
452
+ if self._childTemplateNameCountingMaps then
453
+ return self._childTemplateNameCountingMaps
454
+ end
455
+
456
+ local countingMaps = self._maid:Add(ObservableMap.new())
457
+ self._childTemplateNameCountingMaps = countingMaps
458
+
459
+ self._maid:GiveTask(self._containerRootCountingMap:ObserveKeysBrio():Subscribe(function(containerBrio)
460
+ if containerBrio:IsDead() then
461
+ return
462
+ end
463
+
464
+ local containerMaid: Maid.Maid, container = containerBrio:ToMaidAndValue()
465
+
466
+ local countingMap: ObservableCountingMap.ObservableCountingMap<string> =
467
+ containerMaid:Add(ObservableCountingMap.new()) :: any
468
+
469
+ self:_addChildTemplateNames(containerMaid, container, countingMap)
470
+ containerMaid:GiveTask(countingMaps:Set(container, countingMap))
471
+ end))
472
+
473
+ return countingMaps
474
+ end
475
+
476
+ function TemplateProvider._addChildTemplateNames(
477
+ self: TemplateProvider,
478
+ topMaid: Maid.Maid,
479
+ container: Instance,
480
+ countingMap: ObservableCountingMap.ObservableCountingMap<string>
481
+ ): ()
482
+ topMaid:GiveTask(RxInstanceUtils.observeChildrenBrio(container):Subscribe(function(brio)
483
+ if brio:IsDead() then
484
+ return
485
+ end
486
+
487
+ local maid, child = brio:ToMaidAndValue()
488
+
489
+ if child:IsA("PackageLink") then
490
+ return
491
+ end
492
+
493
+ -- The server moves a container's templates in here, but they are still its children
494
+ if child:IsA("Camera") and child.Name == PREVENT_REPLICATION_CAMERA_NAME then
495
+ self:_addChildTemplateNames(maid, child, countingMap)
496
+ return
497
+ end
498
+
499
+ maid:GiveTask(RxInstanceUtils.observeProperty(child, "Name")
500
+ :Pipe({
501
+ Rx.map(function()
502
+ return self:_getTemplateNameFromInstance(child)
503
+ end),
504
+ Rx.distinct() :: any,
505
+ } :: any)
506
+ :Subscribe(function(templateName)
507
+ maid._currentTemplateName = countingMap:Add(templateName)
508
+ end))
509
+ end))
510
+ end
511
+
512
+ function TemplateProvider._getTemplateNameFromInstance(_self: TemplateProvider, instance: Instance): string
513
+ local name = instance.Name
514
+
515
+ if instance:GetAttribute(TOMBSTONE_ID_ATTRIBUTE) then
516
+ if String.endsWith(name, TOMBSTONE_NAME_POSTFIX_UNLOADED) then
517
+ return String.removePostfix(name, TOMBSTONE_NAME_POSTFIX_UNLOADED)
518
+ elseif String.endsWith(name, TOMBSTONE_NAME_POSTFIX_LOADED) then
519
+ return String.removePostfix(name, TOMBSTONE_NAME_POSTFIX_LOADED)
520
+ end
521
+ end
522
+
523
+ return name
524
+ end
525
+
322
526
  --[=[
323
527
  Returns the raw template
324
528