2019-05-06 19:15:33 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const config = require('./config')
|
|
|
|
const logger = require('./logger')
|
|
|
|
const moment = require('moment')
|
|
|
|
|
|
|
|
class UpdateDirtyNoteJob {
|
|
|
|
constructor (realtime) {
|
|
|
|
this.realtime = realtime
|
|
|
|
}
|
|
|
|
|
|
|
|
start () {
|
2019-05-27 07:18:44 +00:00
|
|
|
if (this.timer) return
|
|
|
|
this.timer = setInterval(this.updateDirtyNotes.bind(this), 1000)
|
2019-05-06 19:15:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-27 07:18:44 +00:00
|
|
|
stop () {
|
|
|
|
if (!this.timer) return
|
|
|
|
clearInterval(this.timer)
|
|
|
|
this.timer = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
updateDirtyNotes () {
|
2019-05-06 19:15:33 +00:00
|
|
|
const notes = this.realtime.getNotePool()
|
2019-05-27 07:18:44 +00:00
|
|
|
Object.keys(notes).forEach((key) => {
|
2019-05-06 19:15:33 +00:00
|
|
|
const note = notes[key]
|
2019-05-27 07:18:44 +00:00
|
|
|
this.updateDirtyNote(note)
|
|
|
|
.catch((err) => {
|
|
|
|
logger.error('updateDirtyNote: updater error', err)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-06 19:15:33 +00:00
|
|
|
|
2019-05-27 07:18:44 +00:00
|
|
|
async updateDirtyNote (note) {
|
|
|
|
const notes = this.realtime.getNotePool()
|
|
|
|
if (!note.server.isDirty) return
|
2019-05-06 19:15:33 +00:00
|
|
|
|
2019-05-27 07:18:44 +00:00
|
|
|
if (config.debug) logger.info('updateDirtyNote: updater found dirty note: ' + note.id)
|
|
|
|
note.server.isDirty = false
|
2019-05-06 19:15:33 +00:00
|
|
|
|
2019-05-27 07:18:44 +00:00
|
|
|
try {
|
|
|
|
const _note = await this.updateNoteAsync(note)
|
|
|
|
// handle when note already been clean up
|
|
|
|
if (!notes[note.id] || !notes[note.id].server) return
|
|
|
|
|
|
|
|
if (!_note) {
|
|
|
|
this.realtime.io.to(note.id).emit('info', {
|
|
|
|
code: 404
|
|
|
|
})
|
|
|
|
logger.error('updateDirtyNote: note not found: ', note.id)
|
|
|
|
this.realtime.disconnectSocketOnNote(note)
|
|
|
|
}
|
|
|
|
|
|
|
|
note.updatetime = moment(_note.lastchangeAt).valueOf()
|
|
|
|
this.realtime.emitCheck(note)
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('updateDirtyNote: note not found: ', note.id)
|
|
|
|
this.realtime.io.to(note.id).emit('info', {
|
|
|
|
code: 404
|
|
|
|
})
|
|
|
|
this.realtime.disconnectSocketOnNote(note)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updateNoteAsync (note) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.realtime.updateNote(note, (err, _note) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
return resolve(_note)
|
2019-05-06 19:15:33 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.UpdateDirtyNoteJob = UpdateDirtyNoteJob
|