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

Refactor utils into stand-alone functions #292

Merged
merged 5 commits into from
Dec 18, 2016

Conversation

scarmuega
Copy link
Contributor

What was done:

  • moved isSameUser and isSameDay functions into stand-alone utils.js file
  • refactored all components that needed those functions so that they consume the new ones
  • removed all code that was used to propagate the original functions through props
  • removed requirements from propTypes and defaultProps

@@ -0,0 +1,23 @@
import moment from 'moment';

export function isSameDay(currentMessage = {}, diffMessage = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can take the opportunity and also refactor this using the isSame moment's function.

Something like

return moment(diffMessage.createdAt).isSame(currentMessage.createdAt)

}

export function isSameUser(currentMessage = {}, diffMessage = {}) {
if (diffMessage.user && currentMessage.user) {
Copy link
Contributor

@dgdavid dgdavid Nov 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here one if statement could be avoided, although the line length would be greater.

if (diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id) {
  return true;
}

@kfiroo
Copy link
Collaborator

kfiroo commented Nov 28, 2016

@scarmuega Looks solid, @FaridSafi what do you think?

@scarmuega
Copy link
Contributor Author

@dgdavid thanks for the feedback, I agree with the proposed optimizations, but my approach was to avoid any extra modification to original source code, to minimize the impact of the PR.

With that being said, if @kfiroo and @FaridSafi agree, I can apply some adjustments within the utils.js file before closing the PR.

@dgdavid
Copy link
Contributor

dgdavid commented Nov 30, 2016

I supposed that, but as you said if they are agree you could to apply more changes that improve both, legibility and performance of code.

BTW, nice job!

@kfiroo
Copy link
Collaborator

kfiroo commented Nov 30, 2016

@scarmuega Nice job indeed!
I say let's do the changes @dgdavid proposed, it's not a big change and it improves readability and performance.
Moreover, it's in a new file so it doesn't change the original code any more :)

@scarmuega
Copy link
Contributor Author

@dgdavid @kfiroo please check the new commit

}
return false;

return currentCreatedAt.startOf('day').isSame(diffCreatedAt.startOf('day'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According with isSame documentation, you can limit the granularity directly:

return currentCreatedAt.isSame(diffCreatedAt, 'day')

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't know about the granularity param of the isSame function. I'll test it and push it.

return true;
}

if (diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good.

But I only to want share a thought about this: we can even to do something like

return diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id

The risk is that it could return an undefined value - valid for this case - and, in my opinion, is less clear.

Copy link
Collaborator

@kfiroo kfiroo Dec 3, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can always do:

return !!(diffMessage.user && currentMessage.user && diffMessage.user._id === currentMessage.user._id)

@kfiroo
Copy link
Collaborator

kfiroo commented Dec 8, 2016

@scarmuega I think I found another issue with this PR, the isSameUser and isSameDay methods used to be passed as props to all components and all custom renderXXX functions.
For example, when overriding renderTime you should be able to use

renderTime(props) {
    if (props.isSameUser(props.currentMessage, props.nextMessage)) {
        // Do something
    }
} 

Any idea on how to go about this?

@scarmuega
Copy link
Contributor Author

@kfiroo my idea is that implementors may use these functions by importing them directly, like this:

import { GiftedChat, Time, utils } from 'react-native-gifted-chat';

Then, they can use them in any renderXXX like this:

renderTime(props) {
    if (utils.isSameUser(props.currentMessage, props.nextMessage)) {
        // Do something
    }
} 

@dgdavid
Copy link
Contributor

dgdavid commented Dec 9, 2016

@scarmuega, @kfiroo perhaps this information deserves either a section in the README or an entry in the Wiki if finally the changes are accepted. Or even both, a warning in the README file and a more detailed example(s) in the Wiki.

@kfiroo
Copy link
Collaborator

kfiroo commented Dec 12, 2016

@scarmuega @dgdavid Hey guys, The only thing left open here is the backward compatibility issue.
Users who override some renderXXX function, like renderTime for example, might have in their code something like

renderTime(props) {
    if (props.isSameUser(props.currentMessage, props.nextMessage)) {
        return (
            <Time
                {...props}
                textStyle={someCustomTextStyles}
            />
        );
    }
    return (
        // SOMETHING ELSE...
    );
},

So I think we should pass these methods as props to these override methods at least.
What do you think?

@dgdavid
Copy link
Contributor

dgdavid commented Dec 12, 2016

@kfiroo It is a difficult decision, and honestly I don't know what I'd do. But, Did you think about a not backward-compatible update?

I believe that RNGC deserves a major update with all PR that will be accepted and maybe is moment to break the backward compatibility. Why not take the step from 0.0.10 to 1.0.0?. There are a lot of changes pending for be accepted.

Just is an idea. It not answer your question, I know, but just now I see no point to keep these props bloating the component.

@kfiroo
Copy link
Collaborator

kfiroo commented Dec 12, 2016

@dgdavid I will work on a new version with breaking changes, it's on my todo list :)

I still feel like giving people a heads up, like maybe a warning about deprecation before completely breaking something is fair.

@dgdavid
Copy link
Contributor

dgdavid commented Dec 12, 2016

@kfiroo

I will work on a new version with breaking changes

Nice! I very happy to read that.

I was also looking for some free time to review all issues and PR and make a summary for you and @FaridSafi. But free time also flies 😄

@kfiroo
Copy link
Collaborator

kfiroo commented Dec 12, 2016

@dgdavid I know what you mean! No worries we will get this going soon! :)

@scarmuega
Copy link
Contributor Author

@kfiroo @dgdavid sounds good. Just to double check:

  • I'll restore the missing props to maintain backward compatibility
  • Internally, they will work as a wrapper of the new functions in the utils file
  • I'll add a deprecation warning (console.warn??) inside the wrapper that travels inside props
  • In next major release, these props would be removed completely

Let me know if this is what you had in mind and I'll push the new commit.

@benjaminb10
Copy link

@scarmuega finally did you add a utils file?

@scarmuega
Copy link
Contributor Author

@benjaminb10 yes, if you are pulling from this PR, you should be able to use import { GiftedChat, utils } from 'react-native-gifted-chat' and then you can call utils.isSameDay or utils.isSameUser

@scarmuega
Copy link
Contributor Author

@dgdavid @kfiroo would you take a look at the last commit? I added backward compatibility and a deprecation warning. Thanks.

@benjaminb10
Copy link

@scarmuega I prefere wait for the merge.

@kfiroo kfiroo merged commit 2fe34a8 into FaridSafi:master Dec 18, 2016
@kfiroo
Copy link
Collaborator

kfiroo commented Dec 18, 2016

@scarmuega @dgdavid Good job guys :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants