Skip to content

Commit

Permalink
clear the global timeout once an operation is done in the Storage SDK (
Browse files Browse the repository at this point in the history
…#5703)

* clear the global timeout once an operation is done

* Create tame-beans-study.md

* format
  • Loading branch information
Feiyang1 committed Nov 10, 2021
1 parent 3b338db commit e0fe2b6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-beans-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/storage": patch
---

Clear the global timeout once an operation is done in the Storage SDK. Otherwise it may prevent Node.js from exiting.
24 changes: 18 additions & 6 deletions packages/storage/src/implementation/backoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function start(
// Would type this as "number" but that doesn't work for Node so ¯\_(ツ)_/¯
// TODO: find a way to exclude Node type definition for storage because storage only works in browser
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let timeoutId: any = null;
let retryTimeoutId: any = null;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let globalTimeoutId: any = null;
let hitTimeout = false;
let cancelState = 0;

Expand All @@ -58,22 +60,31 @@ export function start(
}

function callWithDelay(millis: number): void {
timeoutId = setTimeout(() => {
timeoutId = null;
retryTimeoutId = setTimeout(() => {
retryTimeoutId = null;
f(handler, canceled());
}, millis);
}

function clearGlobalTimeout(): void {
if (globalTimeoutId) {
clearTimeout(globalTimeoutId);
}
}

function handler(success: boolean, ...args: any[]): void {
if (triggeredCallback) {
clearGlobalTimeout();
return;
}
if (success) {
clearGlobalTimeout();
triggerCallback.call(null, success, ...args);
return;
}
const mustStop = canceled() || hitTimeout;
if (mustStop) {
clearGlobalTimeout();
triggerCallback.call(null, success, ...args);
return;
}
Expand All @@ -97,14 +108,15 @@ export function start(
return;
}
stopped = true;
clearGlobalTimeout();
if (triggeredCallback) {
return;
}
if (timeoutId !== null) {
if (retryTimeoutId !== null) {
if (!wasTimeout) {
cancelState = 2;
}
clearTimeout(timeoutId);
clearTimeout(retryTimeoutId);
callWithDelay(0);
} else {
if (!wasTimeout) {
Expand All @@ -113,7 +125,7 @@ export function start(
}
}
callWithDelay(0);
setTimeout(() => {
globalTimeoutId = setTimeout(() => {
hitTimeout = true;
stop(true);
}, timeout);
Expand Down

0 comments on commit e0fe2b6

Please sign in to comment.