mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 17:55:17 -04:00
dmpWorker.ts: Wrap process.send
in a function to not lose this
🐛
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
d89cf23fdd
commit
2408aef54c
1 changed files with 25 additions and 17 deletions
|
@ -1,19 +1,23 @@
|
||||||
// external modules
|
// external modules
|
||||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
import { diff_match_patch, patch_obj, DIFF_INSERT, DIFF_DELETE } from 'diff-match-patch'
|
import { DIFF_DELETE, DIFF_INSERT, diff_match_patch, patch_obj } from 'diff-match-patch'
|
||||||
import { logger } from '../logger'
|
import { logger } from '../logger'
|
||||||
import { Revision } from '../models'
|
import { Revision } from '../models'
|
||||||
|
|
||||||
// Function for suppressing TS2722
|
// Function for suppressing TS2722
|
||||||
// eslint-disable-next-line @typescript-eslint/unbound-method,@typescript-eslint/no-empty-function
|
// eslint-disable-next-line @typescript-eslint/unbound-method,@typescript-eslint/no-empty-function
|
||||||
const send = process.send || function (): boolean {
|
function processSend (options): boolean {
|
||||||
|
if (process?.send !== undefined) {
|
||||||
|
return process.send(options)
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/camelcase,new-cap
|
// eslint-disable-next-line @typescript-eslint/camelcase,new-cap
|
||||||
const dmp: diff_match_patch = new diff_match_patch()
|
const dmp: diff_match_patch = new diff_match_patch()
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
function getRevision (revisions: Revision[], count: number): {content: string; patch: patch_obj[]; authorship: string} {
|
function getRevision (revisions: Revision[], count: number): { content: string; patch: patch_obj[]; authorship: string } {
|
||||||
const msStart = (new Date()).getTime()
|
const msStart = (new Date()).getTime()
|
||||||
let startContent = ''
|
let startContent = ''
|
||||||
let lastPatch = ''
|
let lastPatch = ''
|
||||||
|
@ -39,7 +43,11 @@ function getRevision (revisions: Revision[], count: number): {content: string; p
|
||||||
for (let i = 0, l = applyPatches.length; i < l; i++) {
|
for (let i = 0, l = applyPatches.length; i < l; i++) {
|
||||||
for (let j = 0, m = applyPatches[i].diffs.length; j < m; j++) {
|
for (let j = 0, m = applyPatches[i].diffs.length; j < m; j++) {
|
||||||
const diff = applyPatches[i].diffs[j]
|
const diff = applyPatches[i].diffs[j]
|
||||||
if (diff[0] === DIFF_INSERT) { diff[0] = DIFF_DELETE } else if (diff[0] === DIFF_DELETE) { diff[0] = DIFF_INSERT }
|
if (diff[0] === DIFF_INSERT) {
|
||||||
|
diff[0] = DIFF_DELETE
|
||||||
|
} else if (diff[0] === DIFF_DELETE) {
|
||||||
|
diff[0] = DIFF_INSERT
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -89,12 +97,12 @@ function createPatch (lastDoc: string, currDoc: string): string {
|
||||||
}
|
}
|
||||||
|
|
||||||
class Data {
|
class Data {
|
||||||
msg: string;
|
msg: string
|
||||||
cacheKey: any;
|
cacheKey: any
|
||||||
lastDoc?: string;
|
lastDoc?: string
|
||||||
currDoc?: string;
|
currDoc?: string
|
||||||
revisions?: Revision[];
|
revisions?: Revision[]
|
||||||
count?: number;
|
count?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on('message', function (data: Data) {
|
process.on('message', function (data: Data) {
|
||||||
|
@ -108,14 +116,14 @@ process.on('message', function (data: Data) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const patch: string = createPatch(data.lastDoc, data.currDoc)
|
const patch: string = createPatch(data.lastDoc, data.currDoc)
|
||||||
send({
|
processSend({
|
||||||
msg: 'check',
|
msg: 'check',
|
||||||
result: patch,
|
result: patch,
|
||||||
cacheKey: data.cacheKey
|
cacheKey: data.cacheKey
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('dmp worker error', err)
|
logger.error('create patch: dmp worker error', err)
|
||||||
send({
|
processSend({
|
||||||
msg: 'error',
|
msg: 'error',
|
||||||
error: err,
|
error: err,
|
||||||
cacheKey: data.cacheKey
|
cacheKey: data.cacheKey
|
||||||
|
@ -128,15 +136,15 @@ process.on('message', function (data: Data) {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
const result: {content: string; patch: patch_obj[]; authorship: string} = getRevision(data.revisions, data.count)
|
const result: { content: string; patch: patch_obj[]; authorship: string } = getRevision(data.revisions, data.count)
|
||||||
send({
|
processSend({
|
||||||
msg: 'check',
|
msg: 'check',
|
||||||
result: result,
|
result: result,
|
||||||
cacheKey: data.cacheKey
|
cacheKey: data.cacheKey
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error('dmp worker error', err)
|
logger.error('get revision: dmp worker error', err)
|
||||||
send({
|
processSend({
|
||||||
msg: 'error',
|
msg: 'error',
|
||||||
error: err,
|
error: err,
|
||||||
cacheKey: data.cacheKey
|
cacheKey: data.cacheKey
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue