| 1 |
import { getActivePlugins, processPlaceholders } from '@page-creator/api/WPApi'; |
| 2 |
import { recordPluginActivity } from '@shared/api/DataApi'; |
| 3 |
|
| 4 |
export const processPatterns = async (patterns) => { |
| 5 |
const maxAttempts = 3; |
| 6 |
const delay = 1000; // 1 second delay between retries |
| 7 |
|
| 8 |
const activePlugins = |
| 9 |
(await getActivePlugins())?.data?.map((path) => path.split('/')[0]) || []; |
| 10 |
|
| 11 |
const pluginsActivity = patterns |
| 12 |
.filter((p) => p.pluginDependency) |
| 13 |
.map((p) => p.pluginDependency) |
| 14 |
.filter((p) => !activePlugins.includes(p)); |
| 15 |
|
| 16 |
for (const plugin of pluginsActivity) { |
| 17 |
recordPluginActivity({ |
| 18 |
slug: plugin, |
| 19 |
source: 'page-creator', |
| 20 |
}); |
| 21 |
} |
| 22 |
|
| 23 |
for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
| 24 |
try { |
| 25 |
return await processPlaceholders(patterns); |
| 26 |
} catch (error) { |
| 27 |
if (attempt === maxAttempts) { |
| 28 |
console.error( |
| 29 |
`Failed to process patterns after ${maxAttempts} attempts:`, |
| 30 |
error, |
| 31 |
); |
| 32 |
return patterns; |
| 33 |
} |
| 34 |
await new Promise((resolve) => setTimeout(resolve, delay)); |
| 35 |
} |
| 36 |
} |
| 37 |
}; |
| 38 |
|