msfs-simconnect-api-wrapper
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -26,17 +26,32 @@ import { | ||
export class MSFS_API { | ||
constructor() { | ||
constructor(appName = "MSFS API") { | ||
this.appName = appName; | ||
// set up a listener list for simconnect event handling: | ||
this.eventListeners = []; | ||
// set up an event/data/request id counter: | ||
this.id = 0; | ||
this.reserved = new Set(); | ||
this.eventListeners = []; | ||
} | ||
async connect(appName = "MSFS API") { | ||
async connect(opts) { | ||
opts.retries ??= 0; | ||
opts.retryInterval ??= 0; | ||
opts.onConnect ??= () => {}; | ||
opts.onRetry ??= () => {}; | ||
try { | ||
const { handle } = await open(appName, Protocol.KittyHawk); | ||
const { handle } = await open(this.appName, Protocol.KittyHawk); | ||
if (!handle) throw new Error(`No connection handle to MSFS`); | ||
this.handle = handle; | ||
this.connected = true; | ||
opts.onConnect(handle); | ||
handle.on("event", (event) => this.handleSystemEvent(event)); | ||
handle.on("exception", (e) => console.error(e)); | ||
} catch (err) { | ||
throw new Error(`No connection to MSFS`); | ||
if (opts.retries) { | ||
opts.retries--; | ||
opts.onRetry(opts.retries, opts.retryInterval); | ||
setTimeout(() => this.connect(opts), 1000 * opts.retryInterval); | ||
} else throw new Error(`No connection to MSFS`); | ||
} | ||
@@ -121,4 +136,4 @@ } | ||
value, | ||
1, // highest priority | ||
16, // group id is priority | ||
1, // highest priority | ||
16 // group id is priority | ||
); | ||
@@ -125,0 +140,0 @@ } |
{ | ||
"name": "msfs-simconnect-api-wrapper", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A convenient SimConnect API for playing with Microsoft Flight Simulator 2020", | ||
"main": "msfs-api.js", | ||
"type": "module", | ||
"dependencies": { | ||
@@ -7,0 +8,0 @@ "node-simconnect": "^3.4.0" |
@@ -11,17 +11,39 @@ # msfs-simconnect-api-wrapper | ||
This API manager has an argless constructor that does nothing other than allocate internal values. In order to work with MSFS, you need to call the `connect` function first: | ||
This API manager has an argless constructor that does nothing other than allocate internal values. In order to work with MSFS, you need to call the `connect` function first, which can take an options object of the following form: | ||
```javascript | ||
{ | ||
retries: positive number or Infinity | ||
retryInterval: positive number, representing number of seconds (not milliseconds) between retries, | ||
onConnect: callback function with the node-simconnect handle as its only argument. | ||
onRetry: callback function retry interval as its only argument, triggers _before_ the next attempt is scheduled. | ||
} | ||
``` | ||
For example, we can set up an API object and have it start trying to connect, eventually dropping us into the code that knows it has an MSFS connection using the following boilerplate: | ||
```javascript | ||
import { MSFS_API } from "./msfs-api.js"; | ||
const api = new MSFS_API(); | ||
try { | ||
await api.connect(); | ||
//...your code here... | ||
} catch (err) { | ||
console.error(err); | ||
api.connect({ | ||
retries: Infinity, | ||
retryInterval: 5, | ||
onConnect: () => run(), | ||
onRetry: (_, interval) => console.log(`Connection failed: retrying in ${interval} seconds.`), | ||
}); | ||
function run() { | ||
console.log(`We have an API connection to MSFS!`); | ||
// | ||
// ...your code here... | ||
// | ||
} | ||
``` | ||
### properties | ||
The API has a single property `.connected` which is either `undefined` or `true` and can be used to determine whether the API has a connection to MSFS outside of code that relies on the `onConnect` callback. | ||
### methods | ||
@@ -37,3 +59,3 @@ | ||
### System events (used for on/off handling): | ||
#### System events (used for on/off handling): | ||
@@ -40,0 +62,0 @@ All event names in https://docs.flightsimulator.com/html/Programming_Tools/SimConnect/API_Reference/Events_And_Data/SimConnect_SubscribeToSystemEvent.htm are supported as constants on the `SystemEvents` object. Event names are keyed using UPPER_SNAKE_CASE. That is, the first few events are encoded as: |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115
Yes
230523
20
6700