react-native-roku 0.0.0-stage → 0.2.0-preview.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +328 -0
- package/CONTRIBUTING.md +34 -0
- package/LICENSE +21 -0
- package/NOTICE.md +10 -0
- package/README.md +66 -2
- package/bin/react-native-roku.mjs +91 -0
- package/build/assets.cjs +311 -0
- package/build/assets.d.cts +50 -0
- package/build/compatibility.d.mts +40 -0
- package/build/compatibility.mjs +272 -0
- package/build/dev.d.mts +34 -0
- package/build/dev.mjs +297 -0
- package/build/devWorkspace.d.mts +11 -0
- package/build/devWorkspace.mjs +86 -0
- package/build/device.d.mts +47 -0
- package/build/device.mjs +310 -0
- package/build/environment.d.mts +29 -0
- package/build/environment.mjs +144 -0
- package/build/esbuild.d.mts +5 -0
- package/build/esbuild.mjs +118 -0
- package/build/fontVariants.d.mts +27 -0
- package/build/fontVariants.mjs +424 -0
- package/build/frontend.d.mts +52 -0
- package/build/frontend.mjs +75 -0
- package/build/identity.mjs +45 -0
- package/build/index.d.mts +22 -0
- package/build/index.mjs +87 -0
- package/build/intl.mjs +121 -0
- package/build/metro.cjs +38 -0
- package/build/metro.d.cts +37 -0
- package/build/metroFrontend.mjs +222 -0
- package/build/metroMetadata.mjs +119 -0
- package/build/metroModules.mjs +397 -0
- package/build/metroRefresh.d.mts +64 -0
- package/build/metroRefresh.mjs +346 -0
- package/build/metroTransformer.cjs +146 -0
- package/build/nativeTextMemo.mjs +296 -0
- package/build/nativewind.d.mts +9 -0
- package/build/nativewind.mjs +92 -0
- package/build/project.d.mts +111 -0
- package/build/project.mjs +615 -0
- package/build/reactCompiler.mjs +113 -0
- package/build/reactProfile.cjs +35 -0
- package/build/refreshRuntime.mjs +187 -0
- package/build/refreshSourceMap.mjs +53 -0
- package/build/release.mjs +99 -0
- package/build/resolution.d.mts +3 -0
- package/build/resolution.mjs +25 -0
- package/build/surfaces.d.mts +1 -0
- package/build/surfaces.mjs +42 -0
- package/build/testing/node.d.mts +29 -0
- package/build/testing/node.mjs +106 -0
- package/build/yoga.d.mts +8 -0
- package/build/yoga.mjs +186 -0
- package/compatibility/public-api.json +327 -0
- package/compatibility/registry.json +2258 -0
- package/config/release.json +15 -0
- package/config/toolchain.json +34 -0
- package/docs/compatibility.md +813 -0
- package/docs/getting-started.md +460 -0
- package/docs/maintenance.md +368 -0
- package/docs/qualification.md +241 -0
- package/docs/standalone-testing.md +56 -0
- package/examples/hello-roku/README.md +35 -0
- package/examples/hello-roku/package.template.json +31 -0
- package/examples/hello-roku/pnpm-workspace.yaml +11 -0
- package/examples/hello-roku/roku.config.json +22 -0
- package/examples/hello-roku/src/App.tsx +54 -0
- package/examples/hello-roku/src/Catalog.tsx +200 -0
- package/examples/hello-roku/src/assets.d.ts +16 -0
- package/examples/hello-roku/src/catalog-cover.png +0 -0
- package/examples/hello-roku/tsconfig.json +1 -0
- package/examples/hello-roku/tsconfig.react.json +1 -0
- package/native/components/RNRAppControl.brs +215 -0
- package/native/components/RNRAppControl.xml +13 -0
- package/native/components/RNRDeviceFacts.brs +19 -0
- package/native/components/RNRDeviceFacts.xml +9 -0
- package/native/components/RNRDeviceFactsTask.brs +74 -0
- package/native/components/RNRDeviceFactsTask.xml +9 -0
- package/native/components/RNRTextRaster.brs +15 -0
- package/native/components/RNRTextRaster.xml +9 -0
- package/native/components/RNRTextRasterTask.brs +77 -0
- package/native/components/RNRTextRasterTask.xml +9 -0
- package/native/manifest.json +7 -0
- package/package.json +183 -3
- package/src/animation/clock.ts +10 -0
- package/src/animation/derivedValue.ts +163 -0
- package/src/animation/legacyAnimated.ts +255 -0
- package/src/animation/reanimated.ts +243 -0
- package/src/animation/sharedValue.ts +285 -0
- package/src/animation/worklets.ts +23 -0
- package/src/application/AppRegistry.ts +31 -0
- package/src/application/mountApplication.ts +136 -0
- package/src/graphics/BitmapLayer.tsx +68 -0
- package/src/graphics/RoundedBorder.tsx +116 -0
- package/src/graphics/TextRasterCache.ts +259 -0
- package/src/graphics/gradient.ts +37 -0
- package/src/graphics/inlineRaster.ts +85 -0
- package/src/graphics/png.ts +77 -0
- package/src/graphics/rasterPixels.ts +89 -0
- package/src/graphics/surfaces.ts +105 -0
- package/src/graphics/textRaster.types.ts +60 -0
- package/src/graphics/textRasterGeometry.ts +116 -0
- package/src/host/host.types.ts +57 -0
- package/src/index.ts +22 -0
- package/src/interop/nativewind.ts +2 -0
- package/src/layout/FontResources.ts +252 -0
- package/src/layout/affine.ts +89 -0
- package/src/layout/flexLayout.ts +398 -0
- package/src/layout/fontFaces.d.mts +19 -0
- package/src/layout/fontFaces.mjs +85 -0
- package/src/layout/fontVariants.d.mts +2 -0
- package/src/layout/fontVariants.mjs +8 -0
- package/src/layout/inlineParagraph.ts +158 -0
- package/src/layout/inlineText.ts +68 -0
- package/src/layout/layoutReuse.ts +117 -0
- package/src/layout/nativeNodeFields.ts +179 -0
- package/src/layout/preparedFields.ts +78 -0
- package/src/layout/textLayout.ts +138 -0
- package/src/layout/transform.ts +113 -0
- package/src/media/SceneGraphAudio.ts +161 -0
- package/src/media/SceneGraphVideo.ts +167 -0
- package/src/media/audioSession.ts +84 -0
- package/src/media/index.ts +2 -0
- package/src/media/videoErrorDetails.ts +40 -0
- package/src/media/videoSession.ts +136 -0
- package/src/navigation/RouterStore.ts +244 -0
- package/src/navigation/expoRouter.ts +115 -0
- package/src/react-native/NativeInput.ts +494 -0
- package/src/react-native/NativeSystemState.ts +95 -0
- package/src/react-native/accessibility.ts +265 -0
- package/src/react-native/activityIndicator.ts +72 -0
- package/src/react-native/compilerPressable.ts +26 -0
- package/src/react-native/createNativeRoot.ts +2126 -0
- package/src/react-native/directionalFocus.ts +55 -0
- package/src/react-native/environment.ts +275 -0
- package/src/react-native/flatList.ts +1001 -0
- package/src/react-native/flatListViewability.ts +231 -0
- package/src/react-native/focusEvents.ts +85 -0
- package/src/react-native/image.ts +247 -0
- package/src/react-native/index.ts +54 -0
- package/src/react-native/interactionManager.ts +76 -0
- package/src/react-native/keyboard.ts +60 -0
- package/src/react-native/linearGradient.ts +94 -0
- package/src/react-native/modal.tsx +148 -0
- package/src/react-native/nativeHostContext.ts +80 -0
- package/src/react-native/nativeModules.ts +86 -0
- package/src/react-native/nativeRoot.types.ts +22 -0
- package/src/react-native/nativeViewHandle.ts +191 -0
- package/src/react-native/nativewindPressable.ts +38 -0
- package/src/react-native/panResponder.ts +73 -0
- package/src/react-native/primitives.ts +685 -0
- package/src/react-native/safeArea.ts +76 -0
- package/src/react-native/scrollView.ts +289 -0
- package/src/react-native/styleInterop.ts +8 -0
- package/src/react-native/styleSheet.ts +54 -0
- package/src/react-native/textInput.ts +334 -0
- package/src/react-native/unsupported.ts +25 -0
- package/src/renderer/SceneRoot.ts +1006 -0
- package/src/scene/SdkDeviceFacts.ts +151 -0
- package/src/scene/SdkDeviceServices.ts +72 -0
- package/src/scene/SdkSceneGraph.ts +124 -0
- package/src/scene/SdkTextEntry.ts +321 -0
- package/src/scene/SdkTextMetrics.ts +136 -0
- package/src/scene/SdkTextRasterTransport.ts +191 -0
- package/src/scene/SdkWebTransport.ts +122 -0
- package/src/scene/createRokuHost.ts +213 -0
- package/src/scene/sceneGraph.types.ts +38 -0
- package/src/services/DeviceServices.ts +53 -0
- package/src/services/RegistryStorage.ts +85 -0
- package/src/services/RokuKeySession.ts +29 -0
- package/src/services/asyncStorage.ts +23 -0
- package/src/services/base64.ts +52 -0
- package/src/services/crypto.ts +18 -0
- package/src/services/expoConstants.ts +11 -0
- package/src/services/expoLinking.ts +12 -0
- package/src/services/expoStatusBar.ts +4 -0
- package/src/services/installWebRuntime.ts +105 -0
- package/src/services/intlEnglish.ts +54 -0
- package/src/services/languageRuntime.ts +65 -0
- package/src/services/nativeTimers.ts +96 -0
- package/src/services/secureStore.ts +23 -0
- package/src/services/textEntryRequest.ts +22 -0
- package/src/services/utf8.ts +57 -0
- package/src/services/webRuntime.ts +346 -0
- package/src/testing/CanvasResources.ts +141 -0
- package/src/testing/CanvasSceneGraph.ts +496 -0
- package/src/testing/CanvasTextEntry.ts +237 -0
- package/src/testing/PreviewTextObservations.ts +82 -0
- package/src/testing/RecordingSceneGraph.ts +136 -0
- package/src/types/normalize-colors.d.ts +4 -0
- package/src/vector/dynamicSvg.tsx +226 -0
- package/src/vector/svg.ts +222 -0
- package/src/vector/svg.types.ts +4 -0
- package/src/vector/svgElements.ts +58 -0
- package/src/vector/svgKey.d.mts +4 -0
- package/src/vector/svgKey.mjs +16 -0
- package/src/vector/svgPath.ts +157 -0
- package/src/vector/svgPixels.ts +133 -0
- package/tsconfig.consumer.json +29 -0
|
@@ -0,0 +1,1006 @@
|
|
|
1
|
+
import { createContext, version as reactVersion, type ReactNode } from "react";
|
|
2
|
+
import Reconciler from "react-reconciler";
|
|
3
|
+
import { ConcurrentRoot, DefaultEventPriority, NoEventPriority } from "react-reconciler/constants";
|
|
4
|
+
import {
|
|
5
|
+
RokuHostError,
|
|
6
|
+
type SceneFields,
|
|
7
|
+
type SceneFieldListener,
|
|
8
|
+
type SceneGraphPort,
|
|
9
|
+
type SceneNodeRef,
|
|
10
|
+
} from "../scene/sceneGraph.types";
|
|
11
|
+
|
|
12
|
+
export interface SceneElementProps {
|
|
13
|
+
readonly children?: ReactNode;
|
|
14
|
+
readonly fields?: SceneFields;
|
|
15
|
+
readonly events?: Readonly<Record<string, SceneFieldListener>>;
|
|
16
|
+
readonly layout?: Readonly<Record<string, unknown>>;
|
|
17
|
+
readonly backgroundFields?: SceneFields;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SceneTreeElement {
|
|
21
|
+
readonly kind: "element";
|
|
22
|
+
readonly hidden?: boolean;
|
|
23
|
+
readonly type: string;
|
|
24
|
+
readonly props: SceneElementProps;
|
|
25
|
+
readonly handle: SceneHandle;
|
|
26
|
+
readonly children: readonly (
|
|
27
|
+
SceneTreeElement | { readonly kind: "text"; readonly text: string; readonly hidden: boolean }
|
|
28
|
+
)[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface SceneRootOptions {
|
|
32
|
+
/** Opt into React's development StrictMode effect/ref replay at the root. */
|
|
33
|
+
strictMode?: boolean;
|
|
34
|
+
prepareFields?(
|
|
35
|
+
tree: readonly SceneTreeElement[],
|
|
36
|
+
reactCommit?: boolean,
|
|
37
|
+
):
|
|
38
|
+
| ReadonlyMap<SceneTreeElement, SceneFields>
|
|
39
|
+
| Promise<ReadonlyMap<SceneTreeElement, SceneFields>>;
|
|
40
|
+
/** Layout owns line fields; the reconciler owns their native lifetime. */
|
|
41
|
+
inlineTextLines?(node: SceneTreeElement): readonly SceneFields[];
|
|
42
|
+
afterCommit?(tree: readonly SceneTreeElement[]): void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type PreparedFields = ReadonlyMap<SceneTreeElement, SceneFields>;
|
|
46
|
+
type SceneChanges = ReadonlySet<SceneTreeElement>;
|
|
47
|
+
interface PreparedSceneRootOptions extends SceneRootOptions {
|
|
48
|
+
prepareFields?(
|
|
49
|
+
tree: readonly SceneTreeElement[],
|
|
50
|
+
reactCommit?: boolean,
|
|
51
|
+
changes?: SceneChanges,
|
|
52
|
+
): PreparedFields | Promise<PreparedFields>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const stableGeometry = new WeakMap<PreparedFields, Map<SceneTreeElement, SceneFields> | null>();
|
|
56
|
+
const generatedFields = new WeakSet<SceneFields>();
|
|
57
|
+
|
|
58
|
+
/** Only the native layout owner can attest that it reused the committed geometry. */
|
|
59
|
+
export function reuseSceneGeometry(
|
|
60
|
+
fields: PreparedFields,
|
|
61
|
+
base?: Map<SceneTreeElement, SceneFields>,
|
|
62
|
+
): void {
|
|
63
|
+
stableGeometry.set(fields, base ?? null);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Renderer-owned scalar inputs, detached from application objects. */
|
|
67
|
+
export function generatedSceneFields<T extends Readonly<Record<string, boolean | number | string>>>(
|
|
68
|
+
fields: T,
|
|
69
|
+
): T {
|
|
70
|
+
generatedFields.add(fields);
|
|
71
|
+
return Object.freeze(fields);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface SceneHandle {
|
|
75
|
+
readonly nativeRef: SceneNodeRef | null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
interface TextNode {
|
|
79
|
+
readonly kind: "text";
|
|
80
|
+
text: string;
|
|
81
|
+
hidden: boolean;
|
|
82
|
+
}
|
|
83
|
+
interface ElementNode {
|
|
84
|
+
readonly kind: "element";
|
|
85
|
+
readonly type: string;
|
|
86
|
+
props: SceneElementProps;
|
|
87
|
+
readonly children: HostChild[];
|
|
88
|
+
background?: ElementNode;
|
|
89
|
+
readonly handle: SceneHandle;
|
|
90
|
+
ref: SceneNodeRef | null;
|
|
91
|
+
hidden: boolean;
|
|
92
|
+
appliedFields: SceneFields;
|
|
93
|
+
appliedAuthoredFields?: SceneFields;
|
|
94
|
+
appliedEvents?: SceneElementProps["events"];
|
|
95
|
+
inlineLines: { ref: SceneNodeRef; fields: SceneFields }[];
|
|
96
|
+
readonly subscriptions: Map<string, () => void>;
|
|
97
|
+
}
|
|
98
|
+
type HostChild = ElementNode | TextNode;
|
|
99
|
+
interface Parent {
|
|
100
|
+
readonly children: HostChild[];
|
|
101
|
+
}
|
|
102
|
+
interface RootState extends Parent {
|
|
103
|
+
readonly port: SceneGraphPort;
|
|
104
|
+
realized: Map<ElementNode, SceneNodeRef>;
|
|
105
|
+
parents: Map<ElementNode, SceneNodeRef>;
|
|
106
|
+
orders: Map<SceneNodeRef, SceneNodeRef[]>;
|
|
107
|
+
commit(reactCommit?: boolean, changes?: SceneChanges): void;
|
|
108
|
+
error: Error | null;
|
|
109
|
+
errorGeneration: number;
|
|
110
|
+
errorKind: "native" | "react" | null;
|
|
111
|
+
generation: number;
|
|
112
|
+
pending: Promise<void>;
|
|
113
|
+
focusPresentation?: { nodes: Set<ElementNode>; timer: ReturnType<typeof setTimeout> };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const HOST_TYPES = new Set([
|
|
117
|
+
"group",
|
|
118
|
+
"rectangle",
|
|
119
|
+
"label",
|
|
120
|
+
"multiStyleLabel",
|
|
121
|
+
"inlineText",
|
|
122
|
+
"poster",
|
|
123
|
+
"font",
|
|
124
|
+
"video",
|
|
125
|
+
"audio",
|
|
126
|
+
"contentNode",
|
|
127
|
+
"effect",
|
|
128
|
+
"maskGroup",
|
|
129
|
+
]);
|
|
130
|
+
const references = new WeakMap<object, SceneHandle>();
|
|
131
|
+
const elementsByHandle = new WeakMap<SceneHandle, ElementNode>();
|
|
132
|
+
const opacityUpdates = new WeakMap<SceneRoot, (handle: SceneHandle, value: number) => boolean>();
|
|
133
|
+
|
|
134
|
+
export type FocusPresentationWrite = {
|
|
135
|
+
readonly node: SceneTreeElement;
|
|
136
|
+
readonly fields: Readonly<{ color: string } | { text: string; numLines: number }>;
|
|
137
|
+
};
|
|
138
|
+
const focusPresentations = new WeakMap<
|
|
139
|
+
SceneRoot,
|
|
140
|
+
(target: SceneTreeElement, writes: readonly FocusPresentationWrite[]) => boolean
|
|
141
|
+
>();
|
|
142
|
+
|
|
143
|
+
/** Submit layout-owner-approved fields without synthesizing a focus observation. */
|
|
144
|
+
export function trySubmitFocusPresentation(
|
|
145
|
+
root: SceneRoot,
|
|
146
|
+
target: SceneTreeElement,
|
|
147
|
+
writes: readonly FocusPresentationWrite[],
|
|
148
|
+
): boolean {
|
|
149
|
+
return focusPresentations.get(root)?.(target, writes) ?? false;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Internal native-host bridge. Decline unless the entire native commit is stable. */
|
|
153
|
+
export function tryUpdateCommittedOpacity(
|
|
154
|
+
root: SceneRoot,
|
|
155
|
+
handle: SceneHandle,
|
|
156
|
+
value: number,
|
|
157
|
+
): boolean {
|
|
158
|
+
return opacityUpdates.get(root)?.(handle, value) ?? false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function sceneNodeReference(handle: SceneHandle): Readonly<Record<string, unknown>> {
|
|
162
|
+
const token = Object.freeze({});
|
|
163
|
+
references.set(token, handle);
|
|
164
|
+
return token;
|
|
165
|
+
}
|
|
166
|
+
function resolveReferences(value: unknown): unknown {
|
|
167
|
+
if (value && typeof value === "object" && references.has(value)) {
|
|
168
|
+
const ref = references.get(value)!.nativeRef;
|
|
169
|
+
if (!ref)
|
|
170
|
+
throw new RokuHostError("TREE_REFERENCE", "A field references an unrealized scene node");
|
|
171
|
+
return ref;
|
|
172
|
+
}
|
|
173
|
+
if (Array.isArray(value)) return value.map(resolveReferences);
|
|
174
|
+
if (value && typeof value === "object" && !("id" in value && "type" in value))
|
|
175
|
+
return Object.fromEntries(
|
|
176
|
+
Object.entries(value).map(([key, entry]) => [key, resolveReferences(entry)]),
|
|
177
|
+
);
|
|
178
|
+
return value;
|
|
179
|
+
}
|
|
180
|
+
const EMPTY_CONTEXT = Object.freeze({});
|
|
181
|
+
const ACTIVATION_FIELDS = new Set(["uri", "maskUri", "control"]);
|
|
182
|
+
let currentPriority: Reconciler.EventPriority = NoEventPriority;
|
|
183
|
+
|
|
184
|
+
function equalField(a: unknown, b: unknown, depth = 0): boolean {
|
|
185
|
+
if (Object.is(a, b)) return true;
|
|
186
|
+
if (depth > 32 || !a || !b || typeof a !== "object" || typeof b !== "object") return false;
|
|
187
|
+
if (Array.isArray(a) || Array.isArray(b))
|
|
188
|
+
return (
|
|
189
|
+
Array.isArray(a) &&
|
|
190
|
+
Array.isArray(b) &&
|
|
191
|
+
a.length === b.length &&
|
|
192
|
+
a.every((item, index) => equalField(item, b[index], depth + 1))
|
|
193
|
+
);
|
|
194
|
+
// Scene node references are capabilities, so retain their identity semantics.
|
|
195
|
+
if (("id" in a && "type" in a) || ("id" in b && "type" in b)) return false;
|
|
196
|
+
const entries = Object.entries(a),
|
|
197
|
+
record = b as Record<string, unknown>;
|
|
198
|
+
return (
|
|
199
|
+
entries.length === Object.keys(b).length &&
|
|
200
|
+
entries.every(
|
|
201
|
+
([key, value]) => Object.hasOwn(b, key) && equalField(value, record[key], depth + 1),
|
|
202
|
+
)
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The applied fields contain resolved values and snapshots of authored arrays.
|
|
207
|
+
// Reference tokens and general compound values need the full resolution path.
|
|
208
|
+
function sameScalarFieldInputs(fields: SceneFields, applied: SceneFields): boolean {
|
|
209
|
+
const keys = Object.keys(fields);
|
|
210
|
+
if (keys.length !== Object.keys(applied).length) return false;
|
|
211
|
+
for (const key of keys) {
|
|
212
|
+
if (!Object.hasOwn(applied, key)) return false;
|
|
213
|
+
const value = fields[key],
|
|
214
|
+
previous = applied[key];
|
|
215
|
+
if (Array.isArray(value)) {
|
|
216
|
+
if (
|
|
217
|
+
!Array.isArray(previous) ||
|
|
218
|
+
value.length !== previous.length ||
|
|
219
|
+
Object.getPrototypeOf(value) !== Array.prototype ||
|
|
220
|
+
Object.hasOwn(value, "constructor") ||
|
|
221
|
+
Object.hasOwn(value, "map")
|
|
222
|
+
)
|
|
223
|
+
return false;
|
|
224
|
+
for (let i = 0; i < value.length; i++) {
|
|
225
|
+
const item: unknown = value[i];
|
|
226
|
+
if (
|
|
227
|
+
!Object.hasOwn(value, i) ||
|
|
228
|
+
(item !== null && typeof item === "object") ||
|
|
229
|
+
!Object.is(item, previous[i])
|
|
230
|
+
)
|
|
231
|
+
return false;
|
|
232
|
+
}
|
|
233
|
+
} else if (!Object.is(value, previous)) {
|
|
234
|
+
return false;
|
|
235
|
+
} else if (
|
|
236
|
+
value &&
|
|
237
|
+
typeof value === "object" &&
|
|
238
|
+
(references.has(value) || !("id" in value && "type" in value))
|
|
239
|
+
) {
|
|
240
|
+
return false;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const textParents = new WeakMap<TextNode, ElementNode | RootState>();
|
|
247
|
+
// React performs the mutation phase synchronously between these two commit hooks.
|
|
248
|
+
let mutations: { structural: boolean; nodes: Set<SceneTreeElement> } | undefined;
|
|
249
|
+
|
|
250
|
+
function noteStructureChange(): void {
|
|
251
|
+
if (mutations) mutations.structural = true;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
function insert(parent: ElementNode | RootState, child: HostChild, before?: HostChild): void {
|
|
255
|
+
if (before === child) return;
|
|
256
|
+
const existing = parent.children.indexOf(child);
|
|
257
|
+
if (existing >= 0) parent.children.splice(existing, 1);
|
|
258
|
+
const index = before ? parent.children.indexOf(before) : parent.children.length;
|
|
259
|
+
if (index < 0)
|
|
260
|
+
throw new RokuHostError("TREE_ANCHOR", "React requested an absent insertion anchor");
|
|
261
|
+
parent.children.splice(index, 0, child);
|
|
262
|
+
if (child.kind === "text") textParents.set(child, parent);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function insertCommitted(
|
|
266
|
+
parent: ElementNode | RootState,
|
|
267
|
+
child: HostChild,
|
|
268
|
+
before?: HostChild,
|
|
269
|
+
): void {
|
|
270
|
+
noteStructureChange();
|
|
271
|
+
insert(parent, child, before);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function remove(parent: Parent, child: HostChild): void {
|
|
275
|
+
const index = parent.children.indexOf(child);
|
|
276
|
+
if (index < 0)
|
|
277
|
+
throw new RokuHostError("TREE_PARENT", "React requested removal from an unrelated parent");
|
|
278
|
+
parent.children.splice(index, 1);
|
|
279
|
+
if (child.kind === "text" && textParents.get(child) === parent) textParents.delete(child);
|
|
280
|
+
noteStructureChange();
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function updateFields(
|
|
284
|
+
root: RootState,
|
|
285
|
+
child: ElementNode,
|
|
286
|
+
ref: SceneNodeRef,
|
|
287
|
+
layoutFields: SceneFields | undefined,
|
|
288
|
+
): SceneFields | undefined {
|
|
289
|
+
const { port } = root;
|
|
290
|
+
const authored = child.props.fields;
|
|
291
|
+
const inputs = { ...authored, ...layoutFields };
|
|
292
|
+
const rawLabel =
|
|
293
|
+
child.type === "label" && child.children.length > 0 && !("text" in (layoutFields ?? {}));
|
|
294
|
+
const hasVisibility = !["font", "contentNode", "effect", "audio"].includes(child.type);
|
|
295
|
+
// Only fold visibility into the raw input when it needs no reference resolution.
|
|
296
|
+
const simpleVisibility = inputs.visible == null || typeof inputs.visible !== "object";
|
|
297
|
+
const reuse = !rawLabel && simpleVisibility && (!("id" in inputs) || !("type" in inputs));
|
|
298
|
+
if (reuse && hasVisibility) inputs.visible = !child.hidden && inputs.visible !== false;
|
|
299
|
+
let fields: Record<string, import("../scene/sceneGraph.types").SceneValue> | undefined;
|
|
300
|
+
if (!reuse || !sameScalarFieldInputs(inputs, child.appliedFields)) {
|
|
301
|
+
fields = resolveReferences(inputs) as Record<
|
|
302
|
+
string,
|
|
303
|
+
import("../scene/sceneGraph.types").SceneValue
|
|
304
|
+
>;
|
|
305
|
+
if (rawLabel) {
|
|
306
|
+
if (child.children.some((item) => item.kind !== "text"))
|
|
307
|
+
throw new RokuHostError("LABEL_CHILD", "Raw SceneGraph labels admit text children only");
|
|
308
|
+
fields = {
|
|
309
|
+
...fields,
|
|
310
|
+
text: child.children
|
|
311
|
+
.map((item) => (item.kind === "text" && !item.hidden ? item.text : ""))
|
|
312
|
+
.join(""),
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
if (!reuse && hasVisibility)
|
|
316
|
+
fields = {
|
|
317
|
+
...fields,
|
|
318
|
+
visible: !child.hidden && fields.visible !== false,
|
|
319
|
+
};
|
|
320
|
+
for (const field of Object.keys(child.appliedFields))
|
|
321
|
+
if (!(field in fields)) port.set(ref, field, null);
|
|
322
|
+
// Loading/playing can begin as soon as these fields are written. Install
|
|
323
|
+
// subscriptions and geometry first, including for cached local images.
|
|
324
|
+
for (const [field, value] of Object.entries(fields)) {
|
|
325
|
+
if (ACTIVATION_FIELDS.has(field)) continue;
|
|
326
|
+
if (!equalField(value, child.appliedFields[field])) port.set(ref, field, value);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
const eventInputs = child.props.events;
|
|
330
|
+
const events = eventInputs ?? {};
|
|
331
|
+
for (const [field, unsubscribe] of child.subscriptions) {
|
|
332
|
+
if (!(field in events)) {
|
|
333
|
+
unsubscribe();
|
|
334
|
+
child.subscriptions.delete(field);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
for (const field of Object.keys(events)) {
|
|
338
|
+
if (!child.subscriptions.has(field)) {
|
|
339
|
+
child.subscriptions.set(
|
|
340
|
+
field,
|
|
341
|
+
port.subscribe(ref, field, (value) => {
|
|
342
|
+
if (child.ref === ref && root.realized.get(child) === ref)
|
|
343
|
+
child.props.events?.[field]?.(value);
|
|
344
|
+
}),
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
child.appliedAuthoredFields = authored;
|
|
349
|
+
child.appliedEvents = eventInputs;
|
|
350
|
+
return fields;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function applyActivationFields(
|
|
354
|
+
port: SceneGraphPort,
|
|
355
|
+
child: ElementNode,
|
|
356
|
+
ref: SceneNodeRef,
|
|
357
|
+
fields: SceneFields | undefined,
|
|
358
|
+
): void {
|
|
359
|
+
if (!fields) return;
|
|
360
|
+
for (const field of ACTIVATION_FIELDS)
|
|
361
|
+
if (field in fields && !equalField(fields[field], child.appliedFields[field]))
|
|
362
|
+
port.set(ref, field, fields[field]);
|
|
363
|
+
child.appliedFields = fields;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** These generated host nodes carry immutable scalar native inputs. */
|
|
367
|
+
export function hasGeneratedSceneFields(node: SceneTreeElement): boolean {
|
|
368
|
+
const { fields, events } = node.props;
|
|
369
|
+
return (
|
|
370
|
+
!!fields &&
|
|
371
|
+
generatedFields.has(fields) &&
|
|
372
|
+
(!events || Object.isFrozen(events)) &&
|
|
373
|
+
(node.type === "group" ||
|
|
374
|
+
node.type === "rectangle" ||
|
|
375
|
+
node.type === "label" ||
|
|
376
|
+
(node.type === "poster" && node.props.layout === plainImageLayout))
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
function commitStableFields(
|
|
381
|
+
root: RootState,
|
|
382
|
+
previous: PreparedFields,
|
|
383
|
+
prepared: PreparedFields,
|
|
384
|
+
changes: SceneChanges,
|
|
385
|
+
sparse: boolean,
|
|
386
|
+
): boolean {
|
|
387
|
+
if ((sparse ? previous : prepared).size !== root.realized.size) return false;
|
|
388
|
+
const affected: ElementNode[] = [];
|
|
389
|
+
// Full commits retain nodes in native sibling order. Keep that write order
|
|
390
|
+
// without allocating nodes, sorting siblings or rebuilding the ownership maps.
|
|
391
|
+
for (const node of root.realized.keys()) {
|
|
392
|
+
const previewed = root.focusPresentation?.nodes.has(node);
|
|
393
|
+
const changed = prepared.get(node);
|
|
394
|
+
if (sparse && !changed && !previewed && !changes.has(node)) continue;
|
|
395
|
+
const before = previous.get(node);
|
|
396
|
+
const next = changed ?? (sparse ? before : undefined);
|
|
397
|
+
if (!hasGeneratedSceneFields(node) || !next || !before) return false;
|
|
398
|
+
if (root.focusPresentation) {
|
|
399
|
+
const inputs = { ...node.props.fields, ...next };
|
|
400
|
+
if (inputs.visible == null || typeof inputs.visible !== "object")
|
|
401
|
+
inputs.visible = !node.hidden && inputs.visible !== false;
|
|
402
|
+
const events = Object.keys(node.props.events ?? {});
|
|
403
|
+
if (
|
|
404
|
+
(node.type !== "label" || "text" in next) &&
|
|
405
|
+
!("type" in inputs && "id" in inputs) &&
|
|
406
|
+
sameScalarFieldInputs(inputs, node.appliedFields) &&
|
|
407
|
+
events.length === node.subscriptions.size &&
|
|
408
|
+
events.every((field) => node.subscriptions.has(field))
|
|
409
|
+
)
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
// A prepared change may come from a different node or from native font
|
|
413
|
+
// readiness. A reconciler mutation alone is insufficient to identify it.
|
|
414
|
+
const authored = node.props.fields!;
|
|
415
|
+
const applied = node.appliedAuthoredFields;
|
|
416
|
+
const authoredChanged =
|
|
417
|
+
changes.has(node) &&
|
|
418
|
+
(!applied ||
|
|
419
|
+
!generatedFields.has(applied) ||
|
|
420
|
+
node.props.events !== node.appliedEvents ||
|
|
421
|
+
(node.type === "label" && !("text" in next)) ||
|
|
422
|
+
(authored !== applied && !sameScalarFieldInputs(authored, applied)));
|
|
423
|
+
if (previewed || authoredChanged || (next !== before && !sameScalarFieldInputs(next, before)))
|
|
424
|
+
affected.push(node);
|
|
425
|
+
}
|
|
426
|
+
if (affected.length)
|
|
427
|
+
root.port.batch(() => {
|
|
428
|
+
for (const node of affected) {
|
|
429
|
+
const ref = root.realized.get(node)!;
|
|
430
|
+
applyActivationFields(
|
|
431
|
+
root.port,
|
|
432
|
+
node,
|
|
433
|
+
ref,
|
|
434
|
+
updateFields(
|
|
435
|
+
root,
|
|
436
|
+
node,
|
|
437
|
+
ref,
|
|
438
|
+
prepared.get(node) ?? (sparse ? previous.get(node) : undefined),
|
|
439
|
+
),
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
return true;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function commitTree(
|
|
447
|
+
root: RootState,
|
|
448
|
+
prepared: ReadonlyMap<SceneTreeElement, SceneFields> = new Map(),
|
|
449
|
+
inlineTextLines?: SceneRootOptions["inlineTextLines"],
|
|
450
|
+
): void {
|
|
451
|
+
const { port } = root;
|
|
452
|
+
const realized = new Map<ElementNode, SceneNodeRef>(),
|
|
453
|
+
parents = new Map<ElementNode, SceneNodeRef>();
|
|
454
|
+
const orders = new Map<SceneNodeRef, SceneNodeRef[]>();
|
|
455
|
+
port.batch(() => {
|
|
456
|
+
const allocate = (parent: Parent) => {
|
|
457
|
+
for (const child of parent.children)
|
|
458
|
+
if (child.kind === "element") {
|
|
459
|
+
child.ref ??= port.create(child.type === "inlineText" ? "group" : child.type);
|
|
460
|
+
if (
|
|
461
|
+
child.type !== "label" &&
|
|
462
|
+
child.type !== "multiStyleLabel" &&
|
|
463
|
+
child.type !== "inlineText" &&
|
|
464
|
+
!child.props.layout?.textRaster
|
|
465
|
+
)
|
|
466
|
+
allocate(child);
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
allocate(root);
|
|
470
|
+
const visit = (parent: Parent, parentRef: SceneNodeRef) => {
|
|
471
|
+
const children = parent.children
|
|
472
|
+
.filter((child): child is ElementNode => child.kind === "element")
|
|
473
|
+
.map((node, index) => ({
|
|
474
|
+
node,
|
|
475
|
+
index,
|
|
476
|
+
z: Number((node.props.layout?.style as Record<string, unknown> | undefined)?.zIndex ?? 0),
|
|
477
|
+
}))
|
|
478
|
+
.sort((a, b) => a.z - b.z || a.index - b.index)
|
|
479
|
+
.map((entry) => entry.node);
|
|
480
|
+
for (const child of parent.children) {
|
|
481
|
+
if (child.kind === "text" && child.text.trim())
|
|
482
|
+
throw new RokuHostError("RAW_TEXT", "Scene text must be inside a label");
|
|
483
|
+
}
|
|
484
|
+
for (const child of children) {
|
|
485
|
+
const ref = (child.ref ??= port.create(child.type === "inlineText" ? "group" : child.type));
|
|
486
|
+
realized.set(child, ref);
|
|
487
|
+
parents.set(child, parentRef);
|
|
488
|
+
const fields = updateFields(root, child, ref, prepared.get(child));
|
|
489
|
+
if (child.type === "inlineText") {
|
|
490
|
+
const lines = inlineTextLines?.(child);
|
|
491
|
+
if (!lines)
|
|
492
|
+
throw new RokuHostError(
|
|
493
|
+
"C04_INLINE_LAYOUT",
|
|
494
|
+
"Inline paragraphs require measured native lines",
|
|
495
|
+
);
|
|
496
|
+
const previous = child.inlineLines;
|
|
497
|
+
child.inlineLines = lines.map((fields, index) => {
|
|
498
|
+
const line = previous[index] ?? { ref: port.create("multiStyleLabel"), fields: {} };
|
|
499
|
+
for (const name of Object.keys(line.fields))
|
|
500
|
+
if (!(name in fields)) port.set(line.ref, name, null);
|
|
501
|
+
for (const [name, value] of Object.entries(fields))
|
|
502
|
+
if (!equalField(value, line.fields[name])) port.set(line.ref, name, value);
|
|
503
|
+
if (!previous[index]) port.insert(ref, line.ref);
|
|
504
|
+
line.fields = fields;
|
|
505
|
+
return line;
|
|
506
|
+
});
|
|
507
|
+
for (const line of previous.slice(lines.length)) port.remove(ref, line.ref);
|
|
508
|
+
orders.set(
|
|
509
|
+
ref,
|
|
510
|
+
child.inlineLines.map((line) => line.ref),
|
|
511
|
+
);
|
|
512
|
+
} else if (
|
|
513
|
+
child.type !== "label" &&
|
|
514
|
+
child.type !== "multiStyleLabel" &&
|
|
515
|
+
!child.props.layout?.textRaster
|
|
516
|
+
)
|
|
517
|
+
visit(child, ref);
|
|
518
|
+
applyActivationFields(port, child, ref, fields);
|
|
519
|
+
}
|
|
520
|
+
// Reparent/reorder live nodes before removing obsolete ancestors.
|
|
521
|
+
const order = children.map((child) => child.ref!);
|
|
522
|
+
orders.set(parentRef, order);
|
|
523
|
+
const previous = root.orders.get(parentRef);
|
|
524
|
+
if (
|
|
525
|
+
!previous ||
|
|
526
|
+
previous.length !== order.length ||
|
|
527
|
+
previous.some((ref, index) => ref !== order[index])
|
|
528
|
+
) {
|
|
529
|
+
const desired = new Set(order),
|
|
530
|
+
working = previous?.filter((ref) => desired.has(ref)) ?? [];
|
|
531
|
+
let before: SceneNodeRef | undefined;
|
|
532
|
+
for (let index = order.length - 1; index >= 0; index--) {
|
|
533
|
+
const ref = order[index],
|
|
534
|
+
current = working.indexOf(ref);
|
|
535
|
+
if (current < 0 || working[current + 1] !== before) {
|
|
536
|
+
port.insert(parentRef, ref, before);
|
|
537
|
+
if (current >= 0) working.splice(current, 1);
|
|
538
|
+
const position = before ? working.indexOf(before) : working.length;
|
|
539
|
+
working.splice(position, 0, ref);
|
|
540
|
+
}
|
|
541
|
+
before = ref;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
visit(root, port.root);
|
|
546
|
+
const liveRefs = new Set(realized.values());
|
|
547
|
+
// Stop decoders before removing any ancestor (ports may retire descendants
|
|
548
|
+
// immediately). A write requests release; it is not a stop acknowledgement.
|
|
549
|
+
for (const [node, ref] of root.realized)
|
|
550
|
+
if (!realized.has(node) && (node.type === "video" || node.type === "audio"))
|
|
551
|
+
port.set(ref, "control", "stop");
|
|
552
|
+
for (const [node, ref] of root.realized) {
|
|
553
|
+
if (realized.has(node)) continue;
|
|
554
|
+
for (const unsubscribe of node.subscriptions.values()) unsubscribe();
|
|
555
|
+
node.subscriptions.clear();
|
|
556
|
+
const parentRef = root.parents.get(node)!;
|
|
557
|
+
if (parentRef === port.root || liveRefs.has(parentRef)) port.remove(parentRef, ref);
|
|
558
|
+
node.ref = null;
|
|
559
|
+
node.inlineLines = [];
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
root.realized = realized;
|
|
563
|
+
root.parents = parents;
|
|
564
|
+
root.orders = orders;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
type HostConfig = Reconciler.HostConfig<
|
|
568
|
+
string,
|
|
569
|
+
SceneElementProps,
|
|
570
|
+
RootState,
|
|
571
|
+
ElementNode,
|
|
572
|
+
TextNode,
|
|
573
|
+
never,
|
|
574
|
+
never,
|
|
575
|
+
never,
|
|
576
|
+
SceneHandle,
|
|
577
|
+
Readonly<Record<string, never>>,
|
|
578
|
+
never,
|
|
579
|
+
ReturnType<typeof setTimeout>,
|
|
580
|
+
-1,
|
|
581
|
+
null
|
|
582
|
+
>;
|
|
583
|
+
|
|
584
|
+
declare const __DEV__: boolean;
|
|
585
|
+
|
|
586
|
+
// Plain group paint has the same placement and lifetime as a decoration
|
|
587
|
+
// child, without scheduling a separate React element for every style callback.
|
|
588
|
+
export const plainBackgroundLayout = Object.freeze({ decoration: "background" });
|
|
589
|
+
// A square-corner Image poster paints inside its semantic wrapper.
|
|
590
|
+
export const plainImageLayout = Object.freeze({ decoration: "background" });
|
|
591
|
+
|
|
592
|
+
function sceneNode(type: string, props: SceneElementProps): ElementNode {
|
|
593
|
+
const node: ElementNode = {
|
|
594
|
+
kind: "element",
|
|
595
|
+
type,
|
|
596
|
+
props,
|
|
597
|
+
children: [],
|
|
598
|
+
ref: null,
|
|
599
|
+
hidden: false,
|
|
600
|
+
appliedFields: {},
|
|
601
|
+
inlineLines: [],
|
|
602
|
+
subscriptions: new Map(),
|
|
603
|
+
handle: {
|
|
604
|
+
get nativeRef() {
|
|
605
|
+
return node.ref;
|
|
606
|
+
},
|
|
607
|
+
},
|
|
608
|
+
};
|
|
609
|
+
elementsByHandle.set(node.handle, node);
|
|
610
|
+
return node;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
function updateBackground(node: ElementNode, fields: SceneFields | undefined): void {
|
|
614
|
+
const background = node.background;
|
|
615
|
+
if (fields && background) {
|
|
616
|
+
if (background.props.fields !== fields) {
|
|
617
|
+
background.props = { fields, layout: plainBackgroundLayout };
|
|
618
|
+
mutations?.nodes.add(background);
|
|
619
|
+
}
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
if (fields) {
|
|
623
|
+
const background = sceneNode("rectangle", { fields, layout: plainBackgroundLayout });
|
|
624
|
+
node.background = background;
|
|
625
|
+
node.children.unshift(background);
|
|
626
|
+
noteStructureChange();
|
|
627
|
+
} else if (background) {
|
|
628
|
+
node.background = undefined;
|
|
629
|
+
remove(node, background);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
const configuration: HostConfig & { rendererVersion: string; rendererPackageName: string } = {
|
|
634
|
+
rendererVersion: reactVersion,
|
|
635
|
+
rendererPackageName: "react-native-roku",
|
|
636
|
+
supportsMutation: true,
|
|
637
|
+
supportsPersistence: false,
|
|
638
|
+
supportsHydration: false,
|
|
639
|
+
isPrimaryRenderer: true,
|
|
640
|
+
createInstance(type, props) {
|
|
641
|
+
if (!HOST_TYPES.has(type))
|
|
642
|
+
throw new RokuHostError("UNSUPPORTED_NODE", `Scene node ${type} has no admitted renderer`);
|
|
643
|
+
const node = sceneNode(type, props);
|
|
644
|
+
updateBackground(node, props.backgroundFields);
|
|
645
|
+
return node;
|
|
646
|
+
},
|
|
647
|
+
createTextInstance: (text) => ({ kind: "text", text, hidden: false }),
|
|
648
|
+
appendInitialChild: insert,
|
|
649
|
+
finalizeInitialChildren: () => false,
|
|
650
|
+
shouldSetTextContent: () => false,
|
|
651
|
+
getRootHostContext: () => EMPTY_CONTEXT,
|
|
652
|
+
getChildHostContext: (context) => context,
|
|
653
|
+
getPublicInstance: (instance) =>
|
|
654
|
+
instance.kind === "element" ? instance.handle : { nativeRef: null },
|
|
655
|
+
prepareForCommit: () => {
|
|
656
|
+
mutations = { structural: false, nodes: new Set() };
|
|
657
|
+
return null;
|
|
658
|
+
},
|
|
659
|
+
resetAfterCommit(root) {
|
|
660
|
+
const changes = mutations;
|
|
661
|
+
mutations = undefined;
|
|
662
|
+
root.commit(true, changes?.structural === false ? changes.nodes : undefined);
|
|
663
|
+
},
|
|
664
|
+
preparePortalMount: () => undefined,
|
|
665
|
+
scheduleTimeout: setTimeout,
|
|
666
|
+
cancelTimeout: clearTimeout,
|
|
667
|
+
noTimeout: -1,
|
|
668
|
+
supportsMicrotasks: true,
|
|
669
|
+
scheduleMicrotask: (callback) => {
|
|
670
|
+
void Promise.resolve().then(callback);
|
|
671
|
+
},
|
|
672
|
+
getInstanceFromNode: () => null,
|
|
673
|
+
beforeActiveInstanceBlur: () => undefined,
|
|
674
|
+
afterActiveInstanceBlur: () => undefined,
|
|
675
|
+
prepareScopeUpdate: () => {
|
|
676
|
+
throw new RokuHostError("UNSUPPORTED_SCOPE", "React Scope requires an adapter");
|
|
677
|
+
},
|
|
678
|
+
getInstanceFromScope: () => null,
|
|
679
|
+
detachDeletedInstance: () => undefined,
|
|
680
|
+
appendChild: insertCommitted,
|
|
681
|
+
appendChildToContainer: insertCommitted,
|
|
682
|
+
insertBefore: insertCommitted,
|
|
683
|
+
insertInContainerBefore: insertCommitted,
|
|
684
|
+
removeChild: remove,
|
|
685
|
+
removeChildFromContainer: remove,
|
|
686
|
+
resetTextContent: (node) => {
|
|
687
|
+
noteStructureChange();
|
|
688
|
+
node.children.length = 0;
|
|
689
|
+
},
|
|
690
|
+
commitTextUpdate: (node, _previous, text) => {
|
|
691
|
+
const parent = textParents.get(node);
|
|
692
|
+
if (parent && "kind" in parent && parent.type === "label") mutations?.nodes.add(parent);
|
|
693
|
+
else noteStructureChange();
|
|
694
|
+
node.text = text;
|
|
695
|
+
},
|
|
696
|
+
commitUpdate: (node, _type, _previous, props) => {
|
|
697
|
+
mutations?.nodes.add(node);
|
|
698
|
+
if (node.background || props.backgroundFields) updateBackground(node, props.backgroundFields);
|
|
699
|
+
node.props = props;
|
|
700
|
+
},
|
|
701
|
+
hideInstance: (node) => {
|
|
702
|
+
noteStructureChange();
|
|
703
|
+
node.hidden = true;
|
|
704
|
+
},
|
|
705
|
+
hideTextInstance: (node) => {
|
|
706
|
+
noteStructureChange();
|
|
707
|
+
node.hidden = true;
|
|
708
|
+
},
|
|
709
|
+
unhideInstance: (node) => {
|
|
710
|
+
noteStructureChange();
|
|
711
|
+
node.hidden = false;
|
|
712
|
+
},
|
|
713
|
+
unhideTextInstance: (node) => {
|
|
714
|
+
noteStructureChange();
|
|
715
|
+
node.hidden = false;
|
|
716
|
+
},
|
|
717
|
+
clearContainer: (root) => {
|
|
718
|
+
noteStructureChange();
|
|
719
|
+
root.children.length = 0;
|
|
720
|
+
},
|
|
721
|
+
NotPendingTransition: null,
|
|
722
|
+
HostTransitionContext: createContext(null) as unknown as HostConfig["HostTransitionContext"],
|
|
723
|
+
setCurrentUpdatePriority: (priority) => {
|
|
724
|
+
currentPriority = priority;
|
|
725
|
+
},
|
|
726
|
+
getCurrentUpdatePriority: () => currentPriority,
|
|
727
|
+
resolveUpdatePriority: () => currentPriority || DefaultEventPriority,
|
|
728
|
+
resetFormInstance: () => {
|
|
729
|
+
throw new RokuHostError("UNSUPPORTED_FORM", "DOM forms are not SceneGraph nodes");
|
|
730
|
+
},
|
|
731
|
+
// React scheduling/profiling callback; this does not acknowledge physical presentation.
|
|
732
|
+
requestPostPaintCallback: (callback) => {
|
|
733
|
+
setTimeout(() => callback(Date.now()), 0);
|
|
734
|
+
},
|
|
735
|
+
shouldAttemptEagerTransition: () => false,
|
|
736
|
+
trackSchedulerEvent: () => undefined,
|
|
737
|
+
resolveEventType: () => null,
|
|
738
|
+
resolveEventTimeStamp: () => Date.now(),
|
|
739
|
+
maySuspendCommit: () => false,
|
|
740
|
+
preloadInstance: () => true,
|
|
741
|
+
startSuspendingCommit: () => undefined,
|
|
742
|
+
suspendInstance: () => undefined,
|
|
743
|
+
waitForCommitToBeReady: () => null,
|
|
744
|
+
};
|
|
745
|
+
|
|
746
|
+
const reconciler = Reconciler(configuration);
|
|
747
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
748
|
+
// Pinned reconciler 0.33 reads identity from HostConfig and takes no argument.
|
|
749
|
+
// Its published type still describes the preceding DevTools API.
|
|
750
|
+
(reconciler.injectIntoDevTools as unknown as () => boolean)();
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
export interface SceneRoot {
|
|
754
|
+
portal(element: ReactNode, key: string): ReactNode;
|
|
755
|
+
render(element: ReactNode): void;
|
|
756
|
+
runSync(work: () => void): void;
|
|
757
|
+
flush(): void;
|
|
758
|
+
refreshNative(): void;
|
|
759
|
+
whenCommitted(): Promise<void>;
|
|
760
|
+
unmount(): void;
|
|
761
|
+
readonly mounted: boolean;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
export function createSceneRoot(
|
|
765
|
+
port: SceneGraphPort,
|
|
766
|
+
onError: (error: Error, kind: "uncaught" | "caught" | "recoverable") => void = () => undefined,
|
|
767
|
+
options: SceneRootOptions = {},
|
|
768
|
+
): SceneRoot {
|
|
769
|
+
return createPreparedSceneRoot(port, onError, options);
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
export function createPreparedSceneRoot(
|
|
773
|
+
port: SceneGraphPort,
|
|
774
|
+
onError: (error: Error, kind: "uncaught" | "caught" | "recoverable") => void = () => undefined,
|
|
775
|
+
options: PreparedSceneRootOptions = {},
|
|
776
|
+
): SceneRoot {
|
|
777
|
+
let mounted = true;
|
|
778
|
+
let retiring = false;
|
|
779
|
+
let syncDepth = 0;
|
|
780
|
+
let committedGeneration = -1;
|
|
781
|
+
let committedFields: PreparedFields | undefined;
|
|
782
|
+
const state: RootState = {
|
|
783
|
+
port,
|
|
784
|
+
children: [],
|
|
785
|
+
realized: new Map(),
|
|
786
|
+
parents: new Map(),
|
|
787
|
+
orders: new Map(),
|
|
788
|
+
error: null,
|
|
789
|
+
errorGeneration: -1,
|
|
790
|
+
errorKind: null,
|
|
791
|
+
generation: 0,
|
|
792
|
+
pending: Promise.resolve(),
|
|
793
|
+
commit(reactCommit = false, changes) {
|
|
794
|
+
const generation = ++state.generation;
|
|
795
|
+
const tree = state.children.filter((child): child is ElementNode => child.kind === "element");
|
|
796
|
+
const apply = (fields: ReadonlyMap<SceneTreeElement, SceneFields>) => {
|
|
797
|
+
if (generation !== state.generation) return;
|
|
798
|
+
const preview = state.focusPresentation;
|
|
799
|
+
const base = stableGeometry.get(fields);
|
|
800
|
+
if (
|
|
801
|
+
!changes ||
|
|
802
|
+
!committedFields ||
|
|
803
|
+
committedGeneration !== generation - 1 ||
|
|
804
|
+
!stableGeometry.has(fields) ||
|
|
805
|
+
(base != null && base !== committedFields) ||
|
|
806
|
+
!commitStableFields(state, committedFields, fields, changes, base != null)
|
|
807
|
+
) {
|
|
808
|
+
const complete = base ? new Map(base) : undefined;
|
|
809
|
+
if (complete) for (const [node, next] of fields) complete.set(node, next);
|
|
810
|
+
commitTree(state, complete ?? fields, options.inlineTextLines);
|
|
811
|
+
}
|
|
812
|
+
if (base) {
|
|
813
|
+
if (generation !== state.generation) return;
|
|
814
|
+
for (const [node, next] of fields) base.set(node, next);
|
|
815
|
+
}
|
|
816
|
+
if (preview && state.focusPresentation === preview) {
|
|
817
|
+
clearTimeout(preview.timer);
|
|
818
|
+
state.focusPresentation = undefined;
|
|
819
|
+
}
|
|
820
|
+
options.afterCommit?.(tree);
|
|
821
|
+
committedGeneration = generation;
|
|
822
|
+
committedFields = base ?? fields;
|
|
823
|
+
if (
|
|
824
|
+
!retiring &&
|
|
825
|
+
state.errorGeneration < generation &&
|
|
826
|
+
(state.errorKind !== "react" || reactCommit)
|
|
827
|
+
) {
|
|
828
|
+
state.error = null;
|
|
829
|
+
state.errorKind = null;
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
const fail = (error: unknown) => {
|
|
833
|
+
if (generation !== state.generation) return;
|
|
834
|
+
state.error = error instanceof Error ? error : new Error(String(error));
|
|
835
|
+
state.errorGeneration = generation;
|
|
836
|
+
state.errorKind = "native";
|
|
837
|
+
onError(state.error, "uncaught");
|
|
838
|
+
throw state.error;
|
|
839
|
+
};
|
|
840
|
+
try {
|
|
841
|
+
const settledChanges =
|
|
842
|
+
committedGeneration === generation - 1 && !state.error ? changes : undefined;
|
|
843
|
+
const prepared = options.prepareFields?.(tree, reactCommit, settledChanges) ?? new Map();
|
|
844
|
+
if (prepared instanceof Promise) state.pending = prepared.then(apply).catch(fail);
|
|
845
|
+
else {
|
|
846
|
+
apply(prepared);
|
|
847
|
+
state.pending = Promise.resolve();
|
|
848
|
+
}
|
|
849
|
+
} catch (error) {
|
|
850
|
+
// React may commit a query/store update outside a caller's runSync.
|
|
851
|
+
// Keep preparation failures on the same observed error channel as
|
|
852
|
+
// asynchronous font/layout failures instead of throwing from a task.
|
|
853
|
+
state.pending = Promise.resolve().then(() => fail(error));
|
|
854
|
+
}
|
|
855
|
+
// Surface through whenCommitted/onError, without an unhandled background rejection.
|
|
856
|
+
void state.pending.catch(() => undefined);
|
|
857
|
+
},
|
|
858
|
+
};
|
|
859
|
+
const container = reconciler.createContainer(
|
|
860
|
+
state,
|
|
861
|
+
ConcurrentRoot,
|
|
862
|
+
null,
|
|
863
|
+
options.strictMode ?? false,
|
|
864
|
+
null,
|
|
865
|
+
"roku-",
|
|
866
|
+
(error) => {
|
|
867
|
+
state.error = error;
|
|
868
|
+
state.errorGeneration = state.generation;
|
|
869
|
+
state.errorKind = "react";
|
|
870
|
+
onError(error, "uncaught");
|
|
871
|
+
},
|
|
872
|
+
(error) => onError(error, "caught"),
|
|
873
|
+
(error) => onError(error, "recoverable"),
|
|
874
|
+
() => undefined,
|
|
875
|
+
);
|
|
876
|
+
const flush = () => {
|
|
877
|
+
syncDepth++;
|
|
878
|
+
try {
|
|
879
|
+
reconciler.flushSyncWork();
|
|
880
|
+
reconciler.flushPassiveEffects();
|
|
881
|
+
} finally {
|
|
882
|
+
syncDepth--;
|
|
883
|
+
}
|
|
884
|
+
if (state.error) {
|
|
885
|
+
const error = state.error;
|
|
886
|
+
state.error = null;
|
|
887
|
+
throw error;
|
|
888
|
+
}
|
|
889
|
+
};
|
|
890
|
+
const assertMounted = () => {
|
|
891
|
+
if (!mounted) throw new RokuHostError("RETIRED_ROOT", "A retired React root cannot be reused");
|
|
892
|
+
};
|
|
893
|
+
const root: SceneRoot = {
|
|
894
|
+
portal(element, key) {
|
|
895
|
+
assertMounted();
|
|
896
|
+
return reconciler.createPortal(element, state, null, key) as unknown as ReactNode;
|
|
897
|
+
},
|
|
898
|
+
render(element) {
|
|
899
|
+
assertMounted();
|
|
900
|
+
reconciler.updateContainerSync(element, container, null, null);
|
|
901
|
+
flush();
|
|
902
|
+
},
|
|
903
|
+
runSync(work) {
|
|
904
|
+
assertMounted();
|
|
905
|
+
if (syncDepth) {
|
|
906
|
+
work();
|
|
907
|
+
return;
|
|
908
|
+
}
|
|
909
|
+
syncDepth++;
|
|
910
|
+
try {
|
|
911
|
+
reconciler.flushSyncFromReconciler(work);
|
|
912
|
+
} finally {
|
|
913
|
+
syncDepth--;
|
|
914
|
+
}
|
|
915
|
+
flush();
|
|
916
|
+
},
|
|
917
|
+
flush,
|
|
918
|
+
refreshNative() {
|
|
919
|
+
assertMounted();
|
|
920
|
+
state.commit();
|
|
921
|
+
},
|
|
922
|
+
async whenCommitted() {
|
|
923
|
+
let pending: Promise<void>;
|
|
924
|
+
do {
|
|
925
|
+
pending = state.pending;
|
|
926
|
+
await pending;
|
|
927
|
+
} while (pending !== state.pending);
|
|
928
|
+
if (state.error) throw state.error;
|
|
929
|
+
},
|
|
930
|
+
unmount() {
|
|
931
|
+
if (!mounted) return;
|
|
932
|
+
retiring = true;
|
|
933
|
+
try {
|
|
934
|
+
reconciler.updateContainerSync(null, container, null, null);
|
|
935
|
+
flush();
|
|
936
|
+
} finally {
|
|
937
|
+
mounted = false;
|
|
938
|
+
if (state.focusPresentation) clearTimeout(state.focusPresentation.timer);
|
|
939
|
+
state.focusPresentation = undefined;
|
|
940
|
+
}
|
|
941
|
+
},
|
|
942
|
+
get mounted() {
|
|
943
|
+
return mounted;
|
|
944
|
+
},
|
|
945
|
+
};
|
|
946
|
+
focusPresentations.set(root, (target, writes) => {
|
|
947
|
+
if (
|
|
948
|
+
!mounted ||
|
|
949
|
+
retiring ||
|
|
950
|
+
state.error ||
|
|
951
|
+
committedGeneration !== state.generation ||
|
|
952
|
+
state.focusPresentation
|
|
953
|
+
)
|
|
954
|
+
return false;
|
|
955
|
+
const focus = elementsByHandle.get(target.handle);
|
|
956
|
+
if (!focus?.ref || state.realized.get(focus) !== focus.ref) return false;
|
|
957
|
+
const staged: Array<{ element: ElementNode; ref: SceneNodeRef; fields: SceneFields }> = [];
|
|
958
|
+
for (const { node, fields } of writes) {
|
|
959
|
+
const element = elementsByHandle.get(node.handle);
|
|
960
|
+
if (!element?.ref || state.realized.get(element) !== element.ref) return false;
|
|
961
|
+
staged.push({ element, ref: element.ref, fields });
|
|
962
|
+
}
|
|
963
|
+
const previews = new Set<ElementNode>();
|
|
964
|
+
// Reconcile an unacknowledged request through the ordinary layout/field path.
|
|
965
|
+
const timer = setTimeout(() => {
|
|
966
|
+
if (mounted && state.focusPresentation?.timer === timer) state.commit();
|
|
967
|
+
}, 250);
|
|
968
|
+
state.focusPresentation = { nodes: previews, timer };
|
|
969
|
+
port.batch(() => {
|
|
970
|
+
for (const { element, ref, fields } of staged) {
|
|
971
|
+
for (const [field, value] of Object.entries(fields)) {
|
|
972
|
+
if (Object.is(element.appliedFields[field], value)) continue;
|
|
973
|
+
previews.add(element);
|
|
974
|
+
const applied = { ...element.appliedFields };
|
|
975
|
+
try {
|
|
976
|
+
port.set(ref, field, value);
|
|
977
|
+
applied[field] = value;
|
|
978
|
+
} catch (error) {
|
|
979
|
+
// The port may have thrown after a native set. Require the next
|
|
980
|
+
// commit to assert the canonical field again.
|
|
981
|
+
delete applied[field];
|
|
982
|
+
throw error;
|
|
983
|
+
} finally {
|
|
984
|
+
element.appliedFields = applied;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
port.set(focus.ref!, "gainFocus", true);
|
|
989
|
+
});
|
|
990
|
+
return true;
|
|
991
|
+
});
|
|
992
|
+
opacityUpdates.set(root, (handle, value) => {
|
|
993
|
+
if (!mounted || retiring || state.error || committedGeneration !== state.generation)
|
|
994
|
+
return false;
|
|
995
|
+
const node = elementsByHandle.get(handle);
|
|
996
|
+
if (!node || !node.ref || state.realized.get(node) !== node.ref) return false;
|
|
997
|
+
if (!Object.is(node.appliedFields.opacity, value)) {
|
|
998
|
+
node.appliedAuthoredFields = undefined;
|
|
999
|
+
port.batch(() => port.set(node.ref!, "opacity", value));
|
|
1000
|
+
// A later full commit must compare against the field actually written.
|
|
1001
|
+
node.appliedFields = { ...node.appliedFields, opacity: value };
|
|
1002
|
+
}
|
|
1003
|
+
return true;
|
|
1004
|
+
});
|
|
1005
|
+
return root;
|
|
1006
|
+
}
|