Browse Source

Merge pull request #321 from goto-bus-stop/chore/unmagic

transloadit: remove `this.state` getter
Artur Paikin 7 years ago
parent
commit
fbf53b4576
2 changed files with 36 additions and 31 deletions
  1. 30 25
      src/plugins/Transloadit/index.js
  2. 6 6
      src/plugins/Webcam/index.js

+ 30 - 25
src/plugins/Transloadit/index.js

@@ -125,13 +125,14 @@ 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 assemblyList = this.state.uploadsAssemblies[uploadID]
-      const uploadsAssemblies = Object.assign({}, this.state.uploadsAssemblies, {
+      const state = this.core.state.transloadit
+      const assemblyList = state.uploadsAssemblies[uploadID]
+      const uploadsAssemblies = Object.assign({}, state.uploadsAssemblies, {
         [uploadID]: assemblyList.concat([ assembly.assembly_id ])
         [uploadID]: assemblyList.concat([ assembly.assembly_id ])
       })
       })
 
 
-      this.updateState({
-        assemblies: Object.assign(this.state.assemblies, {
+      this.setPluginState({
+        assemblies: Object.assign(state.assemblies, {
           [assembly.assembly_id]: assembly
           [assembly.assembly_id]: assembly
         }),
         }),
         uploadsAssemblies
         uploadsAssemblies
@@ -212,7 +213,8 @@ module.exports = class Transloadit extends Plugin {
       return
       return
     }
     }
 
 
-    const assembly = this.state.assemblies[file.transloadit.assembly]
+    const state = this.core.state.transloadit
+    const assembly = state.assemblies[file.transloadit.assembly]
 
 
     this.client.addFile(assembly, file).catch((err) => {
     this.client.addFile(assembly, file).catch((err) => {
       this.core.log(err)
       this.core.log(err)
@@ -233,9 +235,10 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   onFileUploadComplete (assemblyId, uploadedFile) {
   onFileUploadComplete (assemblyId, uploadedFile) {
+    const state = this.core.state.transloadit
     const file = this.findFile(uploadedFile)
     const file = this.findFile(uploadedFile)
-    this.updateState({
-      files: Object.assign({}, this.state.files, {
+    this.setPluginState({
+      files: Object.assign({}, state.files, {
         [uploadedFile.id]: {
         [uploadedFile.id]: {
           id: file.id,
           id: file.id,
           uploadedFile
           uploadedFile
@@ -246,20 +249,22 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   onResult (assemblyId, stepName, result) {
   onResult (assemblyId, stepName, result) {
-    const file = this.state.files[result.original_id]
+    const state = this.core.state.transloadit
+    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
 
 
-    this.updateState({
-      results: this.state.results.concat(result)
+    this.setPluginState({
+      results: state.results.concat(result)
     })
     })
     this.core.emit('transloadit:result', stepName, result, this.getAssembly(assemblyId))
     this.core.emit('transloadit:result', stepName, result, this.getAssembly(assemblyId))
   }
   }
 
 
   onAssemblyFinished (url) {
   onAssemblyFinished (url) {
     this.client.getAssemblyStatus(url).then((assembly) => {
     this.client.getAssemblyStatus(url).then((assembly) => {
-      this.updateState({
-        assemblies: Object.assign({}, this.state.assemblies, {
+      const state = this.core.state.transloadit
+      this.setPluginState({
+        assemblies: Object.assign({}, state.assemblies, {
           [assembly.assembly_id]: assembly
           [assembly.assembly_id]: assembly
         })
         })
       })
       })
@@ -322,10 +327,11 @@ module.exports = class Transloadit extends Plugin {
       })
       })
     }
     }
 
 
+    const state = this.core.state.transloadit
     const uploadsAssemblies = Object.assign({},
     const uploadsAssemblies = Object.assign({},
-      this.state.uploadsAssemblies,
+      state.uploadsAssemblies,
       { [uploadID]: [] })
       { [uploadID]: [] })
-    this.updateState({ uploadsAssemblies })
+    this.setPluginState({ uploadsAssemblies })
 
 
     let optionsPromise
     let optionsPromise
     if (fileIDs.length > 0) {
     if (fileIDs.length > 0) {
@@ -352,7 +358,8 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   afterUpload (fileIDs, uploadID) {
   afterUpload (fileIDs, uploadID) {
-    const assemblyIDs = this.state.uploadsAssemblies[uploadID]
+    const state = this.core.state.transloadit
+    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
     // the socket immediately and finish the upload.
     // the socket immediately and finish the upload.
@@ -449,9 +456,10 @@ 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 uploadsAssemblies = Object.assign({}, this.state.uploadsAssemblies)
+      const state = this.core.state.transloadit
+      const uploadsAssemblies = Object.assign({}, state.uploadsAssemblies)
       delete uploadsAssemblies[uploadID]
       delete uploadsAssemblies[uploadID]
-      this.updateState({ uploadsAssemblies })
+      this.setPluginState({ uploadsAssemblies })
     })
     })
   }
   }
 
 
@@ -463,7 +471,7 @@ module.exports = class Transloadit extends Plugin {
       this.core.on('core:upload-success', this.onFileUploadURLAvailable)
       this.core.on('core:upload-success', this.onFileUploadURLAvailable)
     }
     }
 
 
-    this.updateState({
+    this.setPluginState({
       // Contains assembly status objects, indexed by their ID.
       // Contains assembly status objects, indexed by their ID.
       assemblies: {},
       assemblies: {},
       // Contains arrays of assembly IDs, indexed by the upload ID that they belong to.
       // Contains arrays of assembly IDs, indexed by the upload ID that they belong to.
@@ -485,7 +493,8 @@ module.exports = class Transloadit extends Plugin {
   }
   }
 
 
   getAssembly (id) {
   getAssembly (id) {
-    return this.state.assemblies[id]
+    const state = this.core.state.transloadit
+    return state.assemblies[id]
   }
   }
 
 
   getAssemblyFiles (assemblyID) {
   getAssemblyFiles (assemblyID) {
@@ -497,12 +506,8 @@ module.exports = class Transloadit extends Plugin {
     })
     })
   }
   }
 
 
-  get state () {
-    return this.core.state.transloadit || {}
-  }
-
-  updateState (newState) {
-    const transloadit = Object.assign({}, this.state, newState)
+  setPluginState (newState) {
+    const transloadit = Object.assign({}, this.core.state.transloadit, newState)
 
 
     this.core.setState({ transloadit })
     this.core.setState({ transloadit })
   }
   }

+ 6 - 6
src/plugins/Webcam/index.js

@@ -72,7 +72,7 @@ module.exports = class Webcam extends Plugin {
     this.i18n = this.translator.translate.bind(this.translator)
     this.i18n = this.translator.translate.bind(this.translator)
 
 
     this.install = this.install.bind(this)
     this.install = this.install.bind(this)
-    this.updateState = this.updateState.bind(this)
+    this.setPluginState = this.setPluginState.bind(this)
 
 
     this.render = this.render.bind(this)
     this.render = this.render.bind(this)
 
 
@@ -104,7 +104,7 @@ module.exports = class Webcam extends Plugin {
   /**
   /**
    * Little shorthand to update the state with my new state
    * Little shorthand to update the state with my new state
    */
    */
-  updateState (newState) {
+  setPluginState (newState) {
     const {state} = this.core
     const {state} = this.core
     const webcam = Object.assign({}, state.webcam, newState)
     const webcam = Object.assign({}, state.webcam, newState)
 
 
@@ -117,13 +117,13 @@ module.exports = class Webcam extends Plugin {
     this.webcam.start()
     this.webcam.start()
       .then((stream) => {
       .then((stream) => {
         this.stream = stream
         this.stream = stream
-        this.updateState({
+        this.setPluginState({
           // videoStream: stream,
           // videoStream: stream,
           cameraReady: true
           cameraReady: true
         })
         })
       })
       })
       .catch((err) => {
       .catch((err) => {
-        this.updateState({
+        this.setPluginState({
           cameraError: err
           cameraError: err
         })
         })
       })
       })
@@ -141,7 +141,7 @@ module.exports = class Webcam extends Plugin {
     })
     })
     this.recorder.start()
     this.recorder.start()
 
 
-    this.updateState({
+    this.setPluginState({
       isRecording: true
       isRecording: true
     })
     })
   }
   }
@@ -149,7 +149,7 @@ module.exports = class Webcam extends Plugin {
   stopRecording () {
   stopRecording () {
     return new Promise((resolve, reject) => {
     return new Promise((resolve, reject) => {
       this.recorder.addEventListener('stop', () => {
       this.recorder.addEventListener('stop', () => {
-        this.updateState({
+        this.setPluginState({
           isRecording: false
           isRecording: false
         })
         })