Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/plugins/async-storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ And you're done! Now you can see your AsyncStorage in Reactotron.

## Advanced Usage

`asyncStorage()` also accepts an object with an `ignore` key. The value is an array of strings you would like to prevent sending to Reactotron.
`asyncStorage()` also accepts an object with an `ignore` key to filter against. The value of `ignore` is an array of string or RegExp patterns that match against AsyncStorage keys you want to prevent from being sent to Reactotron.

> String patterns match any key containing that text, while RegExp patterns use standard regex matching.

```js
asyncStorage({
ignore: ["secret"],
ignore: ["secret", /^debug_/, "temp"],
})
```
26 changes: 19 additions & 7 deletions lib/reactotron-react-native/src/plugins/asyncStorage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import type { ReactotronCore, Plugin } from "reactotron-core-client"
import type { AsyncStorageStatic } from "@react-native-async-storage/async-storage"
export interface AsyncStorageOptions {
ignore?: string[]
ignore?: (string | RegExp)[]
}

const PLUGIN_DEFAULTS: AsyncStorageOptions = {
ignore: [],
}

function shouldIgnore(key, ignore) {
for (const pattern of ignore) {
if (typeof pattern === "string") {
return key.includes(pattern)
}
if (pattern instanceof RegExp) {
return pattern.test(key)
}
}
return false
}

const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronCore) => {
// setup configuration
const config = Object.assign({}, PLUGIN_DEFAULTS, options || {})
Expand All @@ -28,7 +40,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC

const setItem: AsyncStorageStatic["setItem"] = async (key, value, callback) => {
try {
if (ignore.indexOf(key) < 0) {
if (!shouldIgnore(key, ignore)) {
sendToReactotron("setItem", { key, value })
}
} catch (e) {}
Expand All @@ -37,7 +49,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC

const removeItem: AsyncStorageStatic["removeItem"] = async (key, callback) => {
try {
if (ignore.indexOf(key) < 0) {
if (!shouldIgnore(key, ignore)) {
sendToReactotron("removeItem", { key })
}
} catch (e) {}
Expand All @@ -46,7 +58,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC

const mergeItem: AsyncStorageStatic["mergeItem"] = async (key, value, callback) => {
try {
if (ignore.indexOf(key) < 0) {
if (!shouldIgnore(key, ignore)) {
sendToReactotron("mergeItem", { key, value })
}
} catch (e) {}
Expand All @@ -63,7 +75,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC
const multiSet: AsyncStorageStatic["multiSet"] = async (pairs, callback) => {
try {
const shippablePairs = (pairs || []).filter(
(pair) => pair && pair[0] && ignore.indexOf(pair[0]) < 0
(pair) => pair && pair[0] && !shouldIgnore(pair[0], ignore)
)
if (shippablePairs.length > 0) {
sendToReactotron("multiSet", { pairs: shippablePairs })
Expand All @@ -74,7 +86,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC

const multiRemove: AsyncStorageStatic["multiRemove"] = async (keys, callback) => {
try {
const shippableKeys = (keys || []).filter((key) => ignore.indexOf(key) < 0)
const shippableKeys = (keys || []).filter((key) => !shouldIgnore(key, ignore))
if (shippableKeys.length > 0) {
sendToReactotron("multiRemove", { keys: shippableKeys })
}
Expand All @@ -85,7 +97,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC
const multiMerge: AsyncStorageStatic["multiMerge"] = async (pairs, callback) => {
try {
const shippablePairs = (pairs || []).filter(
(pair) => pair && pair[0] && ignore.indexOf(pair[0]) < 0
(pair) => pair && pair[0] && !shouldIgnore(pair[0], ignore)
)
if (shippablePairs.length > 0) {
sendToReactotron("multiMerge", { pairs: shippablePairs })
Expand Down