Repository: dotnet/runtime
File: src/native/libs/Common/JavaScript/loader/assets.ts (TypeScript)

Code before:
        const response = await responsePromise;
        dotnetAssert.check(response, "Bad response in loadResourceThrottle");

        if (!noBuffer[asset.behavior]) {
            asset.buffer = await response.arrayBuffer();
        }
        if (!leaveAfterInstantiation[asset.behavior]) {
            leaveThrottling(asset);
        }
        return response;
    } catch (err) {
        leaveThrottling(asset);
        throw err;
    }
}

function enterThrottling(asset: AssetEntryInternal) {
    asset.inprogress = true;
    ++currentParallelDownloads;
    if (currentParallelDownloads === loaderConfig.maxParallelDownloads) {
        dotnetLogger.debug("Throttling further parallel downloads");
        throttlingPCS = createPromiseCompletionSource<void>();
    }
}

function leaveThrottling(asset: AssetEntryInternal) {
    dotnetAssert.check(asset.inprogress, "Asset is not in progress in leaveThrottling");
    asset.inprogress = false;
    --currentParallelDownloads;
    if (throttlingPCS && currentParallelDownloads == loaderConfig.maxParallelDownloads! - 1) {
        dotnetLogger.debug("Resuming more parallel downloads");
        const oldThrottlingPCS = throttlingPCS;
        throttlingPCS = undefined;
        oldThrottlingPCS.resolve();
    }
}

async function loadResourceFetch(asset: AssetEntryInternal): Promise<Response> {
    const expectedContentType = behaviorToContentTypeMap[asset.behavior];
    dotnetAssert.check(expectedContentType, `Unsupported asset behavior: ${asset.behavior}`);
    if (asset.buffer) {
        const arrayBuffer = await asset.buffer;
        return responseLike(asset.resolvedUrl!, arrayBuffer, {
            status: 200,
            statusText: "OK",
            headers: {
                "Content-Length": arrayBuffer.byteLength.toString(),
                "Content-Type": expectedContentType,
            }
        });
    }

Review comment (suggestion):
At first I could not understand in what context we "leave" it (e.g. leave this asset away from throttling count or what). We have `enterThrottling` so exit is probably a better opposite word. But the best would be `start/finish`.

Code after:
        throw err;
    }
}

function startThrottling(asset: AssetEntryInternal) {
    asset.inprogress = true;
    ++currentParallelDownloads;
    if (currentParallelDownloads === loaderConfig.maxParallelDownloads) {
        dotnetLogger.debug("Throttling further parallel downloads");
        throttlingPCS = createPromiseCompletionSource<void>();
    }
}

function finishThrottling(asset: AssetEntryInternal) {
    dotnetAssert.check(asset.inprogress, "Asset is not in progress in finishThrottling");
    asset.inprogress = false;
    --currentParallelDownloads;
    if (throttlingPCS && currentParallelDownloads == loaderConfig.maxParallelDownloads! - 1) {
        dotnetLogger.debug("Resuming more parallel downloads");
        const oldThrottlingPCS = throttlingPCS;
        throttlingPCS = undefined;
        oldThrottlingPCS.resolve();
    }
}

async function loadResourceFetch(asset: AssetEntryInternal): Promise<Response> {
    const expectedContentType = behaviorToContentTypeMap[asset.behavior];
    dotnetAssert.check(expectedContentType, `Unsupported asset behavior: ${asset.behavior}`);
    if (asset.buffer) {
        const arrayBuffer = await asset.buffer;
        return responseLike(asset.resolvedUrl!, arrayBuffer, {
            status: 200,
            statusText: "OK",
            headers: {
                "Content-Length": arrayBuffer.byteLength.toString(),
                "Content-Type": expectedContentType,
            }
        });
    }
    if (asset.pendingDownload) {
        return asset.pendingDownload.response;
    }
    if (typeof loadBootResourceCallback === "function") {
        const blazorType = behaviorToBlazorAssetTypeMap[asset.behavior];
        dotnetAssert.check(blazorType, `Unsupported asset behavior: ${asset.behavior}`);
        const customLoadResult = loadBootResourceCallback(blazorType, asset.name, asset.resolvedUrl!, asset.integrity!, asset.behavior);
        if (typeof customLoadResult === "string") {
            asset.resolvedUrl = makeURLAbsoluteWithApplicationBase(customLoadResult);
        } else if (typeof customLoadResult === "object") {
            return customLoadResult as any;
        }