changed for loop to foreach

Signed-off-by: Yannick Bungers <git@innay.de>
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
Yannick Bungers 2020-04-11 12:29:41 +02:00 committed by David Mehren
parent 4eb71d6cad
commit c6655d767a
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB

View file

@ -14,17 +14,17 @@ import logger = require('../logger');
const Op = Sequelize.Op;
var dmpWorker: ChildProcess | null = createDmpWorker()
var dmpCallbackCache = {}
let dmpWorker: ChildProcess | null = createDmpWorker();
const dmpCallbackCache = {};
function createDmpWorker() {
var worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), ['ignore'])
const worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), ['ignore']);
logger.debug('dmp worker process started')
worker.on('message', function (data: any) {
if (!data || !data.msg || !data.cacheKey) {
return logger.error('dmp worker error: not enough data on message')
}
var cacheKey = data.cacheKey
const cacheKey = data.cacheKey;
switch (data.msg) {
case 'error':
dmpCallbackCache[cacheKey](data.error, null)
@ -44,7 +44,7 @@ function createDmpWorker() {
function sendDmpWorker(data, callback) {
if (!dmpWorker) dmpWorker = createDmpWorker()
var cacheKey = Date.now() + '_' + shortId.generate()
const cacheKey = Date.now() + '_' + shortId.generate();
dmpCallbackCache[cacheKey] = callback
data = Object.assign(data, {
cacheKey: cacheKey
@ -112,14 +112,13 @@ export class Revision extends Model<Revision> {
},
order: [['createdAt', 'DESC']]
}).then(function (revisions) {
var data: any[] = []
for (var i = 0; i < revisions.length; i++) {
var revision = revisions[i]
const data: any[] = [];
revisions.forEach(function (revision) {
data.push({
time: moment(revision.createdAt).valueOf(),
length: revision.length
})
}
})
callback(null, data)
}).catch(function (err) {
callback(err, null)
@ -197,12 +196,12 @@ export class Revision extends Model<Revision> {
}
}).then(function (notes) {
if (notes.length <= 0) return callback(null, notes)
var savedNotes: any[] = []
const savedNotes: any[] = [];
async.each(notes, function (note: any, _callback) {
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
if (note.lastchangeAt && note.savedAt) {
var lastchangeAt = moment(note.lastchangeAt)
var savedAt = moment(note.savedAt)
const lastchangeAt = moment(note.lastchangeAt);
const savedAt = moment(note.savedAt);
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
savedNotes.push(note)
Revision.saveNoteRevision(note, _callback)
@ -221,7 +220,7 @@ export class Revision extends Model<Revision> {
return callback(err, null)
}
// return null when no notes need saving at this moment but have delayed tasks to be done
var result = ((savedNotes.length === 0) && (notes.length > savedNotes.length)) ? null : savedNotes
const result = ((savedNotes.length === 0) && (notes.length > savedNotes.length)) ? null : savedNotes;
return callback(null, result)
})
}).catch(function (err) {
@ -249,9 +248,9 @@ export class Revision extends Model<Revision> {
return callback(err, null)
})
} else {
var latestRevision = revisions[0]
var lastContent = latestRevision.content || latestRevision.lastContent
var content = note.content
const latestRevision = revisions[0];
const lastContent = latestRevision.content || latestRevision.lastContent;
const content = note.content;
sendDmpWorker({
msg: 'create patch',
lastDoc: lastContent,