Spaces:
Running
Running
File size: 7,726 Bytes
6bcb42f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import bindAll from 'lodash.bindall';
import VM from 'scratch-vm';
import CloudProvider from '../lib/cloud-provider';
import {
getIsShowingWithId
} from '../reducers/project-state';
import {
showAlertWithTimeout
} from '../reducers/alerts';
import {openUsernameModal} from '../reducers/modals';
import {setUsernameInvalid, setCloudHost} from '../reducers/tw';
/*
* Higher Order Component to manage the connection to the cloud server.
* @param {React.Component} WrappedComponent component to manage VM events for
* @returns {React.Component} connected component with vm events bound to redux
*/
const cloudManagerHOC = function (WrappedComponent) {
class CloudManager extends React.Component {
constructor (props) {
super(props);
this.cloudProvider = null;
bindAll(this, [
'handleCloudDataUpdate',
'onInvalidUsername'
]);
this.props.vm.on('HAS_CLOUD_DATA_UPDATE', this.handleCloudDataUpdate);
this.props.onSetReduxCloudHost(this.props.cloudHost);
}
componentDidMount () {
if (this.shouldConnect(this.props)) {
this.connectToCloud();
}
}
componentWillReceiveProps (nextProps) {
if (this.props.reduxCloudHost !== nextProps.cloudHost) {
this.props.onSetReduxCloudHost(nextProps.cloudHost);
}
}
componentDidUpdate (prevProps) {
// TODO need to add cloud provider disconnection logic and cloud data clearing logic
// when loading a new project e.g. via file upload
// (and eventually move it out of the vm.clear function)
if (this.shouldReconnect(this.props, prevProps)) {
this.disconnectFromCloud();
if (this.shouldConnect(this.props)) {
this.connectToCloud();
}
return;
}
if (this.shouldConnect(this.props) && !this.shouldConnect(prevProps)) {
this.connectToCloud();
}
if (this.shouldDisconnect(this.props, prevProps)) {
this.disconnectFromCloud();
}
}
componentWillUnmount () {
this.props.vm.off('HAS_CLOUD_DATA_UPDATE', this.handleCloudDataUpdate);
this.disconnectFromCloud();
}
canUseCloud (props) {
return !!(
props.reduxCloudHost &&
props.username &&
props.vm &&
props.projectId &&
props.hasCloudPermission
);
}
shouldConnect (props) {
return !this.isConnected() && this.canUseCloud(props) &&
props.isShowingWithId && props.vm.runtime.hasCloudData() &&
props.canModifyCloudData;
}
shouldDisconnect (props, prevProps) {
return this.isConnected() &&
( // Can no longer use cloud or cloud provider info is now stale
!this.canUseCloud(props) ||
!props.vm.runtime.hasCloudData() ||
(props.projectId !== prevProps.projectId) ||
// tw: username changes are handled in "reconnect"
// (props.username !== prevProps.username) ||
// Editing someone else's project
!props.canModifyCloudData
);
}
shouldReconnect (props, prevProps) {
return this.isConnected() && (
props.username !== prevProps.username ||
props.reduxCloudHost !== prevProps.reduxCloudHost
);
}
isConnected () {
return this.cloudProvider && !!this.cloudProvider.connection;
}
connectToCloud () {
this.cloudProvider = new CloudProvider(
this.props.reduxCloudHost,
this.props.vm,
this.props.username,
this.props.projectId);
this.cloudProvider.onInvalidUsername = this.onInvalidUsername;
this.props.vm.setCloudProvider(this.cloudProvider);
}
disconnectFromCloud () {
if (this.cloudProvider) {
this.cloudProvider.requestCloseConnection();
this.cloudProvider = null;
this.props.vm.setCloudProvider(null);
}
}
handleCloudDataUpdate (projectHasCloudData) {
if (this.isConnected() && !projectHasCloudData) {
this.disconnectFromCloud();
} else if (this.shouldConnect(this.props)) {
this.props.onShowCloudInfo();
this.connectToCloud();
}
}
onInvalidUsername () {
this.props.onInvalidUsername();
}
render () {
const {
/* eslint-disable no-unused-vars */
canModifyCloudData,
cloudHost,
reduxCloudHost,
onSetReduxCloudHost,
projectId,
username,
hasCloudPermission,
isShowingWithId,
onShowCloudInfo,
onInvalidUsername,
/* eslint-enable no-unused-vars */
vm,
...componentProps
} = this.props;
return (
<WrappedComponent
canUseCloud={this.canUseCloud(this.props)}
vm={vm}
{...componentProps}
/>
);
}
}
CloudManager.propTypes = {
canModifyCloudData: PropTypes.bool.isRequired,
cloudHost: PropTypes.string,
reduxCloudHost: PropTypes.string,
onSetReduxCloudHost: PropTypes.func,
hasCloudPermission: PropTypes.bool,
isShowingWithId: PropTypes.bool.isRequired,
onInvalidUsername: PropTypes.func,
onShowCloudInfo: PropTypes.func,
projectId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
username: PropTypes.string,
vm: PropTypes.instanceOf(VM).isRequired
};
CloudManager.defaultProps = {
cloudHost: null,
onShowCloudInfo: () => {},
username: null
};
const mapStateToProps = (state, ownProps) => {
const loadingState = state.scratchGui.projectState.loadingState;
return {
reduxCloudHost: state.scratchGui.tw.cloudHost,
isShowingWithId: getIsShowingWithId(loadingState),
projectId: state.scratchGui.projectState.projectId,
hasCloudPermission: state.scratchGui.tw.cloud,
username: state.scratchGui.tw.username,
canModifyCloudData: (!state.scratchGui.mode.hasEverEnteredEditor || ownProps.canSave)
};
};
const mapDispatchToProps = dispatch => ({
onSetReduxCloudHost: cloudHost => dispatch(setCloudHost(cloudHost)),
onShowCloudInfo: () => showAlertWithTimeout(dispatch, 'cloudInfo'),
onInvalidUsername: () => {
dispatch(setUsernameInvalid(true));
dispatch(openUsernameModal());
}
});
// Allow incoming props to override redux-provided props. Used to mock in tests.
const mergeProps = (stateProps, dispatchProps, ownProps) => Object.assign(
{}, stateProps, dispatchProps, ownProps
);
return connect(
mapStateToProps,
mapDispatchToProps,
mergeProps
)(CloudManager);
};
export default cloudManagerHOC;
|