-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsubscribe.js
65 lines (53 loc) · 2.11 KB
/
subscribe.js
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
65
import Helper from "../Utility/helper";
import { useEffect } from "react";
/**
* Functional component that subscribes to current group's playback, volume, and playback metadata events
* Unsubscribes on unmounting of component
* @param props.groupId {string} Used to target current group in Sonos API calls
*/
export default function Subscribe(props) {
// Used to make API calls
const helper = new Helper();
useEffect(() => {
// Playback subscription URL
const endPointPB = helper.getGroupsURL() + props.groupId + "/playback/subscription";
// Group volume subscription URL
const endPointGV = helper.getGroupsURL() + props.groupId + "/groupVolume/subscription";
// Playback metadata subscription URL
const endPointMD = helper.getGroupsURL() + props.groupId + "/playbackMetadata/subscription";
// Contains access token and API response format specifier
const headers = helper.getHeaderBearer();
// Data sent to Sonos API (no data needed for subscriptions)
const data = {};
// Calls Sonos API to subscribe to playback events for current group
helper.apiCall(endPointPB, headers, "POST", data)
.catch(function (error) {
console.error(error);
});
// Calls Sonos API to subscribe to group volume events for current group
helper.apiCall(endPointGV, headers, "POST", data)
.catch(function (error) {
console.error(error);
});
// Calls Sonos API to subscribe to playback metadata events for current group
helper.apiCall(endPointMD, headers, "POST", data)
.catch(function (error) {
console.error(error);
});
// On component unmounting, it unsubscribes to all three types of events
return () => {
helper.apiCall(endPointPB, headers, "DELETE", data)
.catch(function (error) {
console.error(error);
});
helper.apiCall(endPointGV, headers, "DELETE", data)
.catch(function (error) {
console.error(error);
});
helper.apiCall(endPointMD, headers, "DELETE", data)
.catch(function (error) {
console.error(error);
});
};
}, []);
}