chore: start of a webworker

This commit is contained in:
Jamie Peabody
2022-01-15 20:01:37 +00:00
parent aa74599361
commit 94399bcbf3
4 changed files with 36 additions and 5 deletions

View File

@@ -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",

View File

@@ -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');
}
}
};

8
src/diff-worker.js Normal file
View File

@@ -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);
};

View File

@@ -23,6 +23,11 @@ module.exports = {
}, {
loader: 'css-loader'
}]
}, {
test: /worker\.js$/,
use: {
loader: 'worker-loader'
}
}]
},