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): Convert schema-hot-reloader to typescript #22497

Merged
merged 2 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
const { debounce, cloneDeep } = require(`lodash`)
const { emitter, store } = require(`../redux`)
const { rebuild } = require(`../schema`)
const { haveEqualFields } = require(`../schema/infer/inference-metadata`)
const { updateStateAndRunQueries } = require(`../query/query-watcher`)
const report = require(`gatsby-cli/lib/reporter`)

const inferredTypesChanged = (typeMap, prevTypeMap) =>
import { debounce, cloneDeep } from "lodash"
import { emitter, store } from "../redux"
import { rebuild } from "../schema"
import { haveEqualFields } from "../schema/infer/inference-metadata"
import { updateStateAndRunQueries } from "../query/query-watcher"
import report from "gatsby-cli/lib/reporter"
import { IGatsbyState } from "../redux/types"

type TypeMap = IGatsbyState["inferenceMetadata"]["typeMap"]

const inferredTypesChanged = (
typeMap: TypeMap,
prevTypeMap: TypeMap
): boolean =>
Object.keys(typeMap).some(
type =>
typeMap[type].dirty && !haveEqualFields(typeMap[type], prevTypeMap[type])
)

const schemaChanged = (schemaCustomization, lastSchemaCustomization) =>
const schemaChanged = (
schemaCustomization: IGatsbyState["schemaCustomization"],
lastSchemaCustomization: IGatsbyState["schemaCustomization"]
): boolean =>
[`fieldExtensions`, `printConfig`, `thirdPartySchemas`, `types`].some(
key => schemaCustomization[key] !== lastSchemaCustomization[key]
)

let lastMetadata
let lastSchemaCustomization
let lastMetadata: IGatsbyState["inferenceMetadata"]
let lastSchemaCustomization: IGatsbyState["schemaCustomization"]

// API_RUNNING_QUEUE_EMPTY could be emitted multiple types
// in a short period of time, so debounce seems reasonable
const maybeRebuildSchema = debounce(async () => {
const maybeRebuildSchema = debounce(async (): Promise<void> => {
const { inferenceMetadata, schemaCustomization } = store.getState()

if (
Expand All @@ -40,7 +49,7 @@ const maybeRebuildSchema = debounce(async () => {
activity.end()
}, 1000)

module.exports = () => {
export const bootstrapSchemaHotReloader = (): void => {
const { inferenceMetadata, schemaCustomization } = store.getState()
lastMetadata = cloneDeep(inferenceMetadata)
lastSchemaCustomization = schemaCustomization
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/commands/develop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import * as WorkerPool from "../utils/worker/pool"
import http from "http"
import https from "https"

import bootstrapSchemaHotReloader from "../bootstrap/schema-hot-reloader"
import { bootstrapSchemaHotReloader } from "../bootstrap/schema-hot-reloader"
import bootstrapPageHotReloader from "../bootstrap/page-hot-reloader"
import { developStatic } from "./develop-static"
import withResolverContext from "../schema/context"
Expand Down