Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何中止程式? #3

Closed
mengcdemenka opened this issue Feb 28, 2024 · 2 comments
Closed

如何中止程式? #3

mengcdemenka opened this issue Feb 28, 2024 · 2 comments

Comments

@mengcdemenka
Copy link
Contributor

吾嘗運行一例之代碼,「閱止」并未結束程式,每當用戶輸入程式就會輸出,循環不止。

@GLanguage
Copy link
Owner

抱歉,本庫年代較為久遠,許多邏輯和原理我已不甚清晰,我需要一點時間才能定位問題之源頭。您也可以嘗試閱讀一下源代碼。

@GLanguage
Copy link
Owner

簡單來講,問題的核心是process.stdin.end()不再能關閉stdin(可能是node.js版本問題),改為process.stdin.pause()就能暫時解決問題。
然而這不是長久之計。這個庫的設計邏輯似乎有些問題。process.stdin.on()是異步的,這意味著程序會在回調函數處理輸入之前就先運行到下一行,這很符合node.js的異步風格,但並不是交互的主要使用場景。大多時候我們從命令行請求輸入是為了對輸入數據加以操作,如果輸入是異步的,那就無法同步地操作輸入數據。
由於長時間以來,文言的使用者對交互的需求不高,所以這一問題一直沒能被發現。您的issue讓我認識到了這點。我現在在考慮用readline包重寫這個庫,抑或是手動地添加更合理的同步邏輯。屆時我將同時更新範例,加入一個類似猜數遊戲的交互實例。
如果能得到您的幫助,我將不勝感激。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants