Skip to content

Conversation

@paulkaplan
Copy link
Contributor

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 onUpdateProjectData method to handle project JSON saves.

Reason for Changes

Explain why these changes should be made

See linked issue.

Copy link
Contributor

@rschamp rschamp left a 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
Copy link
Contributor

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.

const saveReturnValue = onUpdateProjectData(projectId,
savedVMState, requestParams);
// Allow onUpdateProjectData to either return immediately or a promise.
if (!(saveReturnValue.then && saveReturnValue.catch)) {
Copy link
Contributor

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); 
});

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 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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

Copy link
Contributor Author

@paulkaplan paulkaplan Mar 18, 2019

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
@paulkaplan paulkaplan merged commit e0db28d into scratchfoundation:develop Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow project saving method to be configured

3 participants