-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(gatsby): type exposed workerpool functions #31768
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,9 @@ import * as buildUtils from "./build-utils" | |
import { Span } from "opentracing" | ||
import { IProgram, Stage } from "./types" | ||
import { PackageJson } from "../.." | ||
import type { GatsbyWorkerPool } from "../utils/worker/pool" | ||
|
||
type IActivity = any // TODO | ||
type IWorkerPool = any // TODO | ||
|
||
export interface IBuildArgs extends IProgram { | ||
directory: string | ||
|
@@ -193,15 +193,15 @@ export interface IRenderHtmlResult { | |
} | ||
|
||
const renderHTMLQueue = async ( | ||
workerPool: IWorkerPool, | ||
workerPool: GatsbyWorkerPool, | ||
activity: IActivity, | ||
htmlComponentRendererPath: string, | ||
pages: Array<string>, | ||
stage: Stage = Stage.BuildHTML | ||
): Promise<void> => { | ||
// We need to only pass env vars that are set programmatically in gatsby-cli | ||
// to child process. Other vars will be picked up from environment. | ||
const envVars = [ | ||
const envVars: Array<[string, string | undefined]> = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be undefined, or is this because of process.env having the undefined type? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's just |
||
[`NODE_ENV`, process.env.NODE_ENV], | ||
[`gatsby_executing_command`, process.env.gatsby_executing_command], | ||
[`gatsby_log_level`, process.env.gatsby_log_level], | ||
|
@@ -220,14 +220,15 @@ const renderHTMLQueue = async ( | |
|
||
try { | ||
await Bluebird.map(segments, async pageSegment => { | ||
const htmlRenderMeta: IRenderHtmlResult = await renderHTML({ | ||
const renderHTMLResult = await renderHTML({ | ||
envVars, | ||
htmlComponentRendererPath, | ||
paths: pageSegment, | ||
sessionId, | ||
}) | ||
|
||
if (stage === `build-html`) { | ||
const htmlRenderMeta = renderHTMLResult as IRenderHtmlResult | ||
store.dispatch({ | ||
type: `HTML_GENERATED`, | ||
payload: pageSegment, | ||
|
@@ -304,7 +305,7 @@ export const doBuildPages = async ( | |
rendererPath: string, | ||
pagePaths: Array<string>, | ||
activity: IActivity, | ||
workerPool: IWorkerPool, | ||
workerPool: GatsbyWorkerPool, | ||
stage: Stage | ||
): Promise<void> => { | ||
try { | ||
|
@@ -332,7 +333,7 @@ export const buildHTML = async ({ | |
stage: Stage | ||
pagePaths: Array<string> | ||
activity: IActivity | ||
workerPool: IWorkerPool | ||
workerPool: GatsbyWorkerPool | ||
}): Promise<void> => { | ||
const { rendererPath } = await buildRenderer(program, stage, activity.span) | ||
await doBuildPages(rendererPath, pagePaths, activity, workerPool, stage) | ||
|
@@ -346,7 +347,7 @@ export async function buildHTMLPagesAndDeleteStaleArtifacts({ | |
program, | ||
}: { | ||
pageRenderer: string | ||
workerPool: IWorkerPool | ||
workerPool: GatsbyWorkerPool | ||
buildSpan?: Span | ||
program: IBuildArgs | ||
}): Promise<{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
import Worker from "jest-worker" | ||
import { cpuCoreCount } from "gatsby-core-utils" | ||
|
||
export const create = (): Worker => | ||
// we only import it to get types, typescript will remove it from code if it's only used for types | ||
import * as exposedWorkerPoolMethods from "./child" | ||
import type { CreateWorkerPoolType } from "./types" | ||
|
||
export type GatsbyWorkerPool = CreateWorkerPoolType< | ||
typeof exposedWorkerPoolMethods | ||
> | ||
|
||
export const create = (): GatsbyWorkerPool => | ||
new Worker(require.resolve(`./child`), { | ||
numWorkers: Math.max(1, cpuCoreCount() - 1), | ||
forkOptions: { | ||
silent: false, | ||
}, | ||
}) | ||
}) as GatsbyWorkerPool |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type Worker from "jest-worker" | ||
|
||
type WrapReturnOfAFunctionInAPromise< | ||
FunctionThatDoesNotReturnAPromise extends (...args: Array<any>) => any | ||
> = ( | ||
...a: Parameters<FunctionThatDoesNotReturnAPromise> | ||
) => Promise<ReturnType<FunctionThatDoesNotReturnAPromise>> | ||
|
||
// jest-worker will make sync function async, so to keep proper types we need to adjust types so all functions | ||
// on worker pool are async | ||
type EnsureFunctionReturnsAPromise<MaybeFunction> = MaybeFunction extends ( | ||
...args: Array<any> | ||
) => Promise<any> | ||
? MaybeFunction | ||
: MaybeFunction extends (...args: Array<any>) => any | ||
? WrapReturnOfAFunctionInAPromise<MaybeFunction> | ||
: never | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is complex XD isn't there another way to accomplish the same or not really? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My TS-foo is not strong, so not sure about simpler way (at least to have those types automatically) - there might, I just don't know of any I initially just used exported types from I guess simple approach would be to define types of "exposed functions" manually, but then it's possibly error-prone and easy to get out of sync. |
||
|
||
export type CreateWorkerPoolType<ExposedFunctions> = Worker & | ||
{ | ||
[FunctionName in keyof ExposedFunctions]: EnsureFunctionReturnsAPromise< | ||
ExposedFunctions[FunctionName] | ||
> | ||
} | ||
Comment on lines
+19
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WorkerPool type creator, now used just for main workerPool, but in following PRs will be used for tests where test workerPool will include additional helper functions for tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fancy! 🍷