diff --git a/.gitignore b/.gitignore index 53d5a710..3905a0d6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,14 @@ +# node generated folders node_modules/ lib/ +yarn-error.log +# mac temp file .DS_Store + +# Android studio file +modules.xml +*.iml +vcs.xml +workspace.xml + diff --git a/examples/native/App.js b/examples/native/App.js index 985b2c51..18d31229 100644 --- a/examples/native/App.js +++ b/examples/native/App.js @@ -1,37 +1,49 @@ +import { types } from '@babel/core'; import React from 'react'; -import { StyleSheet, Alert, SafeAreaView } from 'react-native'; +import { StyleSheet, Alert, SafeAreaView, Button } from 'react-native'; import { WebView } from 'react-native-webview'; import { withJavascriptInterface } from 'react-native-webview-comlink'; +var webInterface + +const onButtonClick = () => { + console.log("onButtonClick ::"); + // TODO + // if(webInterface){ + // console.log("onButtonClick :: webInterface is not null"); + // webInterface.mobileToWeb("Mobile To Web"); + // } else { + // console.log("onButtonClick :: webInterface is null"); + // } +} + +// the root obj to be exposed to web +const jsInterface = { + webToMobile(event) { + console.log("event ::"+event); + }, + setMobileInterface( webInterface ) { + console.log("setMobileInterface :: before "+this.webInterface) + + + setTimeout(() => { + webInterface("mobile to Web 1") + }, 2000) + + setTimeout(() => { + webInterface("mobile to Web 2") + }, 4000) + // TODO + // this.webInterface = webInterface + // console.log("setMobileInterface :: after "+this.webInterface) + } +} export default class App extends React.Component { constructor(props) { super(props); - // the root obj to be exposed to web - const rootObj = { - alert: (title, message, onYes, onNo) => { - const withCleanup = (cb) => () => { - cb(); - onYes.release(); - onNo.release(); - }; - Alert.alert(title, message, [ - { - text: 'YES', - onPress: withCleanup(onYes), - }, - { - text: 'NO', - onPress: withCleanup(onNo), - }, - ]); - }, - someMethodWithError: () => - Promise.reject(Object.assign(new Error('something wrong'), { code: -1 })), - }; - // create higher-order WebView component - this.WebViewComponent = withJavascriptInterface(rootObj, 'MyJSInterface', { + this.WebViewComponent = withJavascriptInterface(jsInterface, 'CoreJSInterface', { forwardRef: true, log: true, })(WebView); @@ -45,8 +57,13 @@ export default class App extends React.Component { // you can also use your host ip address by editting `uri` // const uri = 'http://:3000'; const uri = 'http://localhost:3000'; + //const uri = 'http://172.20.10.11:3000'; // on Work Mac connected to public wifi and no proxy in emulator + //const uri = 'https://638082b614b0af602674e1bf--capable-lollipop-05625d.netlify.app/'; return ( +