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

feat(gatsby): Move final parts into develop state machine #25716

Merged
merged 5 commits into from
Jul 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
240 changes: 10 additions & 230 deletions packages/gatsby/src/commands/develop-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,19 @@ import chalk from "chalk"
import telemetry from "gatsby-telemetry"
import express from "express"
import { initTracer } from "../utils/tracer"
import db from "../db"
import { detectPortInUseAndPrompt } from "../utils/detect-port-in-use-and-prompt"
import onExit from "signal-exit"
import {
userPassesFeedbackRequestHeuristic,
showFeedbackRequest,
} from "../utils/feedback"
import { startRedirectListener } from "../bootstrap/redirects-writer"
import { markWebpackStatusAsPending } from "../utils/webpack-status"

import { IProgram } from "./types"
import {
IBuildContext,
initialize,
rebuildSchemaWithSitePage,
writeOutRedirects,
startWebpackServer,
} from "../services"
import { boundActionCreators } from "../redux/actions"
import { ProgramStatus } from "../redux/types"
import {
MachineConfig,
AnyEventObject,
Machine,
interpret,
Actor,
Interpreter,
forwardTo,
State,
} from "xstate"
import { dataLayerMachine } from "../state-machines/data-layer"
import { IDataLayerContext } from "../state-machines/data-layer/types"
import { IBuildContext } from "../services"
import { AnyEventObject, interpret, Actor, Interpreter, State } from "xstate"
import { globalTracer } from "opentracing"
import { IQueryRunningContext } from "../state-machines/query-running/types"
import { queryRunningMachine } from "../state-machines/query-running"
import { IWaitingContext } from "../state-machines/waiting/types"
import { runMutationAndMarkDirty } from "../state-machines/shared-transition-configs"
import { buildActions } from "../state-machines/actions"
import { waitingMachine } from "../state-machines/waiting"
import { developMachine } from "../state-machines/develop"

const tracer = globalTracer()

Expand Down Expand Up @@ -81,7 +55,6 @@ process.on(`message`, msg => {

module.exports = async (program: IProgram): Promise<void> => {
reporter.setVerbose(program.verbose)
const bootstrapSpan = tracer.startSpan(`bootstrap`)

// We want to prompt the feedback request when users quit develop
// assuming they pass the heuristic check to know they are a user
Expand Down Expand Up @@ -125,207 +98,14 @@ module.exports = async (program: IProgram): Promise<void> => {
}

const app = express()
const parentSpan = tracer.startSpan(`bootstrap`)

const developConfig: MachineConfig<IBuildContext, any, AnyEventObject> = {
id: `build`,
initial: `initializing`,
states: {
initializing: {
on: {
// Ignore mutation events because we'll be running everything anyway
ADD_NODE_MUTATION: undefined,
QUERY_FILE_CHANGED: undefined,
WEBHOOK_RECEIVED: undefined,
},
invoke: {
src: `initialize`,
onDone: {
target: `initializingDataLayer`,
actions: [`assignStoreAndWorkerPool`, `spawnMutationListener`],
},
},
},
initializingDataLayer: {
on: {
ADD_NODE_MUTATION: runMutationAndMarkDirty,
// Ignore, because we're about to extract them anyway
QUERY_FILE_CHANGED: undefined,
},
invoke: {
src: `initializeDataLayer`,
data: ({
parentSpan,
store,
firstRun,
webhookBody,
}: IBuildContext): IDataLayerContext => {
return {
parentSpan,
store,
firstRun,
deferNodeMutation: true,
webhookBody,
}
},
onDone: {
actions: [`assignServiceResult`, `clearWebhookBody`],
target: `finishingBootstrap`,
},
},
},
finishingBootstrap: {
on: {
ADD_NODE_MUTATION: runMutationAndMarkDirty,
// Ignore, because we're about to extract them anyway
QUERY_FILE_CHANGED: undefined,
},
invoke: {
src: async (): Promise<void> => {
// These were previously in `bootstrap()` but are now
// in part of the state machine that hasn't been added yet
await rebuildSchemaWithSitePage({ parentSpan: bootstrapSpan })

await writeOutRedirects({ parentSpan: bootstrapSpan })

startRedirectListener()
bootstrapSpan.finish()
},
onDone: {
target: `runningQueries`,
},
},
},
runningQueries: {
on: {
QUERY_FILE_CHANGED: {
actions: forwardTo(`run-queries`),
},
},
invoke: {
id: `run-queries`,
src: `runQueries`,
data: ({
program,
store,
parentSpan,
gatsbyNodeGraphQLFunction,
graphqlRunner,
websocketManager,
firstRun,
}: IBuildContext): IQueryRunningContext => {
return {
firstRun,
program,
store,
parentSpan,
gatsbyNodeGraphQLFunction,
graphqlRunner,
websocketManager,
}
},
onDone: {
target: `doingEverythingElse`,
},
},
},
doingEverythingElse: {
invoke: {
src: async (): Promise<void> => {
// All the stuff that's not in the state machine yet

boundActionCreators.setProgramStatus(
ProgramStatus.BOOTSTRAP_QUERY_RUNNING_FINISHED
)

await db.saveState()

db.startAutosave()
},
onDone: [
{
target: `startingDevServers`,
cond: ({ compiler }: IBuildContext): boolean => !compiler,
},
{
target: `waiting`,
},
],
},
},
startingDevServers: {
invoke: {
src: `startWebpackServer`,
onDone: {
target: `waiting`,
actions: `assignServers`,
},
},
},
waiting: {
on: {
ADD_NODE_MUTATION: {
actions: forwardTo(`waiting`),
},
QUERY_FILE_CHANGED: {
actions: forwardTo(`waiting`),
},
EXTRACT_QUERIES_NOW: {
target: `runningQueries`,
},
},
invoke: {
id: `waiting`,
src: `waitForMutations`,
data: ({
store,
nodeMutationBatch = [],
}: IBuildContext): IWaitingContext => {
return { store, nodeMutationBatch, runningBatch: [] }
},
onDone: {
actions: `assignServiceResult`,
target: `rebuildingPages`,
},
},
},
rebuildingPages: {
invoke: {
src: `initializeDataLayer`,
data: ({ parentSpan, store }: IBuildContext): IDataLayerContext => {
return { parentSpan, store, firstRun: false, skipSourcing: true }
},
onDone: {
actions: `assignServiceResult`,
target: `runningQueries`,
},
},
},
},
// Transitions shared by all states, except where overridden
on: {
ADD_NODE_MUTATION: {
actions: `addNodeMutation`,
},
QUERY_FILE_CHANGED: {
actions: `markQueryFilesDirty`,
},
WEBHOOK_RECEIVED: {
target: `initializingDataLayer`,
actions: `assignWebhookBody`,
},
},
}

const machine = Machine(developConfig, {
services: {
initializeDataLayer: dataLayerMachine,
initialize,
runQueries: queryRunningMachine,
waitForMutations: waitingMachine,
startWebpackServer: startWebpackServer,
},
actions: buildActions,
}).withContext({ program, parentSpan: bootstrapSpan, app, firstRun: true })
const machine = developMachine.withContext({
program,
parentSpan,
app,
firstRun: true,
})

const service = interpret(machine)

Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby/src/services/write-out-redirects.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import reporter from "gatsby-cli/lib/reporter"
import { writeRedirects } from "../bootstrap/redirects-writer"
import {
writeRedirects,
startRedirectListener,
} from "../bootstrap/redirects-writer"
import { IQueryRunningContext } from "../state-machines/query-running/types"

export async function writeOutRedirects({
Expand All @@ -11,5 +14,6 @@ export async function writeOutRedirects({
})
activity.start()
await writeRedirects()
startRedirectListener()
activity.end()
}
2 changes: 1 addition & 1 deletion packages/gatsby/src/state-machines/data-layer/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { assign, DoneInvokeEvent, ActionFunctionMap } from "xstate"
import { createGraphQLRunner } from "../../bootstrap/create-graphql-runner"
import reporter from "gatsby-cli/lib/reporter"
import { IDataLayerContext } from "./types"
import { callApi, markNodesDirty } from "../actions"
import { callApi, markNodesDirty } from "../develop/actions"
import { assertStore } from "../../utils/assert-store"
import { GraphQLRunner } from "../../query/graphql-runner"

Expand Down
35 changes: 31 additions & 4 deletions packages/gatsby/src/state-machines/data-layer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ export type DataLayerResult = Pick<

const dataLayerStates: MachineConfig<IDataLayerContext, any, any> = {
initial: `start`,
id: `dataLayerMachine`,
context: {},
states: {
start: {
always: [
{
target: `buildingSchema`,
cond: ({ skipSourcing }: IDataLayerContext): boolean =>
!!skipSourcing,
cond: `shouldSkipSourcing`,
},
{
target: `customizingSchema`,
Expand Down Expand Up @@ -65,10 +65,10 @@ const dataLayerStates: MachineConfig<IDataLayerContext, any, any> = {
{
target: `creatingPagesStatefully`,
actions: `assignChangedPages`,
cond: (context): boolean => !!context.firstRun,
cond: `firstRun`,
},
{
target: `done`,
target: `rebuildingSchemaWithSitePage`,
actions: `assignChangedPages`,
},
],
Expand All @@ -78,6 +78,28 @@ const dataLayerStates: MachineConfig<IDataLayerContext, any, any> = {
invoke: {
src: `createPagesStatefully`,
id: `creating-pages-statefully`,
onDone: {
target: `rebuildingSchemaWithSitePage`,
},
},
},
rebuildingSchemaWithSitePage: {
invoke: {
src: `rebuildSchemaWithSitePage`,
onDone: [
{
target: `writingOutRedirects`,
cond: `firstRun`,
},
{
target: `done`,
},
],
},
},
writingOutRedirects: {
invoke: {
src: `writeOutRedirects`,
onDone: {
target: `done`,
},
Expand Down Expand Up @@ -105,4 +127,9 @@ const dataLayerStates: MachineConfig<IDataLayerContext, any, any> = {
export const dataLayerMachine = Machine(dataLayerStates, {
actions: dataLayerActions,
services: dataLayerServices,
guards: {
firstRun: ({ firstRun }: IDataLayerContext): boolean => !!firstRun,
shouldSkipSourcing: ({ skipSourcing }: IDataLayerContext): boolean =>
!!skipSourcing,
},
})
4 changes: 4 additions & 0 deletions packages/gatsby/src/state-machines/data-layer/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
createPagesStatefully,
buildSchema,
sourceNodes,
rebuildSchemaWithSitePage,
writeOutRedirects,
} from "../../services"
import { IDataLayerContext } from "./types"

Expand All @@ -17,4 +19,6 @@ export const dataLayerServices: Record<
createPages,
buildSchema,
createPagesStatefully,
rebuildSchemaWithSitePage,
writeOutRedirects,
}
Loading