Skip to content
Permalink
Browse files

chore(gatsby): Translate redux/nodes to TypeScript (#21010)

* type redux/nodes and more of the state

* remove unused import

* Simplify code to allow for a stable type

* handle lint

* fix comments

* fix types

* cleanup

* Clean up

* fix optional types

* dont use lodash

* fix removed lodash

* fix import statement
  • Loading branch information
blainekasten committed Mar 23, 2020
1 parent 8c2528d commit ccf3ec2f04c9508560e9c49834a8071bdc144416
@@ -85,6 +85,11 @@ module.exports = {
plugins: ["@typescript-eslint/eslint-plugin"],
rules: {
...TSEslint.configs.recommended.rules,
// This rule tries to ensure we use camelCase for all variables, properties
// functions, etc. However, it is not always possible to ensure properties
// are camelCase. Specifically we have `node.__gatsby_resolve` which breaks
// this rule. This allows properties to be whatever they need to be.
"@typescript-eslint/camelcase": ["error", { properties: "never" }],
// This rule tries to prevent using `require()`. However in node code,
// there are times where this makes sense. And it specifically is causing
// problems in our tests where we often want this functionality for module
@@ -6,10 +6,10 @@ import {
getPageHtmlFilePath,
} from "../utils/page-html"
import { remove as removePageDataFile, fixedPagePath } from "../utils/page-data"
import { IReduxState } from "../redux/types"
import { IGatsbyState } from "../redux/types"

export const getChangedPageDataKeys = (
state: IReduxState,
state: IGatsbyState,
cachedPageData: Map<string, string>
): string[] => {
if (cachedPageData && state.pageData) {
@@ -31,7 +31,7 @@ export const getChangedPageDataKeys = (
}

export const collectRemovedPageData = (
state: IReduxState,
state: IGatsbyState,
cachedPageData: Map<string, string>
): string[] => {
if (cachedPageData && state.pageData) {
@@ -9,13 +9,13 @@ import {
ExecutionResult,
} from "graphql"
import { debounce } from "lodash"
import nodeStore from "../db/nodes"
import * as nodeStore from "../db/nodes"
import { createPageDependency } from "../redux/actions/add-page-dependency"

import withResolverContext from "../schema/context"
import { LocalNodeModel } from "../schema/node-model"
import { Store } from "redux"
import { IReduxState } from "../redux/types"
import { IGatsbyState } from "../redux/types"

type Query = string | Source

@@ -30,7 +30,7 @@ class GraphQLRunner {
validDocuments: WeakSet<DocumentNode>
scheduleClearCache: () => void

constructor(protected store: Store<IReduxState>) {
constructor(protected store: Store<IGatsbyState>) {
const { schema, schemaCustomization } = this.store.getState()

this.nodeModel = new LocalNodeModel({
@@ -9,7 +9,7 @@ export const createPageDependency = ({
}: {
path: string
nodeId: string
connection: string
connection?: string
}): void => {
const { componentDataDependencies } = store.getState()

@@ -23,7 +23,7 @@ export const createPageDependency = ({
if (
nodeId &&
componentDataDependencies.nodes.has(nodeId) &&
componentDataDependencies.nodes.get(nodeId).has(path)
componentDataDependencies.nodes.get(nodeId)!.has(path)
) {
nodeDependencyExists = true
}
@@ -33,7 +33,7 @@ export const createPageDependency = ({
if (
connection &&
componentDataDependencies.connections.has(connection) &&
componentDataDependencies.connections.get(connection).has(path)
componentDataDependencies.connections.get(connection)!.has(path)
) {
connectionDependencyExists = true
}
@@ -16,18 +16,14 @@ import {
/**
* Create a dependency between a page and data. Probably for
* internal use only.
* @param {Object} $0
* @param {string} $0.path the path to the page
* @param {string} $0.nodeId A node ID
* @param {string} $0.connection A connection type
* @private
*/
export const createPageDependency = (
{
path,
nodeId,
connection,
}: { path: string; nodeId: string; connection: string },
}: { path: string; nodeId?: string; connection?: string },
plugin = ``
): ICreatePageDependencyAction => {
return {
@@ -44,7 +40,6 @@ export const createPageDependency = (
/**
* Delete dependencies between an array of pages and data. Probably for
* internal use only. Used when deleting pages.
* @param {Array} paths the paths to delete.
* @private
*/
export const deleteComponentsDependencies = (
@@ -99,11 +94,6 @@ export const replaceStaticQuery = (
*
* Report that a query has been extracted from a component. Used by
* query-compiler.js.
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @param {query} $0.query The GraphQL query that was extracted from the component.
* @private
*/
export const queryExtracted = (
@@ -122,11 +112,6 @@ export const queryExtracted = (
/**
*
* Report that the Relay Compiler found a graphql error when attempting to extract a query
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @param {error} $0.error The GraphQL query that was extracted from the component.
* @private
*/
export const queryExtractionGraphQLError = (
@@ -146,10 +131,6 @@ export const queryExtractionGraphQLError = (
*
* Report that babel was able to extract the graphql query.
* Indicates that the file is free of JS errors.
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @private
*/
export const queryExtractedBabelSuccess = (
@@ -168,11 +149,6 @@ export const queryExtractedBabelSuccess = (
/**
*
* Report that the Relay Compiler found a babel error when attempting to extract a query
*
* @param {Object} $0
* @param {componentPath} $0.componentPath The path to the component that just had
* its query read.
* @param {error} $0.error The Babel error object
* @private
*/
export const queryExtractionBabelError = (
@@ -190,8 +166,6 @@ export const queryExtractionBabelError = (

/**
* Set overall program status e.g. `BOOTSTRAPING` or `BOOTSTRAP_FINISHED`.
*
* @param {string} Program status
* @private
*/
export const setProgramStatus = (
@@ -209,8 +183,6 @@ export const setProgramStatus = (

/**
* Broadcast that a page's query was run.
*
* @param {string} Path to the page component that changed.
* @private
*/
export const pageQueryRun = (
@@ -228,8 +200,6 @@ export const pageQueryRun = (

/**
* Remove jobs which are marked as stale (inputPath doesn't exists)
*
* @param {string} contentDigest
* @private
*/
export const removeStaleJob = (
@@ -11,24 +11,24 @@ import mitt from "mitt"
import thunk from "redux-thunk"
import reducers from "./reducers"
import { writeToCache, readFromCache } from "./persist"
import { IReduxState, ActionsUnion } from "./types"
import { IGatsbyState, ActionsUnion } from "./types"

// Create event emitter for actions
export const emitter = mitt()

// Read old node data from cache.
export const readState = (): IReduxState => {
export const readState = (): IGatsbyState => {
try {
const state = readFromCache() as IReduxState
const state = readFromCache() as IGatsbyState
if (state.nodes) {
// re-create nodesByType
state.nodesByType = new Map()
state.nodes.forEach(node => {
const { type } = node.internal
if (!state.nodesByType!.has(type)) {
state.nodesByType!.set(type, new Map())
if (!state.nodesByType.has(type)) {
state.nodesByType.set(type, new Map())
}
state.nodesByType!.get(type).set(node.id, node)
state.nodesByType.get(type)!.set(node.id, node)
})
}

@@ -42,7 +42,7 @@ export const readState = (): IReduxState => {
}
// BUG: Would this not cause downstream bugs? seems likely. Why wouldn't we just
// throw and kill the program?
return {} as IReduxState
return {} as IGatsbyState
}

/**
@@ -53,9 +53,11 @@ const multi: Middleware = ({ dispatch }) => next => (
): ActionsUnion | ActionsUnion[] =>
Array.isArray(action) ? action.filter(Boolean).map(dispatch) : next(action)

export const configureStore = (initialState: IReduxState): Store<IReduxState> =>
export const configureStore = (
initialState: IGatsbyState
): Store<IGatsbyState> =>
createStore(
combineReducers<IReduxState>({ ...reducers }),
combineReducers<IGatsbyState>({ ...reducers }),
initialState,
applyMiddleware(thunk, multi)
)

0 comments on commit ccf3ec2

Please sign in to comment.
You can’t perform that action at this time.