Skip to content

Commit c77bd12

Browse files
committed
Add test
1 parent cfac4a8 commit c77bd12

4 files changed

Lines changed: 36702 additions & 3 deletions

File tree

__tests__/assets/custom-element/event-stream-adapter.ce.html

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#activities = [];
2525
#activityBuffer = [];
2626
#activityObservers = [];
27+
#activityWaiters = new Set();
2728
#directLine;
2829

2930
constructor() {
@@ -108,13 +109,19 @@
108109
#emitEvent(activity) {
109110
this.#activities.push(activity);
110111

112+
for (const resolve of this.#activityWaiters) {
113+
resolve();
114+
}
115+
116+
this.#activityWaiters.clear();
117+
111118
const meta = getActivityLivestreamingMetadata(activity);
112-
const allActivities = this.#activities;
119+
const element = this;
113120
const detail = { activity };
114121

115122
meta && Object.defineProperty(detail, 'activities', {
116123
enumerable: true,
117-
get: () => allActivities.values()
124+
get: () => element.#iterateStreamActivities(meta.sessionId)
118125
});
119126

120127
this.dispatchEvent(new CustomEvent('activity', {
@@ -123,6 +130,29 @@
123130
}));
124131
}
125132

133+
async *#iterateStreamActivities(sessionId) {
134+
let cursor = 0;
135+
136+
for (;;) {
137+
while (cursor < this.#activities.length) {
138+
const activity = this.#activities[cursor++];
139+
const activityMeta = getActivityLivestreamingMetadata(activity);
140+
141+
if (!activityMeta || activityMeta.sessionId !== sessionId) {
142+
continue;
143+
}
144+
145+
yield activity;
146+
147+
if (activityMeta.type === 'final activity') {
148+
return;
149+
}
150+
}
151+
152+
await new Promise(resolve => this.#activityWaiters.add(resolve));
153+
}
154+
}
155+
126156
async #load() {
127157
const href = this.getAttribute('href');
128158

@@ -145,7 +175,7 @@
145175
}
146176

147177
useAdapter && this.#emitToAdapter(activity);
148-
useEvents && this.#emitEvent(activity);
178+
useEvents && this.#emitEvent(activity.raw);
149179
}
150180
} catch (error) {
151181
signal.aborted || console.error('event-stream-adapter:', error);
@@ -173,6 +203,8 @@
173203

174204
const activity = JSON.parse(data);
175205

206+
activity.raw = { ...activity };
207+
176208
if (
177209
activity.type === 'typing' &&
178210
activity.text &&

0 commit comments

Comments
 (0)