Skip to content

Commit 1e7b360

Browse files
committed
MOBILE-925 filepool: Allow using strings as componentId
1 parent 15e3259 commit 1e7b360

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

www/core/lib/filepool.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ angular.module('mm.core')
222222
* @param {String} siteId The site ID.
223223
* @param {String} fileId The file ID.
224224
* @param {String} component The component to link the file to.
225-
* @param {Number} [componentId] An ID to use in conjunction with the component.
225+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
226226
* @return {Promise} Resolved on success. Rejected on failure. It is advised to silently ignore failures.
227227
* @protected
228228
*/
@@ -250,7 +250,7 @@ angular.module('mm.core')
250250
* @param {String} siteId The site ID.
251251
* @param {String} fileUrl The file Url.
252252
* @param {String} component The component to link the file to.
253-
* @param {Number} [componentId] An ID to use in conjunction with the component.
253+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
254254
* @return {Promise} Resolved on success. Rejected on failure. It is advised to silently ignore failures.
255255
* @description
256256
* Use this method to create a link between a URL and a component. You usually do not need to call
@@ -317,7 +317,7 @@ angular.module('mm.core')
317317
* @param {String} siteId The site ID.
318318
* @param {String} fileUrl The absolute URL to the file.
319319
* @param {String} [component] The component to link the file to.
320-
* @param {Number} [componentId] An ID to use in conjunction with the component (optional).
320+
* @param {Mixed} [componentId] An ID to use in conjunction with the component (optional).
321321
* @param {Number} [timemodified=0] The time this file was modified. Can be used to check file state.
322322
* @param {String} [filePath] Filepath to download the file to.
323323
* @param {Number} [priority=0] The priority this file should get in the queue (range 0-999).
@@ -351,7 +351,7 @@ angular.module('mm.core')
351351
if (typeof component !== 'undefined') {
352352
link = {
353353
component: component,
354-
componentId: componentId
354+
componentId: self._fixComponentId(componentId)
355355
};
356356
}
357357

@@ -518,7 +518,7 @@ angular.module('mm.core')
518518
* @name $mmFilepool#componentHasFiles
519519
* @param {String} siteId The site ID.
520520
* @param {String} component The component to link the file to.
521-
* @param {Number} [componentId] An ID to use in conjunction with the component.
521+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
522522
* @return {Promise} Resolved means yes, rejected means no.
523523
*/
524524
self.componentHasFiles = function(siteId, component, componentId) {
@@ -589,7 +589,7 @@ angular.module('mm.core')
589589
* @param {Object[]} fileList List of files to download.
590590
* @param {Boolean} prefetch True if should prefetch the contents (queue), false if they should be downloaded right now.
591591
* @param {String} component The component to link the file to.
592-
* @param {Number} [componentId] An ID to use in conjunction with the component.
592+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
593593
* @param {Number} [revision] Package's revision. If not defined, it will be calculated using the list of files.
594594
* @param {Number} [timemod] Package's timemodified. If not defined, it will be calculated using the list of files.
595595
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
@@ -687,7 +687,7 @@ angular.module('mm.core')
687687
* @param {String} siteId The site ID.
688688
* @param {Object[]} fileList List of files to download.
689689
* @param {String} component The component to link the file to.
690-
* @param {Number} componentId An ID to identify the download. Must be unique.
690+
* @param {Mixed} componentId An ID to identify the download. Must be unique.
691691
* @param {Number} [revision] Package's revision. If not defined, it will be calculated using the list of files.
692692
* @param {Number} [timemodified] Package's timemodified. If not defined, it will be calculated using the list of files.
693693
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
@@ -708,7 +708,7 @@ angular.module('mm.core')
708708
* @param {String} fileUrl The file URL.
709709
* @param {Boolean} [ignoreStale] True if 'stale' should be ignored.
710710
* @param {String} component The component to link the file to.
711-
* @param {Number} [componentId] An ID to use in conjunction with the component.
711+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
712712
* @param {Number} [timemodified=0] The time this file was modified. Can be used to check file state.
713713
* @param {String} [filePath] Filepath to download the file to.
714714
* @return {Promise} Resolved with internal URL on success, rejected otherwise.
@@ -859,14 +859,20 @@ angular.module('mm.core')
859859
* @module mm.core
860860
* @ngdoc method
861861
* @name $mmFilepool#_fixComponentId
862-
* @param {String|Number|undefined} The component ID.
862+
* @param {Mixed} componentId The component ID.
863863
* @return {Number} The normalised component ID. -1 when undefined was passed.
864864
* @protected
865865
*/
866866
self._fixComponentId = function(componentId) {
867+
// Check if it's a number.
867868
var id = parseInt(componentId, 10);
868869
if (isNaN(id)) {
869-
return -1;
870+
// Not a number.
871+
if (typeof componentId == 'undefined' || componentId === null) {
872+
return -1;
873+
} else {
874+
return componentId;
875+
}
870876
}
871877
return id;
872878
};
@@ -960,7 +966,7 @@ angular.module('mm.core')
960966
* @name $mmFilepool#getPackageDownloadPromise
961967
* @param {String} siteId Site ID.
962968
* @param {String} component The component of the package.
963-
* @param {Number} [componentId] An ID to use in conjunction with the component.
969+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
964970
* @return {String} Download promise or undefined.
965971
*/
966972
self.getPackageDownloadPromise = function(siteId, component, componentId) {
@@ -977,7 +983,7 @@ angular.module('mm.core')
977983
* @ngdoc method
978984
* @name $mmFilepool#getPackageId
979985
* @param {String} component Package's component.
980-
* @param {Number} [componentId] An ID to use in conjunction with the component.
986+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
981987
* @return {String} Package ID.
982988
*/
983989
self.getPackageId = function(component, componentId) {
@@ -992,7 +998,7 @@ angular.module('mm.core')
992998
* @name $mmFilepool#getPackagePreviousStatus
993999
* @param {String} siteId Site ID.
9941000
* @param {String} component Package's component.
995-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1001+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
9961002
* @return {Promise} Promise resolved with the status.
9971003
*/
9981004
self.getPackagePreviousStatus = function(siteId, component, componentId) {
@@ -1015,14 +1021,16 @@ angular.module('mm.core')
10151021
* @name $mmFilepool#getPackageStatus
10161022
* @param {String} siteId Site ID.
10171023
* @param {String} component Package's component.
1018-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1024+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
10191025
* @param {Number|String} [revision=0] Package's revision.
10201026
* @param {Number} [timemodified=0] Package's timemodified.
10211027
* @return {Promise} Promise resolved with the status.
10221028
*/
10231029
self.getPackageStatus = function(siteId, component, componentId, revision, timemodified) {
10241030
revision = revision || 0;
10251031
timemodified = timemodified || 0;
1032+
componentId = self._fixComponentId(componentId);
1033+
10261034
return $mmSitesManager.getSite(siteId).then(function(site) {
10271035
var db = site.getDb(),
10281036
packageId = self.getPackageId(component, componentId);
@@ -1065,7 +1073,7 @@ angular.module('mm.core')
10651073
* @name $mmFilepool#getPackageTimemodified
10661074
* @param {String} siteId Site ID.
10671075
* @param {String} component Package's component.
1068-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1076+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
10691077
* @return {Promise} Promise resolved with the timemodified.
10701078
*/
10711079
self.getPackageTimemodified = function(siteId, component, componentId) {
@@ -1291,7 +1299,7 @@ angular.module('mm.core')
12911299
* @param {String} fileUrl The absolute URL to the file.
12921300
* @param {String} [mode=url] The type of URL to return. Accepts 'url' or 'src'.
12931301
* @param {String} component The component to link the file to.
1294-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1302+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
12951303
* @param {Number} [timemodified=0] The time this file was modified.
12961304
* @param {Boolean} [checkSize=true] True if we shouldn't download files if their size is big, false otherwise.
12971305
* @return {Promise} Resolved with the URL to use. When rejected, nothing could be done.
@@ -1642,7 +1650,7 @@ angular.module('mm.core')
16421650
* @param {String} siteId The site ID.
16431651
* @param {String} fileUrl The absolute URL to the file.
16441652
* @param {String} component The component to link the file to.
1645-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1653+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
16461654
* @param {Number} [timemodified] The time this file was modified.
16471655
* @param {Boolean} [checkSize=true] True if we shouldn't download files if their size is big, false otherwise.
16481656
* @return {Promise} Resolved with the URL to use. When rejected, nothing could be done,
@@ -1686,7 +1694,7 @@ angular.module('mm.core')
16861694
* @param {String} siteId The site ID.
16871695
* @param {String} fileUrl The absolute URL to the file.
16881696
* @param {String} component The component to link the file to.
1689-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1697+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
16901698
* @param {Number} [timemodified] The time this file was modified.
16911699
* @param {Boolean} [checkSize=true] True if we shouldn't download files if their size is big, false otherwise.
16921700
* @return {Promise} Resolved with the URL to use. When rejected, nothing could be done,
@@ -1816,7 +1824,7 @@ angular.module('mm.core')
18161824
* @name $mmFilepool#invalidateFilesByComponent
18171825
* @param {String} siteId The site ID.
18181826
* @param {String} component The component to link the file to.
1819-
* @param {Number} [componentId] An ID to use in conjunction with the component.
1827+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
18201828
* @return {Promise} Resolved on success. Rejected on failure. It is advised to ignore a failure.
18211829
* @description
18221830
* Invalidates a file by marking it stale. See {@link $mmFilepool#invalidateFileByUrl} for more details.
@@ -1918,7 +1926,7 @@ angular.module('mm.core')
19181926
* @param {String} siteId The site ID.
19191927
* @param {Object[]} fileList List of files to download.
19201928
* @param {String} component The component to link the file to.
1921-
* @param {Number} componentId An ID to identify the download. Must be unique.
1929+
* @param {Mixed} componentId An ID to identify the download. Must be unique.
19221930
* @param {Number} [revision] Package's revision. If not defined, it will be calculated using the list of files.
19231931
* @param {Number} [timemodified] Package's timemodified. If not defined, it will be calculated using the list of files.
19241932
* @param {String} [dirPath] Name of the directory where to store the files (inside filepool dir). If not defined, store
@@ -2175,7 +2183,7 @@ angular.module('mm.core')
21752183
* @name $mmFilepool#removeFilesByComponent
21762184
* @param {String} siteId The site ID.
21772185
* @param {String} component The component to link the file to.
2178-
* @param {Number} [componentId] An ID to use in conjunction with the component.
2186+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
21792187
* @return {Promise} Resolved on success. Rejected on failure.
21802188
*/
21812189
self.removeFilesByComponent = function(siteId, component, componentId) {
@@ -2297,11 +2305,13 @@ angular.module('mm.core')
22972305
* @name $mmFilepool#setPackagePreviousStatus
22982306
* @param {String} siteId Site ID.
22992307
* @param {String} component Package's component.
2300-
* @param {Number} [componentId] An ID to use in conjunction with the component.
2308+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
23012309
* @return {Promise} Promise resolved when the status is changed. Resolve param: new status.
23022310
*/
23032311
self.setPackagePreviousStatus = function(siteId, component, componentId) {
23042312
$log.debug('Set previous status for package ' + component + ' ' + componentId);
2313+
componentId = self._fixComponentId(componentId);
2314+
23052315
return $mmSitesManager.getSite(siteId).then(function(site) {
23062316
var db = site.getDb(),
23072317
packageId = self.getPackageId(component, componentId);
@@ -2360,14 +2370,15 @@ angular.module('mm.core')
23602370
* @name $mmFilepool#storePackageStatus
23612371
* @param {String} siteId Site ID.
23622372
* @param {String} component Package's component.
2363-
* @param {Number} [componentId] An ID to use in conjunction with the component.
2373+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
23642374
* @param {String} status New package status.
23652375
* @param {Number} [revision] Package's revision. If not provided, try to use the current value.
23662376
* @param {Number} [timemodified] Package's timemodified. If not provided, try to use the current value.
23672377
* @return {Promise} Promise resolved when status is stored.
23682378
*/
23692379
self.storePackageStatus = function(siteId, component, componentId, status, revision, timemodified) {
23702380
$log.debug('Set status \'' + status + '\' for package ' + component + ' ' + componentId);
2381+
componentId = self._fixComponentId(componentId);
23712382

23722383
return $mmSitesManager.getSite(siteId).then(function(site) {
23732384
var db = site.getDb(),
@@ -2445,7 +2456,7 @@ angular.module('mm.core')
24452456
* @name $mmFilepool#_triggerPackageStatusChanged
24462457
* @param {String} siteId Site ID.
24472458
* @param {String} component Package's component.
2448-
* @param {Number} [componentId] An ID to use in conjunction with the component.
2459+
* @param {Mixed} [componentId] An ID to use in conjunction with the component.
24492460
* @param {String} status New package status.
24502461
* @return {Void}
24512462
* @protected
@@ -2454,7 +2465,7 @@ angular.module('mm.core')
24542465
var data = {
24552466
siteid: siteId,
24562467
component: component,
2457-
componentId: componentId,
2468+
componentId: self._fixComponentId(componentId),
24582469
status: status
24592470
};
24602471
$mmEvents.trigger(mmCoreEventPackageStatusChanged, data);

0 commit comments

Comments
 (0)