Skip to content

Commit 80654fb

Browse files
authoredJul 1, 2020
refactor(gatsby): Move store assertion into helper (#25438)
1 parent 5c4c008 commit 80654fb

8 files changed

+25
-23
lines changed
 

‎packages/gatsby/src/services/build-schema.ts

-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@ import { build } from "../schema"
44
import reporter from "gatsby-cli/lib/reporter"
55

66
export async function buildSchema({
7-
store,
87
parentSpan,
98
}: Partial<IBuildContext>): Promise<void> {
10-
if (!store) {
11-
reporter.panic(`Cannot build schema before store initialization`)
12-
}
139
const activity = reporter.activityTimer(`building schema`, {
1410
parentSpan,
1511
})

‎packages/gatsby/src/services/calculate-dirty-queries.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { calcInitialDirtyQueryIds, groupQueryIds } from "../query"
22
import { IBuildContext, IGroupedQueryIds } from "./"
3-
import reporter from "gatsby-cli/lib/reporter"
3+
import { assertStore } from "../utils/assert-store"
44

55
export async function calculateDirtyQueries({
66
store,
77
}: Partial<IBuildContext>): Promise<{
88
queryIds: IGroupedQueryIds
99
}> {
10-
if (!store) {
11-
reporter.panic(`Cannot run service without a redux store`)
12-
}
10+
assertStore(store)
11+
1312
const state = store.getState()
1413
// TODO: Check filesDirty from context
1514

‎packages/gatsby/src/services/create-pages.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { IBuildContext } from "./"
22

33
import reporter from "gatsby-cli/lib/reporter"
44
import apiRunnerNode from "../utils/api-runner-node"
5+
import { assertStore } from "../utils/assert-store"
56

67
export async function createPages({
78
parentSpan,
89
gatsbyNodeGraphQLFunction,
910
store,
1011
}: Partial<IBuildContext>): Promise<void> {
11-
if (!store) {
12-
reporter.panic(`store not initialized`)
13-
}
12+
assertStore(store)
1413
const activity = reporter.activityTimer(`createPages`, {
1514
parentSpan,
1615
})

‎packages/gatsby/src/services/run-page-queries.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { processPageQueries } from "../query"
22
import { IBuildContext } from "./"
33
import reporter from "gatsby-cli/lib/reporter"
4+
import { assertStore } from "../utils/assert-store"
45

56
export async function runPageQueries({
67
parentSpan,
@@ -9,9 +10,8 @@ export async function runPageQueries({
910
program,
1011
graphqlRunner,
1112
}: Partial<IBuildContext>): Promise<void> {
12-
if (!store) {
13-
reporter.panic(`Cannot run service without a redux store`)
14-
}
13+
assertStore(store)
14+
1515
if (!queryIds) {
1616
return
1717
}

‎packages/gatsby/src/services/run-static-queries.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { processStaticQueries } from "../query"
22
import { IBuildContext } from "./"
33
import reporter from "gatsby-cli/lib/reporter"
4+
import { assertStore } from "../utils/assert-store"
45

56
export async function runStaticQueries({
67
parentSpan,
@@ -9,9 +10,8 @@ export async function runStaticQueries({
910
program,
1011
graphqlRunner,
1112
}: Partial<IBuildContext>): Promise<void> {
12-
if (!store) {
13-
reporter.panic(`Cannot run service without a redux store`)
14-
}
13+
assertStore(store)
14+
1515
if (!queryIds) {
1616
return
1717
}

‎packages/gatsby/src/services/source-nodes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IBuildContext } from "./"
22
import sourceNodesAndRemoveStaleNodes from "../utils/source-nodes"
33
import reporter from "gatsby-cli/lib/reporter"
4+
import { assertStore } from "../utils/assert-store"
45
// import { findChangedPages } from "../utils/check-for-changed-pages"
56
// import { IGatsbyPage } from "../redux/types"
67

@@ -9,9 +10,8 @@ export async function sourceNodes({
910
webhookBody,
1011
store,
1112
}: Partial<IBuildContext>): Promise<void> {
12-
if (!store) {
13-
reporter.panic(`No redux store`)
14-
}
13+
assertStore(store)
14+
1515
const activity = reporter.activityTimer(`source and transform nodes`, {
1616
parentSpan,
1717
})

‎packages/gatsby/src/services/write-out-requires.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { IBuildContext } from "./"
22
import reporter from "gatsby-cli/lib/reporter"
33
import { writeAll } from "../bootstrap/requires-writer"
4+
import { assertStore } from "../utils/assert-store"
45

56
export async function writeOutRequires({
67
store,
78
parentSpan,
89
}: Partial<IBuildContext>): Promise<void> {
9-
if (!store) {
10-
reporter.panic(`No redux store`)
11-
}
10+
assertStore(store)
11+
1212
// Write out files.
1313
const activity = reporter.activityTimer(`write out requires`, {
1414
parentSpan,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Store } from "redux"
2+
import reporter from "gatsby-cli/lib/reporter"
3+
4+
export function assertStore(store?: Store): asserts store {
5+
if (!store) {
6+
reporter.panic(`Could not find Redux store`)
7+
}
8+
}

0 commit comments

Comments
 (0)
Please sign in to comment.