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

CAPI-269(feat): opencv load fix #14

Merged
merged 3 commits into from
Sep 6, 2024
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [2.0.1] - 06-09-2024

- (fix): skip pre-processing step if opencv did not load

## [2.0.0] - 10-06-2024

- (feat): `init` is now initialised with config and callbacks to retrieve image blob
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"author": "Anyline",
"name": "@anyline/anyline-guidance-sdk",
"version": "2.0.0",
"version": "2.0.1",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/modules/OpenCVManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export default class OpenCVManager {
private readonly opencvLoadedPromise: Promise<void>;
private opencvLoadedResolve: (() => void) | null = null;
private opencvLoadedReject: ((reason?: any) => void) | null = null;
public isOpenCVLoaded: boolean = false;

private constructor() {
this.opencvLoadedPromise = new Promise<void>((resolve, reject) => {
Expand All @@ -24,6 +25,7 @@ export default class OpenCVManager {
public loadOpenCV(): void {
if (Boolean((window as any)?.cv) && this.opencvLoadedResolve !== null) {
this.opencvLoadedResolve();
this.isOpenCVLoaded = true;
return;
}

Expand All @@ -40,6 +42,7 @@ export default class OpenCVManager {
cv.onRuntimeInitialized = () => {
if (this.opencvLoadedResolve !== null) {
this.opencvLoadedResolve();
this.isOpenCVLoaded = true;
}
};
};
Expand All @@ -53,6 +56,11 @@ export default class OpenCVManager {
}

public onLoad(callback: (error?: Error) => Promise<void>): void {
if (!this.isOpenCVLoaded) {
void callback(new Error('OpenCV is not fully loaded yet'));
return;
}

this.opencvLoadedPromise
.then(async () => {
await callback();
Expand All @@ -68,5 +76,6 @@ export default class OpenCVManager {
}
OpenCVManager.instance = null;
delete (global as any).cv;
this.isOpenCVLoaded = false;
}
}
12 changes: 12 additions & 0 deletions tests/unit/OpenCVManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('OpenCVManager', () => {
it('loads opencv successfully', async () => {
void expect((global as any).cv).toBeUndefined();

opencvManager.isOpenCVLoaded = true;
const mockCallback = jest.fn();
opencvManager.onLoad(mockCallback);

Expand Down Expand Up @@ -46,6 +47,7 @@ describe('OpenCVManager', () => {
it('handles opencv load error', async () => {
void expect((global as any).cv).toBeUndefined();

opencvManager.isOpenCVLoaded = true;
const mockCallback = jest.fn();
opencvManager.onLoad(mockCallback);

Expand All @@ -66,4 +68,14 @@ describe('OpenCVManager', () => {
void expect((global as any).cv).toBeUndefined();
});
});

it('throws error when opencv has not finished loading', async () => {
opencvManager.isOpenCVLoaded = false;
const mockCallback = jest.fn().mockResolvedValue(undefined);
opencvManager.onLoad(mockCallback);

await waitFor(() => {
void expect(mockCallback).toHaveBeenCalledWith(expect.any(Error));
});
});
});