Skip to content

Commit f892c68

Browse files
committed
refactor(realtime): update dirty note job
Signed-off-by: BoHong Li <[email protected]>
1 parent 0352057 commit f892c68

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

lib/realtimeUpdateDirtyNoteJob.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const async = require('async')
43
const config = require('./config')
54
const logger = require('./logger')
65
const moment = require('moment')
@@ -11,38 +10,67 @@ class UpdateDirtyNoteJob {
1110
}
1211

1312
start () {
14-
setInterval(this.updateDirtyNote.bind(this), 1000)
13+
if (this.timer) return
14+
this.timer = setInterval(this.updateDirtyNotes.bind(this), 1000)
1515
}
1616

17-
updateDirtyNote () {
17+
stop () {
18+
if (!this.timer) return
19+
clearInterval(this.timer)
20+
this.timer = undefined
21+
}
22+
23+
updateDirtyNotes () {
1824
const notes = this.realtime.getNotePool()
19-
async.each(Object.keys(notes), (key, callback) => {
25+
Object.keys(notes).forEach((key) => {
2026
const note = notes[key]
21-
if (!note.server.isDirty) return callback(null, null)
27+
this.updateDirtyNote(note)
28+
.catch((err) => {
29+
logger.error('updateDirtyNote: updater error', err)
30+
})
31+
})
32+
}
2233

23-
if (config.debug) logger.info('updater found dirty note: ' + key)
24-
note.server.isDirty = false
25-
this.realtime.updateNote(note, (err, _note) => {
26-
// handle when note already been clean up
27-
if (!notes[key] || !notes[key].server) return callback(null, null)
28-
if (!_note) {
29-
this.realtime.io.to(note.id).emit('info', {
30-
code: 404
31-
})
32-
logger.error('note not found: ', note.id)
33-
}
34+
async updateDirtyNote (note) {
35+
const notes = this.realtime.getNotePool()
36+
if (!note.server.isDirty) return
3437

35-
if (err || !_note) {
36-
this.realtime.disconnectSocketOnNote(note)
37-
return callback(err, null)
38-
}
38+
if (config.debug) logger.info('updateDirtyNote: updater found dirty note: ' + note.id)
39+
note.server.isDirty = false
3940

40-
note.updatetime = moment(_note.lastchangeAt).valueOf()
41-
this.realtime.emitCheck(note)
42-
return callback(null, null)
41+
try {
42+
const _note = await this.updateNoteAsync(note)
43+
// handle when note already been clean up
44+
if (!notes[note.id] || !notes[note.id].server) return
45+
46+
if (!_note) {
47+
this.realtime.io.to(note.id).emit('info', {
48+
code: 404
49+
})
50+
logger.error('updateDirtyNote: note not found: ', note.id)
51+
this.realtime.disconnectSocketOnNote(note)
52+
}
53+
54+
note.updatetime = moment(_note.lastchangeAt).valueOf()
55+
this.realtime.emitCheck(note)
56+
} catch (err) {
57+
logger.error('updateDirtyNote: note not found: ', note.id)
58+
this.realtime.io.to(note.id).emit('info', {
59+
code: 404
60+
})
61+
this.realtime.disconnectSocketOnNote(note)
62+
throw err
63+
}
64+
}
65+
66+
updateNoteAsync (note) {
67+
return new Promise((resolve, reject) => {
68+
this.realtime.updateNote(note, (err, _note) => {
69+
if (err) {
70+
return reject(err)
71+
}
72+
return resolve(_note)
4373
})
44-
}, (err) => {
45-
if (err) return logger.error('updater error', err)
4674
})
4775
}
4876
}

0 commit comments

Comments
 (0)