-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Add selector to get Posts EmbedVisible flags for RHS. #312
Add selector to get Posts EmbedVisible flags for RHS. #312
Conversation
selectors/rhs.jsx
Outdated
return posts.reduce((accumulatedEmbedVisibleFlags, post) => ({ | ||
...accumulatedEmbedVisibleFlags, | ||
[post.id]: makeGetItem(StoragePrefixes.EMBED_VISIBLE + post.id, previewCollapsed.startsWith('false'))(state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hmhealey I have to send entire state and therefore computes with every state change. Should we have makeGetItem
as memorised selector which depends on state.storage
instead?.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makeGetItem doesn't work with selectors with how it's currently designed, so I don't think we can use it here, but we can still definitely have this memoized properly.
What we can do though is change what the selector takes from the store so that it only takes the things that affect it: the storage state, the current user ID for the storage key prefix, and the COLLAPSE_DISPLAY preference. We'll also want to have a function that creates a selector here because it could possibly be used in multiple places, and we want to make sure each one is memoized separately so they don't clear each other's stored results. It could look something like
export function makeGetPostsEmbedVisibleObj() {
return createSelector(
// This could be its own function or a selector, but that's not necessary since it's really simple
(state) => state.storage,
// We don't need to pass in the previewCollapsed value here since we can just get it ourselves.
// Also, we can use getBool so that we don't need to work with a string later on
(state) => getBool(state, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.COLLAPSE_DISPLAY),
(state, posts) => posts,
(storage, previewCollapsed, posts) => {
const postsEmbedVisibleObj = {};
for (const post of posts) {
// You'll need to write something like makeGetItem that doesn't require the whole store to be passed in
postsEmbedVisibleObj[post.id] = getItemFromStorage(storage, currentUserId, StoragePrefixes.EMBED_VISIBLE + post.id, previewCollapsed);
}
return postsEmbedVisibleObj;
}
);
}
That way, it'll only change when one of those exact 3 things changes, and not just any state change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make the changes. Thanks, this looks great 👍 .
selectors/rhs.jsx
Outdated
export const getPostsEmbedVisibleObj = createSelector( | ||
(state) => state, | ||
(state, {posts, previewCollapsed}) => ({posts, previewCollapsed}), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something important to note here is that this function returns a new object each time it's called, so it'll prevent memoization. Even if you were to do something like (state, props) => (props)
here, props is likely changing each time this is called anyway, so it still wouldn't help. In the future though, you can break it up like this to have it work properly
createSelector(
(state, {posts}) => posts,
(state, {previewCollapsed}) => previewCollapsed,
(posts, previewCollapsed) => {...}
);
As long as posts and previewCollapsed themselves don't change, it'll only be looking at their value to check if anything has changed instead of looking if the object containing them changed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ya true. That's cool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes made changes.
selectors/rhs.jsx
Outdated
return posts.reduce((accumulatedEmbedVisibleFlags, post) => ({ | ||
...accumulatedEmbedVisibleFlags, | ||
[post.id]: makeGetItem(StoragePrefixes.EMBED_VISIBLE + post.id, previewCollapsed.startsWith('false'))(state) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makeGetItem doesn't work with selectors with how it's currently designed, so I don't think we can use it here, but we can still definitely have this memoized properly.
What we can do though is change what the selector takes from the store so that it only takes the things that affect it: the storage state, the current user ID for the storage key prefix, and the COLLAPSE_DISPLAY preference. We'll also want to have a function that creates a selector here because it could possibly be used in multiple places, and we want to make sure each one is memoized separately so they don't clear each other's stored results. It could look something like
export function makeGetPostsEmbedVisibleObj() {
return createSelector(
// This could be its own function or a selector, but that's not necessary since it's really simple
(state) => state.storage,
// We don't need to pass in the previewCollapsed value here since we can just get it ourselves.
// Also, we can use getBool so that we don't need to work with a string later on
(state) => getBool(state, Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.COLLAPSE_DISPLAY),
(state, posts) => posts,
(storage, previewCollapsed, posts) => {
const postsEmbedVisibleObj = {};
for (const post of posts) {
// You'll need to write something like makeGetItem that doesn't require the whole store to be passed in
postsEmbedVisibleObj[post.id] = getItemFromStorage(storage, currentUserId, StoragePrefixes.EMBED_VISIBLE + post.id, previewCollapsed);
}
return postsEmbedVisibleObj;
}
);
}
That way, it'll only change when one of those exact 3 things changes, and not just any state change.
2b46507
to
b216796
Compare
* Add test cases for makeGetPostsEmbedVisibleObj selector.
b216796
to
2570d50
Compare
@sudheerDev Unit test failed. Try to run |
@saturninoabril ya I was working on top of a component branch which has a constant this commit needed. Anyway added that constant to this commit. Test cases should be fine now |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks for making that change. Looks good and fast now
Summary
Adding new selector for RHS posts embedVisible flags.
Checklist
make check-style
to check for style errors (required for all pull requests)