| 1 |
import { pingServer } from '@shared/api/DataApi'; |
| 2 |
|
| 3 |
export const isOnLaunch = () => { |
| 4 |
const q = new URLSearchParams(window.location.search); |
| 5 |
return ['page'].includes(q.get('extendify-launch')); |
| 6 |
}; |
| 7 |
|
| 8 |
export const deepMerge = (target, ...sources) => { |
| 9 |
return sources.reduce((acc, source) => { |
| 10 |
if (!isObject(acc) || !isObject(source)) { |
| 11 |
return null; |
| 12 |
} |
| 13 |
|
| 14 |
const newTarget = { ...acc }; |
| 15 |
|
| 16 |
for (const key in source) { |
| 17 |
if (isObject(source[key]) && key in newTarget) { |
| 18 |
newTarget[key] = deepMerge(newTarget[key], source[key]); |
| 19 |
} else { |
| 20 |
newTarget[key] = source[key]; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
return newTarget; |
| 25 |
}, target); |
| 26 |
}; |
| 27 |
|
| 28 |
export const isObject = (value) => { |
| 29 |
return typeof value === 'object' && !Array.isArray(value) && value !== null; |
| 30 |
}; |
| 31 |
|
| 32 |
export const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); |
| 33 |
|
| 34 |
export const wasPluginInstalled = (activePlugins, pluginSlug) => |
| 35 |
activePlugins?.filter((p) => p.includes(pluginSlug))?.length; |
| 36 |
|
| 37 |
/** |
| 38 |
* Will ping every 1s until we get a 200 response from the server. |
| 39 |
* This is used because we were dealing with a particular issue where |
| 40 |
* servers we're very resource limited and rate limiting was common. |
| 41 |
* */ |
| 42 |
export const waitFor200Response = async () => { |
| 43 |
try { |
| 44 |
// This will error if not 200 |
| 45 |
await pingServer(); |
| 46 |
return true; |
| 47 |
} catch (error) { |
| 48 |
// Do nothing |
| 49 |
} |
| 50 |
await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 51 |
return waitFor200Response(); |
| 52 |
}; |
| 53 |
|
| 54 |
export const retryOperation = async (operation, { maxAttempts = 1 }) => { |
| 55 |
for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
| 56 |
try { |
| 57 |
await waitFor200Response(); |
| 58 |
await operation(); |
| 59 |
break; |
| 60 |
} catch (error) { |
| 61 |
if (attempt === maxAttempts) { |
| 62 |
throw error; |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
}; |
| 67 |
|