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