Skip to content

Commit

Permalink
refactor: align with Promise.withResolvers() (#15171)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Nov 29, 2023
1 parent d207c21 commit 642f9bc
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
13 changes: 0 additions & 13 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,6 @@ export interface DepOptimizationResult {
cancel: () => void
}

export interface DepOptimizationProcessing {
promise: Promise<void>
resolve: () => void
}

export interface OptimizedDepInfo {
id: string
file: string
Expand Down Expand Up @@ -885,14 +880,6 @@ export async function addManuallyIncludedOptimizeDeps(
}
}

export function newDepOptimizationProcessing(): DepOptimizationProcessing {
let resolve: () => void
const promise = new Promise((_resolve) => {
resolve = _resolve
}) as Promise<void>
return { promise, resolve: resolve! }
}

// Convert to { id: src }
export function depsFromOptimizedDepInfo(
depsInfo: Record<string, OptimizedDepInfo>,
Expand Down
17 changes: 6 additions & 11 deletions packages/vite/src/node/optimizer/optimizer.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')

Expand Down Expand Up @@ -142,8 +137,8 @@ async function createDepsOptimizer(
}
}

let depOptimizationProcessing = newDepOptimizationProcessing()
let depOptimizationProcessingQueue: DepOptimizationProcessing[] = []
let depOptimizationProcessing = promiseWithResolvers<void>()
let depOptimizationProcessingQueue: PromiseWithResolvers<void>[] = []
const resolveEnqueuedProcessingPromises = () => {
// Resolve all the processings (including the ones which were delayed)
for (const processing of depOptimizationProcessingQueue) {
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1331,3 +1331,18 @@ export function isDevServer(
): server is ViteDevServer {
return 'pluginContainer' in server
}

export interface PromiseWithResolvers<T> {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
}
export function promiseWithResolvers<T>(): PromiseWithResolvers<T> {
let resolve: any
let reject: any
const promise = new Promise<T>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
})
return { promise, resolve, reject }
}
22 changes: 16 additions & 6 deletions playground/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,7 @@ async function untilBrowserLog(
target?: string | RegExp | Array<string | RegExp>,
expectOrder = true,
): Promise<string[]> {
let resolve: () => void
let reject: (reason: any) => void
const promise = new Promise<void>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
})
const { promise, resolve, reject } = promiseWithResolvers<void>()

const logs = []

Expand Down Expand Up @@ -363,3 +358,18 @@ export async function killProcess(
serverProcess.kill('SIGTERM', { forceKillAfterTimeout: 2000 })
}
}

export interface PromiseWithResolvers<T> {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
}
export function promiseWithResolvers<T>(): PromiseWithResolvers<T> {
let resolve: any
let reject: any
const promise = new Promise<T>((_resolve, _reject) => {
resolve = _resolve
reject = _reject
})
return { promise, resolve, reject }
}

0 comments on commit 642f9bc

Please sign in to comment.