Skip to content

Commit 5029d01

Browse files
committed
feat: Allow user to specify processing wait and retry parameters
1 parent 6c67390 commit 5029d01

File tree

1 file changed

+35
-8
lines changed

1 file changed

+35
-8
lines changed

lib/entities/asset.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,42 @@ const ASSET_PROCESSING_CHECK_RETRIES = 5
3737
*/
3838

3939
function createAssetApi (http) {
40-
function checkIfAssetHasUrl (resolve, reject, id, locale, checkCount = 0) {
40+
function checkIfAssetHasUrl ({
41+
resolve,
42+
reject,
43+
id,
44+
locale,
45+
processingCheckWait = ASSET_PROCESSING_CHECK_WAIT,
46+
processingCheckRetries = ASSET_PROCESSING_CHECK_RETRIES,
47+
checkCount = 0
48+
}) {
4149
http.get('assets/' + id)
4250
.then((response) => wrapAsset(http, response.data), errorHandler)
4351
.then((asset) => {
4452
if (asset.fields.file[locale].url) {
4553
resolve(asset)
46-
} else if (checkCount === ASSET_PROCESSING_CHECK_RETRIES) {
54+
} else if (checkCount === processingCheckRetries) {
4755
const error = new Error()
4856
error.name = 'AssetProcessingTimeout'
4957
error.message = 'Asset is taking longer then expected to process.'
5058
reject(error)
5159
} else {
5260
checkCount++
5361
setTimeout(
54-
() => checkIfAssetHasUrl(resolve, reject, id, locale, checkCount),
55-
ASSET_PROCESSING_CHECK_WAIT
62+
() => checkIfAssetHasUrl({
63+
resolve: resolve,
64+
reject: reject,
65+
id: id,
66+
locale: locale,
67+
checkCount: checkCount
68+
}),
69+
processingCheckWait
5670
)
5771
}
5872
})
5973
}
6074

61-
function processForLocale (locale) {
75+
function processForLocale (locale, {processingCheckWait, processingCheckRetries} = {}) {
6276
const assetId = this.sys.id
6377
return http.put('assets/' + this.sys.id + '/files/' + locale + '/process', null, {
6478
headers: {
@@ -67,15 +81,22 @@ function createAssetApi (http) {
6781
})
6882
.then(() => {
6983
return new Promise(
70-
(resolve, reject) => checkIfAssetHasUrl(resolve, reject, assetId, locale)
84+
(resolve, reject) => checkIfAssetHasUrl({
85+
resolve: resolve,
86+
reject: reject,
87+
id: assetId,
88+
locale: locale,
89+
processingCheckWait: processingCheckWait,
90+
processingCheckRetries: processingCheckRetries
91+
})
7192
)
7293
}, errorHandler)
7394
}
7495

75-
function processForAllLocales () {
96+
function processForAllLocales (options = {}) {
7697
const self = this
7798
const locales = Object.keys(this.fields.file)
78-
return Promise.all(locales.map((locale) => processForLocale.call(self, locale)))
99+
return Promise.all(locales.map((locale) => processForLocale.call(self, locale, options)))
79100
.then((assets) => assets[0])
80101
}
81102

@@ -175,6 +196,9 @@ function createAssetApi (http) {
175196
* @memberof Asset
176197
* @func processForLocale
177198
* @param {string} locale - Locale which processing should be triggered for
199+
* @param {object} options - Additional options for processing
200+
* @prop {number} options.processingCheckWait - Time in milliseconds to wait before checking again if the asset has been processed (default: 500ms)
201+
* @prop {number} options.processingCheckRetries - Maximum amount of times to check if the asset has been processed (default: 5)
178202
* @return {Promise<Asset>} Object returned from the server with updated metadata.
179203
* @throws {AssetProcessingTimeout} If the asset takes too long to process. If this happens, retrieve the asset again, and if the url property is available, then processing has succeeded. If not, your file might be damaged.
180204
* @example
@@ -187,6 +211,9 @@ function createAssetApi (http) {
187211
* Triggers asset processing after an upload, for the files uploaded to all locales of an asset.
188212
* @memberof Asset
189213
* @func processForAllLocales
214+
* @param {object} options - Additional options for processing
215+
* @prop {number} options.processingCheckWait - Time in milliseconds to wait before checking again if the asset has been processed (default: 500ms)
216+
* @prop {number} options.processingCheckRetries - Maximum amount of times to check if the asset has been processed (default: 5)
190217
* @return {Promise<Asset>} Object returned from the server with updated metadata.
191218
* @throws {AssetProcessingTimeout} If the asset takes too long to process. If this happens, retrieve the asset again, and if the url property is available, then processing has succeeded. If not, your file might be damaged.
192219
* @example

0 commit comments

Comments
 (0)