Guest User

Untitled

a guest
Jun 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. handleMessage = (event) => {
  2. let msgData;
  3. try {
  4. msgData = JSON.parse(event.nativeEvent.data);
  5. if (
  6. msgData.hasOwnProperty('prefix') &&
  7. msgData.prefix === MESSAGE_PREFIX
  8. ) {
  9. console.log(`WebViewLeaflet: received message: `, msgData.payload);
  10.  
  11. // If a message object includes an 'event' key then the webpage is sending an event,
  12. // Pass it to the parent by calling the parent function that has the same name
  13. // as the event, and passing the entire payload as the argument to that event
  14. if (
  15. // check to see if an event key exists
  16. msgData.payload.event &&
  17. // check to see if the eventReceiver (typically the parent component) has a matching
  18. // function for this event
  19. this.props.eventReceiver.hasOwnProperty(msgData.payload.event)
  20. ) {
  21. // call the function, and pass it the payload as the argument
  22. this.props.eventReceiver[msgData.payload.event](msgData.payload);
  23. }
  24. // If no event key was found, then update the eventReceiver's state
  25. else {
  26. this.props.eventReceiver.setState({
  27. state: {
  28. ...this.props.eventReceiver.state,
  29. mapState: {
  30. ...this.props.eventReceiver.mapState,
  31. ...msgData.payload
  32. }
  33. }
  34. });
  35. }
  36. }
  37. } catch (err) {
  38. console.warn(err);
  39. return;
  40. }
  41. };
Add Comment
Please, Sign In to add comment