-
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[script-compiler] Mapping sent and received messages and avoiding race conditions #72
Comments
I'm ok that adding identifier to messages. Specifically, the worker code receives the const id = crypto.randomUUID();
worker.addEventListener("message", (event) => {
if(event.data.id === id) {
console.log("result of #1");
}
})
worker.postMessage({
id, // <= added
command: "lint",
text: "text#1 short text; lint finishes quickly",
ext: ".txt"
}) |
Yes. The way you showed looks good to me. |
Another Discussion Points
|
|
I agree with you. TODO
Welcome to PR |
I am trying to make a PR and found another discussion point:
One of my ideas is that the worker responds with a newly-defined format like |
Yeah, |
Sorry, I found a simple oversight in my idea. When |
My another idea is just to throw Error if |
I think that worker should just post User always listen the worker.addEventListener("message", (event) => {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "error") {
console.error(data.error); // missing id error
}
});
worker.postMessage({
command: "lint",
text: "text#1 short text; lint finishes quickly",
ext: ".txt"
}); const id = crypto.randomUUID();
worker.addEventListener("message", (event) => {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "error") {
console.error(data.error, data.error.id); // something error at `id`.
}
});
worker.postMessage({
id,
command: "lint",
text: "text#1 short text; lint finishes quickly",
ext: ".txt"
}); |
It may be better to proceed separately with the PR to add the |
My idea that just throws Error when missing const id = crypto.randomUUID();
worker.addEventListener("message", function onResult(event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.id === id) {
if (data.error) {
console.error(data.error); // id exists but something went wrong
} else {
console.log(data.result) // succeeded
}
worker.removeEventListener("message", onResult);
worker.removeEventListener("error", onError);
}
});
worker.addEventListener("error", function onError(error) {
console.error(error); // missing id error
worker.removeEventListener("message", onResult);
worker.removeEventListener("error", onError);
}); Or more comprehensive idea is that script-compiler provides client-side code to force Textlint message protocol as some other libraries do. Though this is complicated. const textlint = wrap(new Worker("textlint-worker.js"));
const results = await Promise.all([
textlint.lint("text#1 short text; lint finishes quickly", ".txt"),
textlint.lint("text#2 short text; lint finishes quickly", ".txt")
]):
// safe for race conditions by default
// provides best way to work with Textlint worker, id and error handling included |
Ah, I did not know that catch the exception on the worker via |
I am considering that it may be better to first create a PR that provides error response messages for error handling, then make a PR that makes |
I'd like to have a one-to-one correspondence between the text I send to the Textlint worker and the lint result I receive from the Textlint worker.
However, the order I received results from the Textlint worker can differ from the one I posted texts to the worker probably because
kernel.lintText
is async. Ref:editor/packages/@textlint/script-compiler/src/CodeGenerator/worker-codegen.ts
Lines 125 to 136 in b32d016
So relying on message reception order can cause a race condition. Pseudocode:
More higher-level example:
editor/packages/textchecker-element/index.ts
Lines 41 to 63 in b32d016
Some Web Worker libraries use IDs in messages. I think it can be one of the ideas to solve this problem.
For example, comlink:
https://github.com/GoogleChromeLabs/comlink/blob/dffe9050f63b1b39f30213adeb1dd4b9ed7d2594/src/comlink.ts#L596-L615
For another example, minlink:
https://github.com/mizchi/minlink/blob/98f0eed1b1fc00a51709e07c6fe3e18232cdfaad/src/shared.ts#L52-L61
The text was updated successfully, but these errors were encountered: