Skip to content

Commit

Permalink
refactor(memory/memorySlice): extract selector
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Dec 15, 2023
1 parent da3c6bc commit 0338077
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
8 changes: 2 additions & 6 deletions src/features/memory/Memory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ import { classNames, decToHex, invariant, range } from '@/common/utils'
import { MAX_SP } from '@/features/cpu/core'
import { selectCpuPointerRegisters } from '@/features/cpu/cpuSlice'

import {
MemoryView,
selectMemoryDataRows,
selectMemorySourceRows,
selectMemoryView,
} from './memorySlice'
import { MemoryView, selectMemoryDataRows, selectMemoryView } from './memorySlice'
import { selectMemorySourceRows } from './selectors'

const Memory = (): JSX.Element => {
const [isOpen, toggleOpen] = useToggle(true)
Expand Down
11 changes: 1 addition & 10 deletions src/features/memory/memorySlice.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { createSelector, createSlice, type PayloadAction } from '@reduxjs/toolkit'

import { chunk } from '@/common/utils'
import { selectAddressToStatementMap } from '@/features/assembler/assemblerSlice'
import type { AddressToCodeMap } from '@/features/assembler/core'

import { getSourceFrom, initData, initDataFrom, type MemoryData } from './core'
import { initData, initDataFrom, type MemoryData } from './core'

export enum MemoryView {
Hexadecimal = 'Hexadecimal',
Expand Down Expand Up @@ -61,11 +60,3 @@ export const {
} = memorySlice.actions

export const { selectMemoryData, selectMemoryDataRows, selectMemoryView } = memorySlice.selectors

export const selectMemorySourceRows = createSelector(
selectAddressToStatementMap,
(addressToStatementMap) => () => {
const memorySource = getSourceFrom(addressToStatementMap)
return chunk(0x10, memorySource)
},
)
14 changes: 14 additions & 0 deletions src/features/memory/selectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createSelector } from '@reduxjs/toolkit'

import { chunk } from '@/common/utils'
import { selectAddressToStatementMap } from '@/features/assembler/assemblerSlice'

import { getSourceFrom } from './core'

export const selectMemorySourceRows = createSelector(
selectAddressToStatementMap,
(addressToStatementMap) => () => {
const memorySource = getSourceFrom(addressToStatementMap)
return chunk(0x10, memorySource)
},
)

0 comments on commit 0338077

Please sign in to comment.