|
24 | 24 | #activities = []; |
25 | 25 | #activityBuffer = []; |
26 | 26 | #activityObservers = []; |
| 27 | + #activityWaiters = new Set(); |
27 | 28 | #directLine; |
28 | 29 |
|
29 | 30 | constructor() { |
|
108 | 109 | #emitEvent(activity) { |
109 | 110 | this.#activities.push(activity); |
110 | 111 |
|
| 112 | + for (const resolve of this.#activityWaiters) { |
| 113 | + resolve(); |
| 114 | + } |
| 115 | + |
| 116 | + this.#activityWaiters.clear(); |
| 117 | + |
111 | 118 | const meta = getActivityLivestreamingMetadata(activity); |
112 | | - const allActivities = this.#activities; |
| 119 | + const element = this; |
113 | 120 | const detail = { activity }; |
114 | 121 |
|
115 | 122 | meta && Object.defineProperty(detail, 'activities', { |
116 | 123 | enumerable: true, |
117 | | - get: () => allActivities.values() |
| 124 | + get: () => element.#iterateStreamActivities(meta.sessionId) |
118 | 125 | }); |
119 | 126 |
|
120 | 127 | this.dispatchEvent(new CustomEvent('activity', { |
|
123 | 130 | })); |
124 | 131 | } |
125 | 132 |
|
| 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 | + |
126 | 156 | async #load() { |
127 | 157 | const href = this.getAttribute('href'); |
128 | 158 |
|
|
145 | 175 | } |
146 | 176 |
|
147 | 177 | useAdapter && this.#emitToAdapter(activity); |
148 | | - useEvents && this.#emitEvent(activity); |
| 178 | + useEvents && this.#emitEvent(activity.raw); |
149 | 179 | } |
150 | 180 | } catch (error) { |
151 | 181 | signal.aborted || console.error('event-stream-adapter:', error); |
|
173 | 203 |
|
174 | 204 | const activity = JSON.parse(data); |
175 | 205 |
|
| 206 | + activity.raw = { ...activity }; |
| 207 | + |
176 | 208 | if ( |
177 | 209 | activity.type === 'typing' && |
178 | 210 | activity.text && |
|
0 commit comments