flightnethelper 1.0.0 → 1.0.1

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.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ usage:
2
+
3
+ import { FlightnetPredictions } from "flightnethelper";
4
+
5
+ const predictions = new FlightnetPredictions("ws://localhost:9080");
6
+
7
+ setInterval(() => {
8
+ if (predictions.data) {
9
+ // predictions.data is the 24data { "t": "ACFT_DATA", d: { } }
10
+ }
11
+ }, 200);
package/dist/Main.d.ts CHANGED
@@ -1,12 +1,13 @@
1
- import { Msg } from "./types";
2
- export declare class FlightnetPredictions {
3
- private ws;
4
- private latestPayload;
5
- private idx;
6
- private timer;
7
- /** Client reads this: { t, d } */
8
- data: Msg | null;
9
- constructor(url: string, intervalMs?: number);
10
- private start;
11
- private stop;
1
+ import WebSocket from 'ws';
2
+ declare class FlightnetPredictions {
3
+ ws: WebSocket;
4
+ latestPayload: any;
5
+ snapshot: any;
6
+ idx: number;
7
+ timer: NodeJS.Timer | null | any;
8
+ data: any;
9
+ constructor(url: any, intervalMs?: number);
10
+ start(intervalMs: any): void;
11
+ stop(): void;
12
12
  }
13
+ export { FlightnetPredictions };
package/dist/Main.js CHANGED
@@ -8,34 +8,47 @@ const ws_1 = __importDefault(require("ws"));
8
8
  const ORDER = ["1", "2", "3", "4", "5"];
9
9
  class FlightnetPredictions {
10
10
  constructor(url, intervalMs = 200) {
11
+ this.ws = new ws_1.default(url);
11
12
  this.latestPayload = null;
13
+ this.snapshot = null;
12
14
  this.idx = 0;
13
15
  this.timer = null;
14
- /** Client reads this: { t, d } */
16
+ // Client reads this: { t, d }
15
17
  this.data = null;
16
- this.ws = new ws_1.default(url);
17
- this.ws.on("message", raw => {
18
+ this.ws.on('message', (raw) => {
18
19
  try {
19
- this.latestPayload = JSON.parse(raw.toString());
20
+ const parsed = JSON.parse(raw.toString());
21
+ this.latestPayload = parsed;
22
+ try {
23
+ this.snapshot = JSON.parse(JSON.stringify(parsed));
24
+ }
25
+ catch (e) {
26
+ this.snapshot = parsed;
27
+ }
28
+ this.idx = 0; // resync rotation on each new push
20
29
  }
21
- catch {
30
+ catch (e) {
22
31
  // ignore malformed frames
23
32
  }
24
33
  });
25
- this.ws.on("open", () => this.start(intervalMs));
26
- this.ws.on("close", () => this.stop());
34
+ this.ws.on('open', () => this.start(intervalMs));
35
+ this.ws.on('close', () => this.stop());
27
36
  }
28
37
  start(intervalMs) {
29
38
  if (this.timer)
30
39
  return;
31
40
  this.timer = setInterval(() => {
32
- if (!this.latestPayload)
33
- return;
34
- const msg = this.latestPayload[ORDER[this.idx]];
35
- if (!msg)
41
+ const payload = this.snapshot;
42
+ if (!payload)
36
43
  return;
37
- // expose ONLY { t, d }
38
- this.data = msg;
44
+ const key = ORDER[this.idx];
45
+ const msg = payload[key];
46
+ if (!msg) {
47
+ this.data = payload['1'] || null;
48
+ }
49
+ else {
50
+ this.data = msg;
51
+ }
39
52
  this.idx = (this.idx + 1) % ORDER.length;
40
53
  }, intervalMs);
41
54
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flightnethelper",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A npm package that is meant to help a client connecting to flightnet.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",