Skip to content
This repository was archived by the owner on Dec 6, 2023. It is now read-only.

Commit f112a2a

Browse files
authored
feat: Switch to FBs react native flipper plugin
1 parent e0e93dd commit f112a2a

5 files changed

Lines changed: 97 additions & 26 deletions

File tree

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@
4343
"reactotron-core-client": "2.8.9",
4444
"rn-host-detect": "1.1.5"
4545
},
46+
"optionalDependencies": {
47+
"react-native-flipper": "^0.0.1"
48+
},
4649
"devDependencies": {
4750
"@babel/core": "7.6.2",
4851
"@babel/plugin-proposal-class-properties": "7.5.5",
@@ -69,6 +72,7 @@
6972
"prettier": "1.18.2",
7073
"react": "16.9.0",
7174
"react-native": "0.61.1",
75+
"react-native-flipper": "^0.0.1",
7276
"rollup": "1.21.4",
7377
"rollup-plugin-babel": "4.3.3",
7478
"rollup-plugin-babel-minify": "9.1.0",

rollup.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ function getPlugins() {
1818

1919
const EXTERNALS = [
2020
"reactotron-core-client",
21+
"react-native-flipper",
2122
"react",
2223
"react-native",
2324
"react-native/Libraries/Network/XHRInterceptor",
@@ -35,4 +36,13 @@ export default [
3536
plugins: getPlugins(),
3637
external: EXTERNALS,
3738
},
39+
{
40+
input: "src/flipper-connection-manager.ts",
41+
output: {
42+
file: "dist/flipper.js",
43+
format: "cjs",
44+
},
45+
plugins: getPlugins(),
46+
external: EXTERNALS,
47+
},
3848
]

src/connection-manager.ts

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,21 @@
1-
import { NativeEventEmitter, NativeModules } from "react-native"
2-
31
export default class ConnectionManager {
42
private webSocket: any
5-
private flipperConnection: any
63

74
constructor(path?: string) {
85
this.webSocket = new WebSocket(path)
9-
this.flipperConnection = NativeModules.Reactotron
106
}
117

128
send(payload: any) {
139
if (this.webSocket.readyState === 1) {
1410
this.webSocket.send(payload)
1511
}
16-
17-
if (this.flipperConnection) {
18-
this.flipperConnection.sendCommand("command", JSON.parse(payload))
19-
}
2012
}
2113

2214
on(event: "open" | "close" | "message", callback: any) {
2315
if (event === "open") {
24-
if (this.flipperConnection) {
25-
callback()
26-
}
27-
2816
this.webSocket.onopen = callback
29-
} else if (event === "close") {
30-
this.webSocket.onclose = () => {
31-
if (!this.flipperConnection) {
32-
callback()
33-
}
34-
}
3517
} else if (event === "message") {
3618
this.webSocket.onmessage = evt => callback(evt.data)
37-
38-
if (this.flipperConnection) {
39-
const flipperConnectionEmitter = new NativeEventEmitter(NativeModules.Reactotron)
40-
41-
flipperConnectionEmitter.addListener("CommandReceived", command => {
42-
callback(command)
43-
})
44-
}
4519
}
4620
}
4721

src/flipper-connection-manager.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { addPlugin } from "react-native-flipper"
2+
3+
import ConnectionManager from "./connection-manager"
4+
5+
export default class FlipperConnectionManager {
6+
private baseConnectionManager: ConnectionManager
7+
// private flipperConnection: Flipper.FlipperConnection
8+
private flipperConnection: any
9+
10+
private openCallbacks: (() => void)[] = []
11+
private closeCallbacks: (() => void)[] = []
12+
private messageCallbacks: ((message: string) => void)[] = []
13+
14+
constructor(path?: string) {
15+
this.baseConnectionManager = new ConnectionManager(path)
16+
17+
addPlugin({
18+
getId() {
19+
return "flipper-plugin-reactotron"
20+
},
21+
onConnect: this.handleConnect,
22+
onDisconnect: this.handleDisconnect,
23+
runInBackground: () => true,
24+
})
25+
}
26+
27+
// handleConnect = (connection: Flipper.FlipperConnection) => {
28+
handleConnect = (connection: any) => {
29+
this.flipperConnection = connection
30+
31+
connection.receive("sendReactotronCommand", (data, responder) => {
32+
this.handleMessage(data)
33+
responder.success()
34+
})
35+
36+
this.openCallbacks.forEach(callback => callback())
37+
}
38+
39+
handleMessage = data => {
40+
this.messageCallbacks.forEach(callback => callback(data))
41+
}
42+
43+
handleDisconnect = () => {
44+
this.flipperConnection = null
45+
this.closeCallbacks.forEach(callback => callback())
46+
}
47+
48+
send(payload: any) {
49+
const parsedPayload = JSON.parse(payload)
50+
this.baseConnectionManager.send(parsedPayload)
51+
this.flipperConnection.send("Command", parsedPayload)
52+
}
53+
54+
on(event: "open" | "close" | "message", callback: any) {
55+
this.baseConnectionManager.on(event, callback)
56+
57+
switch (event) {
58+
case "open":
59+
if (this.flipperConnection) {
60+
// If we are already connected, let them know right now.
61+
callback()
62+
}
63+
64+
this.openCallbacks.push(callback)
65+
break
66+
case "close":
67+
this.closeCallbacks.push(callback)
68+
break
69+
case "message":
70+
this.messageCallbacks.push(callback)
71+
break
72+
}
73+
}
74+
75+
close() {
76+
this.baseConnectionManager.close()
77+
}
78+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9204,6 +9204,11 @@ react-is@^16.8.1, react-is@^16.8.4:
92049204
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"
92059205
integrity sha512-aUk3bHfZ2bRSVFFbbeVS4i+lNPZr3/WM5jT2J5omUVV1zzcs1nAaf3l51ctA5FFvCRbhrH0bdAsRRQddFJZPtA==
92069206

9207+
react-native-flipper@^0.0.1:
9208+
version "0.0.1"
9209+
resolved "https://registry.yarnpkg.com/react-native-flipper/-/react-native-flipper-0.0.1.tgz#0ab983343f2243ed266b0928665a284e55d125bd"
9210+
integrity sha512-CXlv55ZlO64XB9+ziGyizWge+xJl+RYnqt3fl4i6Fivw2oZwzrDhTxB7tZT1LyRaQUaZhcbOFbDLTHVS2PiLNg==
9211+
92079212
react-native@0.61.1:
92089213
version "0.61.1"
92099214
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.61.1.tgz#ac2559f78f9e689356fedfa93159026a25313d7d"

0 commit comments

Comments
 (0)