-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Move project saving to server to helper, allow external save action #4659
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
Move project saving to server to helper, allow external save action #4659
Conversation
rschamp
left a comment
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.
Inspecting the properties of the return value seems like it could lead to bugs... what do you think about this alternative?
| onRemixing: () => {}, | ||
| onSetProjectThumbnailer: () => {} | ||
| onSetProjectThumbnailer: () => {}, | ||
| onUpdateProjectData: saveProjectToServer |
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.
We should remove this eventually, although adding the default makes this PR seamless, so I think it's the way to go now.
src/lib/project-saver-hoc.jsx
Outdated
| const saveReturnValue = onUpdateProjectData(projectId, | ||
| savedVMState, requestParams); | ||
| // Allow onUpdateProjectData to either return immediately or a promise. | ||
| if (!(saveReturnValue.then && saveReturnValue.catch)) { |
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.
Can we rely on the supplier of onUpdateProjectData to ensure it returns a promise? If not, maybe we should wrap the entire thing in a promise that gets returned? Something like:
return new Promise((resolve, reject) => {
try {
const saveReturnValue = onUpdateProjectData(projectId, savedVMState, requestParams);
} catch (e) {
return reject(e);
}
// if saveReturnValue is a promise, this wrapper promise will resolve to what saveReturnValue resolves to
return resolve(saveReturnValue);
});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.
Yeah i wasn't quite sure whether to enforce the promise requirement. That code block actually started as if (!returnValue.then) { throw new Error('You must return a promise'); } but then i realized i could just resolve at that point. Also you don't need to try/catch because it is already inside a promise chain, so throwing in that function would automatically go to the next .catch in the chain.
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.
Actually i think you can just return Promise.resolve(onUpdateProjectData(...)) since promises unwrap the resolve value from other promises
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.
That looks good to me.
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 wait, even simpler, it is in a promise chain, it doesn't matter if it is a promise or not, either way the next then will get the same result at the right time 00a4db1 🤦♂️
Since this is in a promise chain, it doesn't matter if onUpdateProjectData returns a promise or a primitive, it'll work either way
Resolves
What Github issue does this resolve (please include link)?
Proposed Changes
Describe what this Pull Request does
Move the current save code to a helper that can be eventually moved to WWW, and allow a GUI consumer to pass in a
onUpdateProjectDatamethod to handle project JSON saves.Reason for Changes
Explain why these changes should be made
See linked issue.