Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Slight refactor of message creation #120

Merged
merged 2 commits into from
Feb 7, 2015
Merged

Slight refactor of message creation #120

merged 2 commits into from
Feb 7, 2015

Conversation

rileytomasek
Copy link
Contributor

Since the getCreatedMessageData method of the MessageStore is just a message specific utility, it makes more sense to put it in ChatMessageUtils. The debatable part here is that I then moved the call to the APIs create method to the store. The primary motivation for that was the availability of currentThreadID, however, this may also be more maintainable because it keeps the optimistic store update close to the server/API create call.

Move getCreatedMessageData() from the message stores getters to the
ChatMessageUtils and move call to WebAPI's create method into the
message store.
@fisherwebdev
Copy link
Contributor

Yeah, this makes more sense to me, honestly.

The chat example was something I put together for a presentation at the ForwardJS conference in the fall of 2014 as an attempt to show a more complicated example than the TodoMVC one, yet still keep it simple. Jing and I wanted to show both waitFor() and web API interactions. Unfortunately, my attempt at increased complexity while staying as simple as possible resulted in an example that was a little bit forced.

We wanted to show the API calls coming out of the action creators, as this is the style Jing prefers in most cases. Other folks at FB prefer to make the calls to the web API in the store, as you have done here. I'm pretty neutral about it. There are two different ways to do it, and both are correct.

One advantage of doing the XHR call in the action creator instead of the store, as a rule: this makes it impossible to make the mistake of handling the XHR response directly in the store, rather than creating a new action. Then requiring the action creator modules in the store becomes an easily recognizable red flag. But this is really a matter of taste and of team dynamics, I think.

I think we should let the current code stand as-is, and not cause too much confusion in the Flux community by changing it at this point. But I'll keep this open and think about it some more. I should probably write up a full explanation of all this in blog post or documentation page, and maybe even develop a new example that shows stores making web API calls.

@rileytomasek
Copy link
Contributor Author

Sorry, my intent wasn't to change the location the API calls were made from. All of your points above are valid and I agree that it makes sense to leave the API call in the action creator for this example.

My intent was to move the getCreatedMessageData method to ChatMessageUtils since it is an API utility and not a store getter. Moving the API call from the action creator to store was motivated mainly by easy access to the current thread id, due to not being able to access it in the action creator due to that creating a circular dependency between the MessageStore and the ThreadStore.

Would it make sense to pass the current thread id in the payload with the message text, or do you think adding the extra dependency to the MessageComposer component is worse than just leaving the util method in the store?

@fisherwebdev
Copy link
Contributor

ok that's cool, we can do that. If you revise this in that way, I'll merge it in.

@rileytomasek
Copy link
Contributor Author

Sorry I forgot about this. I will try and remember to do it after work this evening...

@rileytomasek
Copy link
Contributor Author

@fisherwebdev sorry for the series of delays. Is this an acceptable compromise of not making an API call from the store, but factoring the utils method out of the store?

@@ -21,6 +21,18 @@ module.exports = {
text: rawMessage.text,
isRead: rawMessage.threadID === currentThreadID
};
},

getCreatedMessageData: function(text, currentThreadID) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I might have named this createMessageData, as it's more generative, not a really a query. Probably should have been named that originally, but I was thinking in terms of store getters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that makes more sense. I just copied the name from the original method in the store.

fisherwebdev added a commit that referenced this pull request Feb 7, 2015
Slight refactor of message creation
@fisherwebdev fisherwebdev merged commit 7372820 into facebookarchive:master Feb 7, 2015
fisherwebdev added a commit that referenced this pull request Feb 9, 2015
Removing unused store.  Change related to #120
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants