Skip to content
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

feat(script-compiler): add id property in lint and fix messages #73

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@textlint/script-compiler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ worker.addEventListener('message', function (event) {
}
},
});
const id = crypto.randomUUID();
setTimeout(() => {
// lint
worker.postMessage({
id,
command: "lint",
text: "お刺身が食べれない",
ext: ".md"
Expand Down
2 changes: 2 additions & 0 deletions packages/@textlint/script-compiler/example/webindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
}
},
});
const id = crypto.randomUUID();
setTimeout(() => {
worker.postMessage({
id,
command: "lint",
text: "お刺身が食べれない",
ext: ".md"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import { TextlintConfigDescriptor } from "@textlint/config-loader";
import type { TextlintFixResult, TextlintResult } from "@textlint/types";
import { TextlintScriptMetadata } from "@textlint/script-parser";

export type MessageId = string;
export type TextlintWorkerCommandLint = {
id?: MessageId;
command: "lint";
text: string;
ext: string;
ruleId?: string;
};
export type TextlintWorkerCommandFix = {
id?: MessageId;
command: "fix";
text: string;
ext: string;
Expand All @@ -28,10 +31,12 @@ export type TextlintWorkerCommandResponseInit = {
metadata: TextlintScriptMetadata;
};
export type TextlintWorkerCommandResponseLint = {
id: MessageId | undefined;
command: "lint:result";
result: TextlintResult;
};
export type TextlintWorkerCommandResponseFix = {
id: MessageId | undefined;
command: "fix:result";
result: TextlintFixResult;
};
Expand Down Expand Up @@ -130,6 +135,7 @@ self.addEventListener('message', (event) => {
ext: data.ext,
}).then(result => {
return self.postMessage({
id: data.id,
command: "lint:result",
result
});
Expand All @@ -143,6 +149,7 @@ self.addEventListener('message', (event) => {
ext: data.ext,
}).then(result => {
return self.postMessage({
id: data.id,
command: "fix:result",
result
});
Expand Down
43 changes: 19 additions & 24 deletions packages/textchecker-element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,22 @@ const waiterForInit = (worker: Worker) => {
}
};
};
const generateMessageId = () => crypto.randomUUID();
const createTextlint = ({ worker, ext }: { worker: Worker; ext: string }) => {
const lintText: LintEngineAPI["lintText"] = async ({ text }: { text: string }): Promise<TextlintResult[]> => {
updateStatus("linting...");
return new Promise((resolve, _reject) => {
worker.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "lint:result") {
resolve([data.result]);
}
updateStatus("linted");
},
{
once: true
const id = generateMessageId();
worker.addEventListener("message", function handler(event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "lint:result" && data.id === id) {
resolve([data.result]);
worker.removeEventListener("message", handler);
}
);
updateStatus("linted");
});
return worker.postMessage({
id,
command: "lint",
text,
ext: ext
Expand All @@ -70,20 +68,17 @@ const createTextlint = ({ worker, ext }: { worker: Worker; ext: string }) => {
}): Promise<TextlintFixResult> => {
updateStatus("fixing...");
return new Promise((resolve, _reject) => {
worker.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "fix:result") {
resolve(data.result);
}
updateStatus("fixed");
},
{
once: true
const id = generateMessageId();
worker.addEventListener("message", function handler(event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "fix:result" && data.id === id) {
resolve(data.result);
worker.removeEventListener("message", handler);
}
);
updateStatus("fixed");
});
return worker.postMessage({
id,
command: "fix",
text,
ruleId: message?.ruleId,
Expand Down
39 changes: 17 additions & 22 deletions packages/webextension/app/scripts/background/textlint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const createWorkerRef = (worker: Worker) => {
}
};
};
const generateMessageId = () => crypto.randomUUID();
export type TextlintWorker = ReturnType<typeof createTextlintWorker>;
export const createTextlintWorker = (script: Script) => {
const blob = new Blob([script.code], { type: "application/javascript" });
Expand All @@ -56,19 +57,16 @@ export const createTextlintWorker = (script: Script) => {
const workerRef = createWorkerRef(defaultWorker);
const lintText = async ({ text, ext }: { text: string; ext: string }): Promise<TextlintResult[]> => {
return new Promise((resolve, _reject) => {
workerRef.current.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "lint:result") {
resolve([data.result]);
}
},
{
once: true
const id = generateMessageId();
workerRef.current.addEventListener("message", function handler(event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "lint:result" && data.id === id) {
resolve([data.result]);
workerRef.current.removeEventListener("message", handler);
}
);
});
return workerRef.current.postMessage({
id,
command: "lint",
text,
ext
Expand All @@ -88,19 +86,16 @@ export const createTextlintWorker = (script: Script) => {
message?: TextlintMessage;
}): Promise<TextlintFixResult> => {
return new Promise((resolve, _reject) => {
workerRef.current.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "fix:result") {
resolve(data.result);
}
},
{
once: true
const id = generateMessageId();
workerRef.current.addEventListener("message", function handler(event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "fix:result" && data.id === id) {
resolve(data.result);
workerRef.current.removeEventListener("message", handler);
}
);
});
return workerRef.current.postMessage({
id,
command: "fix",
text,
ruleId: message?.ruleId,
Expand Down