| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f611583 commit 7c5e322
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -479,9 +479,13 @@ class ReadableStream { | |||
| 479 | 479 | ||
| 480 | 480 | // eslint-disable-next-line no-use-before-define | |
| 481 | 481 | const reader = new ReadableStreamDefaultReader(this); | |
| 482 | - let done = false; | ||
| 482 | + | ||
| 483 | + // No __proto__ here to avoid the performance hit. | ||
| 484 | + const state = { | ||
| 485 | + done: false, | ||
| 486 | + current: undefined, | ||
| 487 | + }; | ||
| 483 | 488 | let started = false; | |
| 484 | - let current; | ||
| 485 | 489 | ||
| 486 | 490 | // The nextSteps function is not an async function in order | |
| 487 | 491 | // to make it more efficient. Because nextSteps explicitly | |
@@ -490,7 +494,7 @@ class ReadableStream { | |||
| 490 | 494 | // unnecessary Promise allocations to occur, which just add | |
| 491 | 495 | // cost. | |
| 492 | 496 | function nextSteps() { | |
| 493 | - if (done) | ||
| 497 | + if (state.done) | ||
| 494 | 498 | return PromiseResolve({ done: true, value: undefined }); | |
| 495 | 499 | ||
| 496 | 500 | if (reader[kState].stream === undefined) { | |
@@ -500,31 +504,15 @@ class ReadableStream { | |||
| 500 | 504 | } | |
| 501 | 505 | const promise = createDeferredPromise(); | |
| 502 | 506 | ||
| 503 | - readableStreamDefaultReaderRead(reader, { | ||
| 504 | - [kChunk](chunk) { | ||
| 505 | - current = undefined; | ||
| 506 | - promise.resolve({ value: chunk, done: false }); | ||
| 507 | - }, | ||
| 508 | - [kClose]() { | ||
| 509 | - current = undefined; | ||
| 510 | - done = true; | ||
| 511 | - readableStreamReaderGenericRelease(reader); | ||
| 512 | - promise.resolve({ done: true, value: undefined }); | ||
| 513 | - }, | ||
| 514 | - [kError](error) { | ||
| 515 | - current = undefined; | ||
| 516 | - done = true; | ||
| 517 | - readableStreamReaderGenericRelease(reader); | ||
| 518 | - promise.reject(error); | ||
| 519 | - }, | ||
| 520 | - }); | ||
| 507 | + // eslint-disable-next-line no-use-before-define | ||
| 508 | + readableStreamDefaultReaderRead(reader, new ReadableStreamAsyncIteratorReadRequest(reader, state, promise)); | ||
| 521 | 509 | return promise.promise; | |
| 522 | 510 | } | |
| 523 | 511 | ||
| 524 | 512 | async function returnSteps(value) { | |
| 525 | - if (done) | ||
| 513 | + if (state.done) | ||
| 526 | 514 | return { done: true, value }; // eslint-disable-line node-core/avoid-prototype-pollution | |
| 527 | - done = true; | ||
| 515 | + state.done = true; | ||
| 528 | 516 | ||
| 529 | 517 | if (reader[kState].stream === undefined) { | |
| 530 | 518 | throw new ERR_INVALID_STATE.TypeError( | |
@@ -561,19 +549,19 @@ class ReadableStream { | |||
| 561 | 549 | // need to investigate if it's a bug in our impl or | |
| 562 | 550 | // the spec. | |
| 563 | 551 | if (!started) { | |
| 564 | - current = PromiseResolve(); | ||
| 552 | + state.current = PromiseResolve(); | ||
| 565 | 553 | started = true; | |
| 566 | 554 | } | |
| 567 | - current = current !== undefined ? | ||
| 568 | - PromisePrototypeThen(current, nextSteps, nextSteps) : | ||
| 555 | + state.current = state.current !== undefined ? | ||
| 556 | + PromisePrototypeThen(state.current, nextSteps, nextSteps) : | ||
| 569 | 557 | nextSteps(); | |
| 570 | - return current; | ||
| 558 | + return state.current; | ||
| 571 | 559 | }, | |
| 572 | 560 | ||
| 573 | 561 | return(error) { | |
| 574 | - return current ? | ||
| 562 | + return state.current ? | ||
| 575 | 563 | PromisePrototypeThen( | |
| 576 | - current, | ||
| 564 | + state.current, | ||
| 577 | 565 | () => returnSteps(error), | |
| 578 | 566 | () => returnSteps(error)) : | |
| 579 | 567 | returnSteps(error); | |
@@ -774,6 +762,33 @@ function createReadableStreamBYOBRequest(controller, view) { | |||
| 774 | 762 | return stream; | |
| 775 | 763 | } | |
| 776 | 764 | ||
| 765 | + class ReadableStreamAsyncIteratorReadRequest { | ||
| 766 | + constructor(reader, state, promise) { | ||
| 767 | + this.reader = reader; | ||
| 768 | + this.state = state; | ||
| 769 | + this.promise = promise; | ||
| 770 | + } | ||
| 771 | + | ||
| 772 | + [kChunk](chunk) { | ||
| 773 | + this.state.current = undefined; | ||
| 774 | + this.promise.resolve({ value: chunk, done: false }); | ||
| 775 | + } | ||
| 776 | + | ||
| 777 | + [kClose]() { | ||
| 778 | + this.state.current = undefined; | ||
| 779 | + this.state.done = true; | ||
| 780 | + readableStreamReaderGenericRelease(this.reader); | ||
| 781 | + this.promise.resolve({ done: true, value: undefined }); | ||
| 782 | + } | ||
| 783 | + | ||
| 784 | + [kError](error) { | ||
| 785 | + this.state.current = undefined; | ||
| 786 | + this.state.done = true; | ||
| 787 | + readableStreamReaderGenericRelease(this.reader); | ||
| 788 | + this.promise.reject(error); | ||
| 789 | + } | ||
| 790 | + } | ||
| 791 | + | ||
| 777 | 792 | class DefaultReadRequest { | |
| 778 | 793 | constructor() { | |
| 779 | 794 | this[kState] = createDeferredPromise(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments