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-cli): migrate redux folder to typescript #22292

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
138 changes: 138 additions & 0 deletions packages/gatsby-cli/src/reporter/redux/action-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { Actions, ActivityStatuses } from "../constants"
import { IActivity } from "./reducer"
import { Dispatch, AnyAction } from "redux"

type dispatchAction = (dispatch: Dispatch<AnyAction>) => void
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved

export type actionsToEmitType = Array<
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
dispatchAction | { type: Actions; payload: Partial<IActivity> }
>

export interface IActionTypes {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the need for this to be reusable (in the sense of being an export in a common file). Could you just transfer these types to exist on the functions themselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to export them types manually instead of an interface? I put them in a different file cause it grew quite a lot.

setStatus(
status: string,
force?: boolean | undefined
): (dispatch: Dispatch<AnyAction>) => void
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
createLog(arg0: {
text: string
level: string
duration: number
group?: string
code?: string
type?: string
filePath?: string
location?: string
docsUrl?: string
context?: string
statusText?: string
activity_uuid: string
activity_current: string
activity_total: number
activity_type: string
stack?: string
}): {
type: Actions
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
payload: {
level: string
text: string
statusText: string | undefined
duration: number
group: string | undefined
code: string | undefined
type: string | undefined
filePath: string | undefined
location: string | undefined
docsUrl: string | undefined
context: string | undefined
activity_current: string
activity_total: number
activity_type: string
activity_uuid: string
timestamp: string
stack: string | undefined
}
}

startActivity: ({
id,
text,
type,
status,
current,
total,
}: {
id: string
text: string
type: string
status?: ActivityStatuses | undefined
current: string
total: number
}) => actionsToEmitType

endActivity: ({
id,
status,
}: {
id: string
status: ActivityStatuses
}) => actionsToEmitType | null

createPendingActivity: ({
id,
status,
}: {
id: string
status?: ActivityStatuses | undefined
}) => actionsToEmitType

updateActivity: (
activity: Partial<IActivity>
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
) => {
type: Actions
payload: Partial<IActivity>
} | null

setActivityErrored: ({
id,
}: {
id: string
}) => {
type: Actions
payload: {
id: IActivity["id"]
}
} | null

setActivityStatusText: ({
id,
statusText,
}: {
id: string
statusText: string
}) => {
type: Actions
payload: Partial<IActivity>
} | null

setActivityTotal: ({
id,
total,
}: {
id: string
total: number
}) => {
type: Actions
payload: Partial<IActivity>
} | null

activityTick: ({
id,
increment,
}: {
id: string
increment?: number | undefined
}) => {
type: Actions
payload: Partial<IActivity>
} | null
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
// @flow
const uuidv4 = require(`uuid/v4`)
const convertHrtime = require(`convert-hrtime`)
const { trackCli } = require(`gatsby-telemetry`)
const { bindActionCreators } = require(`redux`)
const { dispatch, getStore } = require(`./index`)
const {
/* eslint-disable @typescript-eslint/camelcase */

import uuidv4 from "uuid/v4"
import convertHrtime from "convert-hrtime"
import { trackCli } from "gatsby-telemetry"
import { bindActionCreators } from "redux"

import { iface } from "./index"
import {
Actions,
ActivityLogLevels,
ActivityStatuses,
ActivityTypes,
} = require(`../constants`)
const signalExit = require(`signal-exit`)
} from "../constants"
import signalExit from "signal-exit"

import { IActivity } from "./reducer"
import { IActionTypes, actionsToEmitType } from "./action-types"

const getActivity = id => getStore().getState().logs.activities[id]
const { dispatch, getStore } = iface

const getActivity = (id: string): IActivity =>
getStore().getState().logs.activities[id]

/**
* @returns {Number} Milliseconds from activity start
*/
const getElapsedTimeMS = activity => {
const getElapsedTimeMS = (activity: IActivity): number => {
const elapsed = process.hrtime(activity.startTime)
return convertHrtime(elapsed).milliseconds
}
Expand All @@ -28,7 +36,7 @@ const ActivityStatusToLogLevel = {
[ActivityStatuses.Success]: ActivityLogLevels.Success,
}

const getGlobalStatus = (id, status) => {
const getGlobalStatus = (id: string, status: ActivityStatuses): string => {
const { logs } = getStore().getState()

const currentActivities = [id, ...Object.keys(logs.activities)]
Expand Down Expand Up @@ -57,32 +65,36 @@ const getGlobalStatus = (id, status) => {
}, ActivityStatuses.Success)
}

let cancelDelayedSetStatus = null
type voidFunc = () => void

let cancelDelayedSetStatus: voidFunc | null = null
let weShouldExit = false
signalExit(() => {
weShouldExit = true
})

/**
* Like setTimeout, but also handle signalExit
*/
const delayedCall = (fn, timeout) => {
const fnWrap = () => {
const delayedCall = (fn: voidFunc, timeout: number): voidFunc => {
const fnWrap = (): void => {
fn()
// eslint-disable-next-line @typescript-eslint/no-use-before-define
clear()
}

const timeoutID = setTimeout(fnWrap, timeout)
const cancelSignalExit = signalExit(fnWrap)

const clear = () => {
const clear = (): void => {
clearTimeout(timeoutID)
cancelSignalExit()
}

return clear
}

const actions = {
const actions: IActionTypes = {
createLog: ({
level,
text,
Expand Down Expand Up @@ -125,7 +137,7 @@ const actions = {
}
},
createPendingActivity: ({ id, status = ActivityStatuses.NotStarted }) => {
const actionsToEmit = []
const actionsToEmit: actionsToEmitType = []

const logsState = getStore().getState().logs

Expand All @@ -146,7 +158,7 @@ const actions = {

return actionsToEmit
},
setStatus: (status, force = false) => dispatch => {
setStatus: (status, force = false) => (dispatch): void => {
const currentStatus = getStore().getState().logs.status

if (cancelDelayedSetStatus) {
Expand Down Expand Up @@ -175,7 +187,7 @@ const actions = {
current,
total,
}) => {
const actionsToEmit = []
const actionsToEmit: actionsToEmitType = []

const logsState = getStore().getState().logs

Expand Down Expand Up @@ -209,7 +221,7 @@ const actions = {
if (!activity) {
return null
}
const actionsToEmit = []
const actionsToEmit: actionsToEmitType = []
if (activity.type === ActivityTypes.Pending) {
actionsToEmit.push({
type: Actions.CancelActivity,
Expand Down Expand Up @@ -278,7 +290,7 @@ const actions = {
return actionsToEmit
},

updateActivity: ({ id, ...rest }) => {
updateActivity: ({ id = ``, ...rest }) => {
const activity = getActivity(id)
if (!activity) {
return null
Expand All @@ -293,7 +305,7 @@ const actions = {
},
}
},
setActivityErrored: ({ id, ...rest }) => {
setActivityErrored: ({ id }) => {
const activity = getActivity(id)
if (!activity) {
return null
Expand Down Expand Up @@ -329,4 +341,4 @@ const actions = {
},
}

module.exports = bindActionCreators(actions, dispatch)
module.exports = bindActionCreators<any, any>(actions, dispatch)
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
const Redux = require(`redux`)
const reducer = require(`./reducer`)
import {
createStore,
combineReducers,
AnyAction,
Dispatch,
CombinedState,
Store,
} from "redux"
import { reducer, IState } from "./reducer"

const { ActivityTypes, Actions } = require(`../constants`)
import { ActivityTypes, Actions } from "../constants"

let store = Redux.createStore(
Redux.combineReducers({
let store = createStore(
combineReducers({
logs: reducer,
}),
{}
)

const storeSwapListeners = []
const onLogActionListeners = new Set()
type store = Store<CombinedState<{ logs: IState }>, AnyAction>

const isInternalAction = action => {
type storeListener = (store: store) => void

const storeSwapListeners: storeListener[] = []
const onLogActionListeners = new Set<Dispatch<AnyAction>>()

const isInternalAction = (action): boolean => {
if (
[
Actions.PendingActivity,
Expand All @@ -31,7 +42,15 @@ const isInternalAction = action => {
return false
}

const iface = {
interface IIface {
dispatch(dispatch: any)
getStore: () => store
onStoreSwap: (fn: storeListener) => void
onLogAction: (fn: Dispatch<AnyAction>) => () => void
setStore: (s: store) => void
}

export const iface: IIface = {
MichaelDeBoey marked this conversation as resolved.
Show resolved Hide resolved
getStore: () => store,
dispatch: action => {
if (!action) {
Expand Down Expand Up @@ -67,7 +86,7 @@ const iface = {
},
onLogAction: fn => {
onLogActionListeners.add(fn)
return () => {
return (): void => {
onLogActionListeners.delete(fn)
}
},
Expand All @@ -80,5 +99,3 @@ const iface = {
storeSwapListeners.forEach(fn => fn(store))
},
}

module.exports = iface
Loading