Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove string selector #287

Merged
merged 2 commits into from
Jul 27, 2023
Merged
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
20 changes: 7 additions & 13 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-continue */
import {deepEqual} from 'fast-equals';
import lodashGet from 'lodash/get';
import _ from 'underscore';
import * as Logger from './Logger';
import cache from './OnyxCache';
Expand Down Expand Up @@ -48,17 +47,13 @@ let defaultKeyStates = {};
const deferredInitTask = createDeferredTask();

/**
* Uses a selector string or function to return a simplified version of sourceData
* Uses a selector function to return a simplified version of sourceData
* @param {Mixed} sourceData
* @param {String|Function} selector
* @param {Function} selector Function that takes sourceData and returns a simplified version of it
* @param {Object} [withOnyxInstanceState]
* If it's a string, the selector is passed to lodashGet on the sourceData
* If it's a function, it is passed the sourceData and it should return the simplified data
* @returns {Mixed}
*/
const getSubsetOfData = (sourceData, selector, withOnyxInstanceState) => (_.isFunction(selector)
? selector(sourceData, withOnyxInstanceState)
: lodashGet(sourceData, selector));
const getSubsetOfData = (sourceData, selector, withOnyxInstanceState) => selector(sourceData, withOnyxInstanceState);

/**
* Takes a collection of items (eg. {testKey_1:{a:'a'}, testKey_2:{b:'b'}})
Expand Down Expand Up @@ -666,11 +661,10 @@ function getCollectionDataAndSendAsObject(matchingKeys, mapping) {
* @param {Boolean} [mapping.initWithStoredValues] If set to false, then no data will be prefilled into the
* component
* @param {Boolean} [mapping.waitForCollectionCallback] If set to true, it will return the entire collection to the callback as a single object
* @param {String|Function} [mapping.selector] THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data.
* If the selector is a string, the selector is passed to lodashGet on the sourceData. If the selector is a function, the sourceData and withOnyx state are
* passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive performance benefits because the component
* will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally cause the component to re-render (and that can
* be expensive from a performance standpoint).
* @param {Function} [mapping.selector] THIS PARAM IS ONLY USED WITH withOnyx(). If included, this will be used to subscribe to a subset of an Onyx key's data.
* The sourceData and withOnyx state are passed to the selector and should return the simplified data. Using this setting on `withOnyx` can have very positive
* performance benefits because the component will only re-render when the subset of data changes. Otherwise, any change of data on any property would normally
* cause the component to re-render (and that can be expensive from a performance standpoint).
* @returns {Number} an ID to use when calling disconnect
*/
function connect(mapping) {
Expand Down
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
"dependencies": {
"ascii-table": "0.0.9",
"fast-equals": "^4.0.3",
"lodash": "^4.17.21",
"underscore": "^1.13.1"
},
"devDependencies": {
Expand Down
30 changes: 0 additions & 30 deletions tests/unit/subscribeToPropertiesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,6 @@ describe('Only the specific property changes when using withOnyx() and ', () =>
});
};

it('connecting to a single non-collection key with a selector string', () => {
const TestComponentWithOnyx = withOnyx({
propertyA: {
key: ONYX_KEYS.TEST_KEY,
selector: 'a',
},
})(ViewWithObject);
return runAssertionsWithComponent(TestComponentWithOnyx);
});

it('connecting to a single non-collection key with a selector function', () => {
const mockedSelector = jest.fn(obj => obj && obj.a);
const TestComponentWithOnyx = withOnyx({
Expand Down Expand Up @@ -171,16 +161,6 @@ describe('Only the specific property changes when using withOnyx() and ', () =>
});
};

it('connecting to a collection with a selector string', () => {
const TestComponentWithOnyx = withOnyx({
collectionWithPropertyA: {
key: ONYX_KEYS.COLLECTION.TEST_KEY,
selector: 'a',
},
})(ViewWithObject);
return runAllAssertionsForCollection(TestComponentWithOnyx);
});

it('connecting to a collection with a selector function', () => {
const mockedSelector = jest.fn(obj => obj && obj.a);
const TestComponentWithOnyx = withOnyx({
Expand Down Expand Up @@ -259,16 +239,6 @@ describe('Only the specific property changes when using withOnyx() and ', () =>
});
};

it('connecting to a collection member with a selector string', () => {
const TestComponentWithOnyx = withOnyx({
itemWithPropertyA: {
key: `${ONYX_KEYS.COLLECTION.TEST_KEY}1`,
selector: 'a',
},
})(ViewWithObject);
return runAllAssertionsForCollectionMemberKey(TestComponentWithOnyx);
});

it('connecting to a collection member with a selector function', () => {
const TestComponentWithOnyx = withOnyx({
itemWithPropertyA: {
Expand Down