mobility-toolbox-js 3.6.13 → 3.6.14-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { WebSocketAPIMessageCallback } from '../../api/WebSocketAPI';
|
|
2
|
+
import type { RealtimeDeparture, RealtimeDepartureExtended } from '../../types';
|
|
3
|
+
/**
|
|
4
|
+
* This function returns a WebSocket api callback, and call the onCallsUpdate function with the list of current calls and arrival to display.
|
|
5
|
+
* @param {function(calls: RealtimeDepartureExtended[])} onCallsUpdate callback when timetable changes, called after 100 ms
|
|
6
|
+
* @param {boolean} [sortByMinArrivalTime = true] Sort calls by arrival time
|
|
7
|
+
* @param {number} [maxDepartureAge = 30] max departure age of calls in minutes
|
|
8
|
+
* @param {number} [timeout = 100] debounce timeout in ms
|
|
9
|
+
* @private
|
|
10
|
+
*/
|
|
11
|
+
declare const debounceTimeTableMessages: (onCallsUpdate: (departures: RealtimeDepartureExtended[]) => unknown, sortByMinArrivalTime?: boolean, maxDepartureAge?: number, timeout?: number) => WebSocketAPIMessageCallback<RealtimeDeparture>;
|
|
12
|
+
export default debounceTimeTableMessages;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import sortAndfilterTimetableCall from './sortAndFilterTimetableCall';
|
|
2
|
+
/**
|
|
3
|
+
* This function returns a WebSocket api callback, and call the onCallsUpdate function with the list of current calls and arrival to display.
|
|
4
|
+
* @param {function(calls: RealtimeDepartureExtended[])} onCallsUpdate callback when timetable changes, called after 100 ms
|
|
5
|
+
* @param {boolean} [sortByMinArrivalTime = true] Sort calls by arrival time
|
|
6
|
+
* @param {number} [maxDepartureAge = 30] max departure age of calls in minutes
|
|
7
|
+
* @param {number} [timeout = 100] debounce timeout in ms
|
|
8
|
+
* @private
|
|
9
|
+
*/
|
|
10
|
+
const debounceTimeTableMessages = (onCallsUpdate, sortByMinArrivalTime = false, maxDepartureAge = 30, timeout = 100) => {
|
|
11
|
+
const callUpdateTimeout = {};
|
|
12
|
+
const callObject = {};
|
|
13
|
+
return (data) => {
|
|
14
|
+
const { content: call, source } = data;
|
|
15
|
+
if (callUpdateTimeout[source]) {
|
|
16
|
+
window.clearTimeout(callUpdateTimeout[source]);
|
|
17
|
+
}
|
|
18
|
+
if (!call) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
callObject[call.call_id] = call;
|
|
22
|
+
callUpdateTimeout[source] = window.setTimeout(() => {
|
|
23
|
+
const calls = sortAndfilterTimetableCall(callObject, sortByMinArrivalTime || false, maxDepartureAge);
|
|
24
|
+
onCallsUpdate(calls);
|
|
25
|
+
}, timeout);
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export default debounceTimeTableMessages;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RealtimeAPIDeparturesById } from '../../api/RealtimeAPI';
|
|
2
|
+
import type { RealtimeDepartureExtended } from '../../types';
|
|
3
|
+
/**
|
|
4
|
+
* This function sort Departures by arrival time and filter out unwanted departures:
|
|
5
|
+
* - when dparture time is in the past
|
|
6
|
+
* - when departure are duplicated
|
|
7
|
+
* - when departure is not in the next 30 min
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} depObject The object containing departures by id.
|
|
10
|
+
* @param {boolean} [sortByMinArrivalTime=false] If true sort departures by arrival time.
|
|
11
|
+
* @param {number} [maxDepartureAge=30] The maximum departure age in minutes.
|
|
12
|
+
* @return {RealtimeDeparture[]} Return departures array.
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
declare const sortAndfilterTimetableCall: (depObject: RealtimeAPIDeparturesById, sortByMinArrivalTime?: boolean, maxDepartureAge?: number) => RealtimeDepartureExtended[];
|
|
16
|
+
export default sortAndfilterTimetableCall;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import compareDepartures from './compareDepartures';
|
|
2
|
+
/**
|
|
3
|
+
* This function sort Departures by arrival time and filter out unwanted departures:
|
|
4
|
+
* - when dparture time is in the past
|
|
5
|
+
* - when departure are duplicated
|
|
6
|
+
* - when departure is not in the next 30 min
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} depObject The object containing departures by id.
|
|
9
|
+
* @param {boolean} [sortByMinArrivalTime=false] If true sort departures by arrival time.
|
|
10
|
+
* @param {number} [maxDepartureAge=30] The maximum departure age in minutes.
|
|
11
|
+
* @return {RealtimeDeparture[]} Return departures array.
|
|
12
|
+
* @private
|
|
13
|
+
*/
|
|
14
|
+
const sortAndfilterTimetableCall = (depObject, sortByMinArrivalTime = false, maxDepartureAge = 30) => {
|
|
15
|
+
var _a;
|
|
16
|
+
const calls = Object.keys(depObject).map((k) => {
|
|
17
|
+
return depObject[k];
|
|
18
|
+
});
|
|
19
|
+
calls.sort((a, b) => {
|
|
20
|
+
return compareDepartures(a, b, sortByMinArrivalTime);
|
|
21
|
+
});
|
|
22
|
+
const futureDate = new Date();
|
|
23
|
+
futureDate.setMinutes(futureDate.getMinutes() + maxDepartureAge);
|
|
24
|
+
const future = futureDate.getTime();
|
|
25
|
+
const pastDate = new Date();
|
|
26
|
+
pastDate.setMinutes(pastDate.getMinutes() - maxDepartureAge);
|
|
27
|
+
const past = pastDate.getTime();
|
|
28
|
+
const callsArray = [];
|
|
29
|
+
const platformsBoarding = [];
|
|
30
|
+
let previousCall = null;
|
|
31
|
+
for (let i = calls.length - 1; i >= 0; i -= 1) {
|
|
32
|
+
const call = Object.assign({}, calls[i]);
|
|
33
|
+
const chosenTime = (_a = call.time) !== null && _a !== void 0 ? _a : call.arrivalTime;
|
|
34
|
+
if (!chosenTime) {
|
|
35
|
+
// eslint-disable-next-line no-console
|
|
36
|
+
console.warn('Call without time found, skipping it.', call);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const time = new Date(chosenTime).getTime();
|
|
40
|
+
// Only show departures within the next 30 minutes
|
|
41
|
+
if (time > past && time < future) {
|
|
42
|
+
// If 2 trains are boarding at the same platform,
|
|
43
|
+
// remove the older one.
|
|
44
|
+
if (call.state === 'BOARDING') {
|
|
45
|
+
if (call.platform && !platformsBoarding.includes(call.platform)) {
|
|
46
|
+
platformsBoarding.push(call.platform);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
call.state = 'HIDDEN';
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// If two trains with the same line number and destinatin
|
|
53
|
+
// and a departure difference < 1 minute, hide the second one.
|
|
54
|
+
if (previousCall &&
|
|
55
|
+
call.to[0] === previousCall.to[0] &&
|
|
56
|
+
Math.abs(time - (previousCall.time || 0)) < 1000 &&
|
|
57
|
+
call.line.name === previousCall.line.name) {
|
|
58
|
+
call.state = 'HIDDEN';
|
|
59
|
+
}
|
|
60
|
+
if (/(STOP_CANCELLED|JOURNEY_CANCELLED)/.test(call.state)) {
|
|
61
|
+
call.cancelled = true;
|
|
62
|
+
}
|
|
63
|
+
previousCall = call;
|
|
64
|
+
previousCall.time = time;
|
|
65
|
+
callsArray.unshift(call);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return callsArray;
|
|
69
|
+
};
|
|
70
|
+
export default sortAndfilterTimetableCall;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "mobility-toolbox-js",
|
|
3
3
|
"license": "MIT",
|
|
4
4
|
"description": "Toolbox for JavaScript applications in the domains of mobility and logistics.",
|
|
5
|
-
"version": "3.6.
|
|
5
|
+
"version": "3.6.14-beta.0",
|
|
6
6
|
"homepage": "https://mobility-toolbox-js.geops.io/",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./index.js",
|