souvenir/src/store.js

106 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-03-08 22:17:59 +00:00
import Vue from 'vue'
import Vuex from 'vuex'
2019-03-10 03:22:29 +00:00
import { capture } from '@/services/capture.js'
import { encode } from '@/services/encode.js'
2019-03-08 22:17:59 +00:00
Vue.use(Vuex)
export default new Vuex.Store({
state: {
2019-03-10 01:28:07 +00:00
mediaStream: null,
2019-03-09 21:54:07 +00:00
timer: {
selected: 2,
list: [2, 3, 5]
2019-03-09 23:14:30 +00:00
},
capturing: {
status: false,
state: 0
2019-03-09 23:28:47 +00:00
},
encoding: {
status: false
2019-03-09 23:55:37 +00:00
},
downloading: {
2019-03-10 03:49:55 +00:00
status: false,
2019-03-10 04:58:19 +00:00
dataUrl: null,
timestamp: null
2019-03-09 21:54:07 +00:00
}
2019-03-08 22:17:59 +00:00
},
mutations: {
updateMediaStream (store, mediaStream) {
2019-03-10 01:28:07 +00:00
if (store.mediaStream) {
store.mediaStream.getTracks().forEach(track => track.stop())
}
store.mediaStream = mediaStream
2019-03-09 21:54:07 +00:00
},
updateTimer (store, time) {
store.timer.selected = time
2019-03-09 23:14:30 +00:00
},
startCapture (store) {
store.capturing.status = true
},
stopCapture (store) {
store.capturing.status = false
},
updateCaptureState (store, percent) {
store.capturing.state = percent
2019-03-09 23:28:47 +00:00
},
startEncoding (store) {
store.encoding.status = true
},
stopEncoding (store) {
store.encoding.status = false
2019-03-09 23:55:37 +00:00
},
2019-03-10 03:49:55 +00:00
startDownloading (store, dataUrl) {
2019-03-09 23:55:37 +00:00
store.downloading.status = true
2019-03-10 03:49:55 +00:00
store.downloading.dataUrl = dataUrl
2019-03-10 04:58:19 +00:00
store.downloading.timestamp = Date.now()
2019-03-09 23:55:37 +00:00
},
stopDownloading (store) {
store.downloading.status = false
2019-03-10 03:49:55 +00:00
store.downloading.dataUrl = null
2019-03-10 04:58:19 +00:00
store.downloading.timestamp = null
2019-03-09 21:54:07 +00:00
}
2019-03-08 22:17:59 +00:00
},
actions: {
2019-03-10 01:28:07 +00:00
requestCamera ({ commit }) {
2019-03-10 05:34:37 +00:00
const constaints = {
video: {
facingMode: 'user'
},
audio: false
}
navigator.mediaDevices.getUserMedia(constaints)
2019-03-10 01:28:07 +00:00
.then(mediaStream => {
commit('updateMediaStream', mediaStream)
})
.catch(error => console.error(error))
},
capture ({ commit, dispatch, state }) {
commit('startCapture')
capture(commit, state.mediaStream, state.timer.selected * 1000)
.then(captureData => {
commit('stopCapture')
commit('updateCaptureState', 0)
dispatch('encode', captureData)
})
.catch(error => console.error(error))
},
encode ({ commit }, captureData) {
commit('startEncoding')
console.log(captureData)
encode(captureData)
2019-03-10 03:49:55 +00:00
.then(dataUrl => {
commit('stopEncoding')
2019-03-10 03:49:55 +00:00
commit('startDownloading', dataUrl)
})
2019-03-10 03:49:55 +00:00
.catch(error => console.error(error))
2019-03-10 01:28:07 +00:00
}
2019-03-08 22:17:59 +00:00
}
})