From 642f9bcb5d4a8c2455569117ef2984fe09c3acd4 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 29 Nov 2023 08:13:27 +0100 Subject: [PATCH] refactor: align with Promise.withResolvers() (#15171) --- packages/vite/src/node/optimizer/index.ts | 13 ----------- packages/vite/src/node/optimizer/optimizer.ts | 17 +++++--------- packages/vite/src/node/utils.ts | 15 +++++++++++++ playground/test-utils.ts | 22 ++++++++++++++----- 4 files changed, 37 insertions(+), 30 deletions(-) diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 3bdb0620e7683f..12e40fef5ff14f 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -166,11 +166,6 @@ export interface DepOptimizationResult { cancel: () => void } -export interface DepOptimizationProcessing { - promise: Promise - resolve: () => void -} - export interface OptimizedDepInfo { id: string file: string @@ -885,14 +880,6 @@ export async function addManuallyIncludedOptimizeDeps( } } -export function newDepOptimizationProcessing(): DepOptimizationProcessing { - let resolve: () => void - const promise = new Promise((_resolve) => { - resolve = _resolve - }) as Promise - return { promise, resolve: resolve! } -} - // Convert to { id: src } export function depsFromOptimizedDepInfo( depsInfo: Record, diff --git a/packages/vite/src/node/optimizer/optimizer.ts b/packages/vite/src/node/optimizer/optimizer.ts index edb4af5232b995..f580f98a0af622 100644 --- a/packages/vite/src/node/optimizer/optimizer.ts +++ b/packages/vite/src/node/optimizer/optimizer.ts @@ -1,5 +1,6 @@ import colors from 'picocolors' -import { createDebugger, getHash } from '../utils' +import { createDebugger, getHash, promiseWithResolvers } from '../utils' +import type { PromiseWithResolvers } from '../utils' import { getDepOptimizationConfig } from '../config' import type { ResolvedConfig, ViteDevServer } from '..' import { @@ -14,17 +15,11 @@ import { getOptimizedDepPath, initDepsOptimizerMetadata, loadCachedDepOptimizationMetadata, - newDepOptimizationProcessing, optimizeServerSsrDeps, runOptimizeDeps, toDiscoveredDependencies, } from '.' -import type { - DepOptimizationProcessing, - DepOptimizationResult, - DepsOptimizer, - OptimizedDepInfo, -} from '.' +import type { DepOptimizationResult, DepsOptimizer, OptimizedDepInfo } from '.' const debug = createDebugger('vite:deps') @@ -142,8 +137,8 @@ async function createDepsOptimizer( } } - let depOptimizationProcessing = newDepOptimizationProcessing() - let depOptimizationProcessingQueue: DepOptimizationProcessing[] = [] + let depOptimizationProcessing = promiseWithResolvers() + let depOptimizationProcessingQueue: PromiseWithResolvers[] = [] const resolveEnqueuedProcessingPromises = () => { // Resolve all the processings (including the ones which were delayed) for (const processing of depOptimizationProcessingQueue) { @@ -269,7 +264,7 @@ async function createDepsOptimizer( // Create a new promise for the next rerun, discovered missing // dependencies will be assigned this promise from this point - depOptimizationProcessing = newDepOptimizationProcessing() + depOptimizationProcessing = promiseWithResolvers() } function prepareKnownDeps() { diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 032dea8b79f5ed..4e893968c44444 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1331,3 +1331,18 @@ export function isDevServer( ): server is ViteDevServer { return 'pluginContainer' in server } + +export interface PromiseWithResolvers { + promise: Promise + resolve: (value: T | PromiseLike) => void + reject: (reason?: any) => void +} +export function promiseWithResolvers(): PromiseWithResolvers { + let resolve: any + let reject: any + const promise = new Promise((_resolve, _reject) => { + resolve = _resolve + reject = _reject + }) + return { promise, resolve, reject } +} diff --git a/playground/test-utils.ts b/playground/test-utils.ts index af5c3f65e42bd6..0e2f54fc7c6298 100644 --- a/playground/test-utils.ts +++ b/playground/test-utils.ts @@ -267,12 +267,7 @@ async function untilBrowserLog( target?: string | RegExp | Array, expectOrder = true, ): Promise { - let resolve: () => void - let reject: (reason: any) => void - const promise = new Promise((_resolve, _reject) => { - resolve = _resolve - reject = _reject - }) + const { promise, resolve, reject } = promiseWithResolvers() const logs = [] @@ -363,3 +358,18 @@ export async function killProcess( serverProcess.kill('SIGTERM', { forceKillAfterTimeout: 2000 }) } } + +export interface PromiseWithResolvers { + promise: Promise + resolve: (value: T | PromiseLike) => void + reject: (reason?: any) => void +} +export function promiseWithResolvers(): PromiseWithResolvers { + let resolve: any + let reject: any + const promise = new Promise((_resolve, _reject) => { + resolve = _resolve + reject = _reject + }) + return { promise, resolve, reject } +}