From f2288371b5ccbd6c568315b66a6c260acc784811 Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Fri, 6 Sep 2024 08:37:53 +0200 Subject: [PATCH 1/3] CAPI-269(fix): do not process image if opencv has not finished loading --- src/modules/OpenCVManager.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/modules/OpenCVManager.ts b/src/modules/OpenCVManager.ts index bcbccd4..79a6e18 100644 --- a/src/modules/OpenCVManager.ts +++ b/src/modules/OpenCVManager.ts @@ -6,6 +6,7 @@ export default class OpenCVManager { private readonly opencvLoadedPromise: Promise; private opencvLoadedResolve: (() => void) | null = null; private opencvLoadedReject: ((reason?: any) => void) | null = null; + public isOpenCVLoaded: boolean = false; private constructor() { this.opencvLoadedPromise = new Promise((resolve, reject) => { @@ -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; } @@ -40,6 +42,7 @@ export default class OpenCVManager { cv.onRuntimeInitialized = () => { if (this.opencvLoadedResolve !== null) { this.opencvLoadedResolve(); + this.isOpenCVLoaded = true; } }; }; @@ -53,6 +56,11 @@ export default class OpenCVManager { } public onLoad(callback: (error?: Error) => Promise): void { + if (!this.isOpenCVLoaded) { + void callback(new Error('OpenCV is not fully loaded yet')); + return; + } + this.opencvLoadedPromise .then(async () => { await callback(); @@ -68,5 +76,6 @@ export default class OpenCVManager { } OpenCVManager.instance = null; delete (global as any).cv; + this.isOpenCVLoaded = false; } } From dee23e3e72209765c4c6bfe7e3033cbc949e84cc Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Fri, 6 Sep 2024 08:38:07 +0200 Subject: [PATCH 2/3] CAPI-269(test): do not process image if opencv has not finished loading --- tests/unit/OpenCVManager.test.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/unit/OpenCVManager.test.ts b/tests/unit/OpenCVManager.test.ts index 4123a66..9375446 100644 --- a/tests/unit/OpenCVManager.test.ts +++ b/tests/unit/OpenCVManager.test.ts @@ -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); @@ -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); @@ -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)); + }); + }); }); From 90d279d2be59c94ad9062ec52c253ad47379f72c Mon Sep 17 00:00:00 2001 From: Aditya Singh Date: Fri, 6 Sep 2024 08:46:17 +0200 Subject: [PATCH 3/3] CAPI-269(chore): bump to v2.0.1 --- CHANGELOG.md | 4 ++++ package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b5f3953..c194aaf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/package.json b/package.json index 5da9dd8..9b0e662 100644 --- a/package.json +++ b/package.json @@ -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",