• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CaptainFact / captain-fact-frontend / 12936416000

23 Jan 2025 08:45AM UTC coverage: 5.515% (-1.4%) from 6.953%
12936416000

push

github

Betree
chore: Migrate to tailwind

43 of 1641 branches covered (2.62%)

Branch coverage included in aggregate %.

6 of 301 new or added lines in 93 files covered. (1.99%)

93 existing lines in 38 files now uncovered.

192 of 2620 relevant lines covered (7.33%)

0.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/app/API/socket_api.js
1
import { Socket } from 'phoenix'
2

3
import { WS_API_URL } from '../config'
4
import { getFromLocalStorage, LOCAL_STORAGE_KEYS } from '../lib/local_storage'
5
import parseServerError from './server_error'
6

7
class CaptainFactSocketApi {
8
  constructor(url) {
UNCOV
9
    const token = getFromLocalStorage(LOCAL_STORAGE_KEYS.TOKEN)
×
UNCOV
10
    this.socketUrl = url
×
UNCOV
11
    this.channels = {}
×
UNCOV
12
    this.createSocket(token)
×
13
  }
14

15
  setAuthorizationToken(token) {
16
    this.createSocket(token)
×
17
  }
18

19
  resetToken() {
20
    this.socket = new Socket(this.socketUrl)
×
21
  }
22

23
  createSocket(token) {
UNCOV
24
    this.socket = new Socket(this.socketUrl, { params: { token } })
×
UNCOV
25
    this.socket.onError((e) => {
×
26
      // eslint-disable-next-line no-console
27
      console.warn('Socket error:', e)
×
28
    })
UNCOV
29
    this.socket.onClose((e) => {
×
30
      if (!e.wasClean) {
×
31
        // eslint-disable-next-line no-console
NEW
32
        console.warn('Socket closed with errors:', e)
×
33
      }
34
    })
35
  }
36

37
  /**
38
   * Joins a channel. Automatically open the socket if its closed
39
   * @param {String} identifier
40
   * @param {String} channelAddress
41
   * @param {{}} mapEventsToFuncs - functions to call when events are triggered
42
   * @return {Promise} promise
43
   */
44
  joinChannel(identifier, channelAddress, mapEventsToFuncs = {}) {
×
45
    return new Promise((fulfill, reject) => {
×
46
      if (['closed', 'closing'].includes(this.socket.connectionState())) {
×
47
        this.socket.connect()
×
48
      }
49
      const channel = this.socket.channel(channelAddress)
×
50
      this.channels[identifier] = channel
×
51
      for (const [event, func] of Object.entries(mapEventsToFuncs)) {
×
52
        channel.on(event, func)
×
53
      }
54
      channel
×
55
        .join()
56
        .receive('ok', fulfill)
57
        .receive('error', () => reject('noInternet'))
×
58
        .receive('timeout', () => reject('noInternet'))
×
59
    })
60
  }
61

62
  /**
63
   * Leaves a channel. Automatically close the socket if there's no more channel
64
   * @param {String} identifier
65
   * @return {Phoenix.Channel} channel
66
   */
67
  leaveChannel(identifier) {
68
    const socketState = this.socket.connectionState()
×
69
    // Leave channel gracefully
70
    if (this.channels[identifier]) {
×
71
      this.channels[identifier].leave()
×
72
      delete this.channels[identifier]
×
73
    }
74
    // If no more channels, close the socket
75
    if (!Object.keys(this.channels).length && ['connecting', 'open'].includes(socketState)) {
×
76
      this.socket.disconnect()
×
77
    }
78
  }
79

80
  /**
81
   * Push given message on the channel
82
   * @param {String} channelIdentifier
83
   * @param {String} message
84
   * @param {Object} params
85
   * @return {Promise} channel
86
   */
87
  push(channelIdentifier, message, params = {}) {
×
88
    return new Promise((fulfill, reject) => {
×
89
      return this.channels[channelIdentifier]
×
90
        .push(message, params)
91
        .receive('ok', (data) => fulfill(data))
×
92
        .receive('error', (err) => reject(parseServerError(err)))
×
93
    })
94
  }
95
}
96

97
export default new CaptainFactSocketApi(WS_API_URL)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc