Generic Devices: Please give function analyzeDeviceInput() access to the previous state

I got it working! And quite elegantly so I think:

var xac_globals;
function analyzeDeviceInput(targetDevice, reportID, reportDataHex)
{
    let lastReport =
        ((xac_globals ||= {})[targetDevice] ||= {})["lastReport"] ||= 
            buffer.Buffer.alloc(reportDataHex.length, 0);
    
    let thisReport = reportDataHex;
    xac_globals[targetDevice]["lastReport"] = thisReport;

    let Buttons = [
        [ "A", 14, 0x01 ],
        [ "B", 14, 0x02 ]
    ];

    for (const [ b_name, b_byte, b_mask ] of Buttons) {      
        let last_b_state = (lastReport.readUInt8(b_byte) & b_mask);
        let this_b_state = (thisReport.readUInt8(b_byte) & b_mask);

        if (last_b_state ^ this_b_state) {
            let b_event = this_b_state ? b_name : b_name + "_UP";
            bttTriggerDeviceTrigger(targetDevice, b_event)
        }
    }

    // If you want to get the next report even though,
    // the data has not changed, call this function:
    // bttGetNextEvenWithoutChange(targetDevice, reportID)
}

The global variable (I chose xac_globals, feel free to come up with a convention) needs to be defined in order for the code to know about it, and var appears to do exactly the right thing, i.e. it will only create the variable if it does not already exist and otherwise leave it alone. Also note that your reportBuffer = buffer.Buffer.from(reportDataHex, 'hex'); from the sample code is not needed because reportDataHex is a buffer already.

Thanks again for your help @Andreas_Hegenberg!