GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: simplify prefinish · nodejs/node@f2f993a

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/streams
    • writable.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/streams/writable.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/writable.js
+33-40Lines changed: 33 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -865,52 +865,45 @@ function needFinish(state) {
865865
)) === (kEnding | kConstructed) && state.length === 0);
866866
}
867867

868-
function callFinal(stream, state) {
869-
let called = false;
870-
871-
function onFinish(err) {
872-
if (called) {
873-
errorOrDestroy(stream, err ?? ERR_MULTIPLE_CALLBACK());
874-
return;
875-
}
876-
called = true;
877-
878-
state.pendingcb--;
879-
if (err) {
880-
callFinishedCallbacks(state, err);
881-
errorOrDestroy(stream, err, (state[kState] & kSync) !== 0);
882-
} else if (needFinish(state)) {
883-
state[kState] |= kPrefinished;
884-
stream.emit('prefinish');
885-
// Backwards compat. Don't check state.sync here.
886-
// Some streams assume 'finish' will be emitted
887-
// asynchronously relative to _final callback.
888-
state.pendingcb++;
889-
process.nextTick(finish, stream, state);
890-
}
868+
function onFinish(stream, state, err) {
869+
if ((state[kState] & kPrefinished) !== 0) {
870+
errorOrDestroy(stream, err ?? ERR_MULTIPLE_CALLBACK());
871+
return;
891872
}
892-
893-
state[kState] |= kSync;
894-
state.pendingcb++;
895-
896-
try {
897-
stream._final(onFinish);
898-
} catch (err) {
899-
onFinish(err);
873+
state.pendingcb--;
874+
if (err) {
875+
callFinishedCallbacks(state, err);
876+
errorOrDestroy(stream, err, (state[kState] & kSync) !== 0);
877+
} else if (needFinish(state)) {
878+
state[kState] |= kPrefinished;
879+
stream.emit('prefinish');
880+
// Backwards compat. Don't check state.sync here.
881+
// Some streams assume 'finish' will be emitted
882+
// asynchronously relative to _final callback.
883+
state.pendingcb++;
884+
process.nextTick(finish, stream, state);
900885
}
901-
902-
state[kState] &= ~kSync;
903886
}
904887

905888
function prefinish(stream, state) {
906-
if ((state[kState] & (kPrefinished | kFinalCalled)) === 0) {
907-
if (typeof stream._final === 'function' && (state[kState] & kDestroyed) === 0) {
908-
state[kState] |= kFinalCalled;
909-
callFinal(stream, state);
910-
} else {
911-
state[kState] |= kPrefinished;
912-
stream.emit('prefinish');
889+
if ((state[kState] & (kPrefinished | kFinalCalled)) !== 0) {
890+
return;
891+
}
892+
893+
if (typeof stream._final === 'function' && (state[kState] & kDestroyed) === 0) {
894+
state[kState] |= kFinalCalled | kSync;
895+
state.pendingcb++;
896+
897+
try {
898+
stream._final((err) => onFinish(stream, state, err));
899+
} catch (err) {
900+
onFinish(stream, state, err);
913901
}
902+
903+
state[kState] &= ~kSync;
904+
} else {
905+
state[kState] |= kFinalCalled | kPrefinished;
906+
stream.emit('prefinish');
914907
}
915908
}
916909

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.