Skip to content

Commit

Permalink
fix: extra guards
Browse files Browse the repository at this point in the history
  • Loading branch information
triniwiz committed Jan 12, 2024
1 parent 4420c36 commit ca185b6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 13 additions & 1 deletion packages/canvas/Canvas/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 7 additions & 3 deletions packages/canvas/Canvas/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ca185b6

Please sign in to comment.