how to run external libs functions inside iOS and Android in React Native?

39 Views Asked by At

I want to integrate stream datafeed for charting library. Stream will be communicated with msgpack (like a JSON). I need to decode and encode sended message and received data. Here is the explanation how whole process going on: Checking platform base for rendering html:

const uri =
  Platform.OS === 'ios'
    ? './charting_library/index.html'
    : 'file:///android_asset/index.html';

I am trying to inject libraries encode and decode functions that we need inside charting library. Here is the injected value and necessary functions:

import {encode, Decoder} from '@msgpack/msgpack';
...
const encodeHandler = (value: any): Uint8Array => {
  const encoded = encode(value);
  return encoded;
};
...
const runFirst = `
    window.addEventListener('DOMContentLoaded', () => initOnReady('${symbol}', ${encodeHandler}, ${decodeHandler}), false);
`;

<WebView
 ref={ref => (webViewRef.current = ref)}
 source={{uri}}
 injectedJavaScriptBeforeContentLoaded={runFirst}
 ...otherProps
/> 

Here is charting library file structure for iOS (for demonstration): charting library

I am calling sended functions inside html like that:

 const plainText = 'something';
 const result = encode([plainText]);

Once I call normal JS functions in the html that I pass like that way, it is working properly. But I can not call library specific functions. How to solve that?

0

There are 0 best solutions below