* @param {Express} app * @param {string} name * @param {string} route * @param {*} context * @param {ContextCache[]} dependantCaches * @returns
(app, name, route, context, dependantCaches)
| 53 | * @returns |
| 54 | */ |
| 55 | function createRoute(app, name, route, context, dependantCaches) { |
| 56 | const cache = new ContextCache(context); |
| 57 | app.get(route, async function (req, res, next) { |
| 58 | const fileName = path.basename(req.originalUrl); |
| 59 | |
| 60 | // Multiple files may be requested at this path, calling this function in quick succession. |
| 61 | // Await the previous build before re-building again. |
| 62 | try { |
| 63 | await cache.promise; |
| 64 | } catch { |
| 65 | // Error is reported upstream |
| 66 | } |
| 67 | |
| 68 | if (!cache.isBuilt()) { |
| 69 | try { |
| 70 | const start = performance.now(); |
| 71 | if (dependantCaches) { |
| 72 | await Promise.all( |
| 73 | dependantCaches.map((dependantCache) => { |
| 74 | if (!dependantCache.isBuilt()) { |
| 75 | return dependantCache.rebuild(); |
| 76 | } |
| 77 | }), |
| 78 | ); |
| 79 | } |
| 80 | await cache.rebuild(); |
| 81 | console.log( |
| 82 | `Built ${name} in ${formatTimeSinceInSeconds(start)} seconds.`, |
| 83 | ); |
| 84 | } catch (e) { |
| 85 | next(e); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | const result = /** @type {BuildResult} */ (cache.result); |
| 90 | return serveResult(result, fileName, res, next); |
| 91 | }); |
| 92 | |
| 93 | return cache; |
| 94 | } |
| 95 | |
| 96 | export default createRoute; |
no test coverage detected
searching dependent graphs…