Skip to content

Commit

Permalink
refactor(chore): use di-wise
Browse files Browse the repository at this point in the history
  • Loading branch information
exuanbo committed Nov 28, 2024
1 parent bc6018c commit e8d3ac7
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 33 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@replit/codemirror-vim": "6.2.1",
"@unocss/reset": "0.63.1",
"ackee-tracker": "patch:ackee-tracker@npm%3A5.1.0#~/.yarn/patches/ackee-tracker-npm-5.1.0-0db5cc0193.patch",
"di-wise": "0.2.7",
"immer": "npm:[email protected]",
"js-base64": "3.7.7",
"mutative": "1.0.11",
Expand Down
8 changes: 4 additions & 4 deletions src/core/clock/clock.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { Bus } from '../bus/bus'
import { inject } from 'di-wise'

import { Bus } from '../bus/bus'

export class Clock {
constructor(
private readonly bus: Bus,
) {}
private bus = inject(Bus)

tick = (): void => {
this.bus.setControl({ CLK: 0b1 })
Expand Down
20 changes: 9 additions & 11 deletions src/core/controller/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
import { createContainer, Scope } from 'di-wise'
import { firstValueFrom } from 'rxjs'
import { describe, expect, it } from 'vitest'

import { Bus } from '../bus/bus'
import { Clock } from '../clock/clock'
import { Cpu } from '../cpu/cpu'
import { Memory } from '../memory/memory'
import { Controller } from './controller'

describe('Controller', () => {
it('should step', async () => {
const bus = new Bus()
const memory = new Memory(bus)
const container = createContainer({
defaultScope: Scope.Container,
autoRegister: true,
})

const memory = container.resolve(Memory)
memory.load(new Uint8Array([0x01, 0x02]), 0x00)
const controller = new Controller(
bus,
new Cpu(bus),
new Clock(bus),
memory,
)

const controller = container.resolve(Controller)
await firstValueFrom(controller.step())
expect(memory.getData()[0x02]).toBe(0x03)
})
Expand Down
19 changes: 9 additions & 10 deletions src/core/controller/controller.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { inject } from 'di-wise'
import { Observable, type Subscription } from 'rxjs'

import type { Bus } from '../bus/bus'
import type { Clock } from '../clock/clock'
import type { Cpu } from '../cpu/cpu'
import type { Memory } from '../memory/memory'
import { Bus } from '../bus/bus'
import { Clock } from '../clock/clock'
import { Cpu } from '../cpu/cpu'
import { Memory } from '../memory/memory'

export class Controller {
constructor(
private readonly bus: Bus,
private readonly cpu: Cpu,
private readonly clock: Clock,
private readonly memory: Memory,
) {}
private bus = inject(Bus)
private cpu = inject(Cpu)
private clock = inject(Clock)
private memory = inject(Memory)

step = (): Observable<void> => {
return new Observable<void>((subscriber) => {
Expand Down
7 changes: 3 additions & 4 deletions src/core/cpu/cpu.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { inject } from 'di-wise'
import { type Observable, take } from 'rxjs'

import type { Bus, ControlLines } from '../bus/bus'
import { Bus, type ControlLines } from '../bus/bus'

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

export class Cpu {
constructor(
private readonly bus: Bus,
) {}
private bus = inject(Bus);

*step(): AsyncControlGenerator<void> {
const x = yield* this.readMemory(0x00)
Expand Down
9 changes: 5 additions & 4 deletions src/core/memory/memory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { inject } from 'di-wise'
import { filter, map, type Observable, share } from 'rxjs'

import type { Bus } from '../bus/bus'
import { Bus } from '../bus/bus'

export enum MemoryOperationType {
READ = 'READ',
Expand All @@ -20,9 +21,9 @@ export class Memory {
readonly read$: Observable<MemoryOperation>
readonly write$: Observable<MemoryOperation>

constructor(
private readonly bus: Bus,
) {
private bus = inject(Bus)

constructor() {
const control$ = this.bus.control$.pipe(
filter((control) => control.MREQ),
share(),
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3700,6 +3700,7 @@ __metadata:
"@vitest/coverage-v8": "npm:2.1.1"
"@vitest/ui": "npm:2.1.1"
ackee-tracker: "patch:ackee-tracker@npm%3A5.1.0#~/.yarn/patches/ackee-tracker-npm-5.1.0-0db5cc0193.patch"
di-wise: "npm:0.2.7"
eslint: "npm:9.11.1"
eslint-plugin-react: "npm:7.37.0"
eslint-plugin-react-hooks: "npm:5.1.0-rc-3edc000d-20240926"
Expand Down Expand Up @@ -4412,6 +4413,13 @@ __metadata:
languageName: node
linkType: hard

"di-wise@npm:0.2.7":
version: 0.2.7
resolution: "di-wise@npm:0.2.7"
checksum: 10c0/01cd9aa9c7600c57bbac388004c65b183ce395b209fdd212b5870c36e8b322bcc148d7ca03ebc889a515223efca9de2e895e1fdf9c6edda7f2b9de436cd9c69d
languageName: node
linkType: hard

"diff-sequences@npm:^29.6.3":
version: 29.6.3
resolution: "diff-sequences@npm:29.6.3"
Expand Down

0 comments on commit e8d3ac7

Please sign in to comment.