mirror of
https://github.com/Tencent/Hippy.git
synced 2026-10-28 13:38:10 +08:00
fix(hippy-react): remove NetInfo duplicated EventEmitter creating
This commit is contained in:
@@ -1,100 +0,0 @@
|
||||
import React from 'react';
|
||||
import {
|
||||
ScrollView,
|
||||
Text,
|
||||
View,
|
||||
StyleSheet,
|
||||
callNative,
|
||||
HippyEventEmitter,
|
||||
} from '@hippy/react';
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
text: {
|
||||
fontSize: 14,
|
||||
color: '#242424',
|
||||
alignSelf: 'center',
|
||||
},
|
||||
container: {
|
||||
flex: 1,
|
||||
},
|
||||
itemTitle: {
|
||||
alignItems: 'flex-start',
|
||||
justifyContent: 'center',
|
||||
height: 80,
|
||||
borderWidth: 1,
|
||||
borderStyle: 'solid',
|
||||
borderColor: '#e0e0e0',
|
||||
borderRadius: 2,
|
||||
backgroundColor: '#fafafa',
|
||||
padding: 10,
|
||||
marginTop: 10,
|
||||
},
|
||||
itemContent: {
|
||||
alignItems: 'flex-start',
|
||||
justifyContent: 'center',
|
||||
height: 100,
|
||||
borderBottomWidth: 1,
|
||||
borderLeftWidth: 1,
|
||||
borderRightWidth: 1,
|
||||
borderStyle: 'solid',
|
||||
borderRadius: 2,
|
||||
borderColor: '#e0e0e0',
|
||||
backgroundColor: '#ffffff',
|
||||
padding: 10,
|
||||
},
|
||||
normalText: {
|
||||
fontSize: 14,
|
||||
lineHeight: 18,
|
||||
color: 'black',
|
||||
},
|
||||
});
|
||||
|
||||
export default class NativeEvent extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
infoText: '',
|
||||
infoText2: '',
|
||||
};
|
||||
}
|
||||
|
||||
render() {
|
||||
const { infoText, infoText2 } = this.state;
|
||||
return (
|
||||
<ScrollView style={styles.container}>
|
||||
<View
|
||||
style={styles.itemTitle}
|
||||
onPress={() => {
|
||||
const hippyEventEmitter = new HippyEventEmitter();
|
||||
this.call = hippyEventEmitter.addListener('NORMAL_EVENT', (e) => {
|
||||
this.setState({
|
||||
infoText: JSON.stringify(e),
|
||||
});
|
||||
});
|
||||
callNative('MyEvent', 'btnClicked');
|
||||
}}
|
||||
>
|
||||
<Text>客户端向前端单向通信</Text>
|
||||
<Text>点此终端会发送NORMAL_EVENT事件</Text>
|
||||
<Text>{ `事件接收结果:${infoText}` }</Text>
|
||||
</View>
|
||||
<View
|
||||
style={styles.itemTitle}
|
||||
onPress={() => {
|
||||
const hippyEventEmitter = new HippyEventEmitter();
|
||||
this.call = hippyEventEmitter.addListener('COUNT_DOWN', (e) => {
|
||||
this.setState({
|
||||
infoText2: JSON.stringify(e),
|
||||
});
|
||||
});
|
||||
callNative('MyEvent', 'addListener', 'COUNT_DOWN');
|
||||
}}
|
||||
>
|
||||
<Text>客户端向前端单向通信的进阶用法</Text>
|
||||
<Text>点此终端会发送COUNT_DOWN事件</Text>
|
||||
<Text>{ `事件接收结果:${infoText2}` }</Text>
|
||||
</View>
|
||||
</ScrollView>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
import { callNative, callNativeWithPromise } from '@hippy/react';
|
||||
|
||||
/*
|
||||
自定义module
|
||||
*/
|
||||
const TestModule = {
|
||||
log(msg) {
|
||||
callNative('TestModule', 'log', msg);
|
||||
},
|
||||
helloNative(msg) {
|
||||
callNative('TestModule', 'helloNative', msg);
|
||||
},
|
||||
helloNativeWithPromise(msg) {
|
||||
return callNativeWithPromise('TestModule', 'helloNativeWithPromise', msg);
|
||||
},
|
||||
};
|
||||
|
||||
export default TestModule;
|
||||
@@ -1,28 +0,0 @@
|
||||
import React from 'react';
|
||||
import { Text } from '@hippy/react';
|
||||
import TestModule from './TestModule';
|
||||
|
||||
// 展示自定义Module的使用
|
||||
export default class TestModuleDemo extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = { hello: 'TestModule log' };
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
TestModule.log('hello i am from js');
|
||||
TestModule.helloNative({ hello: 'i am form js' });
|
||||
TestModule.helloNativeWithPromise({ hello: 'i am form js' }).then((e) => {
|
||||
this.setState({ hello: JSON.stringify(e) });
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const { hello } = this.state;
|
||||
return (
|
||||
<Text style={{ color: 'red' }}>
|
||||
{hello}
|
||||
</Text>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,6 @@
|
||||
export { default as Animation } from './Animation';
|
||||
export { default as AsyncStorage } from './AsyncStorage';
|
||||
export { default as Clipboard } from './Clipboard';
|
||||
export { default as NativeEvent } from './NativeEvent';
|
||||
export { default as NetInfo } from './NetInfo';
|
||||
export { default as TestModule } from './TestModule';
|
||||
export { default as WebSocket } from './WebSocket';
|
||||
export { default as UIManagerModule } from './UIManagerModule';
|
||||
|
||||
@@ -32,9 +32,9 @@ interface NetInfoRevoker {
|
||||
}
|
||||
|
||||
const DEVICE_CONNECTIVITY_EVENT = 'networkStatusDidChange';
|
||||
const subScriptions = new Map();
|
||||
const subscriptions = new Map();
|
||||
|
||||
let NetInfoEventEmitter: HippyEventEmitter;
|
||||
const NetInfoEventEmitter: HippyEventEmitter = new HippyEventEmitter();
|
||||
|
||||
class NetInfoRevoker implements NetInfoRevoker {
|
||||
public constructor(eventName: string, listener: (data: any) => void) {
|
||||
@@ -60,7 +60,6 @@ class NetInfoRevoker implements NetInfoRevoker {
|
||||
* @returns {object} NetInfoRevoker - The event revoker for destroy the network info event listener.
|
||||
*/
|
||||
function addEventListener(eventName: string, listener: NetworkInfoCallback): NetInfoRevoker {
|
||||
NetInfoEventEmitter = new HippyEventEmitter();
|
||||
let event = eventName;
|
||||
if (event && event === 'change') {
|
||||
event = DEVICE_CONNECTIVITY_EVENT;
|
||||
@@ -75,8 +74,7 @@ function addEventListener(eventName: string, listener: NetworkInfoCallback): Net
|
||||
listener(data);
|
||||
},
|
||||
);
|
||||
// FIXME: Seems only accept one callback for each event, should support multiple callback.
|
||||
subScriptions.set(listener, handler);
|
||||
subscriptions.set(listener, handler);
|
||||
return new NetInfoRevoker(event, listener);
|
||||
}
|
||||
|
||||
@@ -101,12 +99,12 @@ function removeEventListener(eventName: string, listener?: NetInfoRevoker | Netw
|
||||
if (count <= 1) {
|
||||
Bridge.callNative('NetInfo', 'removeListener', event);
|
||||
}
|
||||
const handler = subScriptions.get(listener);
|
||||
const handler = subscriptions.get(listener);
|
||||
if (!handler) {
|
||||
return;
|
||||
}
|
||||
handler.remove();
|
||||
subScriptions.delete(listener);
|
||||
subscriptions.delete(listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user