Преглед на файлове

plugins: add get/setPluginState methods

Renée Kooi преди 7 години
родител
ревизия
2ec1696455
променени са 4 файла, в които са добавени 31 реда и са изтрити 33 реда
  1. 1 0
      src/core/Core.js
  2. 13 0
      src/plugins/Plugin.js
  3. 9 15
      src/plugins/Transloadit/index.js
  4. 8 18
      src/plugins/Webcam/index.js

+ 1 - 0
src/core/Core.js

@@ -91,6 +91,7 @@ class Uppy {
     this.postProcessors = []
     this.postProcessors = []
 
 
     this.state = {
     this.state = {
+      plugins: {},
       files: {},
       files: {},
       capabilities: {
       capabilities: {
         resumableUploads: false
         resumableUploads: false

+ 13 - 0
src/plugins/Plugin.js

@@ -26,6 +26,19 @@ module.exports = class Plugin {
     this.uninstall = this.uninstall.bind(this)
     this.uninstall = this.uninstall.bind(this)
   }
   }
 
 
+  getPluginState () {
+    return this.core.state.plugins[this.id]
+  }
+
+  setPluginState (update) {
+    const plugins = Object.assign({}, this.core.state.plugins)
+    plugins[this.id] = Object.assign({}, plugins[this.id], update)
+
+    this.core.setState({
+      plugins: plugins
+    })
+  }
+
   update (state) {
   update (state) {
     if (typeof this.el === 'undefined') {
     if (typeof this.el === 'undefined') {
       return
       return

+ 9 - 15
src/plugins/Transloadit/index.js

@@ -125,7 +125,7 @@ module.exports = class Transloadit extends Plugin {
       signature: options.signature
       signature: options.signature
     }).then((assembly) => {
     }).then((assembly) => {
       // Store the list of assemblies related to this upload.
       // Store the list of assemblies related to this upload.
-      const state = this.core.state.transloadit
+      const state = this.getPluginState()
       const assemblyList = state.uploadsAssemblies[uploadID]
       const assemblyList = state.uploadsAssemblies[uploadID]
       const uploadsAssemblies = Object.assign({}, state.uploadsAssemblies, {
       const uploadsAssemblies = Object.assign({}, state.uploadsAssemblies, {
         [uploadID]: assemblyList.concat([ assembly.assembly_id ])
         [uploadID]: assemblyList.concat([ assembly.assembly_id ])
@@ -213,7 +213,7 @@ module.exports = class Transloadit extends Plugin {
       return
       return
     }
     }
 
 
-    const state = this.core.state.transloadit
+    const state = this.getPluginState()
     const assembly = state.assemblies[file.transloadit.assembly]
     const assembly = state.assemblies[file.transloadit.assembly]
 
 
     this.client.addFile(assembly, file).catch((err) => {
     this.client.addFile(assembly, file).catch((err) => {
@@ -235,7 +235,7 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   onFileUploadComplete (assemblyId, uploadedFile) {
   onFileUploadComplete (assemblyId, uploadedFile) {
-    const state = this.core.state.transloadit
+    const state = this.getPluginState()
     const file = this.findFile(uploadedFile)
     const file = this.findFile(uploadedFile)
     this.setPluginState({
     this.setPluginState({
       files: Object.assign({}, state.files, {
       files: Object.assign({}, state.files, {
@@ -249,7 +249,7 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   onResult (assemblyId, stepName, result) {
   onResult (assemblyId, stepName, result) {
-    const state = this.core.state.transloadit
+    const state = this.getPluginState()
     const file = state.files[result.original_id]
     const file = state.files[result.original_id]
     // The `file` may not exist if an import robot was used instead of a file upload.
     // The `file` may not exist if an import robot was used instead of a file upload.
     result.localId = file ? file.id : null
     result.localId = file ? file.id : null
@@ -262,7 +262,7 @@ module.exports = class Transloadit extends Plugin {
 
 
   onAssemblyFinished (url) {
   onAssemblyFinished (url) {
     this.client.getAssemblyStatus(url).then((assembly) => {
     this.client.getAssemblyStatus(url).then((assembly) => {
-      const state = this.core.state.transloadit
+      const state = this.getPluginState()
       this.setPluginState({
       this.setPluginState({
         assemblies: Object.assign({}, state.assemblies, {
         assemblies: Object.assign({}, state.assemblies, {
           [assembly.assembly_id]: assembly
           [assembly.assembly_id]: assembly
@@ -327,7 +327,7 @@ module.exports = class Transloadit extends Plugin {
       })
       })
     }
     }
 
 
-    const state = this.core.state.transloadit
+    const state = this.getPluginState()
     const uploadsAssemblies = Object.assign({},
     const uploadsAssemblies = Object.assign({},
       state.uploadsAssemblies,
       state.uploadsAssemblies,
       { [uploadID]: [] })
       { [uploadID]: [] })
@@ -358,7 +358,7 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   afterUpload (fileIDs, uploadID) {
   afterUpload (fileIDs, uploadID) {
-    const state = this.core.state.transloadit
+    const state = this.getPluginState()
     const assemblyIDs = state.uploadsAssemblies[uploadID]
     const assemblyIDs = state.uploadsAssemblies[uploadID]
 
 
     // If we don't have to wait for encoding metadata or results, we can close
     // If we don't have to wait for encoding metadata or results, we can close
@@ -456,7 +456,7 @@ module.exports = class Transloadit extends Plugin {
       this.core.on('transloadit:import-error', onImportError)
       this.core.on('transloadit:import-error', onImportError)
     }).then(() => {
     }).then(() => {
       // Clean up uploadID → assemblyIDs, they're no longer going to be used anywhere.
       // Clean up uploadID → assemblyIDs, they're no longer going to be used anywhere.
-      const state = this.core.state.transloadit
+      const state = this.getPluginState()
       const uploadsAssemblies = Object.assign({}, state.uploadsAssemblies)
       const uploadsAssemblies = Object.assign({}, state.uploadsAssemblies)
       delete uploadsAssemblies[uploadID]
       delete uploadsAssemblies[uploadID]
       this.setPluginState({ uploadsAssemblies })
       this.setPluginState({ uploadsAssemblies })
@@ -493,7 +493,7 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   getAssembly (id) {
   getAssembly (id) {
-    const state = this.core.state.transloadit
+    const state = this.getPluginState()
     return state.assemblies[id]
     return state.assemblies[id]
   }
   }
 
 
@@ -505,10 +505,4 @@ module.exports = class Transloadit extends Plugin {
       return file && file.transloadit && file.transloadit.assembly === assemblyID
       return file && file.transloadit && file.transloadit.assembly === assemblyID
     })
     })
   }
   }
-
-  setPluginState (newState) {
-    const transloadit = Object.assign({}, this.core.state.transloadit, newState)
-
-    this.core.setState({ transloadit })
-  }
 }
 }

+ 8 - 18
src/plugins/Webcam/index.js

@@ -100,16 +100,6 @@ module.exports = class Webcam extends Plugin {
     // }
     // }
   }
   }
 
 
-  /**
-   * Little shorthand to update the state with my new state
-   */
-  setPluginState (newState) {
-    const {state} = this.core
-    const webcam = Object.assign({}, state.webcam, newState)
-
-    this.core.setState({webcam})
-  }
-
   start () {
   start () {
     this.webcamActive = true
     this.webcamActive = true
 
 
@@ -269,15 +259,17 @@ module.exports = class Webcam extends Plugin {
       this.start()
       this.start()
     }
     }
 
 
-    if (!state.webcam.cameraReady && !state.webcam.useTheFlash) {
-      return PermissionsScreen(state.webcam)
+    const webcamState = this.getPluginState()
+
+    if (!webcamState.cameraReady && !webcamState.useTheFlash) {
+      return PermissionsScreen(webcamState)
     }
     }
 
 
     if (!this.streamSrc) {
     if (!this.streamSrc) {
       this.streamSrc = this.stream ? URL.createObjectURL(this.stream) : null
       this.streamSrc = this.stream ? URL.createObjectURL(this.stream) : null
     }
     }
 
 
-    return CameraScreen(Object.assign({}, state.webcam, {
+    return CameraScreen(Object.assign({}, webcamState, {
       onSnapshot: this.takeSnapshot,
       onSnapshot: this.takeSnapshot,
       onStartRecording: this.startRecording,
       onStartRecording: this.startRecording,
       onStopRecording: this.stopRecording,
       onStopRecording: this.stopRecording,
@@ -285,7 +277,7 @@ module.exports = class Webcam extends Plugin {
       onStop: this.stop,
       onStop: this.stop,
       modes: this.opts.modes,
       modes: this.opts.modes,
       supportsRecording: supportsMediaRecorder(),
       supportsRecording: supportsMediaRecorder(),
-      recording: state.webcam.isRecording,
+      recording: webcamState.isRecording,
       getSWFHTML: this.webcam.getSWFHTML,
       getSWFHTML: this.webcam.getSWFHTML,
       src: this.streamSrc
       src: this.streamSrc
     }))
     }))
@@ -293,10 +285,8 @@ module.exports = class Webcam extends Plugin {
 
 
   install () {
   install () {
     this.webcam.init()
     this.webcam.init()
-    this.core.setState({
-      webcam: {
-        cameraReady: false
-      }
+    this.setPluginState({
+      cameraReady: false
     })
     })
 
 
     const target = this.opts.target
     const target = this.opts.target