Skip to content

Commit

Permalink
chore(gatsby): Migrate src/redux/plugin-runner to TypeScript (#22476)
Browse files Browse the repository at this point in the history
* chore(gatsby): Migrate src/redux/plugin-runner to TypeScript

* Improve typing and reduce module side effects

* Add return type

Co-authored-by: Blaine Kasten <[email protected]>
  • Loading branch information
kawamataryo and blainekasten committed Mar 30, 2020
1 parent babc491 commit d8b667a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
3 changes: 2 additions & 1 deletion packages/gatsby/src/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const telemetry = require(`gatsby-telemetry`)
const apiRunnerNode = require(`../utils/api-runner-node`)
import { getBrowsersList } from "../utils/browserslist"
import { createSchemaCustomization } from "../utils/create-schema-customization"
import { startPluginRunner } from "../redux/plugin-runner"
const { store, emitter } = require(`../redux`)
const loadPlugins = require(`./load-plugins`)
const loadThemes = require(`./load-themes`)
Expand Down Expand Up @@ -66,7 +67,7 @@ module.exports = async (args: BootstrapArgs) => {

// Start plugin runner which listens to the store
// and invokes Gatsby API based on actions.
require(`../redux/plugin-runner`)
startPluginRunner()

const directory = slash(args.directory)

Expand Down
13 changes: 0 additions & 13 deletions packages/gatsby/src/redux/plugin-runner.js

This file was deleted.

51 changes: 51 additions & 0 deletions packages/gatsby/src/redux/plugin-runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Span } from "opentracing"
import { emitter } from "./index"
import apiRunnerNode from "../utils/api-runner-node"
import { ActivityTracker } from "../../"

type Plugin = any // TODO

// This might make sense to live somewhere else
interface ICreatePageAction {
graphql<TData, TVariables = any>(
query: string,
variables?: TVariables
): Promise<{
errors?: any
data?: TData
}>
traceId: "initial-createPages"
waitForCascadingActions: boolean
parentSpan: Span
activity: ActivityTracker
type: `CREATE_PAGE`
contextModified: boolean
plugin: Plugin
payload: {
internalComponentName: string
path: string
matchPath: string | undefined
component: string
componentChunkName: string
isCreatedByStatefulCreatePages: boolean
context: {
slug: string
id: string
}
updatedAt: number
pluginCreator__NODE: string
pluginCreatorId: string
componentPath: string
}
}

export const startPluginRunner = (): void => {
emitter.on(`CREATE_PAGE`, (action: ICreatePageAction) => {
const page = action.payload
apiRunnerNode(
`onCreatePage`,
{ page, traceId: action.traceId, parentSpan: action.parentSpan },
{ pluginSource: action.plugin.name, activity: action.activity }
)
})
}

0 comments on commit d8b667a

Please sign in to comment.