-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathOnFetchProfileCallback.ts
More file actions
68 lines (61 loc) · 2.03 KB
/
OnFetchProfileCallback.ts
File metadata and controls
68 lines (61 loc) · 2.03 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
65
66
67
68
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { CallAdapterState } from './CallAdapter';
import { RemoteParticipantState } from '@internal/calling-stateful-client';
import { AdapterStateModifier } from './AzureCommunicationCallAdapter';
import { createParticipantModifier } from '../utils';
/**
* Callback function used to provide custom data to build profile for a user.
*
* @beta
*/
export type OnFetchProfileCallback = (userId: string, defaultProfile?: Profile) => Promise<Profile | undefined>;
/**
* The profile of a user.
*
* @beta
*/
export type Profile = {
/**
* Primary text to display, usually the name of the person.
*/
displayName?: string;
};
/**
* @private
*/
export const createProfileStateModifier = (
onFetchProfile: OnFetchProfileCallback,
notifyUpdate: () => void
): AdapterStateModifier => {
const cachedDisplayName: {
[id: string]: string;
} = {};
return (state: CallAdapterState) => {
const originalParticipants = state.call?.remoteParticipants;
(async () => {
let shouldNotifyUpdates = false;
for (const key in originalParticipants) {
if (cachedDisplayName[key]) {
continue;
}
const profile = await onFetchProfile(key, { displayName: originalParticipants[key].displayName });
if (profile?.displayName && originalParticipants[key].displayName !== profile?.displayName) {
cachedDisplayName[key] = profile?.displayName;
shouldNotifyUpdates = true;
}
}
// notify update only when there is a change, which most likely will trigger modifier and setState again
shouldNotifyUpdates && notifyUpdate();
})();
const participantsModifier = createParticipantModifier(
(id: string, participant: RemoteParticipantState): RemoteParticipantState | undefined => {
if (cachedDisplayName[id]) {
return { ...participant, displayName: cachedDisplayName[id] };
}
return undefined;
}
);
return participantsModifier(state);
};
};