Skip to content
Open
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
55 changes: 55 additions & 0 deletions packages/skia/src/renderer/__tests__/CanvasServerRender.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react";
import { renderToString } from "react-dom/server";

import type { Canvas as CanvasComponent } from "../Canvas";
import type { Group as GroupComponent } from "../components";

// Resolve the platform modules to their web implementation, as a web bundler
// would. CanvasKit is not loaded, as on a server.
jest.mock("../../Platform", () => require("../../Platform/Platform.web"));
jest.mock("../../specs/SkiaPictureViewNativeComponent", () =>
require("../../specs/SkiaPictureViewNativeComponent.web")
);
jest.mock("../../specs/NativeSkiaModule", () =>
require("../../specs/NativeSkiaModule.web")
);
jest.mock("../../skia/Skia", () => require("../../skia/Skia.web"));
jest.mock("../../sksg/Container", () => require("../../sksg/Container.web"));

// What a reconciler hooks itself into when it is built: it wraps
// onStartTransitionFinish (S) around the one already there.
const internals = (
React as unknown as {
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE: {
S: unknown;
};
}
).__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;

// A server that evaluates the bundle once per request against a single React
// instance (the Expo dev server, for web) keeps whatever an evaluation hooks
// into React, and the whole bundle behind it, for the lifetime of the process
// (#4544). Importing the library and rendering a Canvas to HTML must leave
// React as they found it: the reconciler is built by the first render into a
// Canvas, from a layout effect, which a server never runs.
describe("Canvas server render", () => {
it("imports and renders to HTML without building the reconciler", () => {
const untouched = internals.S;
// Installs global.SkiaViewApi
require("../../specs/NativeSkiaModule.web");
const { Canvas } = require("../Canvas") as {
Canvas: typeof CanvasComponent;
};
const { Group } = require("../components") as {
Group: typeof GroupComponent;
};
expect(internals.S).toBe(untouched);
const html = renderToString(
<Canvas style={{ width: 256, height: 256 }}>
<Group />
</Canvas>
);
expect(html).toContain("<canvas");
expect(internals.S).toBe(untouched);
});
});
70 changes: 53 additions & 17 deletions packages/skia/src/sksg/Reconciler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,61 @@ import { createContainer } from "./Container";

import "./Elements";

const skiaReconciler = ReactReconciler(sksgHostConfig);
const createReconciler = () => {
const reconciler = ReactReconciler(sksgHostConfig);
// @ts-expect-error DefinitelyTyped is not up to date
reconciler.injectIntoDevTools();
return reconciler;
};

// @ts-expect-error DefinitelyTyped is not up to date
skiaReconciler.injectIntoDevTools();
let skiaReconciler: ReturnType<typeof createReconciler> | null = null;

/**
* The reconciler is built by the first render, not when this module is
* evaluated: building one hooks it into the React instance for the lifetime
* of the process (ReactSharedInternals.S, the DevTools hook). A server that
* evaluates the bundle once per request against a single React instance, as
* the Expo dev server does for web, would otherwise keep one reconciler, and
* the whole bundle behind it, per request.
*/
const getReconciler = () => {
if (skiaReconciler === null) {
skiaReconciler = createReconciler();
}
return skiaReconciler;
};

export class SkiaSGRoot {
private root: OpaqueRoot;
private root: OpaqueRoot | null = null;
private container: Container;

constructor(
public Skia: Skia,
nativeId = -1
) {
const strictMode = false;
this.container = createContainer(Skia, nativeId);
this.root = skiaReconciler.createContainer(
this.container,
0,
null,
strictMode,
null,
"",
console.error,
null
);
}

/**
* The React root, created by the first render: a Canvas constructs its
* SkiaSGRoot while it renders, which a server does too, and renders into
* it from a layout effect, which a server never runs.
*/
private getRoot() {
if (this.root === null) {
const strictMode = false;
this.root = getReconciler().createContainer(
this.container,
0,
null,
strictMode,
null,
"",
console.error,
null
);
}
return this.root;
}

get sg() {
Expand All @@ -44,8 +74,9 @@ export class SkiaSGRoot {
}

private updateContainer(element: ReactNode) {
const root = this.getRoot();
return new Promise((resolve) => {
skiaReconciler.updateContainer(element, this.root, null, () => {
getReconciler().updateContainer(element, root, null, () => {
resolve(true);
});
});
Expand All @@ -72,8 +103,13 @@ export class SkiaSGRoot {

unmount() {
this.container.unmount();
const { root } = this;
if (root === null) {
// Never rendered: React holds nothing for this root.
return Promise.resolve(true);
}
return new Promise((resolve) => {
skiaReconciler.updateContainer(null, this.root, null, () => {
getReconciler().updateContainer(null, root, null, () => {
debug("unmountContainer");
resolve(true);
});
Expand Down
46 changes: 46 additions & 0 deletions packages/skia/src/sksg/__tests__/LazyReconciler.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from "react";

import { importSkia } from "../../renderer/__tests__/setup";
import { SkiaSGRoot } from "../Reconciler";

// What a reconciler hooks itself into when it is built: it wraps
// onStartTransitionFinish (S) around the one already there.
const internals = (
React as unknown as {
__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE: {
S: unknown;
};
}
).__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE;

// In its own file: the reconciler is built once per module registry, so no
// other test may render before this one.
describe("Reconciler creation", () => {
it("leaves React untouched until the first render", async () => {
const { Skia, Group } = importSkia();
const atImport = internals.S;

// What a server render of a Canvas does: construct the root, never
// render into it.
const idle = new SkiaSGRoot(Skia);
expect(internals.S).toBe(atImport);
expect(await idle.unmount()).toBe(true);
expect(internals.S).toBe(atImport);

const root = new SkiaSGRoot(Skia);
await root.render(<Group />);
const installed = internals.S;
expect(installed).not.toBe(atImport);
expect(root.sg.children).toHaveLength(1);

// One reconciler for every root.
const other = new SkiaSGRoot(Skia);
await other.render(<Group />);
await idle.render(<Group />);
expect(internals.S).toBe(installed);
expect(idle.sg.children).toHaveLength(1);

await Promise.all([root.unmount(), other.unmount(), idle.unmount()]);
expect(internals.S).toBe(installed);
});
});
Loading