Skip to content

Commit 6304c5b

Browse files
Add support for async values and function responses (#1916)
* Add support for async values and function responses * Fix up const * Fix up test * Update test typing
1 parent 1ed22d0 commit 6304c5b

File tree

2 files changed

+349
-5
lines changed

2 files changed

+349
-5
lines changed

injected/src/utils.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,16 @@ const functionMap = {
323323
* @typedef {object} ConfigSetting
324324
* @property {'undefined' | 'number' | 'string' | 'function' | 'boolean' | 'null' | 'array' | 'object'} type
325325
* @property {string} [functionName]
326-
* @property {boolean | string | number} value
326+
* @property {*} [value] - Any value type (string, number, boolean, object, array, null, undefined)
327+
* @property {ConfigSetting} [functionValue] - For function type, the value to return from the function
328+
* @property {boolean} [async] - Whether to wrap the value in a Promise
327329
* @property {object} [criteria]
328-
* @property {string} criteria.arch
330+
* @property {string} [criteria.arch]
329331
*/
330332

331333
/**
332334
* Processes a structured config setting and returns the value according to its type
333-
* @param {ConfigSetting} configSetting
335+
* @param {ConfigSetting | ConfigSetting[]} configSetting
334336
* @param {*} [defaultValue]
335337
* @returns
336338
*/
@@ -343,10 +345,12 @@ export function processAttr(configSetting, defaultValue) {
343345
switch (configSettingType) {
344346
case 'object':
345347
if (Array.isArray(configSetting)) {
346-
configSetting = processAttrByCriteria(configSetting);
347-
if (configSetting === undefined) {
348+
const selectedSetting = processAttrByCriteria(configSetting);
349+
if (selectedSetting === undefined) {
348350
return defaultValue;
349351
}
352+
// Now process the selected setting as a single ConfigSetting
353+
return processAttr(selectedSetting, defaultValue);
350354
}
351355

352356
if (!configSetting.type) {
@@ -357,12 +361,22 @@ export function processAttr(configSetting, defaultValue) {
357361
if (configSetting.functionName && functionMap[configSetting.functionName]) {
358362
return functionMap[configSetting.functionName];
359363
}
364+
if (configSetting.functionValue) {
365+
const functionValue = configSetting.functionValue;
366+
// Return a function that processes the functionValue using processAttr
367+
return () => processAttr(functionValue, undefined);
368+
}
360369
}
361370

362371
if (configSetting.type === 'undefined') {
363372
return undefined;
364373
}
365374

375+
// Handle async wrapping for all types including arrays
376+
if (configSetting.async) {
377+
return DDGPromise.resolve(configSetting.value);
378+
}
379+
366380
// All JSON expressable types are handled here
367381
return configSetting.value;
368382
default:

0 commit comments

Comments
 (0)