( store: WorkStore | undefined, callback: () => Promise<T> )
| 4 | |
| 5 | /** Run a callback, and execute any *new* revalidations added during its runtime. */ |
| 6 | export async function withExecuteRevalidates<T>( |
| 7 | store: WorkStore | undefined, |
| 8 | callback: () => Promise<T> |
| 9 | ): Promise<T> { |
| 10 | if (!store) { |
| 11 | return callback() |
| 12 | } |
| 13 | // If we executed any revalidates during the request, then we don't want to execute them again. |
| 14 | // save the state so we can check if anything changed after we're done running callbacks. |
| 15 | const savedRevalidationState = cloneRevalidationState(store) |
| 16 | try { |
| 17 | return await callback() |
| 18 | } finally { |
| 19 | // Check if we have any new revalidates, and if so, wait until they are all resolved. |
| 20 | const newRevalidates = diffRevalidationState( |
| 21 | savedRevalidationState, |
| 22 | cloneRevalidationState(store) |
| 23 | ) |
| 24 | await executeRevalidates(store, newRevalidates) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | type RevalidationState = Required< |
| 29 | Pick< |
no test coverage detected