fix: removed multiple message events added to CustomWebsocket

This commit is contained in:
Roberto Tonino 2021-03-01 21:22:32 +01:00
parent c6a2f35fbf
commit cbf3a5c677
4 changed files with 1384 additions and 1334 deletions

View file

@ -7,5 +7,6 @@
"trailingComma": "none", "trailingComma": "none",
"printWidth": 120, "printWidth": 120,
"arrowParens": "avoid", "arrowParens": "avoid",
"vueIndentScriptAndStyle": false "vueIndentScriptAndStyle": false,
} "endOfLine": "lf"
}

File diff suppressed because one or more lines are too long

View file

@ -32,6 +32,30 @@ function startApp() {
i18n, i18n,
render: h => h(App) render: h => h(App)
}).$mount('#app') }).$mount('#app')
fetch('connect')
.then(response => response.json())
.then(data => {
store.dispatch('setAppInfo', data.update)
if (data.autologin) {
console.log('Autologin')
let arl = localStorage.getItem('arl')
let accountNum = localStorage.getItem('accountNum')
if (arl) {
arl = arl.trim()
let result
if (accountNum != 0) {
result = get('login', { arl: arl, force: true, child: accountNum || 0 })
} else {
result = get('login', { arl })
}
result.then(loggedIn)
}
}
})
} }
function initClient() { function initClient() {
@ -53,7 +77,7 @@ document.addEventListener('paste', pasteEvent => {
if (router.currentRoute.name === 'Link Analyzer') { if (router.currentRoute.name === 'Link Analyzer') {
socket.emit('analyzeLink', pastedText) socket.emit('analyzeLink', pastedText)
} else { } else {
if (pastedText.indexOf("\n") != -1) pastedText = pastedText.replace(/\n/g, ';'); if (pastedText.indexOf('\n') != -1) pastedText = pastedText.replace(/\n/g, ';')
sendAddToQueue(pastedText) sendAddToQueue(pastedText)
} }
} else { } else {
@ -83,11 +107,11 @@ function setClientModeKeyBindings() {
/* ===== Socketio listeners ===== */ /* ===== Socketio listeners ===== */
// Debug messages for socketio // Debug messages for socketio
socket.on('message', function(msg) { socket.on('message', function (msg) {
console.log(msg) console.log(msg)
}) })
function loggedIn(data){ function loggedIn(data) {
const { status, user } = data const { status, user } = data
switch (status) { switch (status) {
@ -122,27 +146,6 @@ function loggedIn(data){
} }
} }
fetch('connect').then(response => response.json()).then(data => {
store.dispatch('setAppInfo', data.update )
if (data.autologin) {
console.log("Autologin")
let arl = localStorage.getItem('arl')
let accountNum = localStorage.getItem('accountNum')
if (arl) {
arl = arl.trim()
let result
if (accountNum != 0) {
result = get('login', {arl: arl, force:true, child:accountNum || 0})
} else {
result = get('login', {arl})
}
result.then(loggedIn)
}
}
})
/* /*
socket.on('logging_in', function() { socket.on('logging_in', function() {
toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast') toast(i18n.t('toasts.loggingIn'), 'loading', false, 'login-toast')
@ -159,39 +162,39 @@ socket.on('logged_out', function() {
}) })
*/ */
socket.on('restoringQueue', function() { socket.on('restoringQueue', function () {
toast(i18n.t('toasts.restoringQueue'), 'loading', false, 'restoring_queue') toast(i18n.t('toasts.restoringQueue'), 'loading', false, 'restoring_queue')
}) })
socket.on('cancellingCurrentItem', function(uuid) { socket.on('cancellingCurrentItem', function (uuid) {
toast(i18n.t('toasts.cancellingCurrentItem'), 'loading', false, 'cancelling_' + uuid) toast(i18n.t('toasts.cancellingCurrentItem'), 'loading', false, 'cancelling_' + uuid)
}) })
socket.on('currentItemCancelled', function(uuid) { socket.on('currentItemCancelled', function (uuid) {
toast(i18n.t('toasts.currentItemCancelled'), 'done', true, 'cancelling_' + uuid) toast(i18n.t('toasts.currentItemCancelled'), 'done', true, 'cancelling_' + uuid)
}) })
socket.on('startAddingArtist', function(data) { socket.on('startAddingArtist', function (data) {
toast(i18n.t('toasts.startAddingArtist', { artist: data.name }), 'loading', false, 'artist_' + data.id) toast(i18n.t('toasts.startAddingArtist', { artist: data.name }), 'loading', false, 'artist_' + data.id)
}) })
socket.on('finishAddingArtist', function(data) { socket.on('finishAddingArtist', function (data) {
toast(i18n.t('toasts.finishAddingArtist', { artist: data.name }), 'done', true, 'artist_' + data.id) toast(i18n.t('toasts.finishAddingArtist', { artist: data.name }), 'done', true, 'artist_' + data.id)
}) })
socket.on('startConvertingSpotifyPlaylist', function(id) { socket.on('startConvertingSpotifyPlaylist', function (id) {
toast(i18n.t('toasts.startConvertingSpotifyPlaylist'), 'loading', false, 'spotifyplaylist_' + id) toast(i18n.t('toasts.startConvertingSpotifyPlaylist'), 'loading', false, 'spotifyplaylist_' + id)
}) })
socket.on('finishConvertingSpotifyPlaylist', function(id) { socket.on('finishConvertingSpotifyPlaylist', function (id) {
toast(i18n.t('toasts.finishConvertingSpotifyPlaylist'), 'done', true, 'spotifyplaylist_' + id) toast(i18n.t('toasts.finishConvertingSpotifyPlaylist'), 'done', true, 'spotifyplaylist_' + id)
}) })
socket.on('errorMessage', function(error) { socket.on('errorMessage', function (error) {
toast(error, 'error') toast(error, 'error')
}) })
socket.on('queueError', function(queueItem) { socket.on('queueError', function (queueItem) {
if (queueItem.errid) { if (queueItem.errid) {
toast(queueItem.link + ' - ' + i18n.t(`errors.ids.${queueItem.errid}`), 'error') toast(queueItem.link + ' - ' + i18n.t(`errors.ids.${queueItem.errid}`), 'error')
} else { } else {
@ -199,18 +202,18 @@ socket.on('queueError', function(queueItem) {
} }
}) })
socket.on('alreadyInQueue', function(data) { socket.on('alreadyInQueue', function (data) {
toast(i18n.t('toasts.alreadyInQueue', { item: data.title }), 'playlist_add_check') toast(i18n.t('toasts.alreadyInQueue', { item: data.title }), 'playlist_add_check')
}) })
socket.on('loginNeededToDownload', function(data) { socket.on('loginNeededToDownload', function (data) {
toast(i18n.t('toasts.loginNeededToDownload'), 'report') toast(i18n.t('toasts.loginNeededToDownload'), 'report')
}) })
socket.on('startGeneratingItems', function(data) { socket.on('startGeneratingItems', function (data) {
toast(i18n.t('toasts.startGeneratingItems', { n: data.total }), 'loading', false, 'batch_' + data.uuid) toast(i18n.t('toasts.startGeneratingItems', { n: data.total }), 'loading', false, 'batch_' + data.uuid)
}) })
socket.on('finishGeneratingItems', function(data) { socket.on('finishGeneratingItems', function (data) {
toast(i18n.t('toasts.finishGeneratingItems', { n: data.total }), 'done', true, 'batch_' + data.uuid) toast(i18n.t('toasts.finishGeneratingItems', { n: data.total }), 'done', true, 'batch_' + data.uuid)
}) })

View file

@ -1,21 +1,31 @@
import store from '@/store' import store from '@/store'
let wasEventListenerAdded = false
class CustomSocket extends WebSocket { class CustomSocket extends WebSocket {
constructor(args) { constructor(args) {
super(args) super(args)
}
emit(key, data) {
if (this.readyState != WebSocket.OPEN) return false
this.send(JSON.stringify({key:key, data:data}))
} }
emit(key, data) {
if (this.readyState !== WebSocket.OPEN) return false
this.send(JSON.stringify({ key: key, data: data }))
}
on(key, cb) {
if (!wasEventListenerAdded) {
wasEventListenerAdded = true
on(key, callback) { this.addEventListener('message', event => {
this.addEventListener('message', function(event){ const messageData = JSON.parse(event.data)
let data = JSON.parse(event.data)
console.log(data) if (messageData.key === key) {
if (data.key == key) callback(data.data) cb(messageData.data)
}) }
})
}
}
off() {
console.log('off!')
// this.removeEventListener('message')
} }
} }