Repository: jk-gan/redux-flipper Branch: master Commit: 70acfba590c1 Files: 8 Total size: 10.3 KB Directory structure: gitextract_nspd88ld/ ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── package.json ├── src/ │ └── index.ts └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc.js ================================================ module.exports = { env: { browser: true, es6: true, }, extends: ["airbnb-typescript", "prettier/@typescript-eslint"], globals: { Atomics: "readonly", SharedArrayBuffer: "readonly", }, parser: "@typescript-eslint/parser", parserOptions: { ecmaVersion: 2018, sourceType: "module", }, plugins: ["@typescript-eslint"], rules: {}, }; ================================================ FILE: .gitignore ================================================ node_modules /lib .DS_Store ================================================ FILE: .prettierrc ================================================ { "trailingComma": "all", "singleQuote": true } ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2022 Gan Jun Kai & Wai Pai Lee Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Redux Flipper ![screenshot of the plugin](https://i.imgur.com/blqn8oT.png) Redux middleware for [Flipper](https://fbflipper.com/). It can log redux actions and show inside Flipper using [flipper-plugin-redux-debugger](https://github.com/jk-gan/flipper-plugin-redux-debugger). ### Support - React Native - For `react-native` >= 0.62, flipper support is enabled by default - For `react-native` < 0.62, follow [these steps](https://fbflipper.com/docs/getting-started/react-native.html#manual-setup) to setup your app - Redux or Redux-Toolkit ## Get Started 1. Install [redux-flipper](https://github.com/jk-gan/redux-flipper) middleware and `react-native-flipper` in your React Native app: ```bash yarn add redux-flipper react-native-flipper # for iOS cd ios && pod install ``` 2. Add the middleware into your redux store: ```javascript import { createStore, applyMiddleware } from 'redux'; const middlewares = [ /* other middlewares */ ]; if (__DEV__) { const createDebugger = require('redux-flipper').default; middlewares.push(createDebugger()); } const store = createStore(RootReducer, applyMiddleware(...middlewares)); ``` Redux Toolkit ```javascript const middlewares = [ /* other middlewares */ ]; if (__DEV__) { const createDebugger = require('redux-flipper').default; middlewares.push(createDebugger()); } const store = configureStore({ reducer: {}, middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(middleware), }); ``` 3. Install [flipper-plugin-redux-debugger](https://github.com/jk-gan/flipper-plugin-redux-debugger) in Flipper desktop client: ``` Manage Plugins > Install Plugins > search "redux-debugger" > Install ``` 4. Start your app, then you should be able to see Redux Debugger on your Flipper app ## Optional Configuration ### State whitelisting Many times you are only interested in certain part of the Redux state when debugging. You can pass array of string which have to match to the root key of the Redux state. For example if Redux schema is something like this and you are only interested in user then you can whitelist only that part of the state ```typescript type ReduxState = { todos: string[]; notifications: string[]; user: { name: string; }; }; ``` ```javascript let reduxDebugger = createDebugger({ stateWhitelist: ['user'] }); ``` If you app has very big state tree it is also good idea to whitelist certain keys from Redux state otherwise Flipper can be very slow. ### Resolve cyclic reference Redux Debugger does not support cyclic reference objects by default as resolving it makes application slow. This feature can be enabled by passing `{ resolveCyclic: true }` into `createDebugger`. This is just a temporary solution if debugging is urgent. It is advisable to restructure your redux state structure. For more information about cyclic reference, visit [MDN Cyclic Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cyclic_object_value). ```javascript let reduxDebugger = createDebugger({ resolveCyclic: true }); ``` ### Actions Blacklist You may specify an actions blacklist the same way as with React Native Debugger, by providing an array of strings to match against the action.type field. This feature can be enabled by passing `{ actionsBlacklist }` into `createDebugger`, where `actionsBlacklist` is an array of strings. For example: ```javascript const actionsBlacklist = ['EVENTS/', 'LOCAL/setClock']; const reduxDebugger = createDebugger({ actionsBlacklist }); ``` This will exclude any actions that contain the substrings in the blacklist. So an action with type `EVENTS/foo` will not be sent to the redux debugger flipper plugin, but an action with type `LOCAL/anotherAction` will. ================================================ FILE: package.json ================================================ { "name": "redux-flipper", "version": "2.0.3", "description": "Redux middleware for flipper", "main": "lib/index.js", "types": "lib/index.d.ts", "author": "jk-gan (https://github.com/jk-gan)", "contributors": [ { "name": "Wai Pai Lee", "email": "pailee.wai@gmail.com", "url": "https://plwai.com/" } ], "homepage": "https://github.com/jk-gan/redux-flipper", "bugs": "https://github.com/jk-gan/redux-flipper/issues", "repository": { "url": "https://github.com/jk-gan/redux-flipper", "type": "git" }, "files": [ "lib/**/*" ], "license": "MIT", "scripts": { "build": "tsc", "dev": "tsc -w" }, "peerDependencies": { "react-native": ">=0.63.0", "react-native-flipper": ">=0.100.0", "redux": ">=4" }, "devDependencies": { "@types/node": "^16.4.8", "@typescript-eslint/eslint-plugin": "^4.28.5", "@typescript-eslint/parser": "^4.28.5", "eslint": "^7.32.0", "eslint-config-airbnb-typescript": "^12.3.1", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.20.2", "prettier": "^2.0.4", "typescript": "^4.3.5" }, "dependencies": { "cycle": "^1.0.3", "dayjs": "^1.8.29" } } ================================================ FILE: src/index.ts ================================================ import { addPlugin, Flipper } from 'react-native-flipper'; import * as dayjs from 'dayjs'; export type Configuration = { resolveCyclic?: boolean; actionsBlacklist?: Array; stateWhitelist?: string[]; }; const defaultConfig: Configuration = { resolveCyclic: false, actionsBlacklist: [], }; let currentConnection: Flipper.FlipperConnection | null = null; const error = { NO_STORE: 'NO_STORE', }; const createStateForAction = (state: any, config: Configuration) => { return config.stateWhitelist ? config.stateWhitelist.reduce( (acc, stateWhitelistedKey) => ({ ...acc, [stateWhitelistedKey]: state[stateWhitelistedKey], }), {}, ) : state; }; // To initiate initial state tree const createInitialAction = (store: any, config: Configuration) => { const startTime = Date.now(); let initState = store.getState(); if (config.resolveCyclic) { const cycle = require('cycle'); initState = cycle.decycle(initState); } let state = { id: startTime, time: dayjs(startTime).format('HH:mm:ss.SSS'), took: `-`, action: { type: '@@INIT' }, before: createStateForAction({}, config), after: createStateForAction(initState, config), }; currentConnection.send('actionInit', state); }; const createDebugger = (config = defaultConfig) => (store: any) => { if (currentConnection == null) { addPlugin({ getId() { return 'flipper-plugin-redux-debugger'; }, onConnect(connection: any) { currentConnection = connection; currentConnection.receive( 'dispatchAction', (data: any, responder: any) => { console.log('flipper redux dispatch action data', data); // respond with some data if (store) { store.dispatch({ type: data.type, ...data.payload }); responder.success({ ack: true, }); } else { responder.success({ error: error.NO_STORE, message: 'store is not setup in flipper plugin', }); } }, ); createInitialAction(store, config); }, onDisconnect() { currentConnection = null; }, runInBackground() { return true; }, }); } else { createInitialAction(store, config); } return (next: any) => (action: { type: string }) => { let startTime = Date.now(); let before = store.getState(); let result = next(action); if (currentConnection) { let after = store.getState(); let now = Date.now(); let decycledAction = null; if (config.resolveCyclic) { const cycle = require('cycle'); before = cycle.decycle(before); after = cycle.decycle(after); decycledAction = cycle.decycle(action); } let state = { id: startTime, time: dayjs(startTime).format('HH:mm:ss.SSS'), took: `${now - startTime} ms`, action: decycledAction || action, before: createStateForAction(before, config), after: createStateForAction(after, config), }; const blackListed = !!config.actionsBlacklist?.some( (blacklistedActionType) => action.type?.includes(blacklistedActionType), ); if (!blackListed) { currentConnection.send('actionDispatched', state); } } return result; }; }; export default createDebugger; ================================================ FILE: tsconfig.json ================================================ { "lib": ["es2015", "es2016"], "compilerOptions": { "target": "es2015", "module": "commonjs", "declaration": true, "outDir": "./lib", "strict": true, "allowJs": true }, "rules": { "ordered-imports": [false], "object-literal-sort-keys": [false] }, "include": ["src"], "exclude": ["node_modules", "**/__tests__/*"] }