Option to enable streaming

This commit is contained in:
Henry Jameson 2019-12-10 21:30:27 +02:00
parent 505fb26061
commit 6acd889589
8 changed files with 62 additions and 4 deletions

View file

@ -84,7 +84,7 @@ const settings = {
} }
}]) }])
.reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}), .reduce((acc, [key, value]) => ({ ...acc, [key]: value }), {}),
// Special cases (need to transform values) // Special cases (need to transform values or perform actions first)
muteWordsString: { muteWordsString: {
get () { return this.$store.getters.mergedConfig.muteWords.join('\n') }, get () { return this.$store.getters.mergedConfig.muteWords.join('\n') },
set (value) { set (value) {
@ -93,6 +93,18 @@ const settings = {
value: filter(value.split('\n'), (word) => trim(word).length > 0) value: filter(value.split('\n'), (word) => trim(word).length > 0)
}) })
} }
},
useStreamingApi: {
get () { return this.$store.getters.mergedConfig.useStreamingApi },
set (value) {
const promise = value
? this.$store.dispatch('enableMastoSockets')
: this.$store.dispatch('disableMastoSockets')
promise.then(() => {
this.$store.dispatch('setOption', { name: 'useStreamingApi', value })
})
}
} }
}, },
// Updating nested properties // Updating nested properties

View file

@ -73,6 +73,15 @@
</li> </li>
</ul> </ul>
</li> </li>
<li>
<Checkbox v-model="useStreamingApi">
{{ $t('settings.useStreamingApi') }}
<br/>
<small>
{{ $t('settings.useStreamingApiWarning') }}
</small>
</Checkbox>
</li>
<li> <li>
<Checkbox v-model="autoLoad"> <Checkbox v-model="autoLoad">
{{ $t('settings.autoload') }} {{ $t('settings.autoload') }}

View file

@ -358,6 +358,8 @@
"post_status_content_type": "Post status content type", "post_status_content_type": "Post status content type",
"stop_gifs": "Play-on-hover GIFs", "stop_gifs": "Play-on-hover GIFs",
"streaming": "Enable automatic streaming of new posts when scrolled to the top", "streaming": "Enable automatic streaming of new posts when scrolled to the top",
"useStreamingApi": "Receive posts and notifications real-time",
"useStreamingApiWarning": "(Not recommended, experimental, known to skip posts)",
"text": "Text", "text": "Text",
"theme": "Theme", "theme": "Theme",
"theme_help": "Use hex color codes (#rrggbb) to customize your color theme.", "theme_help": "Use hex color codes (#rrggbb) to customize your color theme.",

View file

@ -219,6 +219,8 @@
"subject_input_always_show": "Всегда показывать поле ввода темы", "subject_input_always_show": "Всегда показывать поле ввода темы",
"stop_gifs": "Проигрывать GIF анимации только при наведении", "stop_gifs": "Проигрывать GIF анимации только при наведении",
"streaming": "Включить автоматическую загрузку новых сообщений при прокрутке вверх", "streaming": "Включить автоматическую загрузку новых сообщений при прокрутке вверх",
"useStreamingApi": "Получать сообщения и уведомления в реальном времени",
"useStreamingApiWarning": "(Не рекомендуется, экспериментально, сообщения могут пропадать)",
"text": "Текст", "text": "Текст",
"theme": "Тема", "theme": "Тема",
"theme_help": "Используйте шестнадцатеричные коды цветов (#rrggbb) для настройки темы.", "theme_help": "Используйте шестнадцатеричные коды цветов (#rrggbb) для настройки темы.",

View file

@ -31,6 +31,18 @@ const api = {
} }
}, },
actions: { actions: {
// Global MastoAPI socket control, in future should disable ALL sockets/(re)start relevant sockets
enableMastoSockets (store) {
const { state, dispatch } = store
if (state.mastoUserSocket) return
dispatch('startMastoUserSocket')
},
disableMastoSockets (store) {
const { state, dispatch } = store
if (!state.mastoUserSocket) return
dispatch('stopMastoUserSocket')
},
// MastoAPI 'User' sockets // MastoAPI 'User' sockets
startMastoUserSocket (store) { startMastoUserSocket (store) {
const { state, dispatch } = store const { state, dispatch } = store
@ -81,6 +93,12 @@ const api = {
dispatch('stopFetchingNotifications') dispatch('stopFetchingNotifications')
}) })
}, },
stopMastoUserSocket ({ state, dispatch }) {
dispatch('startFetchingTimeline', { timeline: 'friends' })
dispatch('startFetchingNotifications')
console.log(state.mastoUserSocket)
state.mastoUserSocket.close()
},
// Timelines // Timelines
startFetchingTimeline (store, { startFetchingTimeline (store, {

View file

@ -35,6 +35,7 @@ export const defaultState = {
highlight: {}, highlight: {},
interfaceLanguage: browserLocale, interfaceLanguage: browserLocale,
hideScopeNotice: false, hideScopeNotice: false,
useStreamingApi: false,
scopeCopy: undefined, // instance default scopeCopy: undefined, // instance default
subjectLineBehavior: undefined, // instance default subjectLineBehavior: undefined, // instance default
alwaysShowSubjectInput: undefined, // instance default alwaysShowSubjectInput: undefined, // instance default

View file

@ -469,14 +469,22 @@ const users = {
store.dispatch('initializeSocket') store.dispatch('initializeSocket')
} }
store.dispatch('startMastoUserSocket').catch((error) => { const startPolling = () => {
console.error('Failed initializing MastoAPI Streaming socket', error)
// Start getting fresh posts. // Start getting fresh posts.
store.dispatch('startFetchingTimeline', { timeline: 'friends' }) store.dispatch('startFetchingTimeline', { timeline: 'friends' })
// Start fetching notifications // Start fetching notifications
store.dispatch('startFetchingNotifications') store.dispatch('startFetchingNotifications')
}
if (store.getters.mergedConfig.useStreamingApi) {
store.dispatch('enableMastoSockets').catch((error) => {
console.error('Failed initializing MastoAPI Streaming socket', error)
startPolling()
}) })
} else {
startPolling()
}
// Get user mutes // Get user mutes
store.dispatch('fetchMutes') store.dispatch('fetchMutes')

View file

@ -983,18 +983,24 @@ export const ProcessedWS = ({
wsEvent wsEvent
) )
}) })
// Commented code reason: very spammy, uncomment to enable message debug logging
/*
socket.addEventListener('message', (wsEvent) => { socket.addEventListener('message', (wsEvent) => {
console.debug( console.debug(
`[WS][${id}] Message received`, `[WS][${id}] Message received`,
wsEvent wsEvent
) )
}) })
/**/
proxy(socket, 'open') proxy(socket, 'open')
proxy(socket, 'close') proxy(socket, 'close')
proxy(socket, 'message', preprocessor) proxy(socket, 'message', preprocessor)
proxy(socket, 'error') proxy(socket, 'error')
// 1000 = Normal Closure
eventTarget.close = () => { socket.close(1000, 'Shutting down socket') }
return eventTarget return eventTarget
} }