Skip to content

Commit 68b2fd5

Browse files
authored
Merge pull request #39 from dogabudak/master
Passing single object rather than series of params
2 parents 67ae7ca + 66cd03c commit 68b2fd5

File tree

7 files changed

+81
-81
lines changed

7 files changed

+81
-81
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ npx react-native-performance-monitor get
1818
# Usage
1919
```
2020
import withPerformanceMonitor from 'react-native-performance-monitor/provider';
21-
export default withPerformanceMonitor(YourScreen, 'Screen Name');
21+
export default withPerformanceMonitor({WrappedComponent: YourScreen, _id: 'Screen Name'});
2222
```
2323

2424
# An example
@@ -51,14 +51,14 @@ With this before and after I observed the following within a large flat list.
5151
In order to connect to a real device you will need to set the IP of your computer, for example:
5252

5353
```
54-
export default withPerformanceMonitor(AwesomeChat, 'AwesomeChat', '192.168.1.10');
54+
export default withPerformanceMonitor({ WrappedComponent: AwesomeChat, _id: 'AwesomeChat', ip: '192.168.1.10'});
5555
```
5656

5757
By default the server is listening on port 8125 for event updates and 8126 for websocket.
5858
If you need to configure the port, because you are tunneling your request or similar, and or disable the Websocket communication, you can do it like this:
5959

6060
```
61-
export default withPerformanceMonitor(AwesomeChat, 'AwesomeChat', '192.168.1.10', undefined, undefined, 80, false);
61+
export default withPerformanceMonitor({WrappedComponent: AwesomeChat, _id: 'AwesomeChat', ip: '192.168.1.10', events: undefined, showLogs: undefined, port: 80, enableWS: false});
6262
```
6363

6464
# How it works

components/App.js

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import ReactFC from 'react-fusioncharts/lib/ReactFC';
77
import _ from 'lodash';
88
import parseInput from './parse-input';
99
import Row from './Row';
10-
import { max } from 'moment';
1110

1211
document.addEventListener('click', function(e) {
1312
if (document.activeElement.toString() == '[object HTMLButtonElement]') {

example/App.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ const styles = StyleSheet.create({
7070
}
7171
});
7272

73-
export default withPerformance(App, 'App');
73+
export default withPerformance({WrappedComponent: App, _id: 'App'});

lib/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ npx react-native-performance-monitor get
1414
# Usage
1515
```
1616
import withPerformanceMonitor from 'react-native-performance-monitor/provider';
17-
export default withPerformanceMonitor(YourScreen, 'Screen Name');
17+
export default withPerformanceMonitor({WrappedComponent: YourScreen, _id: 'Screen Name'});
1818
```
1919

2020
# An example

lib/provider.js

+68-68
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,83 @@ import React, { Component } from 'react';
33
import hoistNonReactStatics from 'hoist-non-react-statics';
44

55
const Profiler = React.Profiler;
6-
export default (WrappedComponent, _id, ip = '127.0.0.1', events = ['mount', 'update'], showLogs = false, port = 8125, enableWS = true) => {
7-
const remote = `http://${ip}:${port}/value`;
8-
const log = (message) => {
9-
if (showLogs) {
10-
console.log(message);
11-
}
12-
};
13-
class HOC extends Component {
14-
static displayName = 'withPerformance';
6+
export default ({ WrappedComponent, _id, ip = '127.0.0.1', events = ['mount', 'update'], showLogs = false, port = 8125, enableWS = true }) => {
7+
const remote = `http://${ip}:${port}/value`;
8+
const log = (message) => {
9+
if (showLogs) {
10+
console.log(message);
11+
}
12+
};
13+
class HOC extends Component {
14+
static displayName = 'withPerformance';
1515

16-
constructor(props) {
17-
super(props);
18-
this.state = {};
19-
}
16+
constructor(props) {
17+
super(props);
18+
this.state = {};
19+
}
2020

21-
componentDidMount() {
22-
if (!enableWS) return;
23-
this.socket = new WebSocket(`ws://${ip}:8126`);
24-
this.socket.onopen = function () {
25-
log('RNPM: connected');
26-
};
21+
componentDidMount() {
22+
if (!enableWS) return;
23+
this.socket = new WebSocket(`ws://${ip}:8126`);
24+
this.socket.onopen = function () {
25+
log('RNPM: connected');
26+
};
2727

28-
this.socket.onmessage = (event) => {
29-
switch (event.data) {
30-
case 'remount': {
31-
log('RNPM: remount');
32-
this.setState({ unmount: true }, () => {
33-
setTimeout(() => this.setState({ unmount: false }), 200);
34-
});
35-
break;
36-
}
37-
case 'forceUpdate': {
38-
log('RNPM: force update');
39-
this.forceUpdate();
40-
break;
41-
}
42-
default:
43-
break;
44-
}
45-
};
28+
this.socket.onmessage = (event) => {
29+
switch (event.data) {
30+
case 'remount': {
31+
log('RNPM: remount');
32+
this.setState({ unmount: true }, () => {
33+
setTimeout(() => this.setState({ unmount: false }), 200);
34+
});
35+
break;
36+
}
37+
case 'forceUpdate': {
38+
log('RNPM: force update');
39+
this.forceUpdate();
40+
break;
41+
}
42+
default:
43+
break;
4644
}
45+
};
46+
}
4747

48-
componentWillUnmount() {
49-
this.socket && this.socket.close();
50-
}
48+
componentWillUnmount() {
49+
this.socket && this.socket.close();
50+
}
5151

52-
logMeasurement = async (id, phase, actualDuration) => {
53-
if (!events.includes(phase)) {
54-
return;
55-
}
56-
if (actualDuration < 0.1) {
57-
return;
58-
}
52+
logMeasurement = async (id, phase, actualDuration) => {
53+
if (!events.includes(phase)) {
54+
return;
55+
}
56+
if (actualDuration < 0.1) {
57+
return;
58+
}
5959

60-
if (remote) {
61-
fetch(remote, {
62-
method: 'POST',
63-
headers: {
64-
'Content-Type': 'application/json',
65-
},
66-
body: JSON.stringify({ value: actualDuration }),
67-
});
68-
}
69-
}
60+
if (remote) {
61+
fetch(remote, {
62+
method: 'POST',
63+
headers: {
64+
'Content-Type': 'application/json',
65+
},
66+
body: JSON.stringify({ value: actualDuration }),
67+
});
68+
}
69+
}
7070

71-
render() {
72-
return this.state.unmount ? null : (
73-
<Profiler id={_id} onRender={this.logMeasurement}>
74-
<WrappedComponent
75-
{...this.props}
76-
{...this.state}
77-
/>
78-
</Profiler>
71+
render() {
72+
return this.state.unmount ? null : (
73+
<Profiler id={_id} onRender={this.logMeasurement}>
74+
<WrappedComponent
75+
{...this.props}
76+
{...this.state}
77+
/>
78+
</Profiler>
7979

80-
);
81-
}
80+
);
8281
}
82+
}
8383

84-
return hoistNonReactStatics(HOC, WrappedComponent);;
84+
return hoistNonReactStatics(HOC, WrappedComponent);
8585
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "ssg-homepage",
2+
"name": "react-native-performance-monitor",
33
"version": "1.0.0",
44
"description": "",
55
"main": "index.js",

server.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import { join } from 'path';
2+
import cacheableResponse from 'cacheable-response';
3+
import express from 'express';
4+
import bodyParser from 'body-parser';
5+
16
require('@babel/polyfill');
27

3-
const { join } = require('path');
4-
const cacheableResponse = require('cacheable-response');
5-
const express = require('express');
68
const next = require('next');
7-
89
const port = parseInt(process.env.PORT, 10) || 3000;
910
const dev = process.env.NODE_ENV !== 'production';
1011
const app = next({ dev });
@@ -70,8 +71,8 @@ Promise.all([
7071
});
7172
}
7273

73-
server.post('/value', require('body-parser').json(), (req, res) => {
74-
if (req.body && req.body.value) {
74+
server.post('/value', bodyParser.json(), (req, res) => {
75+
if (req?.body?.value) {
7576
io.emit('data', req.body.value);
7677
}
7778
res.send('');

0 commit comments

Comments
 (0)