@quenty/contentproviderutils 6.3.0 → 6.4.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,17 @@
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
+ # [6.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/contentproviderutils@6.3.0...@quenty/contentproviderutils@6.4.0) (2023-03-06)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add ImageLabelLoaded ([b24af97](https://github.com/Quenty/NevermoreEngine/commit/b24af97b56fb11edd8577598a44bbcacdb31e98e))
12
+
13
+
14
+
15
+
16
+
6
17
  # [6.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/contentproviderutils@6.2.0...@quenty/contentproviderutils@6.3.0) (2023-03-05)
7
18
 
8
19
  **Note:** Version bump only for package @quenty/contentproviderutils
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/contentproviderutils",
3
- "version": "6.3.0",
3
+ "version": "6.4.0",
4
4
  "description": "Utility functions to ensure that content is preloaded (wrapping calls in promises)",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -27,11 +27,16 @@
27
27
  "Quenty"
28
28
  ],
29
29
  "dependencies": {
30
+ "@quenty/baseobject": "^6.2.0",
31
+ "@quenty/instanceutils": "^7.8.0",
30
32
  "@quenty/loader": "^6.2.0",
31
- "@quenty/promise": "^6.3.0"
33
+ "@quenty/maid": "^2.5.0",
34
+ "@quenty/promise": "^6.3.0",
35
+ "@quenty/rx": "^7.7.0",
36
+ "@quenty/signal": "^2.3.0"
32
37
  },
33
38
  "publishConfig": {
34
39
  "access": "public"
35
40
  },
36
- "gitHead": "e62b5eac2e5d9084ab9083f128452dd2d98b6f1a"
41
+ "gitHead": "ce54e6f72519719e6155643418f2496f30a55f8e"
37
42
  }
@@ -0,0 +1,144 @@
1
+ --[=[
2
+ @class ImageLabelLoaded
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local BaseObject = require("BaseObject")
8
+ local Promise = require("Promise")
9
+ local Signal = require("Signal")
10
+ local Maid = require("Maid")
11
+ local ContentProviderUtils = require("ContentProviderUtils")
12
+ local RxInstanceUtils = require("RxInstanceUtils")
13
+ local Rx = require("Rx")
14
+
15
+ local ImageLabelLoaded = setmetatable({}, BaseObject)
16
+ ImageLabelLoaded.ClassName = "ImageLabelLoaded"
17
+ ImageLabelLoaded.__index = ImageLabelLoaded
18
+
19
+ function ImageLabelLoaded.new()
20
+ local self = setmetatable(BaseObject.new(), ImageLabelLoaded)
21
+
22
+ self._isLoaded = Instance.new("BoolValue")
23
+ self._isLoaded.Value = false
24
+ self._maid:GiveTask(self._isLoaded)
25
+
26
+ self._defaultTimeout = 1
27
+
28
+ self._preloadImage = Instance.new("BoolValue")
29
+ self._preloadImage.Value = true
30
+ self._maid:GiveTask(self._preloadImage)
31
+
32
+ self.ImageChanged = Signal.new()
33
+ self._maid:GiveTask(self.ImageChanged)
34
+
35
+ return self
36
+ end
37
+
38
+ function ImageLabelLoaded:SetDefaultTimeout(defaultTimeout)
39
+ assert(type(defaultTimeout) == "number" or defaultTimeout == nil, "Bad defaultTimeout")
40
+
41
+ self._defaultTimeout = defaultTimeout
42
+ end
43
+
44
+ function ImageLabelLoaded:IsLoaded()
45
+ return self._isLoaded.Value
46
+ end
47
+
48
+ function ImageLabelLoaded:SetPreloadImage(preloadImage)
49
+ assert(type(preloadImage) == "boolean", "Bad preloadImage")
50
+
51
+ self._preloadImage.Value = preloadImage
52
+ end
53
+
54
+ function ImageLabelLoaded:PromiseLoaded(timeout)
55
+ assert(type(timeout) == "number" or timeout == nil, "Bad timeout")
56
+
57
+ local originalTimeout = timeout
58
+ timeout = timeout or self._defaultTimeout
59
+
60
+ if self._isLoaded.Value then
61
+ return Promise.resolved()
62
+ end
63
+
64
+ local promise = Promise.new()
65
+
66
+ local maid = Maid.new()
67
+ self._maid[promise] = maid
68
+
69
+ maid:GiveTask(self._isLoaded.Changed:Connect(function()
70
+ if self._isLoaded.Value then
71
+ promise:Resolve()
72
+ end
73
+ end))
74
+
75
+ maid:GiveTask(self.ImageChanged:Connect(function(isVisible)
76
+ if not isVisible then
77
+ promise:Reject()
78
+ end
79
+ end))
80
+
81
+ if timeout then
82
+ maid:GiveTask(task.delay(timeout, function()
83
+ if originalTimeout then
84
+ promise:Reject("[ImageLabelLoaded] - Failed to load image after default timeout time")
85
+ else
86
+ promise:Reject()
87
+ end
88
+ end))
89
+ end
90
+
91
+ promise:Finally(function()
92
+ self._maid[promise] = nil
93
+ end)
94
+
95
+ return promise
96
+ end
97
+
98
+ function ImageLabelLoaded:SetImageLabel(imageLabel)
99
+ assert(typeof(imageLabel) == "Instance" and imageLabel:IsA("ImageLabel") or imageLabel == nil, "Bad imageLabel")
100
+ if self._imageLabel == imageLabel then
101
+ return
102
+ end
103
+
104
+ local maid = Maid.new()
105
+
106
+ self._imageLabel = imageLabel
107
+
108
+ if self._imageLabel then
109
+ self._isLoaded.Value = self._imageLabel.IsLoaded
110
+
111
+ maid:GiveTask(self._imageLabel:GetPropertyChangedSignal("IsLoaded"):Connect(function()
112
+ self._isLoaded.Value = self._imageLabel.IsLoaded
113
+ end))
114
+
115
+ -- Setup preloading as necessary
116
+ maid:GiveTask(RxInstanceUtils.observeProperty(self._preloadImage, "Value"):Pipe({
117
+ Rx.switchMap(function(preload)
118
+ if preload then
119
+ return Rx.combineLatest({
120
+ isLoaded = RxInstanceUtils.observeProperty(self._isLoaded, "Value");
121
+ image = RxInstanceUtils.observeProperty(self._imageLabel, "Image");
122
+ })
123
+ else
124
+ return Rx.EMPTY
125
+ end
126
+ end);
127
+ }):Subscribe(function(state)
128
+ if not state.isLoaded and state.image ~= "" then
129
+ maid:GivePromise(ContentProviderUtils.promisePreload({self._imageLabel}))
130
+ :Then(function()
131
+ self._isLoaded.Value = true
132
+ end)
133
+ end
134
+ end))
135
+ else
136
+ self._isLoaded.Value = false
137
+ end
138
+
139
+ self.ImageChanged:Fire()
140
+
141
+ self._maid._imageLabelMaid = maid
142
+ end
143
+
144
+ return ImageLabelLoaded