connection.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. import { defineStore } from 'pinia'
  3. import { ref } from 'vue'
  4. import log from '@shared/utils/log'
  5. import {
  6. NotificationTypes,
  7. useNotifications,
  8. } from '@shared/components/CommonNotifications'
  9. let connectionNotificationId: string
  10. export const useConnectionStore = defineStore('connection', () => {
  11. const notifications = useNotifications()
  12. const connected = ref(false)
  13. const bringConnectionUp = (): void => {
  14. if (connected.value) return
  15. log.debug('Application connection just came up.')
  16. if (connectionNotificationId) {
  17. notifications.removeNotification(connectionNotificationId)
  18. }
  19. connected.value = true
  20. }
  21. const takeConnectionDown = (): void => {
  22. if (!connected.value) return
  23. log.debug('Application connection just went down.')
  24. connectionNotificationId = notifications.notify({
  25. message: __('The connection to the server was lost.'),
  26. type: NotificationTypes.Error,
  27. persistent: true,
  28. })
  29. connected.value = false
  30. }
  31. return {
  32. connected,
  33. bringConnectionUp,
  34. takeConnectionDown,
  35. }
  36. })