Skip to content
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

Merged
merged 2 commits into from
Jun 4, 2021
Merged

Conversation

pieh
Copy link
Contributor

@pieh pieh commented Jun 4, 2021

Description

Currently our worker pool has type any when actually interacting with it (in build-html.ts), this just adds types to exposed functions

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 4, 2021
@pieh pieh force-pushed the pieh/worker-pool-types branch from 1b51012 to c0f879c Compare June 4, 2021 09:19
@pieh pieh added topic: TypeScript Issues and PRs related to TS in general, public typings or gatsby-plugin-typescript and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jun 4, 2021
? MaybeFunction
: MaybeFunction extends (...args: Array<any>) => any
? WrapReturnOfAFunctionInAPromise<MaybeFunction>
: never
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never to disallow non-functions if export is not a function

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 child module as they are (the way jest itself does it - https://github.com/facebook/jest/blob/5f4dd187d89070d07617444186684c20d9213031/packages/jest-runner/src/index.ts#L38-L40), but if you have sync method in child - using it on workerPool would return promise anyway, so types were not always correct.

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.

Comment on lines +19 to +24
export type CreateWorkerPoolType<ExposedFunctions> = Worker &
{
[FunctionName in keyof ExposedFunctions]: EnsureFunctionReturnsAPromise<
ExposedFunctions[FunctionName]
>
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-namespace, @typescript-eslint/naming-convention */
Copy link
Contributor Author

@pieh pieh Jun 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our lint rules want to discourage declaring namespace in source code files. Unfortunately having it in declaration file seemed to cause problems for packages other than gatsby ( https://app.circleci.com/pipelines/github/gatsbyjs/gatsby/64194/workflows/e70153e0-70d7-45b6-afa7-a7dcaf4a3471/jobs/707758 was failing for gatsby-plugin-graphql-config).

@pieh pieh force-pushed the pieh/worker-pool-types branch from cc31a2d to 304208c Compare June 4, 2021 11:13
@pieh pieh marked this pull request as ready for review June 4, 2021 11:52
Copy link
Contributor

@wardpeet wardpeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -23,7 +23,7 @@ export async function bootstrap(
initialContext: Partial<IBuildContext>
): Promise<{
gatsbyNodeGraphQLFunction: Runner
workerPool: JestWorker
workerPool: GatsbyWorkerPool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fancy! 🍷

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]> = [
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just process.env.X will be string | undefined - so it technically could undefined if it wasn't setup before. We could force TS into making those just string but then it's not really checking for type safety 🤷

? MaybeFunction
: MaybeFunction extends (...args: Array<any>) => any
? WrapReturnOfAFunctionInAPromise<MaybeFunction>
: never
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

@LekoArts LekoArts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@pieh pieh merged commit 4732449 into master Jun 4, 2021
@pieh pieh deleted the pieh/worker-pool-types branch June 4, 2021 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: TypeScript Issues and PRs related to TS in general, public typings or gatsby-plugin-typescript
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants