From ca185b6932d92a23e041cc6fcb314c03f703c3bd Mon Sep 17 00:00:00 2001 From: Osei Fortune Date: Fri, 12 Jan 2024 19:48:48 -0400 Subject: [PATCH] fix: extra guards --- packages/canvas/Canvas/index.android.ts | 14 +++++++++++++- packages/canvas/Canvas/index.ios.ts | 10 +++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/canvas/Canvas/index.android.ts b/packages/canvas/Canvas/index.android.ts index bd5f8440a..6691373c9 100644 --- a/packages/canvas/Canvas/index.android.ts +++ b/packages/canvas/Canvas/index.android.ts @@ -106,6 +106,10 @@ export class Canvas extends CanvasBase { if (this.getMeasuredWidth() > 0) { return this.getMeasuredWidth() / Screen.mainScreen.scale; } + if (this._canvas === undefined || this._canvas === null) { + return 0; + } + const width = this._canvas.getWidth(); if (width === 0) { let rootParams = this._canvas.getLayoutParams(); @@ -129,6 +133,9 @@ export class Canvas extends CanvasBase { if (this.getMeasuredHeight() > 0) { return this.getMeasuredHeight() / Screen.mainScreen.scale; } + if (this._canvas === undefined || this._canvas === null) { + return 0; + } const height = this._canvas.getHeight(); if (height === 0) { let rootParams = this._canvas.getLayoutParams(); @@ -285,6 +292,11 @@ export class Canvas extends CanvasBase { if ((typeof this.width === 'string' && this.width.indexOf('%')) || (typeof this.height === 'string' && this.height.indexOf('%'))) { return; } + + if (this._canvas === undefined || this._canvas === null) { + return; + } + const size = this._realSize; org.nativescript.canvas.NSCCanvas.layoutView(size.width || 0, size.height || 0, this._canvas); @@ -382,7 +394,7 @@ export class Canvas extends CanvasBase { private get _boundingClientRect() { if (this._jsBuffer === undefined) { this._jsBuffer = new Float32Array(8); - this._canvas.setBoundsBuffer(this._jsBuffer); + this._canvas?.setBoundsBuffer?.(this._jsBuffer); } return this._jsBuffer; } diff --git a/packages/canvas/Canvas/index.ios.ts b/packages/canvas/Canvas/index.ios.ts index 5c444df56..32beaefdf 100644 --- a/packages/canvas/Canvas/index.ios.ts +++ b/packages/canvas/Canvas/index.ios.ts @@ -239,7 +239,7 @@ export class Canvas extends CanvasBase { } disposeNativeView(): void { - this._canvas.setListener(null); + this._canvas?.setListener?.(null); this._readyListener = undefined; this._canvas = undefined; super.disposeNativeView(); @@ -263,6 +263,10 @@ export class Canvas extends CanvasBase { return; } + if (this._canvas === undefined || this._canvas === null) { + return 0; + } + const size = this._realSize; // todo revisit @@ -273,7 +277,7 @@ export class Canvas extends CanvasBase { this._canvas.forceLayout(size.width, size.height); if (this._is2D) { - this._2dContext.native.__resize(width, height); + this._2dContext?.native?.__resize?.(width, height); } this._didLayout = true; @@ -364,7 +368,7 @@ export class Canvas extends CanvasBase { } toDataURL(type = 'image/png', encoderOptions = 0.92) { - return this.native.__toDataURL(type, encoderOptions); + return this.native?.__toDataURL?.(type, encoderOptions); } snapshot(flip: boolean = false): ImageSource | null {