Skip to content

Commit 588bbf6

Browse files
committed
Don’t need to trigger intermediate responses
1 parent ac63214 commit 588bbf6

File tree

3 files changed

+18
-73
lines changed

3 files changed

+18
-73
lines changed

src/PatchResolver.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,23 @@ PatchResolver.prototype.handleChunk = function(data) {
4646
this.chunkBuffer += data;
4747
const { newBuffer, parts } = parseMultipartHttp(this.chunkBuffer);
4848
this.chunkBuffer = newBuffer;
49-
for (const part of parts) {
50-
if (this.processedChunks === 0) {
51-
this.previousResponse = part;
52-
this.onResponse(this.previousResponse);
53-
} else {
54-
if (!(Array.isArray(part.path) && typeof part.data !== 'undefined')) {
55-
throw new Error('invalid patch format ' + JSON.stringify(part, null, 2));
49+
if (parts.length) {
50+
parts.forEach(part => {
51+
if (this.processedChunks === 0) {
52+
this.previousResponse = part;
53+
} else {
54+
if (!(Array.isArray(part.path) && typeof part.data !== 'undefined')) {
55+
throw new Error('invalid patch format ' + JSON.stringify(part, null, 2));
56+
}
57+
this.previousResponse = {
58+
...this.previousResponse,
59+
data: applyPatch(this.previousResponse.data, part.path, part.data),
60+
errors: mergeErrors(this.previousResponse.errors, part.errors),
61+
};
5662
}
57-
this.previousResponse = {
58-
...this.previousResponse,
59-
data: applyPatch(this.previousResponse.data, part.path, part.data),
60-
errors: mergeErrors(this.previousResponse.errors, part.errors),
61-
};
62-
63-
this.onResponse(this.previousResponse);
64-
}
65-
this.processedChunks += 1;
63+
this.processedChunks += 1;
64+
});
65+
// don't need to re-trigger every intermediate state
66+
this.onResponse(this.previousResponse);
6667
}
6768
};

src/__test__/PatchResolver.spec.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ describe('PathResolver', function() {
130130

131131
resolver.handleChunk(chunk1 + chunk2);
132132
expect(onResponse.mock.calls[0][0]).toMatchSnapshot();
133-
expect(onResponse.mock.calls[1][0]).toMatchSnapshot();
134133
});
135134

136135
it('should work when chunks are combined and split', function() {
@@ -145,7 +144,6 @@ describe('PathResolver', function() {
145144

146145
resolver.handleChunk(chunk1 + chunk2 + chunk3a);
147146
expect(onResponse.mock.calls[0][0]).toMatchSnapshot();
148-
expect(onResponse.mock.calls[1][0]).toMatchSnapshot();
149147
onResponse.mockClear();
150148

151149
resolver.handleChunk(chunk3b);

src/__test__/__snapshots__/PatchResolver.spec.js.snap

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -218,33 +218,6 @@ Object {
218218
`;
219219

220220
exports[`PathResolver should work when chunks are combined 1`] = `
221-
Object {
222-
"data": Object {
223-
"viewer": Object {
224-
"currencies": null,
225-
"user": Object {
226-
"items": Object {
227-
"edges": Array [
228-
Object {
229-
"node": Object {
230-
"isFavorite": null,
231-
},
232-
},
233-
Object {
234-
"node": Object {
235-
"isFavorite": null,
236-
},
237-
},
238-
],
239-
},
240-
"profile": null,
241-
},
242-
},
243-
},
244-
}
245-
`;
246-
247-
exports[`PathResolver should work when chunks are combined 2`] = `
248221
Object {
249222
"data": Object {
250223
"viewer": Object {
@@ -344,33 +317,6 @@ Object {
344317
`;
345318

346319
exports[`PathResolver should work when chunks are combined and split 1`] = `
347-
Object {
348-
"data": Object {
349-
"viewer": Object {
350-
"currencies": null,
351-
"user": Object {
352-
"items": Object {
353-
"edges": Array [
354-
Object {
355-
"node": Object {
356-
"isFavorite": null,
357-
},
358-
},
359-
Object {
360-
"node": Object {
361-
"isFavorite": null,
362-
},
363-
},
364-
],
365-
},
366-
"profile": null,
367-
},
368-
},
369-
},
370-
}
371-
`;
372-
373-
exports[`PathResolver should work when chunks are combined and split 2`] = `
374320
Object {
375321
"data": Object {
376322
"viewer": Object {
@@ -406,7 +352,7 @@ Object {
406352
}
407353
`;
408354

409-
exports[`PathResolver should work when chunks are combined and split 3`] = `
355+
exports[`PathResolver should work when chunks are combined and split 2`] = `
410356
Object {
411357
"data": Object {
412358
"viewer": Object {

0 commit comments

Comments
 (0)