Skip to content

Commit

Permalink
Adding preview for AsyncStorage messages. (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrannp authored and skellock committed Apr 15, 2017
1 parent 4d592a7 commit 754dfda
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ lerna-debug.log
npm-debug.log*
npm-debug.log
.vscode
.idea
yarn.lock
yarn-error.log
.happypack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ class AsyncStorageValuesCommand extends Component {

render () {
const { command } = this.props
const { payload = [] } = command
const rows = mapIndexed(this.renderItem, payload)
const { preview, value = [] } = command.payload
const rows = mapIndexed(this.renderItem, value)
return (
<Command command={command} title={COMMAND_TITLE}>
<Command command={command} title={COMMAND_TITLE} preview={preview}>
<div style={Styles.watches}>
{rows}
{rows.length > 0 ? rows : 'Empty storage.'}
</div>
</Command>
)
Expand Down
28 changes: 17 additions & 11 deletions packages/reactotron-react-native/src/plugins/async-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default options => reactotron => {
/**
* Sends the contents of AsyncStorage to Reactotron.
*/
const reactotronShipStorageValues = () => {
const reactotronShipStorageValues = (methodName, args) => {
AsyncStorage.getAllKeys(
(_, keys) => AsyncStorage.multiGet(keys, (_, values = []) => {
const valuesToSend = reject(
Expand All @@ -36,7 +36,12 @@ export default options => reactotron => {
)
// NOTE(steve): for now let's ship everything... until we get a UI in place
// to make request/response calls
reactotron.send('asyncStorage.values.change', valuesToSend)
let previewArgs = ''
if (args && args.length > 1) {
previewArgs = Array.isArray(args[0]) ? `Array: ${args[0].length}` : args[0]
}
const preview = methodName ? `${methodName}(${previewArgs})` : ''
reactotron.send('asyncStorage.values.change', { preview, value: valuesToSend })
})
)
}
Expand All @@ -45,8 +50,9 @@ export default options => reactotron => {
* Swizzles one of the AsyncStorage public api functions.
*
* @param {function} originalMethod - The original function to override.
* @param {string} methodName - The original method name we are overriding.
*/
const reactotronStorageHijacker = originalMethod => (...args) => {
const reactotronStorageHijacker = (originalMethod, methodName) => (...args) => {
// Depending on the call we could have the callback in one of any of the three args, walk backwards till we find the callback
let oldCallback = args.length > 0 ? args[args.length - 1] : null

Expand All @@ -58,7 +64,7 @@ export default options => reactotron => {
const newArgs = [
...args.slice(0, args.length - 1),
(...cbArgs) => {
reactotronShipStorageValues()
reactotronShipStorageValues(methodName, args)
oldCallback(...cbArgs)
}
]
Expand All @@ -70,25 +76,25 @@ export default options => reactotron => {
if (isSwizzled) return

swizzSetItem = AsyncStorage.setItem
AsyncStorage.setItem = reactotronStorageHijacker(swizzSetItem)
AsyncStorage.setItem = reactotronStorageHijacker(swizzSetItem, 'setItem')

swizzRemoveItem = AsyncStorage.removeItem
AsyncStorage.removeItem = reactotronStorageHijacker(swizzRemoveItem)
AsyncStorage.removeItem = reactotronStorageHijacker(swizzRemoveItem, 'removeItem')

swizzMergeItem = AsyncStorage.mergeItem
AsyncStorage.mergeItem = reactotronStorageHijacker(swizzMergeItem)
AsyncStorage.mergeItem = reactotronStorageHijacker(swizzMergeItem, 'mergeItem')

swizzClear = AsyncStorage.clear
AsyncStorage.clear = reactotronStorageHijacker(swizzClear)
AsyncStorage.clear = reactotronStorageHijacker(swizzClear, 'clear')

swizzMultiSet = AsyncStorage.multiSet
AsyncStorage.multiSet = reactotronStorageHijacker(swizzMultiSet)
AsyncStorage.multiSet = reactotronStorageHijacker(swizzMultiSet, 'multiSet')

swizzMultiRemove = AsyncStorage.multiRemove
AsyncStorage.multiRemove = reactotronStorageHijacker(swizzMultiRemove)
AsyncStorage.multiRemove = reactotronStorageHijacker(swizzMultiRemove, 'multiRemove')

swizzMultiMerge = AsyncStorage.multiMerge
AsyncStorage.multiMerge = reactotronStorageHijacker(swizzMultiMerge)
AsyncStorage.multiMerge = reactotronStorageHijacker(swizzMultiMerge, 'multiMerge')

isSwizzled = true
}
Expand Down

0 comments on commit 754dfda

Please sign in to comment.