@quenty/templateprovider 3.1.0 → 3.2.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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,25 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.2.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/templateprovider@3.2.0...@quenty/templateprovider@3.2.1) (2021-10-06)
7
+
8
+ **Note:** Version bump only for package @quenty/templateprovider
9
+
10
+
11
+
12
+
13
+
14
+ # [3.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/templateprovider@3.1.0...@quenty/templateprovider@3.2.0) (2021-10-02)
15
+
16
+
17
+ ### Features
18
+
19
+ * Allow template provider to replicate assets from the cloud ([dbfab97](https://github.com/Quenty/NevermoreEngine/commit/dbfab97070945c19fc0d9fb8029db54d150f1a53))
20
+
21
+
22
+
23
+
24
+
6
25
  # [3.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/templateprovider@3.0.1...@quenty/templateprovider@3.1.0) (2021-09-22)
7
26
 
8
27
 
package/README.md CHANGED
@@ -21,10 +21,14 @@ npm install @quenty/templateprovider --save
21
21
  ## Usage
22
22
  Usage is designed to be simple.
23
23
 
24
- ### `TemplateProvider.new(container)`
24
+ ### `TemplateProvider.new(container, replicationParent)`
25
+
26
+ If `replicationParent` is given then contents loaded from the cloud will be replicated to the replicationParent when on the server.
25
27
 
26
28
  ### `TemplateProvider:Init()`
27
29
 
30
+ Initializes the template provider, downloading components and other things needed
31
+
28
32
  ### `TemplateProvider:Clone(templateName)`
29
33
 
30
34
  ### `TemplateProvider:Get(templateName)`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/templateprovider",
3
- "version": "3.1.0",
3
+ "version": "3.2.1",
4
4
  "description": "Base of a template retrieval system",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -25,13 +25,15 @@
25
25
  "Quenty"
26
26
  ],
27
27
  "dependencies": {
28
- "@quenty/baseobject": "^3.0.1",
29
- "@quenty/loader": "^3.0.1",
30
- "@quenty/maid": "^2.0.0",
28
+ "@quenty/baseobject": "^3.1.1",
29
+ "@quenty/insertserviceutils": "^3.1.1",
30
+ "@quenty/loader": "^3.1.0",
31
+ "@quenty/maid": "^2.0.1",
32
+ "@quenty/promise": "^3.1.1",
31
33
  "@quenty/string": "^2.1.0"
32
34
  },
33
35
  "publishConfig": {
34
36
  "access": "public"
35
37
  },
36
- "gitHead": "6288c63115f31544fce6663d018826e81ef01c7f"
38
+ "gitHead": "8d5773447b4fd5d07e1714d2cc2421771a260d32"
37
39
  }
@@ -8,30 +8,43 @@ local StarterGui = game:GetService("StarterGui")
8
8
 
9
9
  local Maid = require("Maid")
10
10
  local String = require("String")
11
+ local InsertServiceUtils = require("InsertServiceUtils")
12
+ local Promise = require("Promise")
11
13
 
12
14
  local TemplateProvider = {}
13
15
  TemplateProvider.ClassName = "TemplateProvider"
14
16
  TemplateProvider.__index = TemplateProvider
15
17
 
16
18
  -- @param[opt=nil] container
17
- function TemplateProvider.new(container)
19
+ function TemplateProvider.new(container, replicationParent)
18
20
  local self = setmetatable({}, TemplateProvider)
19
21
 
20
- if typeof(container) == "Instance" then
21
- self._containersToInitializeSet = { [container] = true }
22
+ self._replicationParent = replicationParent
23
+ self._containersToInitializeSet = {}
24
+
25
+ if typeof(container) == "Instance" or type(container) == "number" then
26
+ self._containersToInitializeSet[container] = true
22
27
  elseif typeof(container) == "table" then
23
- self._containersToInitializeSet = {}
24
28
  for _, item in pairs(container) do
25
- assert(typeof(item) == "Instance", "Bad item in initialization set")
29
+ assert(typeof(item) == "Instance" or type(item) == "number", "Bad item in initialization set")
26
30
  self._containersToInitializeSet[item] = true
27
31
 
28
32
  -- For easy debugging/iteration loop
29
- if item:IsDescendantOf(StarterGui) and item:IsA("ScreenGui") and RunService:IsRunning() then
33
+ if typeof(item) == "Instance"
34
+ and item:IsDescendantOf(StarterGui)
35
+ and item:IsA("ScreenGui")
36
+ and RunService:IsRunning() then
37
+
30
38
  item.Enabled = false
31
39
  end
32
40
  end
33
41
  end
34
42
 
43
+ -- Make sure to replicate our parent
44
+ if self._replicationParent then
45
+ self._containersToInitializeSet[self._replicationParent] = true
46
+ end
47
+
35
48
  return self
36
49
  end
37
50
 
@@ -42,13 +55,58 @@ function TemplateProvider:Init()
42
55
  self._maid = Maid.new()
43
56
  self._initialized = true
44
57
  self._registry = {} -- [name] = rawTemplate
45
- self._containersSet = {} -- [parent] = true
58
+ self._containersSet = {} -- [parentOrAssetId] = true
46
59
 
47
- if self._containersToInitializeSet then
48
- for container, _ in pairs(self._containersToInitializeSet) do
49
- self:AddContainer(container)
50
- end
60
+ self._promises = {} -- [name] = Promise
61
+
62
+ for container, _ in pairs(self._containersToInitializeSet) do
63
+ self:AddContainer(container)
64
+ end
65
+ end
66
+
67
+ function TemplateProvider:PromiseClone(templateName)
68
+ assert(type(templateName) == "string", "templateName must be a string")
69
+
70
+ self:_verifyInit()
71
+
72
+ local template = self._registry[templateName]
73
+ if template then
74
+ return Promise.resolved(self:Clone(templateName))
75
+ end
76
+
77
+ if not self._promises[templateName] then
78
+ local promise = Promise.new()
79
+ self._promises[templateName] = promise
80
+
81
+ -- Make sure to clean up the promise afterwards
82
+ self._maid[promise] = promise
83
+ promise:Then(function()
84
+ self._maid[promise] = nil
85
+ end)
86
+
87
+ delay(5, function()
88
+ if promise:IsPending() then
89
+ warn(("[TemplateProvider.PromiseClone] - May fail to replicate template %q from cloud. %s")
90
+ :format(templateName, self:_getReplicationHint()))
91
+ end
92
+ end)
51
93
  end
94
+
95
+ return self._promises[templateName]
96
+ :Then(function()
97
+ -- Get a new copy
98
+ return self:Clone(templateName)
99
+ end)
100
+ end
101
+
102
+ function TemplateProvider:_getReplicationHint()
103
+ local hint
104
+
105
+ if RunService:IsClient() then
106
+ hint = "Make sure the template provider is initialized on the server."
107
+ end
108
+
109
+ return hint
52
110
  end
53
111
 
54
112
  -- Clones the template. If it has a prefix of "Template" then it will remove it
@@ -78,12 +136,19 @@ end
78
136
 
79
137
  -- Adds a new container to the provider for provision of assets
80
138
  function TemplateProvider:AddContainer(container)
81
- assert(typeof(container) == "Instance", "Bad container")
82
- assert(not self._containersSet[container], "Already added")
139
+ assert(typeof(container) == "Instance" or type(container) == "number", "Bad container")
83
140
  self:_verifyInit()
84
141
 
85
- self._containersSet[container] = true
86
- self._maid[container] = self:_loadFolder(container)
142
+ if not self._containersSet[container] then
143
+ self._containersSet[container] = true
144
+ if type(container) == "number" then
145
+ self._maid[container] = self:_loadCloudAsset(container)
146
+ elseif typeof(container) == "Instance" then
147
+ self._maid[container] = self:_loadFolder(container)
148
+ else
149
+ error("Unknown container type to load")
150
+ end
151
+ end
87
152
  end
88
153
 
89
154
  function TemplateProvider:RemoveContainer(container)
@@ -136,6 +201,28 @@ function TemplateProvider:_verifyInit()
136
201
  assert(self._initialized, "TemplateProvider is not initialized")
137
202
  end
138
203
 
204
+ function TemplateProvider:_loadCloudAsset(assetId)
205
+ assert(type(assetId) == "number", "Bad assetId")
206
+ local maid = Maid.new()
207
+
208
+ -- Load on server
209
+ if RunService:IsServer() or not RunService:IsRunning() then
210
+ maid:GivePromise(InsertServiceUtils.promiseAsset(assetId)):Then(function(result)
211
+ if RunService:IsRunning() then
212
+ for _, item in pairs(result:GetChildren()) do
213
+ -- Replicate in children
214
+ item.Parent = self._replicationParent
215
+ end
216
+ else
217
+ -- Load without parenting
218
+ maid:GiveTask(self:_loadFolder(result))
219
+ end
220
+ end)
221
+ end
222
+
223
+ return maid
224
+ end
225
+
139
226
  function TemplateProvider:_transformParent(getParent)
140
227
  if typeof(getParent) == "Instance" then
141
228
  return getParent
@@ -198,6 +285,11 @@ function TemplateProvider:_addToRegistery(child)
198
285
  end
199
286
 
200
287
  self._registry[childName] = child
288
+
289
+ if self._promises[childName] then
290
+ self._promises[childName]:Resolve(child)
291
+ self._promises[childName] = nil
292
+ end
201
293
  end
202
294
 
203
295
  function TemplateProvider:_removeFromRegistry(child)