Skip to content

Commit

Permalink
fixed hanging
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoulder4 committed Aug 20, 2023
1 parent 0da003d commit dc0c4f3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 52 deletions.
9 changes: 9 additions & 0 deletions dist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
Object.defineProperty(exports, "__esModule", { value: true });
const findTrains_1 = require("./findTrains");
const trackTrain_1 = require("./trackTrain");
// const util = require("util");
module.exports = {
findTrains: findTrains_1.default,
trackTrain: trackTrain_1.trackTrain,
};
// trackTrain("P71987", "2023-08-19").then((emitter) => {
// emitter.on("journey", (data) => {
// console.log(util.inspect(data, false, null, true));
// });
// emitter.on("information", (data) => {
// console.log(util.inspect(data, false, null, true));
// });
// });
42 changes: 20 additions & 22 deletions dist/src/trackTrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,11 @@ function trackTrain(serviceID, date = getCurrentDayTime("YYYY-MM-DD"), timeTillR
}
let previousState;
let currentState;
if (!serviceID) {
return "Enter a service ID.";
}
const trainUpdateEmitter = new EventEmitter();
//loop here every 5s. 'const loop =' needed for strange js behaviour
const loop = setInterval(() => __awaiter(this, void 0, void 0, function* () {
let html = yield getHTML(serviceID, date);
let $ = cheerio.load(html);
//-- //if no locationlist
if (!locationListExists($)) {
return informationObject("Error", "Check service ID.");
}
const firstDepAct = $(".dep.act").first();
const firstDepExp = $(".dep.exp").first();
let origin = null;
Expand All @@ -73,25 +66,30 @@ function trackTrain(serviceID, date = getCurrentDayTime("YYYY-MM-DD"), timeTillR
else {
origin = null;
}
//if no origin
if (!origin) {
return informationObject("Null origin. (Service cancelled?)", $(".callout p").text());
}
//get current state of train as currentState
currentState = getCurrentState($);
//check if end of loop
if (currentState.hidden.action == "end") {
//stop loop
if (!locationListExists($) || !serviceID || !origin) {
emitUpdate(trainUpdateEmitter, informationObject("Error", $(".callout p").text() ||
$(".callout p").text() ||
$(".callout h3").text() ||
"Check the service ID and date. (Maybe the train departed yesterday?)"));
clearInterval(loop);
}
//if the refreshed state is different
if (!equal(currentState, previousState)) {
emitUpdate(trainUpdateEmitter, currentState);
previousState = currentState;
else {
//get current state of train as currentState
currentState = getCurrentState($);
//check if end of loop
if (currentState.hidden.action == "end") {
//stop loop
clearInterval(loop);
}
//if the refreshed state is different
if (!equal(currentState, previousState)) {
emitUpdate(trainUpdateEmitter, currentState);
previousState = currentState;
}
}
}), timeTillRefresh);
//return the emitter for subscription
return trainUpdateEmitter;
//return the emitter for subscription
});
}
exports.trackTrain = trackTrain;
Expand Down Expand Up @@ -317,5 +315,5 @@ function stateObject(_status, _station, _action, _callingPoints) {
//update to train state
function emitUpdate(emitter, stateUpdate) {
//if it's a journey update
emitter.emit(`${stateUpdate.hidden.update_type}`, stateUpdate.body);
emitter.emit(stateUpdate.hidden.update_type, stateUpdate.body);
}
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import findTrains from "./findTrains";
import { trackTrain } from "./trackTrain";
// const util = require("util");
module.exports = {
findTrains,
trackTrain,
};

// trackTrain("P71987", "2023-08-19").then((emitter) => {
// emitter.on("journey", (data) => {
// console.log(util.inspect(data, false, null, true));
// });
// emitter.on("information", (data) => {
// console.log(util.inspect(data, false, null, true));
// });
// });
54 changes: 28 additions & 26 deletions src/trackTrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,18 @@ export async function trackTrain(
serviceID: string,
date = getCurrentDayTime("YYYY-MM-DD"),
timeTillRefresh = 5000
) {
): Promise<typeof EventEmitter | information["body"]> {
if (timeTillRefresh < 5000) {
timeTillRefresh = 5000;
}
let previousState: state | information;
let currentState: state | information;
if (!serviceID) {
return "Enter a service ID.";
}
const trainUpdateEmitter = new EventEmitter();
//loop here every 5s. 'const loop =' needed for strange js behaviour

const loop = setInterval(async () => {
let html = await getHTML(serviceID, date);
let $ = cheerio.load(html);
//-- //if no locationlist
if (!locationListExists($)) {
return informationObject("Error", "Check service ID.");
}
const firstDepAct = $(".dep.act").first();
const firstDepExp = $(".dep.exp").first();
let origin: cheerio.Cheerio | null = null;
Expand All @@ -64,28 +58,36 @@ export async function trackTrain(
} else {
origin = null;
}
//if no origin
if (!origin) {
return informationObject(
"Null origin. (Service cancelled?)",
$(".callout p").text()

if (!locationListExists($) || !serviceID || !origin) {
emitUpdate(
trainUpdateEmitter,
informationObject(
"Error",
$(".callout p").text() ||
$(".callout p").text() ||
$(".callout h3").text() ||
"Check the service ID and date. (Maybe the train departed yesterday?)"
)
);
}
//get current state of train as currentState
currentState = getCurrentState($);
//check if end of loop
if (currentState.hidden.action == "end") {
//stop loop
clearInterval(loop);
}
//if the refreshed state is different
if (!equal(currentState, previousState)) {
emitUpdate(trainUpdateEmitter, currentState);
previousState = currentState;
} else {
//get current state of train as currentState
currentState = getCurrentState($);
//check if end of loop
if (currentState.hidden.action == "end") {
//stop loop
clearInterval(loop);
}
//if the refreshed state is different
if (!equal(currentState, previousState)) {
emitUpdate(trainUpdateEmitter, currentState);
previousState = currentState;
}
}
}, timeTillRefresh);
//return the emitter for subscription
return trainUpdateEmitter;
//return the emitter for subscription
}
function informationObject(
informationString: string,
Expand Down Expand Up @@ -371,5 +373,5 @@ function emitUpdate(
stateUpdate: state | information
) {
//if it's a journey update
emitter.emit(`${stateUpdate.hidden.update_type}`, stateUpdate.body);
emitter.emit(stateUpdate.hidden.update_type, stateUpdate.body);
}

0 comments on commit dc0c4f3

Please sign in to comment.