Skip to content

Commit

Permalink
Complete typings for redux store
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Lucas committed Oct 17, 2021
1 parent da9693a commit 6794e53
Show file tree
Hide file tree
Showing 24 changed files with 200 additions and 151 deletions.
3 changes: 2 additions & 1 deletion packages/git/src/Git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
GetSpawn,
StatusFile,
FileText,
Remote,
} from './types'

import { GitRefs } from './GitRefs'
Expand Down Expand Up @@ -146,7 +147,7 @@ export class Git {
return sha.trim()
}

getAllRemotes = async () => {
getAllRemotes = async (): Promise<Remote[]> => {
const spawn = await this._getSpawn()
if (!spawn) {
return []
Expand Down
4 changes: 4 additions & 0 deletions packages/git/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,7 @@ export interface SpawnOpts {
}
export type Spawn = (args: string[], opts?: SpawnOpts) => Promise<string>
export type GetSpawn = () => Promise<Spawn | null>

export interface Remote {
name: string
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
export function measure(name, callback) {
export function measure<T>(name: string, callback: () => T): T

export function measure<T>(name: string, callback: () => Promise<T>): Promise<T>

export function measure<T>(name: string, callback: () => Promise<T> | T): any {
const uuid = 'GITERM/' + name + '/' + performance.now()
const startMark = uuid + '/start'
const endMark = uuid + '/end'

performance.mark(startMark)
const value = callback()
if (value.then) {
if ('then' in value) {
return value.then((result) => {
performance.mark(endMark)
performance.measure(uuid, startMark, endMark)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { SHOW_REMOTE_BRANCHES, SHOW_BRANCH_TAGS, CWD_UPDATED } from './actions'

import { INITIAL_CWD } from 'app/lib/cwd'

const initialState = {
export interface ConfigReducer {
cwd: string
showRemoteBranches: boolean
showBranchTags: boolean
}

const initialState: ConfigReducer = {
cwd: INITIAL_CWD,

// Show data from remotes
Expand All @@ -12,7 +18,7 @@ const initialState = {
showBranchTags: true,
}

export const reducer = (state = initialState, action) => {
export const reducer = (state = initialState, action: any): ConfigReducer => {
switch (action.type) {
case CWD_UPDATED: {
const { cwd } = action
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GraphResponse } from '@giterm/gitgraph'
import { Holistic } from './types'

export const GRAPH_UPDATED = 'graph/updated'
export const graphUpdated = (holistics, nodes, links, rehydrationPackage) => ({
export const graphUpdated = (holistics: Holistic, graph: GraphResponse) => ({
type: GRAPH_UPDATED,
holistics,
nodes,
links,
rehydrationPackage,
graph,
})

export const GRAPH_UPDATE_SKIPPED = 'graph/update_skipped'
Expand Down
25 changes: 0 additions & 25 deletions packages/giterm/app/renderer/store/graph/reducer.js

This file was deleted.

39 changes: 39 additions & 0 deletions packages/giterm/app/renderer/store/graph/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GRAPH_UPDATED, graphUpdated } from './actions'

import { GraphResponse } from '@giterm/gitgraph'
import { Holistic } from './types'

export interface GraphReducer {
holistics: Holistic
nodes: GraphResponse['nodes']
links: GraphResponse['links']
rehydrationPackage?: GraphResponse['rehydrationPackage']
width: number
}

const initialState: GraphReducer = {
holistics: { digest: undefined },
nodes: [],
links: [],
rehydrationPackage: undefined,
width: 0
}

export function reducer(state = initialState, action: any): GraphReducer {
switch (action.type) {
case GRAPH_UPDATED: {
const { graph, holistics } = action as ReturnType<typeof graphUpdated>

return {
holistics: holistics,
nodes: graph.nodes,
links: graph.links,
rehydrationPackage: graph.rehydrationPackage,
width: graph.nodes.reduce((max, node) => Math.max(node.column + 1, max), 3),
}
}

default:
return state
}
}
53 changes: 0 additions & 53 deletions packages/giterm/app/renderer/store/graph/sagas.js

This file was deleted.

34 changes: 34 additions & 0 deletions packages/giterm/app/renderer/store/graph/sagas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { takeEvery, put, select } from 'redux-saga/effects'

import { graphUpdateSkipped, graphUpdated } from './actions'
import { commitsToGraph } from '@giterm/gitgraph'
import { COMMITS_UPDATED } from 'app/store/commits/actions'
import { sentrySafeWrapper } from 'app/store/helpers'
import { measure } from 'app/lib/profiling'
import { State } from 'app/store'

function* recalculateGraph(): any {
const { commits, digest } = yield select((state) => state.commits)

const currentGraph: State['graph'] = yield select((state) => state.graph)

const commitsUnchanged = digest === currentGraph.holistics.digest
if (commitsUnchanged || !commits || !commits.length) {
yield put(graphUpdateSkipped())
return
}

const graph = measure('calculate-graph', () =>
commitsToGraph(
commits,
),
)

yield put(graphUpdated({
digest
}, graph))
}

export function* watch() {
yield takeEvery([COMMITS_UPDATED], sentrySafeWrapper(recalculateGraph))
}
3 changes: 3 additions & 0 deletions packages/giterm/app/renderer/store/graph/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Holistic = {
digest?: string
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { call } from 'redux-saga/effects'
import * as Sentry from '@sentry/electron'

export function updateReducer(updateType, initialState) {
return function (state = initialState, action) {
export function updateReducer<T>(updateType: string, initialState: T) {
return function (state = initialState, action: any): T {
switch (action.type) {
case updateType: {
const { type, ...payload } = action
Expand All @@ -18,10 +18,13 @@ export function updateReducer(updateType, initialState) {
}
}

export function sentrySafeWrapper(effect, { restartOnError = false } = {}) {
return function* self(...args) {
export function sentrySafeWrapper(
effect: (...args: []) => any,
{ restartOnError = false } = {},
) {
return function* self(...args: any[]): ReturnType<typeof effect> {
try {
yield call(effect, ...args)
yield call(effect as any, ...args)
} catch (e) {
Sentry.captureException(e)
console.warn('Error caught', e)
Expand Down
6 changes: 0 additions & 6 deletions packages/giterm/app/renderer/store/remotes/actions.js

This file was deleted.

8 changes: 8 additions & 0 deletions packages/giterm/app/renderer/store/remotes/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Remote } from '@giterm/git'

export const REMOTES_UPDATED = 'remotes/updated'

export const remotesUpdated = (remotes: Remote[]) => ({
type: REMOTES_UPDATED,
remotes,
})
4 changes: 0 additions & 4 deletions packages/giterm/app/renderer/store/remotes/reducer.js

This file was deleted.

5 changes: 5 additions & 0 deletions packages/giterm/app/renderer/store/remotes/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Remote } from '@giterm/git'
import { updateReducer } from 'app/store/helpers'
import { REMOTES_UPDATED } from './actions'

export const reducer = updateReducer<Remote[]>(REMOTES_UPDATED, [])
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { takeLatest, select, call, put } from 'redux-saga/effects'
import { remotesUpdated } from './actions'
import { GIT_REFS_CHANGED } from 'app/store/emitters/actions'
import { CWD_UPDATED } from 'app/store/config/actions'
import { Git } from '@giterm/git'
import { Git, Remote } from '@giterm/git'
import { CORE_INIT } from 'app/store/core/actions'
import { sentrySafeWrapper } from 'app/store/helpers'
import { State } from 'app/store'

function* updateRemotes() {
const cwd = yield select((state) => state.config.cwd)
function* updateRemotes(): any {
const cwd: string = yield select((state: State) => state.config.cwd)
const git = new Git(cwd)

const remotes = yield call(() => git.getAllRemotes())
const remotes: Remote[] = yield call(git.getAllRemotes)

yield put(remotesUpdated(remotes))
}
Expand Down
9 changes: 0 additions & 9 deletions packages/giterm/app/renderer/store/status/actions.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/giterm/app/renderer/store/status/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { StatusFile } from '@giterm/git'

export const STATUS_UPDATED = 'status/updated'
export const statusUpdated = (
files: StatusFile[],
state: string,
headSHA: string,
) => ({
type: STATUS_UPDATED,
payload: {
files,
state,
headSHA,
},
})
20 changes: 0 additions & 20 deletions packages/giterm/app/renderer/store/status/reducer.js

This file was deleted.

17 changes: 17 additions & 0 deletions packages/giterm/app/renderer/store/status/reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { StatusFile } from '@giterm/git'
import { updateReducer } from 'app/store/helpers'
import { STATUS_UPDATED } from './actions'

export interface StatusReducer {
files: StatusFile[],
state: string,
headSHA: string,
}

const initialState = {
files: [],
state: '',
headSHA: '',
}

export const reducer = updateReducer<StatusReducer>(STATUS_UPDATED, initialState)
Loading

0 comments on commit 6794e53

Please sign in to comment.