Skip to content

Commit

Permalink
change options api and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leyhline committed Jun 23, 2023
1 parent 1de2810 commit 0fdd5f0
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 25 deletions.
98 changes: 88 additions & 10 deletions src/MecabWorker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("MecabWorker integration tests", function () {
});

it("creates a worker with UNIDIC2 and parses a string, inserting spaces", async function () {
const worker = await MecabWorker.create(UNIDIC2, { noCache: true });
const worker = await MecabWorker.create(UNIDIC2, undefined, true);
const result = await worker.parse(
"青森県と秋田県にまたがり所在する十和田湖、御鼻部山展望台からの展望"
);
Expand All @@ -27,7 +27,7 @@ describe("MecabWorker integration tests", function () {
});

it("creates a worker with UNIDIC2 and parses a string, returning a node for each word", async function () {
const worker = await MecabWorker.create(UNIDIC2, { noCache: true });
const worker = await MecabWorker.create(UNIDIC2, undefined, true);
const nodes = await worker.parseToNodes(
"青森県と秋田県にまたがり所在する十和田湖、御鼻部山展望台からの展望"
);
Expand All @@ -42,7 +42,7 @@ describe("MecabWorker integration tests", function () {
});

it("parses a list of words using Array.map", async function () {
const worker = await MecabWorker.create(UNIDIC2, { noCache: true });
const worker = await MecabWorker.create(UNIDIC2, undefined, true);
const result = (
await Promise.all(
[
Expand All @@ -67,12 +67,90 @@ describe("MecabWorker integration tests", function () {

it("loads IPADIC from network and receives messages during worker creation", async function () {
const onLoadLog: { type: string; name: string }[] = [];
await MecabWorker.create(IPADIC, {
onLoad: ({ type, name }) => {
onLoadLog.push({ type, name });
},
await MecabWorker.create(IPADIC, ({ type, name }) => {
onLoadLog.push({ type, name });
});
expect(onLoadLog).has.length(9);
expect(onLoadLog[0]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/",
});
expect(onLoadLog[1]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/dicrc",
});
expect(onLoadLog[2]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/README",
});
expect(onLoadLog[3]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/unk.dic",
});
expect(onLoadLog[4]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/AUTHORS",
});
expect(onLoadLog[5]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/COPYING",
});
expect(onLoadLog[6]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/sys.dic",
});
expect(onLoadLog[7]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/matrix.bin",
});
expect(onLoadLog[8]).to.deep.equal({
type: "unzip",
name: "ipadic-2.7.0_bin/char.bin",
});
});

it("loads IPADIC from cache and receives messages during worker creation", async function () {
const onLoadLog: { type: string; name: string }[] = [];
await MecabWorker.create(IPADIC, ({ type, name }) => {
onLoadLog.push({ type, name });
});
expect(onLoadLog).has.length(9);
expect(onLoadLog[0]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/",
});
expect(onLoadLog[1]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/dicrc",
});
expect(onLoadLog[2]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/README",
});
expect(onLoadLog[3]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/unk.dic",
});
expect(onLoadLog[4]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/AUTHORS",
});
expect(onLoadLog[5]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/COPYING",
});
expect(onLoadLog[6]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/sys.dic",
});
expect(onLoadLog[7]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/matrix.bin",
});
expect(onLoadLog[8]).to.deep.equal({
type: "cache",
name: "ipadic-2.7.0_bin/char.bin",
});
expect(onLoadLog).has.length(8);
});

it("creates a worker with IPADIC and parses a string, inserting spaces", async function () {
Expand Down Expand Up @@ -100,7 +178,7 @@ describe("MecabWorker integration tests", function () {
});

it("creates a worker with JUMANDIC and parses a string, inserting spaces", async function () {
const worker = await MecabWorker.create(JUMANDIC, { noCache: true });
const worker = await MecabWorker.create(JUMANDIC, undefined, true);
const result = await worker.parse(
"青森県と秋田県にまたがり所在する十和田湖、御鼻部山展望台からの展望"
);
Expand All @@ -110,7 +188,7 @@ describe("MecabWorker integration tests", function () {
});

it("creates a worker with JUMANDIC and parses a string, returning a node for each word", async function () {
const worker = await MecabWorker.create(JUMANDIC, { noCache: true });
const worker = await MecabWorker.create(JUMANDIC, undefined, true);
const nodes = await worker.parseToNodes(
"青森県と秋田県にまたがり所在する十和田湖、御鼻部山展望台からの展望"
);
Expand Down
29 changes: 14 additions & 15 deletions src/MecabWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface MecabCallInit extends MecabDataType {
type: "init";
cacheName: string;
url: string;
noCache?: boolean;
noCache: boolean;
}

export interface MecabCallParse extends MecabDataType {
Expand All @@ -73,11 +73,6 @@ export type MecabCallData =

export type MecabMessageCallEvent = MessageEvent<MecabCallData>;

interface MecabWorkerOptions {
noCache?: boolean;
onLoad?: (message: MecabUnzip | MecabCache) => void;
}

/**
* MecabWorker subclasses Worker to provide a simple interface for interacting
* with the actual worker script. Instead of using `postMessage` one can
Expand All @@ -99,13 +94,17 @@ export class MecabWorker<T extends Features | null = null> {
*/
static async create<T extends Features | null = null>(
dictionary: Dictionary<T>,
options?: MecabWorkerOptions
onLoad?: (message: MecabUnzip | MecabCache) => void,
noCache = false
): Promise<MecabWorker<T>> {
const mecabWorker = new MecabWorker<T>(dictionary.wrapper);
return mecabWorker.init(dictionary, options).then(() => mecabWorker);
const mecabWorker = new MecabWorker<T>(dictionary.wrapper, onLoad);
return mecabWorker.init(dictionary, noCache).then(() => mecabWorker);
}

constructor(wrapper?: (feature: string[]) => T | null) {
constructor(
wrapper?: (feature: string[]) => T | null,
onLoad?: (message: MecabUnzip | MecabCache) => void
) {
if (!testModuleWorkerSupport()) {
throw new Error(
"Cannot initialize MeCab. Module workers are not supported in your browser."
Expand All @@ -119,6 +118,9 @@ export class MecabWorker<T extends Features | null = null> {
type: "module",
});
this.worker.onmessage = (e: MecabMessageEvent) => {
if (onLoad && (e.data.type === "unzip" || e.data.type === "cache")) {
onLoad(e.data);
}
const callback = this.pending.get(e.data.id);
if (callback) {
callback(e.data);
Expand All @@ -129,16 +131,13 @@ export class MecabWorker<T extends Features | null = null> {
};
}

async init(
dictionary: Dictionary<T>,
options?: MecabWorkerOptions
): Promise<void> {
async init(dictionary: Dictionary<T>, noCache = false): Promise<void> {
const message: MecabCallInit = {
id: this.counter,
type: "init",
cacheName: dictionary.cacheName,
url: dictionary.url,
noCache: options?.noCache,
noCache: noCache,
};
this.counter++;
return new Promise((resolve, reject) => {
Expand Down

0 comments on commit 0fdd5f0

Please sign in to comment.