Skip to content

Commit

Permalink
OCREngine: explicit resource management
Browse files Browse the repository at this point in the history
  • Loading branch information
wydengyre committed Jun 15, 2024
1 parent 9ad0b8e commit 1cd4013
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/ocr-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class OCREngine {
/**
* Shut down the OCR engine and free up resources.
*/
destroy() {
[Symbol.dispose]() {
this._engine.delete();
this._engine = null;
}
Expand Down
26 changes: 9 additions & 17 deletions test/ocr-engine-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,8 @@ describe("supportsFastBuild", () => {
});
});

describe("OCREngine", () => {
let ocr;

before(async () => {
ocr = await createEngine();
});

after(() => {
ocr.destroy();
});
describe("OCREngine", async () => {
using ocr = await createEngine();

[
// Image size does not match buffer size
Expand Down Expand Up @@ -93,7 +85,7 @@ describe("OCREngine", () => {
});

it("throws an error if OCR is attempted before image is loaded", async () => {
const ocr = await createEngine();
using ocr = await createEngine();

assert.throws(
() => {
Expand Down Expand Up @@ -121,7 +113,7 @@ describe("OCREngine", () => {
});

it("throws an error if OCR is attempted before model is loaded", async () => {
const ocr = await createEngine({ loadModel: false });
using ocr = await createEngine({ loadModel: false });
const imageData = await loadImage(resolve("./small-test-page.jpg"));
ocr.loadImage(imageData);

Expand All @@ -143,7 +135,7 @@ describe("OCREngine", () => {
});

it("throws an error if you attempt get the value of a nonsense variable", async () => {
const ocr = await createEngine({ loadModel: false });
using ocr = await createEngine({ loadModel: false });
assert.throws(
() => {
ocr.getVariable("nonsense");
Expand All @@ -154,7 +146,7 @@ describe("OCREngine", () => {
});

it("throws an error if you attempt set the value of a nonsense variable", async () => {
const ocr = await createEngine({ loadModel: false });
using ocr = await createEngine({ loadModel: false });
assert.throws(
() => {
ocr.setVariable("nonsense", "nonsense");
Expand All @@ -165,7 +157,7 @@ describe("OCREngine", () => {
});

it("successfully sets configuration variables", async () => {
const ocr = await createEngine({ loadModel: false });
using ocr = await createEngine({ loadModel: false });
const varName = "user_defined_dpi";
const varValue = "300";
ocr.setVariable(varName, varValue);
Expand Down Expand Up @@ -210,7 +202,7 @@ describe("OCREngine", () => {
);

it("can extract bounding boxes without a model loaded", async function () {
const ocr = await createEngine({ loadModel: false });
using ocr = await createEngine({ loadModel: false });

const imageData = await loadImage(resolve("./small-test-page.jpg"));
ocr.loadImage(imageData);
Expand Down Expand Up @@ -333,7 +325,7 @@ describe("OCREngine", () => {
stderr += s;
};

const ocr = await createEngine({
using ocr = await createEngine({
emscriptenModuleOptions: { printErr: writeToStderr },
});

Expand Down

0 comments on commit 1cd4013

Please sign in to comment.