Skip to content

Commit

Permalink
fix(🐛): improve Canvas unmounting (#2619)
Browse files Browse the repository at this point in the history
So far Canvas unmounting was treated as a regular SkiaDOM update where the drawing is simply empty. But we want to keep the last frame visible until the view is destroyed.
  • Loading branch information
wcandillon authored Sep 10, 2024
1 parent 5938d7b commit d51beb3
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 12 deletions.
1 change: 1 addition & 0 deletions packages/skia/src/renderer/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Skia } from "../skia/types";
export class Container {
private _root: RenderNode<GroupProps>;
public Sk: SkDOM;
public unmounted = false;
constructor(
Skia: Skia,
public redraw: () => void = () => {},
Expand Down
10 changes: 7 additions & 3 deletions packages/skia/src/renderer/HostConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ const appendNode = (parent: Node<unknown>, child: Node<unknown>) => {
parent.addChild(child);
};

const removeNode = (parent: Node<unknown>, child: Node<unknown>) => {
const removeNode = (parent: Node<unknown>, child: Node<unknown>, unmounted = false) => {
// If the drawing is unmounted we don't want to update it.
// We can just stop the reanimated mappers
unbindReanimatedNode(child);
return parent.removeChild(child);
if (!unmounted) {
parent.removeChild(child);
}
};

const insertBefore = (
Expand Down Expand Up @@ -224,7 +228,7 @@ export const skHostConfig: SkiaHostConfig = {
},

removeChildFromContainer: (container, child) => {
removeNode(container.root, child);
removeNode(container.root, child, container.unmounted);
},

insertInContainerBefore: (container, child, before) => {
Expand Down
1 change: 1 addition & 0 deletions packages/skia/src/renderer/Reconciler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class SkiaRoot {
}

unmount() {
this.container.unmounted = true;
skiaReconciler.updateContainer(null, this.root, null, () => {
hostDebug("unmountContainer");
});
Expand Down
8 changes: 0 additions & 8 deletions packages/skia/src/views/SkiaDomView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,6 @@ See: https://shopify.github.io/react-native-skia/docs/animations/gestures`
SkiaViewApi.requestRedraw(this._nativeId);
}

/**
* Clear up the dom node when unmounting to release resources.
*/
componentWillUnmount(): void {
assertSkiaViewApi();
SkiaViewApi.setJsiProperty(this._nativeId, "root", null);
}

render() {
const { mode, debug = false, ...viewProps } = this.props;
return (
Expand Down
2 changes: 1 addition & 1 deletion turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"!apps/*/src/app/build"
],
"outputs": [
"apps/*/ios/android/**"
"apps/*/android/**"
]
},
"build:ios": {
Expand Down

0 comments on commit d51beb3

Please sign in to comment.