Skip to content

Commit

Permalink
refactor(webetension): curve out textlint backgroun page
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 1, 2020
1 parent ee55ff1 commit 22fd569
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 85 deletions.
90 changes: 5 additions & 85 deletions packages/webextension/app/scripts/background.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { browser } from "webextension-polyfill-ts";
import { TextlintFixResult, TextlintMessage, TextlintResult } from "@textlint/types";
import { TextlintWorkerCommandFix, TextlintWorkerCommandLint, TextlintWorkerCommandResponse } from "@textlint/compiler";
import { createBackgroundEndpoint, isMessagePort } from "comlink-extension";
import * as Comlink from "comlink";
import { createTextlintPage } from "./background/textlint";

browser.runtime.onInstalled.addListener((details) => {
console.log("previousVersion", details.previousVersion);
Expand All @@ -12,96 +11,17 @@ browser.tabs.onUpdated.addListener(async (tabId) => {
browser.pageAction.show(tabId);
});

const worker = new Worker("download/textlint.js");
const waiterForInit = (worker: Worker) => {
let initialized = false;
let _resolve: null | ((init: boolean) => void) = null;
const deferred = new Promise((resolve) => {
_resolve = resolve;
});
worker.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "init") {
initialized = true;
_resolve && _resolve(initialized);
}
},
{
once: true
}
);
return {
ready() {
return deferred;
}
};
};

const workerStatus = waiterForInit(worker);

const lintText = async ({ text, ext }: { text: string; ext: string }): Promise<TextlintResult> => {
return new Promise((resolve, _reject) => {
worker.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "lint:result") {
resolve(data.result);
}
},
{
once: true
}
);
return worker.postMessage({
command: "lint",
text,
ext
} as TextlintWorkerCommandLint);
});
};
const fixText = async ({
text,
ext,
message
}: {
text: string;
ext: string;
message: TextlintMessage;
}): Promise<TextlintFixResult> => {
return new Promise((resolve, _reject) => {
worker.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "fix:result") {
resolve(data.result);
}
},
{
once: true
}
);
return worker.postMessage({
command: "fix",
text,
ruleId: message.ruleId,
ext: ext
} as TextlintWorkerCommandFix);
});
};
const textlint = createTextlintPage();
// receive
const backgroundExposedObject = {
lintText: lintText,
fixText: fixText
lintText: textlint.lintText,
fixText: textlint.fixText
};
export type backgroundExposedObject = typeof backgroundExposedObject;
browser.runtime.onConnect.addListener(async (port) => {
if (isMessagePort(port)) {
return;
}
await workerStatus.ready();
await textlint.ready();
Comlink.expose(backgroundExposedObject, createBackgroundEndpoint(port));
});
113 changes: 113 additions & 0 deletions packages/webextension/app/scripts/background/textlint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { browser } from "webextension-polyfill-ts";
import { TextlintFixResult, TextlintMessage, TextlintResult } from "@textlint/types";
import { TextlintWorkerCommandFix, TextlintWorkerCommandLint, TextlintWorkerCommandResponse } from "@textlint/compiler";

browser.runtime.onInstalled.addListener((details) => {
console.log("previousVersion", details.previousVersion);
});

browser.tabs.onUpdated.addListener(async (tabId) => {
browser.pageAction.show(tabId);
});
const waiterForInit = (worker: Worker) => {
let initialized = false;
let _resolve: null | ((init: boolean) => void) = null;
const deferred = new Promise((resolve) => {
_resolve = resolve;
});
worker.addEventListener(
"message",
function (event) {
const data: TextlintWorkerCommandResponse = event.data;
if (data.command === "init") {
initialized = true;
_resolve && _resolve(initialized);
}
},
{
once: true
}
);
return {
ready() {
return deferred;
}
};
};
const createWorkerRef = (worker: Worker) => {
let currentWorker: Worker = worker;
let currentWorkerStatus = waiterForInit(worker);
return {
current: currentWorker,
ready: currentWorkerStatus.ready,
updateWorker(worker: Worker) {
currentWorker = worker;
currentWorkerStatus = waiterForInit(worker);
}
};
};
export const createTextlintPage = (defaultWorkerUrl: string | URL = "download/textlint.js") => {
const defaultWorker = new Worker(defaultWorkerUrl);
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
}
);
return workerRef.current.postMessage({
command: "lint",
text,
ext
} as TextlintWorkerCommandLint);
});
};
const fixText = async ({
text,
ext,
message
}: {
text: string;
ext: string;
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
}
);
return workerRef.current.postMessage({
command: "fix",
text,
ruleId: message.ruleId,
ext: ext
} as TextlintWorkerCommandFix);
});
};
return {
lintText,
fixText,
ready() {
return workerRef.ready();
},
updateWorker(workerUrl: string | URL) {
workerRef.updateWorker(new Worker(workerUrl));
}
};
};

0 comments on commit 22fd569

Please sign in to comment.