import StateMachine from 'javascript-state-machine' import EventEmitter from 'events' /** * 上传的文档的状态管理 */ const STATES = { NOT_UPLOAD: 'notUpload', FILE_TOO_LARGE: 'fileTooLarge', UPLOADING: 'uploading', UPLOAD_SUCCESS: 'uploadSuccess', UPLOAD_ERROR: 'uploadError', CONVERTING: 'converting', CONVERT_SUCCESS: 'convertSuccess', CONVERT_ERROR: 'convertError', } const TRANSITIONS = { NOT_UPLOAD_TO_FILE_TOO_LARGE: 'notUploadToFileTooLarge', NOT_UPLOAD_TO_UPLOADING: 'notUploadToUploading', UPLOADING_TO_UPLOAD_SUCCESS: 'uploadingToUploadSuccess', UPLOADING_TO_UPLOAD_ERROR: 'uploadingToUploadError', UPLOAD_SUCCESS_TO_CONVERTING: 'uploadSuccessToConverting', CONVERTING_TO_CONVERT_SUCCESS: 'convertingToConvertSuccess', CONVERTING_TO_CONVERT_ERROR: 'convertingToConvertError', } class SlideStateManager extends EventEmitter { constructor(initState = STATES.NOT_UPLOAD) { super() this.stateMachine = new StateMachine({ init: initState, transitions: [ { name: TRANSITIONS.NOT_UPLOAD_TO_FILE_TOO_LARGE, from: STATES.NOT_UPLOAD, to: STATES.FILE_TOO_LARGE, }, { name: TRANSITIONS.NOT_UPLOAD_TO_UPLOADING, from: STATES.NOT_UPLOAD, to: STATES.UPLOADING, }, { name: TRANSITIONS.UPLOADING_TO_UPLOAD_SUCCESS, from: STATES.UPLOADING, to: STATES.UPLOAD_SUCCESS, }, { name: TRANSITIONS.UPLOADING_TO_UPLOAD_ERROR, from: STATES.UPLOADING, to: STATES.UPLOAD_ERROR, }, { name: TRANSITIONS.UPLOAD_SUCCESS_TO_CONVERTING, from: STATES.UPLOAD_SUCCESS, to: STATES.CONVERTING, }, { name: TRANSITIONS.CONVERTING_TO_CONVERT_SUCCESS, from: STATES.CONVERTING, to: STATES.CONVERT_SUCCESS, }, { name: TRANSITIONS.CONVERTING_TO_CONVERT_ERROR, from: STATES.CONVERTING, to: STATES.CONVERT_ERROR, }, ], methods: { onNotUploadToFileTooLarge: () => { this.emit(TRANSITIONS.NOT_UPLOAD_TO_FILE_TOO_LARGE) }, onNotUploadToUploading: () => { this.emit(TRANSITIONS.NOT_UPLOAD_TO_UPLOADING) }, onUploadingToUploadSuccess: () => { this.emit(TRANSITIONS.UPLOADING_TO_UPLOAD_SUCCESS) }, onUploadingToUploadError: () => { this.emit(TRANSITIONS.UPLOADING_TO_UPLOAD_ERROR) }, onUploadSuccessToConverting: () => { this.emit(TRANSITIONS.UPLOAD_SUCCESS_TO_CONVERTING) }, onConvertingToConvertSuccess: () => { this.emit(TRANSITIONS.CONVERTING_TO_CONVERT_SUCCESS) }, onConvertingToConvertError: () => { this.emit(TRANSITIONS.CONVERTING_TO_CONVERT_ERROR) }, }, }) } stateMachine onNotUploadToFileTooLarge = (cb) => { this.on(TRANSITIONS.NOT_UPLOAD_TO_FILE_TOO_LARGE, cb) } onNotUploadToUploading = (cb) => { this.on(TRANSITIONS.NOT_UPLOAD_TO_UPLOADING, cb) } onUploadingToUploadSuccess = (cb) => { this.on(TRANSITIONS.UPLOADING_TO_UPLOAD_SUCCESS, cb) } onUploadingToUploadError = (cb) => { this.on(TRANSITIONS.UPLOADING_TO_UPLOAD_ERROR, cb) } onUploadSuccessToConverting = (cb) => { this.on(TRANSITIONS.UPLOAD_SUCCESS_TO_CONVERTING, cb) } onConvertingToConvertSuccess = (cb) => { this.on(TRANSITIONS.CONVERTING_TO_CONVERT_SUCCESS, cb) } onConvertingToConvertError = (cb) => { this.on(TRANSITIONS.CONVERTING_TO_CONVERT_ERROR, cb) } } export default SlideStateManager export { TRANSITIONS as MACHINE_TRANSITIONS, STATES as MACHINE_STATE, }