Skip to content

Commit 5dd2bd0

Browse files
committed
メモリリーク修正
- 無限ループの可能性を排除 - タイマーの確実なクリーンアップ - CodeMirrorインスタンスの有効性チェック追加
1 parent 3146691 commit 5dd2bd0

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

js/kunai/yata.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -205,28 +205,49 @@ class Yata {
205205
// 何も表示されない
206206
// †最大の闇†
207207

208-
let id = this.cmRefreshTimers.size
209-
while (true) {
210-
++id
211-
if (!this.cmRefreshTimers.has(id)) {
208+
let id = 0
209+
// より安全なID生成方法に変更
210+
for (let i = 0; i <= this.cmRefreshTimers.size + 1; i++) {
211+
if (!this.cmRefreshTimers.has(i)) {
212+
id = i
212213
break
213214
}
214215
}
216+
215217
let info = new RefreshTimerInfo(id)
216218
this.log.debug(`autoRefresh engaged (id: #${id})`)
217219
this.cmRefreshTimers.set(id, info)
218220

219-
this.cmRefreshTimers.get(id).realID = setInterval((e) => {
221+
const timerId = setInterval(() => {
222+
// infoがまだ存在するか確認
223+
if (!this.cmRefreshTimers.has(id)) {
224+
clearInterval(timerId)
225+
return
226+
}
227+
220228
++info.count
221-
this.cm.refresh()
229+
230+
// CodeMirrorインスタンスが有効か確認
231+
if (this.cm && typeof this.cm.refresh === 'function') {
232+
try {
233+
this.cm.refresh()
234+
} catch (e) {
235+
this.log.error(`Failed to refresh CodeMirror: ${e}`)
236+
clearInterval(timerId)
237+
this.cmRefreshTimers.delete(id)
238+
return
239+
}
240+
}
222241

223242
if (info.count > 10) {
243+
this.log.debug(`removing autoRefresh timer (id: #${id}, realID: #${timerId})`)
244+
clearInterval(timerId)
224245
this.cmRefreshTimers.delete(id)
225-
226-
this.log.debug(`removing autoRefresh timer (id: #${id}, realID: #${info.realID})`)
227-
clearInterval(info.realID)
228246
}
229247
}, 200)
248+
249+
// タイマーIDを保存
250+
info.realID = timerId
230251
}
231252

232253
onResize(e) {

0 commit comments

Comments
 (0)