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

[DO NOT MERGE] Add note's tags to history screen - Use action generator #2837

Closed
wants to merge 1 commit into from

Conversation

fluiddot
Copy link
Contributor

@fluiddot fluiddot commented Apr 9, 2021

This PR shows a potential alternative for generating a Redux action based on data from the state.

The idea I introduced here is similar to Thunk actions but quite simplified just to demonstrate how it could work. Basically I added a middleware that checks if the action is a function, if so it does a call with the current state and uses the return as the action to be passed to the next middleware. This way we could dispatch functions (aka action generators) that produce an action object based on the current state.

This option could help us to free up the middlewares and migrate some logic we're doing in the middlewares to actions. One good example for this would be the CREATE_NOTE action, here we’re using a Redux action to generate another one based on data from the state.

@fluiddot fluiddot self-assigned this Apr 9, 2021
Comment on lines +88 to +111
const revision = state.data.noteRevisions.get(noteId)?.get(version);

if (!revision) {
return;
}

const note = state.data.notes.get(noteId);
const noteEmailTags =
note?.tags.filter((tagName) => isEmailTag(tagName)) ?? [];

const revisionCanonicalTags = revision.tags.filter((tagName) => {
const tagHash = tagHashOf(tagName);
const hasTag = state.data.tags.has(tagHash);
return !isEmailTag(tagName) && (hasTag || includeDeletedTags);
});

return {
type: 'RESTORE_NOTE_REVISION',
noteId,
note: {
...revision,
tags: [...noteEmailTags, ...revisionCanonicalTags],
},
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This action contains the logic that we had in the data middleware.

Comment on lines +59 to +61
if (!result || !result.type) {
return;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This gives control to the action generator to prevent the action dispatch, it could be useful if for example we determine that the Redux action shouldn't be dispatched.

@dmsnell
Copy link
Member

dmsnell commented Apr 9, 2021

Thanks for exploring this route. I strongly prefer avoiding this kind of change because it fundamentally breaks the Redux model that we still maintain; that is, it prevents us from having a synchronous store update with inspectable and fake-able actions. The middleware gives us a chance to perform operations on the actions that leaves a fairly clear audit trail. Once we hide computation in thunks we lose all that.

@fluiddot
Copy link
Contributor Author

Thanks for exploring this route. I strongly prefer avoiding this kind of change because it fundamentally breaks the Redux model that we still maintain; that is, it prevents us from having a synchronous store update with inspectable and fake-able actions. The middleware gives us a chance to perform operations on the actions that leaves a fairly clear audit trail. Once we hide computation in thunks we lose all that.

Yeah, unfortunately this option as well as thunk actions comes with their tradeoffs. Ok, let's avoid this option, thank you very much for review 🙇 !

@fluiddot fluiddot closed this Apr 12, 2021
@fluiddot fluiddot deleted the add/tags-history-action-generator branch April 12, 2021 14:02
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.

2 participants