From dba950a04e4cfce9bce5ea17a0e82bd96591b4a5 Mon Sep 17 00:00:00 2001 From: mottox2 Date: Tue, 24 Mar 2020 02:49:23 +0900 Subject: [PATCH 1/2] chore(gatsby): Convert schema-hot-reloader to typescript --- ...hot-reloader.js => schema-hot-reloader.ts} | 35 ++++++++++++------- packages/gatsby/src/commands/develop.ts | 2 +- 2 files changed, 23 insertions(+), 14 deletions(-) rename packages/gatsby/src/bootstrap/{schema-hot-reloader.js => schema-hot-reloader.ts} (56%) diff --git a/packages/gatsby/src/bootstrap/schema-hot-reloader.js b/packages/gatsby/src/bootstrap/schema-hot-reloader.ts similarity index 56% rename from packages/gatsby/src/bootstrap/schema-hot-reloader.js rename to packages/gatsby/src/bootstrap/schema-hot-reloader.ts index 6cd04d8324706..b3206678cd231 100644 --- a/packages/gatsby/src/bootstrap/schema-hot-reloader.js +++ b/packages/gatsby/src/bootstrap/schema-hot-reloader.ts @@ -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 => { const { inferenceMetadata, schemaCustomization } = store.getState() if ( @@ -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 diff --git a/packages/gatsby/src/commands/develop.ts b/packages/gatsby/src/commands/develop.ts index 9e89fe1afcc12..819cad1ce18d6 100644 --- a/packages/gatsby/src/commands/develop.ts +++ b/packages/gatsby/src/commands/develop.ts @@ -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" From 68401085d89aec8ea4f3bcf5ca79fb45b29eb4e4 Mon Sep 17 00:00:00 2001 From: Blaine Kasten Date: Mon, 23 Mar 2020 14:05:27 -0500 Subject: [PATCH 2/2] simplify typing a little --- packages/gatsby/src/bootstrap/schema-hot-reloader.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/gatsby/src/bootstrap/schema-hot-reloader.ts b/packages/gatsby/src/bootstrap/schema-hot-reloader.ts index b3206678cd231..808b0ab70b89c 100644 --- a/packages/gatsby/src/bootstrap/schema-hot-reloader.ts +++ b/packages/gatsby/src/bootstrap/schema-hot-reloader.ts @@ -7,6 +7,8 @@ import report from "gatsby-cli/lib/reporter" import { IGatsbyState } from "../redux/types" type TypeMap = IGatsbyState["inferenceMetadata"]["typeMap"] +type SchemaCustomization = IGatsbyState["schemaCustomization"] +type InferenceMetadata = IGatsbyState["inferenceMetadata"] const inferredTypesChanged = ( typeMap: TypeMap, @@ -18,15 +20,15 @@ const inferredTypesChanged = ( ) const schemaChanged = ( - schemaCustomization: IGatsbyState["schemaCustomization"], - lastSchemaCustomization: IGatsbyState["schemaCustomization"] + schemaCustomization: SchemaCustomization, + lastSchemaCustomization: SchemaCustomization ): boolean => [`fieldExtensions`, `printConfig`, `thirdPartySchemas`, `types`].some( key => schemaCustomization[key] !== lastSchemaCustomization[key] ) -let lastMetadata: IGatsbyState["inferenceMetadata"] -let lastSchemaCustomization: IGatsbyState["schemaCustomization"] +let lastMetadata: InferenceMetadata +let lastSchemaCustomization: SchemaCustomization // API_RUNNING_QUEUE_EMPTY could be emitted multiple types // in a short period of time, so debounce seems reasonable