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 page-component to typescript #23277

Merged
merged 22 commits into from
May 8, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { interpret } = require(`xstate`)
const machine = require(`../page-component`)
import { interpret, Interpreter } from "xstate"

import { componentMachine, IContext, IState, IEvent } from "../page-component"

jest.mock(`../../../query`)
const { enqueueExtractedQueryId, runQueuedQueries } = require(`../../../query`)

const getService = (args = {}) =>
const getService = (args = {}): Interpreter<IContext, IState, IEvent> =>
interpret(
machine.withContext({
componentMachine.withContext({
componentPath: `/a/path.js`,
query: ``,
pages: new Set([`/`]),
Expand All @@ -15,7 +16,8 @@ const getService = (args = {}) =>
})
).start()

const sleep = (delay = 50) => new Promise(resolve => setTimeout(resolve, delay))
const sleep = (delay = 50): Promise<void> =>
new Promise(resolve => setTimeout(resolve, delay))

describe(`bootstrap`, () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
const {
Machine,
actions: { assign },
} = require(`xstate`)
import { Machine as machine, assign } from "xstate"

module.exports = Machine(
export interface IContext {
isInBootstrap: boolean
componentPath: string
query: string
pages: Set<string>
}

export interface IState {
states: {
inactive: {}
inactiveWhileBootstrapping: {}
queryExtractionGraphQLError: {}
queryExtractionBabelError: {}
runningPageQueries: {}
idle: {}
}
}

type ActionTypes =
| "BOOTSTRAP_FINISHED"
| "DELETE_PAGE"
| "NEW_PAGE_CREATED"
| "PAGE_CONTEXT_MODIFIED"
| "QUERY_EXTRACTION_GRAPHQL_ERROR"
| "QUERY_EXTRACTION_BABEL_ERROR"
| "QUERY_EXTRACTION_BABEL_SUCCESS"
| "QUERY_CHANGED"
| "QUERY_DID_NOT_CHANGE"
| "QUERIES_COMPLETE"

export interface IEvent {
type: ActionTypes
path?: string
query?: string
page?: { path: string }
}

const defaultContext: IContext = {
isInBootstrap: true,
componentPath: ``,
query: ``,
pages: new Set(``),
}

export const componentMachine = machine<IContext, IState, IEvent>(
{
id: `pageComponents`,
initial: `inactive`,
context: {
isInBootstrap: true,
componentPath: ``,
query: ``,
},
context: defaultContext,
on: {
BOOTSTRAP_FINISHED: {
actions: `setBootstrapFinished`,
Expand Down Expand Up @@ -75,19 +112,19 @@ module.exports = Machine(
},
{
guards: {
isBootstrapping: context => context.isInBootstrap,
isNotBootstrapping: context => !context.isInBootstrap,
isBootstrapping: (context): boolean => context.isInBootstrap,
isNotBootstrapping: (context): boolean => !context.isInBootstrap,
},
actions: {
rerunPageQuery: (_ctx, event) => {
rerunPageQuery: (_ctx, event): void => {
const queryUtil = require(`../../query`)
// Wait a bit as calling this function immediately triggers
// an Action call which Redux squawks about.
setTimeout(() => {
queryUtil.enqueueExtractedQueryId(event.path)
}, 0)
},
runPageComponentQueries: (context, event) => {
runPageComponentQueries: (context): void => {
const queryUtil = require(`../../query`)
// Wait a bit as calling this function immediately triggers
// an Action call which Redux squawks about.
Expand All @@ -96,8 +133,8 @@ module.exports = Machine(
}, 0)
},
setQuery: assign({
query: (ctx, event) => {
if (typeof event.query !== `undefined` || event.query !== null) {
query: (ctx, event): string => {
if (typeof event.query !== `undefined` && event.query !== null) {
pieh marked this conversation as resolved.
Show resolved Hide resolved
return event.query
} else {
return ctx.query
Expand Down Expand Up @@ -125,11 +162,14 @@ module.exports = Machine(
}),
deletePage: assign({
pages: (ctx, event) => {
ctx.pages.delete(event.page.path)
if (event.page) {
ctx.pages.delete(event.page.path)
}
pieh marked this conversation as resolved.
Show resolved Hide resolved
return ctx.pages
},
}),
setBootstrapFinished: assign({
setBootstrapFinished: assign<IContext>({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
pieh marked this conversation as resolved.
Show resolved Hide resolved
isInBootstrap: false,
}),
},
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby/src/redux/reducers/components.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const normalize = require(`normalize-path`)
const { interpret } = require(`xstate`)

const componentMachine = require(`../machines/page-component`)
import { componentMachine } from "../machines/page-component"

const services = new Map()
let programStatus = `BOOTSTRAPPING`
Expand Down