Skip to content

Commit

Permalink
run prettier on scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jjspace committed Aug 28, 2024
1 parent dd3f943 commit 3dff10d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 76 deletions.
48 changes: 24 additions & 24 deletions scripts/ContextCache.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
class ContextCache {
constructor(context) {
this.context = context;
this.promise = Promise.resolve();
this.result = undefined;
}

clear() {
this.result = undefined;
}

async rebuild() {
const promise = (this.promise = this.context.rebuild());
const result = (this.result = await promise);
return result;
}

isBuilt() {
return (
this.result &&
this.result.outputFiles &&
this.result.outputFiles.length > 0
);
}
constructor(context) {
this.context = context;
this.promise = Promise.resolve();
this.result = undefined;
}

export default ContextCache;
clear() {
this.result = undefined;
}

async rebuild() {
const promise = (this.promise = this.context.rebuild());
const result = (this.result = await promise);
return result;
}

isBuilt() {
return (
this.result &&
this.result.outputFiles &&
this.result.outputFiles.length > 0
);
}
}

export default ContextCache;
106 changes: 54 additions & 52 deletions scripts/createRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,67 @@ import ContextCache from "./ContextCache.js";
import path from "path";

function formatTimeSinceInSeconds(start) {
return Math.ceil((performance.now() - start) / 100) / 10;
return Math.ceil((performance.now() - start) / 100) / 10;
}

function serveResult (result, fileName, res, next) {
let bundle, error;
try {
for (const out of result.outputFiles) {
if (path.basename(out.path) === fileName) {
bundle = out.text;
}
function serveResult(result, fileName, res, next) {
let bundle, error;
try {
for (const out of result.outputFiles) {
if (path.basename(out.path) === fileName) {
bundle = out.text;
}
} catch(e) {
error = e;
}

if (!bundle) {
next(new Error(`Failed to generate bundle: ${fileName}`, {
cause: error
}));
return;
}

res.append("Cache-Control", "max-age=0");
res.append("Content-Type", "application/javascript");
res.send(bundle);
};
} catch (e) {
error = e;
}

if (!bundle) {
next(
new Error(`Failed to generate bundle: ${fileName}`, {
cause: error,
})
);
return;
}

res.append("Cache-Control", "max-age=0");
res.append("Content-Type", "application/javascript");
res.send(bundle);
}

function createRoute(app, name, route, context, dependantCaches) {
const cache = new ContextCache(context);
app.get(route, async function (req, res, next) {
// Multiple files may be requested at this path, calling this function in quick succession.
// Await the previous build before re-building again.
await cache.promise;

if (!cache.isBuilt()) {
try {
const start = performance.now();
if (dependantCaches) {
await Promise.all(
dependantCaches.map((dependantCache) => {
if (!dependantCache.isBuilt()) {
return dependantCache.rebuild();
}
})
);
}
await cache.rebuild();
console.log(
`Built ${name} in ${formatTimeSinceInSeconds(start)} seconds.`
const cache = new ContextCache(context);
app.get(route, async function (req, res, next) {
// Multiple files may be requested at this path, calling this function in quick succession.
// Await the previous build before re-building again.
await cache.promise;

if (!cache.isBuilt()) {
try {
const start = performance.now();
if (dependantCaches) {
await Promise.all(
dependantCaches.map((dependantCache) => {
if (!dependantCache.isBuilt()) {
return dependantCache.rebuild();
}
})
);
} catch (e) {
next(e);
}
await cache.rebuild();
console.log(
`Built ${name} in ${formatTimeSinceInSeconds(start)} seconds.`
);
} catch (e) {
next(e);
}

return serveResult(cache.result, path.basename(req.originalUrl), res, next);
});

return cache;
}
}

return serveResult(cache.result, path.basename(req.originalUrl), res, next);
});

return cache;
}

export default createRoute;
export default createRoute;

0 comments on commit 3dff10d

Please sign in to comment.