Share objects between JavaScript actions

1
I am using https://github.com/dotintent/react-native-ble-plx to implement BLE communication in Native App. For this, we need to create an instance of BleManager class inside a JavaScript action. This BleManager class provides functionality to scan BLE devices, connect a device and read/write from/to devices.  Now consider a scenario where I have one JavaScript action to scan BLE devices and establish connection after user clicks on a button. At some point, I want to call another JavaScript action based on some user action (button click). This action should use the existing BleManager object holding the connection information and write some data to the connected device via Bluetooth. So here I need something like bleManager.write(); (means accessing the BleManager instance that is already created in another JS action) The problem here is how to share objects between JavaScript actions in Mendix Native App? The first JavaScript action with BleManager object has a callback that keeps running until the connection is alive that means the object is in scope and action is still running. How to use that object in another JS action called sometime later? Mendix version 9.4.0
asked
3 answers
4

I see two ways to do this:

1) You could store the BleManager object as a global variable which could be retrieved elsewhere:

global.blemanager = new BleManager();

 

2) I’m not sure if this will work but I would love to know. You could try creating and exporting the BleManager object in one JS action, and importing it from others

import { BleManager } from 'react-native-ble-plx';

export const manager = new BleManager();

then

import { manager } from './myJSAction';

 

answered
0

Maybe rewrite as pluggable widget, the widget can hold the BleManager reference. If you put it on a separate page and make sure to close it, you can use componentWillUnmount to do any required cleanup.

answered
0

Sharing between JavaScript Actions is not a good idea. Each JavaScript Action should work separately, without any external dependencies.

You can return the object from action 1 and pass this as parameter to action 2 from your nanoflow. So you can still use both actions separately. 

answered