-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathcable.js
More file actions
64 lines (61 loc) · 1.7 KB
/
cable.js
File metadata and controls
64 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
# Copyright 2022 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE.md for more details.
# Modified by OpenC3, Inc.
# All changes Copyright 2026, OpenC3, Inc.
# All Rights Reserved
#
# This file may also be used under the terms of a commercial license
# if purchased from OpenC3, Inc.
*/
import { Api } from '.'
import { createConsumer } from '@anycable/web'
export default class Cable {
constructor(url = '/openc3-api/cable', otpUrl = '/openc3-api/auth/otp') {
this._cable = null
this._url = url
this._otpUrl = otpUrl
}
disconnect() {
if (this._cable) {
this._cable.cable.disconnect()
this._cable = null
}
}
createSubscription(channel, scope, callbacks = {}, additionalOptions = {}) {
return OpenC3Auth.updateToken(OpenC3Auth.defaultMinValidity)
.then((refreshed) => {
if (refreshed) {
OpenC3Auth.setTokens()
}
return Api.get(this._otpUrl, {
params: {
scope,
},
})
})
.then(({ data: otp }) => {
if (this._cable == null) {
const finalUrl = new URL(this._url, document.baseURI)
finalUrl.searchParams.set('scope', scope)
finalUrl.searchParams.set('authorization', otp)
this._cable = createConsumer(finalUrl.href)
}
return this._cable.subscriptions.create(
{
channel,
...additionalOptions,
},
callbacks,
)
})
}
recordPing() {
// Noop with Anycable
}
}