1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
import { Tron } from './tron'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { RootStore } from '../../models/root-store/root-store'
import { onSnapshot } from 'mobx-state-tree'
import {
ReactotronConfig,
DEFAULT_REACTOTRON_CONFIG,
} from './reactotron-config'
import { mst } from 'reactotron-mst'
import { clear } from '../../utils/storage'
import { RootNavigation } from '../../navigators'
import { Platform } from 'react-native'
// Teach TypeScript about the bad things we want to do.
declare global {
interface Console {
/**
* Hey, it's Reactotron if we're in dev, and no-ops if we're in prod.
*/
tron: typeof Tron
}
}
/** Do Nothing. */
const noop = () => undefined
// in dev, we attach Reactotron, in prod we attach a interface-compatible mock.
if (__DEV__) {
console.tron = Tron // attach reactotron to `console.tron`
} else {
// attach a mock so if things sneaky by our __DEV__ guards, we won't crash.
console.tron = {
benchmark: noop,
clear: noop,
close: noop,
configure: noop,
connect: noop,
display: noop,
error: noop,
image: noop,
log: noop,
logImportant: noop,
onCustomCommand: noop,
overlay: noop,
reportError: noop,
send: noop,
startTimer: noop,
storybookSwitcher: noop,
use: noop,
useReactNative: noop,
warn: noop,
}
}
/**
* You'll probably never use the service like this since we hang the Reactotron
* instance off of `console.tron`. This is only to be consistent with the other
* services.
*/
export class Reactotron {
config: ReactotronConfig
rootStore: any
/**
* Create the Reactotron service.
*
* @param config the configuration
*/
constructor(config: ReactotronConfig = DEFAULT_REACTOTRON_CONFIG) {
// merge the passed in config with some defaults
this.config = {
host: 'localhost',
useAsyncStorage: true,
...config,
state: {
initial: false,
snapshots: false,
...(config && config.state),
},
}
}
/**
* Hook into the root store for doing awesome state-related things.
*
* @param rootStore The root store
*/
setRootStore(rootStore: any, initialData: any) {
if (__DEV__) {
rootStore = rootStore as RootStore // typescript hack
this.rootStore = rootStore
const { initial, snapshots } = this.config.state
const name = 'ROOT STORE'
// logging features
if (initial) {
console.tron.display({
name,
value: initialData,
preview: 'Initial State',
})
}
// log state changes?
if (snapshots) {
onSnapshot(rootStore, (snapshot) => {
console.tron.display({ name, value: snapshot, preview: 'New State' })
})
}
console.tron.trackMstNode(rootStore)
}
}
/**
* Configure reactotron based on the the config settings passed in, then connect if we need to.
*/
async setup() {
// only run this in dev... metro bundler will ignore this block: 🎉
if (__DEV__) {
// configure reactotron
Tron.configure({
name: this.config.name || require('../../../package.json').name,
host: this.config.host,
})
// hookup middleware
if (Platform.OS !== 'web') {
if (this.config.useAsyncStorage) {
Tron.setAsyncStorageHandler(AsyncStorage)
}
Tron.useReactNative({
asyncStorage: this.config.useAsyncStorage ? undefined : false,
})
}
// ignore some chatty `mobx-state-tree` actions
const RX = /postProcessSnapshot|@APPLY_SNAPSHOT/
// hookup mobx-state-tree middleware
Tron.use(
mst({
filter: (event) => RX.test(event.name) === false,
}),
)
// connect to the app
Tron.connect()
// Register Custom Commands
Tron.onCustomCommand({
title: 'Reset Root Store',
description: 'Resets the MST store',
command: 'resetStore',
handler: () => {
console.tron.log('resetting store')
clear()
},
})
Tron.onCustomCommand({
title: 'Reset Navigation State',
description: 'Resets the navigation state',
command: 'resetNavigation',
handler: () => {
console.tron.log('resetting navigation state')
RootNavigation.resetRoot({ routes: [] })
},
})
Tron.onCustomCommand({
title: 'Go Back',
description: 'Goes back',
command: 'goBack',
handler: () => {
console.tron.log('Going back')
RootNavigation.goBack()
},
})
// clear if we should
if (this.config.clearOnLoad) {
Tron.clear()
}
}
}
}
|