-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathLivestreamSessionManager.js
More file actions
80 lines (66 loc) · 2.54 KB
/
LivestreamSessionManager.js
File metadata and controls
80 lines (66 loc) · 2.54 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
69
70
71
72
73
74
75
76
77
78
79
80
/* eslint-disable security/detect-object-injection */
/*!
* Copyright (C) Microsoft Corporation. All rights reserved.
*/
import LivestreamSession from './LivestreamSession.js';
const ActiveLivestreamSymbol = Symbol();
export default class LivestreamSessionManager {
constructor() {
this[ActiveLivestreamSymbol] = new Map();
}
*concludeAll() {
for (const [sessionId, session] of this[ActiveLivestreamSymbol]) {
if (!session.isConcluded) {
const { previousActivity } = session;
const entitiesWithoutStreamInfo = (previousActivity?.entities ?? []).filter(
({ type }) => type !== 'streaminfo'
);
yield Object.freeze({
...previousActivity,
channelData: Object.freeze({
...previousActivity?.channelData,
chunkType: undefined,
streamId: sessionId,
streamSequence: undefined,
streamType: 'final'
}),
entities: Object.freeze([...entitiesWithoutStreamInfo]),
id: `${sessionId}/final`,
text: previousActivity?.text,
type: 'message'
});
}
}
}
has(livestreamSessionId) {
return this[ActiveLivestreamSymbol].has(livestreamSessionId);
}
*sequence(livestreamSessionId, activity, isFinal = false) {
let livestreamSession = this[ActiveLivestreamSymbol].get(livestreamSessionId);
if (!livestreamSession) {
livestreamSession = new LivestreamSession(livestreamSessionId);
this[ActiveLivestreamSymbol].set(livestreamSessionId, livestreamSession);
}
if (livestreamSession.isConcluded) {
return;
}
const streamSequence = livestreamSession.getNextLivestreamSequence(isFinal);
const entitiesWithoutStreamInfo = (activity.entities ?? []).filter(({ type }) => type !== 'streaminfo');
// We assume the chat adapter will do delta decompression.
livestreamSession.previousActivity = activity;
yield Object.freeze({
...activity,
channelData: Object.freeze({
...activity.channelData,
chunkType: undefined,
streamId: streamSequence === 1 ? undefined : livestreamSessionId,
streamSequence: streamSequence === Infinity ? undefined : streamSequence,
streamType: streamSequence === Infinity ? 'final' : 'streaming'
}),
entities: Object.freeze([...entitiesWithoutStreamInfo]),
id: streamSequence === 1 ? livestreamSessionId : activity.id,
text: livestreamSession.previousActivity.text,
type: streamSequence === Infinity ? 'message' : 'typing'
});
}
}