feat: add ability to ws.render from websocket
This commit is contained in:
parent
73779a32ad
commit
df42cd8a8b
1 changed files with 24 additions and 4 deletions
28
index.js
28
index.js
|
|
@ -123,14 +123,34 @@ function bootstrapWebsocketHandler(route) {
|
||||||
if (!wsRoutes[routeWithPrefix(route)]) {
|
if (!wsRoutes[routeWithPrefix(route)]) {
|
||||||
// initialize ws router. future updates will only require updating
|
// initialize ws router. future updates will only require updating
|
||||||
// the wsRoutes dict, not create a whole new route
|
// the wsRoutes dict, not create a whole new route
|
||||||
wsRouter.ws(routeWithPrefix(route), (req, res) => {
|
wsRouter.ws(routeWithPrefix(route), (ws, req) => {
|
||||||
return wsRoutes[routeWithPrefix(route)](req, res)
|
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 {
|
try {
|
||||||
return handler(req, res)
|
return handler(ws, req)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`;
|
const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`;
|
||||||
model.createLog(db, route.structure_id, route.id, error, true);
|
model.createLog(db, route.structure_id, route.id, error, true);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue