| 767 | } |
| 768 | |
| 769 | void AsyncWritableStream::finishedWriting(AsyncBufferView::ID bufferID, Function<void(AsyncBufferView::ID)>&& callback, |
| 770 | Result res) |
| 771 | { |
| 772 | SC_ASYNC_STREAMS_ASSERT_RELEASE(state == State::Writing or state == State::Ending); |
| 773 | |
| 774 | if (not res) |
| 775 | { |
| 776 | eventError.emit(res); |
| 777 | } |
| 778 | |
| 779 | bool emitDrain = false; |
| 780 | Request request; |
| 781 | if (writeQueue.popFront(request)) |
| 782 | { |
| 783 | tryAsync(asyncWrite(request.bufferID, request.cb)); |
| 784 | buffers->unrefBuffer(request.bufferID); // 2c. refbuffer in AsyncWritable::write |
| 785 | } |
| 786 | else |
| 787 | { |
| 788 | // Queue is empty |
| 789 | if (state == State::Ending) |
| 790 | { |
| 791 | if (canEndWritable()) |
| 792 | { |
| 793 | state = State::Ended; |
| 794 | } |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | state = State::Stopped; |
| 799 | emitDrain = true; |
| 800 | } |
| 801 | } |
| 802 | if (callback.isValid()) |
| 803 | { |
| 804 | callback(bufferID); |
| 805 | } |
| 806 | |
| 807 | if (state == State::Ended) |
| 808 | { |
| 809 | eventFinish.emit(); |
| 810 | if (autoDestroy) |
| 811 | { |
| 812 | destroy(); |
| 813 | } |
| 814 | } |
| 815 | else if (emitDrain) |
| 816 | { |
| 817 | eventDrain.emit(); |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | void AsyncWritableStream::end() |
| 822 | { |