Skip to content

Commit

Permalink
Merge pull request #337 from ospfranco/osp/revert-devtools
Browse files Browse the repository at this point in the history
Revert "Merge pull request #289 from pac-guerreiro/feature/remotedev"
  • Loading branch information
mountiny authored Sep 12, 2023
2 parents 8991d5b + ba7a7aa commit 3c7448e
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 400 deletions.
1 change: 0 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ Initialize the store with actions and listening for storage events
| [options.captureMetrics] | <code>Boolean</code> | | Enables Onyx benchmarking and exposes the get/print/reset functions |
| [options.shouldSyncMultipleInstances] | <code>Boolean</code> | | Auto synchronize storage events between multiple instances of Onyx running in different tabs/windows. Defaults to true for platforms that support local storage (web/desktop) |
| [options.debugSetState] | <code>Boolean</code> | | Enables debugging setState() calls to connected components. |
| [options.enableDevTools] | <code>Boolean</code> | | Enables debugging using Redux DevTools extension. |

**Example**
```js
Expand Down
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,18 +321,8 @@ Sample output of `Onyx.printMetrics()`

# Debug mode

## Using debugSetState

It can be useful to log why Onyx is calling `setState()` on a particular React component so that we can understand which key changed, what changed about the value, and the connected component that ultimately rendered as a result. When used correctly this can help isolate problem areas and unnecessary renders in the code. To enable this feature, pass `debugSetState: true` to the config and grep JS console logs for `[Onyx-Debug]`.

## Using Redux DevTools extension

It can be useful to check the order of writes to the storage and it's state at a specific point in time.

First, install Redux DevTools through your favorite browser ([Edge](https://microsoftedge.microsoft.com/addons/detail/redux-devtools/nnkgneoiohoecpdiaponcejilbhhikei), [Chrome](https://chrome.google.com/webstore/detail/redux-devtools/lmhkpmbekcpmknklioeibfkpmmfibljd), [Firefox](https://addons.mozilla.org/en-US/firefox/addon/reduxdevtools/))

Then, you can enable this type of debugging by passing `enableDevTools: true` to `Onyx.init({...})`.

# Development

`react-native` bundles source using the `metro` bundler. `metro` does not follow symlinks, so we can't use `npm link` to
Expand Down
55 changes: 0 additions & 55 deletions lib/DevTools.js

This file was deleted.

55 changes: 7 additions & 48 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import createDeferredTask from './createDeferredTask';
import fastMerge from './fastMerge';
import * as PerformanceUtils from './metrics/PerformanceUtils';
import Storage from './storage';
import DevTools from './DevTools';

// Method constants
const METHOD = {
Expand Down Expand Up @@ -38,11 +37,6 @@ let recentlyAccessedKeys = [];
// whatever appears in this list it will NEVER be a candidate for eviction.
let evictionAllowList = [];

let devTools = {
registerAction: () => {},
clearState: () => {},
};

// Holds a map of keys and connectionID arrays whose keys will never be automatically evicted as
// long as we have at least one subscriber that returns false for the canEvict property.
const evictionBlocklist = {};
Expand Down Expand Up @@ -848,10 +842,7 @@ function notifyCollectionSubscribersOnNextTick(key, value) {
function remove(key) {
cache.drop(key);
notifySubscribersOnNextTick(key, null);
return Storage.removeItem(key).then((result) => {
devTools.registerAction(`REMOVE/${key.toUpperCase()}`, undefined, {[key]: undefined});
return result;
});
return Storage.removeItem(key);
}

/**
Expand Down Expand Up @@ -975,7 +966,6 @@ function set(key, value) {
const hasChanged = cache.hasValueChanged(key, valueWithNullRemoved);

// This approach prioritizes fast UI changes without waiting for data to be stored in device storage.
broadcastUpdate(key, value, 'set');
broadcastUpdate(key, valueWithNullRemoved, hasChanged, 'set');

// If the value has not changed, calling Storage.setItem() would be redundant and a waste of performance, so return early instead.
Expand All @@ -984,10 +974,6 @@ function set(key, value) {
}

return Storage.setItem(key, valueWithNullRemoved)
.then((result) => {
devTools.registerAction(`SET/${key.toUpperCase()}`, valueWithNullRemoved, {[key]: valueWithNullRemoved});
return result;
})
.catch(error => evictStorageAndRetry(error, set, key, valueWithNullRemoved));
}

Expand Down Expand Up @@ -1018,11 +1004,6 @@ function multiSet(data) {
// Update cache and optimistically inform subscribers on the next tick
cache.set(key, val);
notifySubscribersOnNextTick(key, val);
if (_.isNull(val)) {
devTools.registerAction(`REMOVE/${key.toUpperCase()}`, val, {[key]: undefined});
} else {
devTools.registerAction(`SET/${key.toUpperCase()}`, val, {[key]: val});
}
});

return Storage.multiSet(keyValuePairs)
Expand Down Expand Up @@ -1117,10 +1098,7 @@ function merge(key, changes) {
return Promise.resolve();
}

return Storage.mergeItem(key, batchedChanges, modifiedData).then((results) => {
devTools.registerAction(`MERGE/${key.toUpperCase()}`, modifiedData, {[key]: modifiedData});
return results;
});
return Storage.mergeItem(key, batchedChanges, modifiedData);
} catch (error) {
Logger.logAlert(`An error occurred while applying merge for key: ${key}, Error: ${error}`);
}
Expand All @@ -1132,20 +1110,16 @@ function merge(key, changes) {
/**
* Merge user provided default key value pairs.
* @private
* @param {boolean} enableDevTools
* @returns {Promise}
*/
function initializeWithDefaultKeyStates(enableDevTools = false) {
function initializeWithDefaultKeyStates() {
return Storage.multiGet(_.keys(defaultKeyStates))
.then((pairs) => {
const asObject = _.object(pairs);

const merged = fastMerge(asObject, defaultKeyStates);
cache.merge(merged);
_.each(merged, (val, key) => keyChanged(key, val));
if (enableDevTools) {
devTools = new DevTools(merged);
}
});
}

Expand Down Expand Up @@ -1193,7 +1167,7 @@ function clear(keysToPreserve = []) {
// since collection key subscribers need to be updated differently
if (!isKeyToPreserve) {
const oldValue = cache.getValue(key);
const newValue = _.get(defaultKeyStates, key, undefined);
const newValue = _.get(defaultKeyStates, key, null);
if (newValue !== oldValue) {
cache.set(key, newValue);
const collectionKey = key.substring(0, key.indexOf('_') + 1);
Expand Down Expand Up @@ -1224,15 +1198,11 @@ function clear(keysToPreserve = []) {
notifyCollectionSubscribersOnNextTick(key, value);
});

const defaultKeyValueState = _.omit(defaultKeyStates, keysToPreserve);
const defaultKeyValuePairs = _.pairs(defaultKeyValueState);
const defaultKeyValuePairs = _.pairs(_.omit(defaultKeyStates, keysToPreserve));

// Remove only the items that we want cleared from storage, and reset others to default
_.each(keysToBeClearedFromStorage, key => cache.drop(key));
return Storage.removeItems(keysToBeClearedFromStorage).then(() => {
devTools.clearState(keysToBeClearedFromStorage);
return Storage.multiSet(defaultKeyValuePairs);
});
return Storage.removeItems(keysToBeClearedFromStorage).then(() => Storage.multiSet(defaultKeyValuePairs));
});
}

Expand Down Expand Up @@ -1306,11 +1276,6 @@ function mergeCollection(collectionKey, collection) {
Promise.all(_.map(existingKeys, get)).then(() => {
cache.merge(collection);
keysChanged(collectionKey, collection);
if (_.isNull(collection)) {
devTools.registerAction(`REMOVE/${collectionKey.toUpperCase()}`, collection, {[collectionKey]: undefined});
} else {
devTools.registerAction(`SET/${collectionKey.toUpperCase()}`, collection, {[collectionKey]: collection});
}
});

return Promise.all(promises)
Expand Down Expand Up @@ -1412,7 +1377,6 @@ function init({
captureMetrics = false,
shouldSyncMultipleInstances = Boolean(global.localStorage),
debugSetState = false,
enableDevTools = false,
} = {}) {
if (captureMetrics) {
// The code here is only bundled and applied when the captureMetrics is set
Expand Down Expand Up @@ -1445,19 +1409,14 @@ function init({
// Initialize all of our keys with data provided then give green light to any pending connections
Promise.all([
addAllSafeEvictionKeysToRecentlyAccessedList(),
initializeWithDefaultKeyStates(enableDevTools),
initializeWithDefaultKeyStates(),
])
.then(deferredInitTask.resolve);

if (shouldSyncMultipleInstances && _.isFunction(Storage.keepInstancesSync)) {
Storage.keepInstancesSync((key, value) => {
cache.set(key, value);
keyChanged(key, value);
if (_.isNull(value)) {
devTools.registerAction(`REMOVE/${key.toUpperCase()}`, value, {[key]: undefined});
} else {
devTools.registerAction(`SET/${key.toUpperCase()}`, value, {[key]: value});
}
});
}
}
Expand Down
Loading

0 comments on commit 3c7448e

Please sign in to comment.