From 94399bcbf3f2b2923b392fcc414b348b77f74206 Mon Sep 17 00:00:00 2001 From: Jamie Peabody Date: Sat, 15 Jan 2022 20:01:37 +0000 Subject: [PATCH] chore: start of a webworker --- package.json | 3 ++- src/diff-view.js | 25 +++++++++++++++++++++---- src/diff-worker.js | 8 ++++++++ webpack.dev.js | 5 +++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 src/diff-worker.js diff --git a/package.json b/package.json index 1fbf97e..574da24 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,8 @@ "style-loader": "^2.0.0", "webpack": "^4.44.2", "webpack-cli": "^4.0.0", - "webpack-dev-server": "^3.11.0" + "webpack-dev-server": "^3.11.0", + "worker-loader": "^3.0.8" }, "scripts": { "build": "npm run test && npm run build:dist", diff --git a/src/diff-view.js b/src/diff-view.js index 82d5a7e..80dd06a 100644 --- a/src/diff-view.js +++ b/src/diff-view.js @@ -993,10 +993,27 @@ CodeMirrorDiffView.prototype._diff = function() { } const lhs = this.editor.lhs.getValue(); const rhs = this.editor.rhs.getValue(); - const comparison = new diff(lhs, rhs, this.settings); - this.changes = DiffParser(comparison.normal_form()); - if (this.settings._debug.includes('change')) { - traceTimeEnd(' change#_diff'); + /* + if (window.Worker) { + if (!this._diffWorker) { + trace(' change#_diff creating diff worker'); + // this._diffWorker = new Worker('./diff-worker.js'); + this._diffWorker = new Worker(new URL('./diff-worker.js', import.meta.url)); + this._diffWorker.onchange = (ev) => { + this.changes = ev.data; + this._clear(); + this._changed(); + } + } + trace(' change#_diff starting worker'); + this._diffWorker.postMessage({ lhs, rhs }); + } else + */{ + const comparison = new diff(lhs, rhs, this.settings); + this.changes = DiffParser(comparison.normal_form()); + if (this.settings._debug.includes('change')) { + traceTimeEnd(' change#_diff'); + } } }; diff --git a/src/diff-worker.js b/src/diff-worker.js new file mode 100644 index 0000000..e1e62b5 --- /dev/null +++ b/src/diff-worker.js @@ -0,0 +1,8 @@ +const diff = require('./diff'); + +onmessage = function (ev) { + const { lhs, rhs } = ev; + const compare = new diff(lhs, rhs, this.settings); + const changes = DiffParser(compare.normal_form()); + postMessage(changes); +}; diff --git a/webpack.dev.js b/webpack.dev.js index c9f2686..bb28bb8 100644 --- a/webpack.dev.js +++ b/webpack.dev.js @@ -23,6 +23,11 @@ module.exports = { }, { loader: 'css-loader' }] + }, { + test: /worker\.js$/, + use: { + loader: 'worker-loader' + } }] },