Skip to content

Commit 344365b

Browse files
committed
feat(upgrade): adjust parse.getMessages signature
1 parent d261274 commit 344365b

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

src/fetch.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ export function fetchEventSource(input: RequestInfo, {
112112

113113
await onopen(response);
114114

115-
await getBytes(response.body!, getLines(getMessages(id => {
115+
await getBytes(response.body!, getLines(getMessages(onmessage,
116+
id => {
116117
if (id) {
117118
// store the id and send it back on the next retry:
118119
headers[LastEventId] = id;
@@ -122,7 +123,7 @@ export function fetchEventSource(input: RequestInfo, {
122123
}
123124
}, retry => {
124125
retryInterval = retry;
125-
}, onmessage)));
126+
})));
126127

127128
onclose?.();
128129
dispose();

src/parse.spec.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,18 @@ describe('parse', () => {
190190
it('happy path', () => {
191191
// arrange:
192192
let msgNum = 0;
193-
const next = parse.getMessages(id => {
194-
expect(id).toEqual('abc');
195-
}, retry => {
196-
expect(retry).toEqual(42);
197-
}, msg => {
193+
const next = parse.getMessages(msg => {
198194
++msgNum;
199195
expect(msg).toEqual({
200196
retry: 42,
201197
id: 'abc',
202198
event: 'def',
203199
data: 'ghi'
204200
});
201+
}, id => {
202+
expect(id).toEqual('abc');
203+
}, retry => {
204+
expect(retry).toEqual(42);
205205
});
206206

207207
// act:
@@ -217,18 +217,18 @@ describe('parse', () => {
217217

218218
it('skip unknown fields', () => {
219219
let msgNum = 0;
220-
const next = parse.getMessages(id => {
221-
expect(id).toEqual('abc');
222-
}, _retry => {
223-
fail('retry should not be called');
224-
}, msg => {
220+
const next = parse.getMessages(msg => {
225221
++msgNum;
226222
expect(msg).toEqual({
227223
id: 'abc',
228224
data: '',
229225
event: '',
230226
retry: undefined,
231227
});
228+
}, id => {
229+
expect(id).toEqual('abc');
230+
}, _retry => {
231+
fail('retry should not be called');
232232
});
233233

234234
// act:
@@ -242,18 +242,18 @@ describe('parse', () => {
242242

243243
it('ignore non-integer retry', () => {
244244
let msgNum = 0;
245-
const next = parse.getMessages(_id => {
246-
fail('id should not be called');
247-
}, _retry => {
248-
fail('retry should not be called');
249-
}, msg => {
245+
const next = parse.getMessages(msg => {
250246
++msgNum;
251247
expect(msg).toEqual({
252248
id: '',
253249
data: '',
254250
event: '',
255251
retry: undefined,
256252
});
253+
}, _id => {
254+
fail('id should not be called');
255+
}, _retry => {
256+
fail('retry should not be called');
257257
});
258258

259259
// act:
@@ -267,18 +267,18 @@ describe('parse', () => {
267267
it('skip comment-only messages', () => {
268268
// arrange:
269269
let msgNum = 0;
270-
const next = parse.getMessages(id => {
271-
expect(id).toEqual('123');
272-
}, _retry => {
273-
fail('retry should not be called');
274-
}, msg => {
270+
const next = parse.getMessages(msg => {
275271
++msgNum;
276272
expect(msg).toEqual({
277273
retry: undefined,
278274
id: '123',
279275
event: 'foo ',
280276
data: '',
281277
});
278+
}, id => {
279+
expect(id).toEqual('123');
280+
}, _retry => {
281+
fail('retry should not be called');
282282
});
283283

284284
// act:
@@ -295,18 +295,18 @@ describe('parse', () => {
295295
it('should append data split across multiple lines', () => {
296296
// arrange:
297297
let msgNum = 0;
298-
const next = parse.getMessages(_id => {
299-
fail('id should not be called');
300-
}, _retry => {
301-
fail('retry should not be called');
302-
}, msg => {
298+
const next = parse.getMessages(msg => {
303299
++msgNum;
304300
expect(msg).toEqual({
305301
data: 'YHOO\n+2\n\n10',
306302
id: '',
307303
event: '',
308304
retry: undefined,
309305
});
306+
}, _id => {
307+
fail('id should not be called');
308+
}, _retry => {
309+
fail('retry should not be called');
310310
});
311311

312312
// act:
@@ -325,19 +325,19 @@ describe('parse', () => {
325325
const expectedIds = ['foo', ''];
326326
let idsIdx = 0;
327327
let msgNum = 0;
328-
const next = parse.getMessages(id => {
329-
expect(id).toEqual(expectedIds[idsIdx]);
330-
++idsIdx;
331-
}, _retry => {
332-
fail('retry should not be called');
333-
}, msg => {
328+
const next = parse.getMessages(msg => {
334329
++msgNum;
335330
expect(msg).toEqual({
336331
data: '',
337332
id: '',
338333
event: '',
339334
retry: undefined,
340335
});
336+
}, id => {
337+
expect(id).toEqual(expectedIds[idsIdx]);
338+
++idsIdx;
339+
}, _retry => {
340+
fail('retry should not be called');
341341
});
342342

343343
// act:

src/parse.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ export function getLines(onLine: (line: Uint8Array, fieldLength: number) => void
117117
* @returns A function that should be called for each incoming line buffer.
118118
*/
119119
export function getMessages(
120-
onId: (id: string) => void,
121-
onRetry: (retry: number) => void,
122-
onMessage?: (msg: EventSourceMessage) => void
120+
onMessage?: (msg: EventSourceMessage) => void,
121+
onId?: (id: string) => void,
122+
onRetry?: (retry: number) => void,
123123
) {
124124
let message = newMessage();
125125
const decoder = new TextDecoder();
@@ -149,12 +149,12 @@ export function getMessages(
149149
message.event = value;
150150
break;
151151
case 'id':
152-
onId(message.id = value);
152+
onId?.(message.id = value);
153153
break;
154154
case 'retry':
155155
const retry = parseInt(value, 10);
156156
if (!isNaN(retry)) { // per spec, ignore non-integers
157-
onRetry(message.retry = retry);
157+
onRetry?.(message.retry = retry);
158158
}
159159
break;
160160
}

0 commit comments

Comments
 (0)