add websocket endpoint /api/workout/:id/subscribe.
This commit is contained in:
@@ -174,5 +174,45 @@ module.exports = function workoutRouter(repo) {
|
||||
}
|
||||
});
|
||||
|
||||
router.ws("/:id/subscribe", (ws, req) => {
|
||||
console.log("HELLO!")
|
||||
|
||||
const workout = workouts.find(w => w.id === req.params.id);
|
||||
if (workout == null) {
|
||||
ws.send(JSON.stringify({error: "workout not found"}));
|
||||
ws.close();
|
||||
}
|
||||
|
||||
workout.listMeasurements().then((list) => {
|
||||
ws.send(JSON.stringify({workoutStatusBackfill: list}));
|
||||
}).catch(err => {
|
||||
ws.send(JSON.stringify({error: `could not list measurements: ${err.message || err}`}));
|
||||
ws.close();
|
||||
});
|
||||
|
||||
const handler = (workotuStatus) => {
|
||||
workotuStatus = {...workotuStatus};
|
||||
delete workotuStatus.id;
|
||||
|
||||
ws.send(JSON.stringify({workotuStatus}))
|
||||
}
|
||||
|
||||
workout.events.on("workoutStatus", handler);
|
||||
|
||||
ws.onclose = () => {
|
||||
workout.events.removeListener("workoutStatus", handler);
|
||||
ws.removeAllListeners();
|
||||
};
|
||||
|
||||
ws.send(JSON.stringify({
|
||||
workout: {
|
||||
id: workout.id,
|
||||
bike: workout.bike,
|
||||
program: workout.program,
|
||||
date: workout.date,
|
||||
},
|
||||
}));
|
||||
})
|
||||
|
||||
return router;
|
||||
}
|
||||
+4
-1
@@ -1,4 +1,6 @@
|
||||
const express = require('express');
|
||||
const express = require("express");
|
||||
const expressWs = require("express-ws");
|
||||
|
||||
const SQLite3Repository = require("./repositories/sqlite3");
|
||||
|
||||
const repo = new SQLite3Repository("stuff.db");
|
||||
@@ -11,6 +13,7 @@ repo.setup().catch(err => {
|
||||
});
|
||||
|
||||
const app = express();
|
||||
expressWs(app);
|
||||
|
||||
app.use(express.json({strict: false}));
|
||||
|
||||
|
||||
@@ -85,7 +85,7 @@ class Workout {
|
||||
|
||||
this.repo.insertMeasurement({...ws, workoutId: this.id});
|
||||
|
||||
this.events.emit("workoutStatus", ws);
|
||||
this.events.emit("workoutStatus", {...ws});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,7 +95,14 @@ class Workout {
|
||||
*/
|
||||
static async create(repo, bikeId, programId) {
|
||||
const bike = await repo.findBike(bikeId);
|
||||
if (bike == null) {
|
||||
throw new Error("bike not found");
|
||||
}
|
||||
const program = await repo.findProgram(programId);
|
||||
if (program == null) {
|
||||
throw new Error("program not found");
|
||||
}
|
||||
|
||||
const date = new Date();
|
||||
const id = await repo.insertWorkout({
|
||||
bikeId: bike.id,
|
||||
@@ -115,7 +122,7 @@ class Workout {
|
||||
const bike = await repo.findBike(data.bikeId);
|
||||
const program = await repo.findWorkout(data.workoutId);
|
||||
|
||||
const workout = new Workout(repo, parseInt(id), bike, program, new Date(data.date));
|
||||
const workout = new Workout(repo, id, bike, program, new Date(data.date));
|
||||
|
||||
const list = await repo.listMeasurements(id);
|
||||
if (list.length > 0) {
|
||||
|
||||
Reference in New Issue
Block a user