feat: add ability to ws.render from websocket

This commit is contained in:
Your Name 2025-01-20 11:49:54 -05:00
parent 73779a32ad
commit df42cd8a8b

View file

@ -123,14 +123,34 @@ function bootstrapWebsocketHandler(route) {
if (!wsRoutes[routeWithPrefix(route)]) {
// initialize ws router. future updates will only require updating
// the wsRoutes dict, not create a whole new route
wsRouter.ws(routeWithPrefix(route), (req, res) => {
return wsRoutes[routeWithPrefix(route)](req, res)
wsRouter.ws(routeWithPrefix(route), (ws, req) => {
ws.render = (template, context) => {
context = context || {};
context.route = function (url) {
return __urlPrefix ? path.join(__urlPrefix, url) : url;
};
const eta = model.getTemplater(route.structure_id);
const structure = model.getStructure(db, route.structure_id);
ws.send(
bootstrapTemplateWithHTMXetc(
eta.render(template, context),
`/workshop/${route.structure_id}/route/${route.id}`,
`/workshop/${route.structure_id}/clone_modal/`,
route.verb == "GET" ? embedHTML(req.originalUrl) : null,
structure.head_injection,
htmxRequest = true,
),
);
};
return wsRoutes[routeWithPrefix(route)](ws, req)
});
}
wsRoutes[routeWithPrefix(route)] = (req, res) => {
wsRoutes[routeWithPrefix(route)] = (ws, req) => {
try {
return handler(req, res)
return handler(ws, req)
} catch (e) {
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`;
model.createLog(db, route.structure_id, route.id, error, true);