Skip to content

Commit

Permalink
Merge pull request #82 from AllenShintani/master
Browse files Browse the repository at this point in the history
formatting dependencies and console
  • Loading branch information
AllenShintani authored Mar 21, 2024
2 parents 5da0a93 + eef9fe7 commit 755bad3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ version](https://img.shields.io/npm/v/edison.svg?style=flat)](https://www.npmjs.
[![NPM
downloads](https://img.shields.io/npm/dm/edison.svg?style=flat)](https://www.npmjs.com/package/edison)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/edison-js/Edison/blob/main/LICENSE)

</div>

## Documentation
Expand Down Expand Up @@ -55,8 +56,11 @@ import React from "react"

const App: React.FC = () => {
return (
<Board>
<Led pin={13} blink={500} />
<Board port={'/dev/ttyUSB0'}> // Please replace with your port
<Led
pin={13}
blink={500}
/>
</Board>
)
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"rxjs": "^7.8.1",
"serialport": "^12.0.0",
"stack-utils": "^2.0.6",
"typescript": "^5.4.2",
"yoga": "^0.0.20",
"yoga-wasm-web": "^0.3.3"
},
Expand All @@ -35,6 +34,7 @@
"eslint-plugin-react-hooks": "^4.6.0",
"is-in-ci": "^0.1.0",
"prettier": "^3.0.3",
"typescript": "^5.4.2",
"vite-node": "^1.2.2",
"vite-plugin-checker": "^0.6.2",
"vitest": "^1.4.0",
Expand Down
Binary file added public/images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions src/__tests__/procedure/utils/board.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ vi.mock('serialport', () => ({
listenerCount?: unknown
}
eventEmitter.listenerCount = (eventName: string) => {
// listenerCountの挙動を模擬
return eventEmitter.listeners(eventName).length
}
mockSerialPortInstance = eventEmitter // 生成されたインスタンスを保持
mockSerialPortInstance = eventEmitter
return {
path,
baudRate,
Expand All @@ -30,23 +29,21 @@ vi.mock('serialport', () => ({
describe('board', () => {
beforeEach(() => {
vi.clearAllMocks()
// currentPortのリセット処理はここで直接行うことはできない
})

it('connectManual should set isReady to true when the board is successfully connected', () => {
expect(board.isReady()).toBe(false)
board.connectManual('/dev/ttyUSB0')
// `boardEmitter`が`ready`イベントをemitするのを模擬
setTimeout(() => {
mockSerialPortInstance.emit('data', 'test-data') // onDataを発火
mockSerialPortInstance.emit('data', 'test-data')
expect(board.isReady()).toBe(true)
}, 1) // 非同期処理の完了を待つ
}, 1)
})

it('should handle "close" event correctly', () => {
board.connectManual('/dev/ttyUSB0')
setTimeout(() => {
mockSerialPortInstance.emit('close') // onCloseを発火
mockSerialPortInstance.emit('close')
setTimeout(() => {
expect(board.isReady()).toBe(false)
}, 1)
Expand Down
22 changes: 15 additions & 7 deletions src/procedure/utils/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,19 @@ let isPortActive = false
const MAX_RECENT_LISTENERS = 80

const connectManual = (arduinoPath: string) => {
const spinner = ora('Now Connecting to Device...').start()

if (currentPort) {
console.error('Port is already used.')
return
spinner.fail('Port is already used.')
process.exit(1)

Check failure on line 16 in src/procedure/utils/board.ts

View workflow job for this annotation

GitHub Actions / Test

src/__tests__/procedure/utils/board.test.ts > board > should handle "close" event correctly

Error: process.exit unexpectedly called with "1" ❯ Object.connectManual src/procedure/utils/board.ts:16:13 ❯ src/__tests__/procedure/utils/board.test.ts:44:11
}
const timeoutId = setTimeout(() => {
if (!isPortActive) {
spinner.fail('Failed to connect to device within 10 seconds.')
process.exit(1)
}
}, 10000)

const spinner = ora('Now Connecting to Device...').start()
try {
const port = new SerialPort({ path: arduinoPath, baudRate: 57600 })
currentPort = port
Expand All @@ -24,7 +31,6 @@ const connectManual = (arduinoPath: string) => {
const allListeners = port.listeners('data') as ((...args: []) => void)[]
const oldListeners = allListeners.slice(0, -MAX_RECENT_LISTENERS)

// biome-ignore lint/complexity/noForEach: <explanation>
oldListeners.forEach((listener) => {
if (listener !== onData) {
port.removeListener('data', listener)
Expand All @@ -33,23 +39,25 @@ const connectManual = (arduinoPath: string) => {
}

if (!isPortActive) {
clearTimeout(timeoutId)
spinner.succeed('Device is connected successfully!')
boardEmitter.emit('ready', port)
isPortActive = true
}
}

port.on('data', onData)

port.on('close', () => {
console.log('Board is closed.')
spinner.fail('Board is closed.')
currentPort = null
port.removeAllListeners()
isPortActive = false
})
} catch (error) {
console.error('Failed to open port:', error)
clearTimeout(timeoutId)
spinner.fail('Failed to open port: ' + error)
currentPort = null
process.exit(1)
}
}

Expand Down

0 comments on commit 755bad3

Please sign in to comment.