A32nx: [TRACKING] Reactive systems

Created on 24 Oct 2020  路  2Comments  路  Source: flybywiresim/a32nx

Currently we poll (check every update cycle) on lots of things, like the positions of switches in the cockpit. As an example, a pilot is not moving ADIRS knobs every 1/60th of a second, so there's no need to check its position that often. Moving to event (reactive) code where possible (using h-events and such) would help improve performance.

Enhancement

Most helpful comment

Using e.g. RxJS would make a lot of the code clearer. Right now I'm seeing a lot of variables being set and then used somewhere 400 lines later. That by itself would already be a good reason for using a different programming model. There's a lack of "flow" to understanding the code in that sense. All I see is events! An example. Lower ECAM Bleed:

Current:

let currentLeftPackState = SimVar.GetSimVarValue("L:A32NX_AIRCOND_PACK1_TOGGLE", "bool");
let currentRightPackState = SimVar.GetSimVarValue("L:A32NX_AIRCOND_PACK2_TOGGLE", "bool");

// Various unrelated lines

// Original code also sets bothPacksOn, but ignored for the example.
if (currentLeftPackState && currentRightPackState) {
    this.singlePackOn = false;
} else if (currentLeftPackState || currentRightPackState) {
    this.singlePackOn = true;
} else {
    this.singlePackOn = false;
}

// 60 lines later and only usage
//sets pack flow to high if TOGA applied/ single pack on / bleed provided by apu alone
if (this.thrustTOGAApplied || this.singlePackOn || (!currentEngineBleedState[0] && !currentEngineBleedState[1] && this.apuProvidesBleed)) {
    currentPackFlow = 2;
}

RxJs:

const leftPackState$ = OurMagicSimVarToObservableMagic.GetObservableOf("L:A32NX_AIRCOND_PACK1_TOGGLE", "bool");
const rightPackState$ = OurMagicSimVarToObservableMagic.GetObservableOf("L:A32NX_AIRCOND_PACK2_TOGGLE", "bool");

const isSinglePackOn$ = combineLatest(leftPackState$, rightPackState$).pipe(
    map(([leftPackState, rightPackState]) => {
        return leftPackState + rightPackState === 1; 
    })
);

const packFlow$ = // ... etc. This goes on a while until we actually need to subscribe to do UI changes.

All 2 comments

Using e.g. RxJS would make a lot of the code clearer. Right now I'm seeing a lot of variables being set and then used somewhere 400 lines later. That by itself would already be a good reason for using a different programming model. There's a lack of "flow" to understanding the code in that sense. All I see is events! An example. Lower ECAM Bleed:

Current:

let currentLeftPackState = SimVar.GetSimVarValue("L:A32NX_AIRCOND_PACK1_TOGGLE", "bool");
let currentRightPackState = SimVar.GetSimVarValue("L:A32NX_AIRCOND_PACK2_TOGGLE", "bool");

// Various unrelated lines

// Original code also sets bothPacksOn, but ignored for the example.
if (currentLeftPackState && currentRightPackState) {
    this.singlePackOn = false;
} else if (currentLeftPackState || currentRightPackState) {
    this.singlePackOn = true;
} else {
    this.singlePackOn = false;
}

// 60 lines later and only usage
//sets pack flow to high if TOGA applied/ single pack on / bleed provided by apu alone
if (this.thrustTOGAApplied || this.singlePackOn || (!currentEngineBleedState[0] && !currentEngineBleedState[1] && this.apuProvidesBleed)) {
    currentPackFlow = 2;
}

RxJs:

const leftPackState$ = OurMagicSimVarToObservableMagic.GetObservableOf("L:A32NX_AIRCOND_PACK1_TOGGLE", "bool");
const rightPackState$ = OurMagicSimVarToObservableMagic.GetObservableOf("L:A32NX_AIRCOND_PACK2_TOGGLE", "bool");

const isSinglePackOn$ = combineLatest(leftPackState$, rightPackState$).pipe(
    map(([leftPackState, rightPackState]) => {
        return leftPackState + rightPackState === 1; 
    })
);

const packFlow$ = // ... etc. This goes on a while until we actually need to subscribe to do UI changes.

@davidwalschots that seems interesting but is unrelated to this issue. this issue is specifically about changing polling GetSimVarValue calls into onInteractionEvent h-events.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mfnalex picture mfnalex  路  4Comments

ukflyer picture ukflyer  路  3Comments

tareksabet picture tareksabet  路  4Comments

maor561 picture maor561  路  4Comments

Shimi1612 picture Shimi1612  路  3Comments