Skip to content

Commit e8d3ac7

Browse files
committed
refactor(chore): use di-wise
1 parent bc6018c commit e8d3ac7

File tree

7 files changed

+39
-33
lines changed

7 files changed

+39
-33
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@replit/codemirror-vim": "6.2.1",
2828
"@unocss/reset": "0.63.1",
2929
"ackee-tracker": "patch:ackee-tracker@npm%3A5.1.0#~/.yarn/patches/ackee-tracker-npm-5.1.0-0db5cc0193.patch",
30+
"di-wise": "0.2.7",
3031
"immer": "npm:[email protected]",
3132
"js-base64": "3.7.7",
3233
"mutative": "1.0.11",

src/core/clock/clock.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { Bus } from '../bus/bus'
1+
import { inject } from 'di-wise'
2+
3+
import { Bus } from '../bus/bus'
24

35
export class Clock {
4-
constructor(
5-
private readonly bus: Bus,
6-
) {}
6+
private bus = inject(Bus)
77

88
tick = (): void => {
99
this.bus.setControl({ CLK: 0b1 })

src/core/controller/controller.spec.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1+
import { createContainer, Scope } from 'di-wise'
12
import { firstValueFrom } from 'rxjs'
23
import { describe, expect, it } from 'vitest'
34

4-
import { Bus } from '../bus/bus'
5-
import { Clock } from '../clock/clock'
6-
import { Cpu } from '../cpu/cpu'
75
import { Memory } from '../memory/memory'
86
import { Controller } from './controller'
97

108
describe('Controller', () => {
119
it('should step', async () => {
12-
const bus = new Bus()
13-
const memory = new Memory(bus)
10+
const container = createContainer({
11+
defaultScope: Scope.Container,
12+
autoRegister: true,
13+
})
14+
15+
const memory = container.resolve(Memory)
1416
memory.load(new Uint8Array([0x01, 0x02]), 0x00)
15-
const controller = new Controller(
16-
bus,
17-
new Cpu(bus),
18-
new Clock(bus),
19-
memory,
20-
)
17+
18+
const controller = container.resolve(Controller)
2119
await firstValueFrom(controller.step())
2220
expect(memory.getData()[0x02]).toBe(0x03)
2321
})

src/core/controller/controller.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1+
import { inject } from 'di-wise'
12
import { Observable, type Subscription } from 'rxjs'
23

3-
import type { Bus } from '../bus/bus'
4-
import type { Clock } from '../clock/clock'
5-
import type { Cpu } from '../cpu/cpu'
6-
import type { Memory } from '../memory/memory'
4+
import { Bus } from '../bus/bus'
5+
import { Clock } from '../clock/clock'
6+
import { Cpu } from '../cpu/cpu'
7+
import { Memory } from '../memory/memory'
78

89
export class Controller {
9-
constructor(
10-
private readonly bus: Bus,
11-
private readonly cpu: Cpu,
12-
private readonly clock: Clock,
13-
private readonly memory: Memory,
14-
) {}
10+
private bus = inject(Bus)
11+
private cpu = inject(Cpu)
12+
private clock = inject(Clock)
13+
private memory = inject(Memory)
1514

1615
step = (): Observable<void> => {
1716
return new Observable<void>((subscriber) => {

src/core/cpu/cpu.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import { inject } from 'di-wise'
12
import { type Observable, take } from 'rxjs'
23

3-
import type { Bus, ControlLines } from '../bus/bus'
4+
import { Bus, type ControlLines } from '../bus/bus'
45

56
type AsyncControlGenerator<T> = Generator<Observable<ControlLines>, T, ControlLines>
67

78
export class Cpu {
8-
constructor(
9-
private readonly bus: Bus,
10-
) {}
9+
private bus = inject(Bus);
1110

1211
*step(): AsyncControlGenerator<void> {
1312
const x = yield* this.readMemory(0x00)

src/core/memory/memory.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { inject } from 'di-wise'
12
import { filter, map, type Observable, share } from 'rxjs'
23

3-
import type { Bus } from '../bus/bus'
4+
import { Bus } from '../bus/bus'
45

56
export enum MemoryOperationType {
67
READ = 'READ',
@@ -20,9 +21,9 @@ export class Memory {
2021
readonly read$: Observable<MemoryOperation>
2122
readonly write$: Observable<MemoryOperation>
2223

23-
constructor(
24-
private readonly bus: Bus,
25-
) {
24+
private bus = inject(Bus)
25+
26+
constructor() {
2627
const control$ = this.bus.control$.pipe(
2728
filter((control) => control.MREQ),
2829
share(),

yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,7 @@ __metadata:
37003700
"@vitest/coverage-v8": "npm:2.1.1"
37013701
"@vitest/ui": "npm:2.1.1"
37023702
ackee-tracker: "patch:ackee-tracker@npm%3A5.1.0#~/.yarn/patches/ackee-tracker-npm-5.1.0-0db5cc0193.patch"
3703+
di-wise: "npm:0.2.7"
37033704
eslint: "npm:9.11.1"
37043705
eslint-plugin-react: "npm:7.37.0"
37053706
eslint-plugin-react-hooks: "npm:5.1.0-rc-3edc000d-20240926"
@@ -4412,6 +4413,13 @@ __metadata:
44124413
languageName: node
44134414
linkType: hard
44144415

4416+
"di-wise@npm:0.2.7":
4417+
version: 0.2.7
4418+
resolution: "di-wise@npm:0.2.7"
4419+
checksum: 10c0/01cd9aa9c7600c57bbac388004c65b183ce395b209fdd212b5870c36e8b322bcc148d7ca03ebc889a515223efca9de2e895e1fdf9c6edda7f2b9de436cd9c69d
4420+
languageName: node
4421+
linkType: hard
4422+
44154423
"diff-sequences@npm:^29.6.3":
44164424
version: 29.6.3
44174425
resolution: "diff-sequences@npm:29.6.3"

0 commit comments

Comments
 (0)