forked from webex/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MembershipJSONAdapter.js
71 lines (65 loc) · 2.31 KB
/
MembershipJSONAdapter.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
66
67
68
69
70
71
import {MembershipsAdapter} from '@webex/component-adapter-interfaces';
import {Observable} from 'rxjs';
// TODO: Figure out how to import JS Doc definitions and remove duplication.
/**
* A Member object that is part of a membership
*
* @external Member
* @see {@link https://github.com/webex/component-adapter-interfaces/blob/master/src/MembershipsAdapter.js#L6}
*/
// TODO: Figure out how to import JS Doc definitions and remove duplication.
/**
* Enum for types of destinations.
*
* @external DestinationType
* @see {@link https://github.com/webex/component-adapter-interfaces/blob/master/src/MembershipsAdapter.js#L21}
*/
/**
* @typedef MembershipsJSON
* @param {object} datasource An object that contains memberships keyed by ID
* @example
* {
* "membership-1": {
* "ID": "membership-1",
* "destinationID": "room-3",
* "destinationType": "room"
* "members": [
* {"ID": "person-1"},
* {"ID": "person-2"},
* {"ID": "person-5"},
* ]
* }
* }
*/
/**
* `MembershipsJSONAdapter` is an implementation of the `MembershipAdapter` interface.
* The implementation utilizes a JSON object as its source of membership data.
*
* @see {@link MembershipsJSON}
* @implements {MembershipsAdapter}
*/
export default class MembershipJSONAdapter extends MembershipsAdapter {
/**
* Returns an observable that emits a list of members for the first destination
* found that matches the given ID and type.
* For this implementation, once the list of members is emitted, the observable completes.
*
* @param {string} destinationID A unique identifier for a meeting/space location
* @param {DestinationType} destinationType Type of destination of the membership
* @returns {Observable.<Array.<Member>>} Observable that emits data of the given ID
*/
getMembersFromDestination(destinationID, destinationType) {
return Observable.create((observer) => {
const membership = Object.values(this.datasource).find(
(destination) => destination.destinationID === destinationID
&& destination.destinationType === destinationType,
);
if (membership) {
observer.next(membership.members);
} else {
observer.error(new Error(`Could not find members for destination "${destinationID}"`));
}
observer.complete();
});
}
}