react-native-roku 0.0.0-stage → 0.2.0-preview.1
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 +318 -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 +362 -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,136 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
import {
|
|
3
|
+
nativeErrorCode,
|
|
4
|
+
nativeVideoErrorInfo,
|
|
5
|
+
type NativeVideoErrorDetails,
|
|
6
|
+
} from "./videoErrorDetails";
|
|
7
|
+
|
|
8
|
+
export type VideoState =
|
|
9
|
+
"none" | "buffering" | "playing" | "paused" | "stopped" | "finished" | "error";
|
|
10
|
+
export interface VideoSnapshot {
|
|
11
|
+
readonly state: VideoState;
|
|
12
|
+
readonly position?: number;
|
|
13
|
+
readonly duration?: number;
|
|
14
|
+
readonly renderedPosition?: number;
|
|
15
|
+
readonly error?: NativeVideoErrorDetails;
|
|
16
|
+
}
|
|
17
|
+
export interface VideoObservation {
|
|
18
|
+
readonly kind: "state" | "progress" | "duration" | "frame" | "seek" | "error" | "error-details";
|
|
19
|
+
readonly snapshot: VideoSnapshot;
|
|
20
|
+
readonly seekTime?: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** One mounted content generation. Only native observations publish truth. */
|
|
24
|
+
export function createVideoSession(notify: (event: VideoObservation) => void) {
|
|
25
|
+
let active = true;
|
|
26
|
+
let snapshot: VideoSnapshot = { state: "none" };
|
|
27
|
+
let seekTarget: number | undefined;
|
|
28
|
+
let frameSeen = false;
|
|
29
|
+
let error: NativeVideoErrorDetails | undefined;
|
|
30
|
+
const publish = (kind: VideoObservation["kind"], extra = {}) => {
|
|
31
|
+
if (active) notify({ kind, snapshot, ...extra });
|
|
32
|
+
};
|
|
33
|
+
const setErrorDetails = (next: NativeVideoErrorDetails | undefined) => {
|
|
34
|
+
if (JSON.stringify(next) === JSON.stringify(error)) return;
|
|
35
|
+
error = next && Object.freeze({ ...next });
|
|
36
|
+
if (snapshot.state !== "error") return;
|
|
37
|
+
const { error: _previous, ...base } = snapshot;
|
|
38
|
+
snapshot = error ? { ...base, error } : base;
|
|
39
|
+
publish("error-details");
|
|
40
|
+
};
|
|
41
|
+
const finite = (value: unknown): value is number =>
|
|
42
|
+
typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
43
|
+
return {
|
|
44
|
+
activate() {
|
|
45
|
+
active = true;
|
|
46
|
+
},
|
|
47
|
+
get snapshot() {
|
|
48
|
+
return snapshot;
|
|
49
|
+
},
|
|
50
|
+
seek(time: number) {
|
|
51
|
+
if (!active) throw new RokuHostError("C30_VIDEO_RETIRED", "The media session is retired");
|
|
52
|
+
if (!finite(time))
|
|
53
|
+
throw new RokuHostError(
|
|
54
|
+
"C30_VIDEO_SEEK",
|
|
55
|
+
"Seek targets must be finite nonnegative seconds",
|
|
56
|
+
);
|
|
57
|
+
seekTarget = time;
|
|
58
|
+
},
|
|
59
|
+
observe(field: string, value: unknown) {
|
|
60
|
+
if (!active) return;
|
|
61
|
+
if (field === "errorCode") {
|
|
62
|
+
if (!nativeErrorCode(value) || value === error?.nativeCode) return;
|
|
63
|
+
const { nativeCode: _previous, ...other } = error ?? {};
|
|
64
|
+
setErrorDetails(
|
|
65
|
+
value !== 0
|
|
66
|
+
? { ...other, nativeCode: value }
|
|
67
|
+
: Object.keys(other).length
|
|
68
|
+
? other
|
|
69
|
+
: undefined,
|
|
70
|
+
);
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (field === "errorInfo") {
|
|
74
|
+
const info = nativeVideoErrorInfo(value);
|
|
75
|
+
if (info)
|
|
76
|
+
setErrorDetails({
|
|
77
|
+
...(error?.nativeCode === undefined ? {} : { nativeCode: error.nativeCode }),
|
|
78
|
+
...info,
|
|
79
|
+
});
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
if (
|
|
83
|
+
field === "state" &&
|
|
84
|
+
["none", "buffering", "playing", "paused", "stopped", "finished", "error"].includes(
|
|
85
|
+
String(value),
|
|
86
|
+
)
|
|
87
|
+
) {
|
|
88
|
+
if (value === "error" && snapshot.state === "error") return;
|
|
89
|
+
const { error: _previous, ...base } = snapshot;
|
|
90
|
+
if (snapshot.state === "error" && value !== "error") error = undefined;
|
|
91
|
+
snapshot = {
|
|
92
|
+
...base,
|
|
93
|
+
state: value as VideoState,
|
|
94
|
+
...(value === "error" && error ? { error } : {}),
|
|
95
|
+
};
|
|
96
|
+
publish(value === "error" ? "error" : "state");
|
|
97
|
+
} else if (field === "position" && finite(value)) {
|
|
98
|
+
snapshot = { ...snapshot, position: value };
|
|
99
|
+
publish("progress");
|
|
100
|
+
} else if (field === "duration" && finite(value)) {
|
|
101
|
+
snapshot = { ...snapshot, duration: value };
|
|
102
|
+
publish("duration");
|
|
103
|
+
} else if (field === "positionInfo" && value && typeof value === "object") {
|
|
104
|
+
const sample = value as { epoch?: unknown; video?: unknown };
|
|
105
|
+
// UTC/live clocks need a source-time mapping. They are never confused
|
|
106
|
+
// with elapsed VOD seconds or used to settle a VOD seek.
|
|
107
|
+
if (sample.epoch !== 0 || !finite(sample.video)) return;
|
|
108
|
+
snapshot = {
|
|
109
|
+
...snapshot,
|
|
110
|
+
position: sample.video,
|
|
111
|
+
renderedPosition: sample.video,
|
|
112
|
+
};
|
|
113
|
+
if (!frameSeen) {
|
|
114
|
+
frameSeen = true;
|
|
115
|
+
publish("frame");
|
|
116
|
+
}
|
|
117
|
+
if (seekTarget !== undefined && Math.abs(sample.video - seekTarget) <= 0.5) {
|
|
118
|
+
const target = seekTarget;
|
|
119
|
+
seekTarget = undefined;
|
|
120
|
+
publish("seek", { seekTime: target });
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
position(): Promise<number> {
|
|
125
|
+
return active && snapshot.position !== undefined
|
|
126
|
+
? Promise.resolve(snapshot.position)
|
|
127
|
+
: Promise.reject(
|
|
128
|
+
new RokuHostError("C30_VIDEO_POSITION", "No current native position has been observed"),
|
|
129
|
+
);
|
|
130
|
+
},
|
|
131
|
+
retire() {
|
|
132
|
+
active = false;
|
|
133
|
+
seekTarget = undefined;
|
|
134
|
+
},
|
|
135
|
+
};
|
|
136
|
+
}
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
2
|
+
|
|
3
|
+
export type RouteParams = Readonly<Record<string, string | readonly string[]>>;
|
|
4
|
+
export type RouterHref =
|
|
5
|
+
| string
|
|
6
|
+
| {
|
|
7
|
+
readonly pathname: string;
|
|
8
|
+
readonly params?: Readonly<
|
|
9
|
+
Record<string, string | number | boolean | readonly string[] | undefined>
|
|
10
|
+
>;
|
|
11
|
+
};
|
|
12
|
+
export interface RouterSnapshot {
|
|
13
|
+
readonly key: string;
|
|
14
|
+
readonly href: string;
|
|
15
|
+
readonly pathname: string;
|
|
16
|
+
readonly params: RouteParams;
|
|
17
|
+
readonly queryParams: RouteParams;
|
|
18
|
+
readonly segments: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function sameParameter(
|
|
22
|
+
value: string | number | boolean | readonly string[] | undefined,
|
|
23
|
+
canonical: string | readonly string[],
|
|
24
|
+
): boolean {
|
|
25
|
+
if (value === undefined) return false;
|
|
26
|
+
if (Array.isArray(value) || Array.isArray(canonical))
|
|
27
|
+
return (
|
|
28
|
+
Array.isArray(value) &&
|
|
29
|
+
Array.isArray(canonical) &&
|
|
30
|
+
value.length === canonical.length &&
|
|
31
|
+
value.every((part, index) => part === canonical[index])
|
|
32
|
+
);
|
|
33
|
+
return String(value) === canonical;
|
|
34
|
+
}
|
|
35
|
+
const owns = (value: object, key: string) => Object.prototype.hasOwnProperty.call(value, key);
|
|
36
|
+
|
|
37
|
+
function normalize(href: RouterHref): {
|
|
38
|
+
pathname: string;
|
|
39
|
+
href: string;
|
|
40
|
+
params: RouteParams;
|
|
41
|
+
} {
|
|
42
|
+
let pathname = typeof href === "string" ? href : href.pathname;
|
|
43
|
+
if (!pathname.startsWith("/") || pathname.startsWith("//"))
|
|
44
|
+
throw new RokuHostError("C09_HREF", "The Roku router requires an absolute application path");
|
|
45
|
+
const hash = pathname.indexOf("#");
|
|
46
|
+
if (hash >= 0) pathname = pathname.slice(0, hash);
|
|
47
|
+
const query = pathname.indexOf("?"),
|
|
48
|
+
params: Record<string, string | string[]> = Object.create(null);
|
|
49
|
+
if (query >= 0) {
|
|
50
|
+
for (const pair of pathname
|
|
51
|
+
.slice(query + 1)
|
|
52
|
+
.split("&")
|
|
53
|
+
.filter(Boolean)) {
|
|
54
|
+
const index = pair.indexOf("="),
|
|
55
|
+
key = decodeURIComponent((index < 0 ? pair : pair.slice(0, index)).replace(/\+/g, " "));
|
|
56
|
+
const value = decodeURIComponent(
|
|
57
|
+
(index < 0 ? "" : pair.slice(index + 1)).replace(/\+/g, " "),
|
|
58
|
+
);
|
|
59
|
+
const previous = params[key];
|
|
60
|
+
params[key] =
|
|
61
|
+
previous == null
|
|
62
|
+
? value
|
|
63
|
+
: Array.isArray(previous)
|
|
64
|
+
? [...previous, value]
|
|
65
|
+
: [previous, value];
|
|
66
|
+
}
|
|
67
|
+
pathname = pathname.slice(0, query);
|
|
68
|
+
}
|
|
69
|
+
if (typeof href !== "string")
|
|
70
|
+
for (const [key, value] of Object.entries(href.params ?? {})) {
|
|
71
|
+
if (value === undefined) continue;
|
|
72
|
+
const placeholder = `[${key}]`,
|
|
73
|
+
catchAll = `[...${key}]`;
|
|
74
|
+
if (pathname.includes(placeholder))
|
|
75
|
+
pathname = pathname.replace(placeholder, encodeURIComponent(String(value)));
|
|
76
|
+
else if (pathname.includes(catchAll))
|
|
77
|
+
pathname = pathname.replace(
|
|
78
|
+
catchAll,
|
|
79
|
+
(Array.isArray(value) ? value : [String(value)])
|
|
80
|
+
.map((part) => encodeURIComponent(part))
|
|
81
|
+
.join("/"),
|
|
82
|
+
);
|
|
83
|
+
else params[key] = Array.isArray(value) ? [...value] : String(value);
|
|
84
|
+
}
|
|
85
|
+
pathname =
|
|
86
|
+
"/" +
|
|
87
|
+
pathname
|
|
88
|
+
.split("/")
|
|
89
|
+
.filter((segment) => segment && !/^\([^/]+\)$/.test(segment))
|
|
90
|
+
.join("/");
|
|
91
|
+
if (/\[|\]/.test(pathname))
|
|
92
|
+
throw new RokuHostError("C09_HREF", `Missing route parameters for ${pathname}`);
|
|
93
|
+
if (pathname.split("/").some((segment) => segment === "." || segment === ".."))
|
|
94
|
+
throw new RokuHostError("C09_HREF", "Relative path segments require an explicit route adapter");
|
|
95
|
+
const search = Object.keys(params)
|
|
96
|
+
.sort()
|
|
97
|
+
.flatMap((key) => {
|
|
98
|
+
const value = params[key];
|
|
99
|
+
return (Array.isArray(value) ? value : [value]).map(
|
|
100
|
+
(item) => `${encodeURIComponent(key)}=${encodeURIComponent(item)}`,
|
|
101
|
+
);
|
|
102
|
+
})
|
|
103
|
+
.join("&");
|
|
104
|
+
for (const value of Object.values(params)) if (Array.isArray(value)) Object.freeze(value);
|
|
105
|
+
return {
|
|
106
|
+
pathname,
|
|
107
|
+
href: pathname + (search ? `?${search}` : ""),
|
|
108
|
+
params: Object.freeze(params),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Generic location mechanics. Product routes and availability remain catalog-owned. */
|
|
113
|
+
export class RouterStore {
|
|
114
|
+
private readonly listeners = new Set<() => void>();
|
|
115
|
+
private sequence = 0;
|
|
116
|
+
private stack: RouterSnapshot[];
|
|
117
|
+
private disposed = false;
|
|
118
|
+
constructor(
|
|
119
|
+
initialHref: RouterHref = "/",
|
|
120
|
+
private readonly resolvePathParams?: (pathname: string) => RouteParams,
|
|
121
|
+
) {
|
|
122
|
+
this.stack = [this.create(initialHref)];
|
|
123
|
+
}
|
|
124
|
+
getSnapshot = (): RouterSnapshot => this.stack[this.stack.length - 1];
|
|
125
|
+
subscribe = (listener: () => void): (() => void) => {
|
|
126
|
+
this.assertActive();
|
|
127
|
+
this.listeners.add(listener);
|
|
128
|
+
return () => this.listeners.delete(listener);
|
|
129
|
+
};
|
|
130
|
+
get listenerCount(): number {
|
|
131
|
+
return this.listeners.size;
|
|
132
|
+
}
|
|
133
|
+
canGoBack(): boolean {
|
|
134
|
+
return this.stack.length > 1;
|
|
135
|
+
}
|
|
136
|
+
push(href: RouterHref): void {
|
|
137
|
+
this.assertActive();
|
|
138
|
+
this.stack.push(this.create(href));
|
|
139
|
+
this.publish();
|
|
140
|
+
}
|
|
141
|
+
replace(href: RouterHref): void {
|
|
142
|
+
this.assertActive();
|
|
143
|
+
if (normalize(href).href === this.getSnapshot().href) return;
|
|
144
|
+
this.stack[this.stack.length - 1] = this.create(href);
|
|
145
|
+
this.publish();
|
|
146
|
+
}
|
|
147
|
+
navigate(href: RouterHref, options?: { readonly dangerouslySingular?: boolean }): void {
|
|
148
|
+
this.assertActive();
|
|
149
|
+
const normalized = normalize(href);
|
|
150
|
+
if (this.getSnapshot().href === normalized.href) return;
|
|
151
|
+
let existing = -1;
|
|
152
|
+
if (options?.dangerouslySingular)
|
|
153
|
+
for (let index = this.stack.length - 1; index >= 0; index--)
|
|
154
|
+
if (this.stack[index].href === normalized.href) {
|
|
155
|
+
existing = index;
|
|
156
|
+
break;
|
|
157
|
+
}
|
|
158
|
+
if (existing >= 0) {
|
|
159
|
+
this.stack = this.stack.slice(0, existing + 1);
|
|
160
|
+
this.publish();
|
|
161
|
+
} else this.push(href);
|
|
162
|
+
}
|
|
163
|
+
back(): void {
|
|
164
|
+
this.dismiss(1);
|
|
165
|
+
}
|
|
166
|
+
dismiss(count = 1): void {
|
|
167
|
+
this.assertActive();
|
|
168
|
+
if (!Number.isInteger(count) || count < 1)
|
|
169
|
+
throw new RokuHostError("C09_DISMISS", "Dismiss count must be a positive integer");
|
|
170
|
+
const length = Math.max(1, this.stack.length - count);
|
|
171
|
+
if (length === this.stack.length) return;
|
|
172
|
+
this.stack = this.stack.slice(0, length);
|
|
173
|
+
this.publish();
|
|
174
|
+
}
|
|
175
|
+
dismissTo(href: RouterHref): void {
|
|
176
|
+
this.assertActive();
|
|
177
|
+
const normalized = normalize(href);
|
|
178
|
+
for (let index = this.stack.length - 1; index >= 0; index--)
|
|
179
|
+
if (this.stack[index].href === normalized.href) {
|
|
180
|
+
if (index === this.stack.length - 1) return;
|
|
181
|
+
this.stack = this.stack.slice(0, index + 1);
|
|
182
|
+
this.publish();
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
this.replace(href);
|
|
186
|
+
}
|
|
187
|
+
dismissAll(): void {
|
|
188
|
+
this.dismiss(Math.max(1, this.stack.length - 1));
|
|
189
|
+
}
|
|
190
|
+
setParams(
|
|
191
|
+
params: Readonly<Record<string, string | number | boolean | readonly string[] | undefined>>,
|
|
192
|
+
): void {
|
|
193
|
+
const current = this.getSnapshot(),
|
|
194
|
+
path = this.resolvePathParams?.(current.pathname) ?? {};
|
|
195
|
+
for (const [key, value] of Object.entries(params))
|
|
196
|
+
if (owns(path, key) && !sameParameter(value, path[key]))
|
|
197
|
+
throw new RokuHostError(
|
|
198
|
+
"C09_PATH_PARAMETER",
|
|
199
|
+
"Changing a path parameter requires explicit navigation",
|
|
200
|
+
);
|
|
201
|
+
const next = {
|
|
202
|
+
...current.queryParams,
|
|
203
|
+
...Object.fromEntries(Object.entries(params).filter(([key]) => !owns(path, key))),
|
|
204
|
+
};
|
|
205
|
+
this.replace({ pathname: current.pathname, params: next });
|
|
206
|
+
}
|
|
207
|
+
dispose(): void {
|
|
208
|
+
if (this.disposed) return;
|
|
209
|
+
this.disposed = true;
|
|
210
|
+
this.listeners.clear();
|
|
211
|
+
this.stack = [this.stack[0]];
|
|
212
|
+
}
|
|
213
|
+
private create(href: RouterHref): RouterSnapshot {
|
|
214
|
+
const route = normalize(href);
|
|
215
|
+
const path: RouteParams = Object.fromEntries(
|
|
216
|
+
Object.entries(this.resolvePathParams?.(route.pathname) ?? {}).map(([key, value]) => [
|
|
217
|
+
key,
|
|
218
|
+
Array.isArray(value) ? Object.freeze([...value]) : value,
|
|
219
|
+
]),
|
|
220
|
+
);
|
|
221
|
+
for (const [key, value] of Object.entries(route.params))
|
|
222
|
+
if (owns(path, key) && !sameParameter(value, path[key]))
|
|
223
|
+
throw new RokuHostError(
|
|
224
|
+
"C09_PATH_PARAMETER",
|
|
225
|
+
"A query value cannot change a canonical path parameter",
|
|
226
|
+
);
|
|
227
|
+
return Object.freeze({
|
|
228
|
+
...route,
|
|
229
|
+
key: `roku-route-${++this.sequence}`,
|
|
230
|
+
queryParams: route.params,
|
|
231
|
+
params: Object.freeze({
|
|
232
|
+
...route.params,
|
|
233
|
+
...path,
|
|
234
|
+
}),
|
|
235
|
+
segments: Object.freeze(route.pathname.split("/").filter(Boolean)),
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
private publish() {
|
|
239
|
+
for (const listener of [...this.listeners]) listener();
|
|
240
|
+
}
|
|
241
|
+
private assertActive() {
|
|
242
|
+
if (this.disposed) throw new RokuHostError("C09_ROUTER_DISPOSED", "The router was disposed");
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
createElement,
|
|
4
|
+
useContext,
|
|
5
|
+
useEffect,
|
|
6
|
+
useLayoutEffect,
|
|
7
|
+
useMemo,
|
|
8
|
+
useSyncExternalStore,
|
|
9
|
+
type ReactNode,
|
|
10
|
+
} from "react";
|
|
11
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
12
|
+
import { RouterStore, type RouteParams, type RouterHref } from "./RouterStore";
|
|
13
|
+
|
|
14
|
+
export type Href = RouterHref;
|
|
15
|
+
const context = createContext<RouterStore | null>(null);
|
|
16
|
+
const registrations: Array<{ readonly store: RouterStore }> = [];
|
|
17
|
+
const required = () => {
|
|
18
|
+
const store = registrations[registrations.length - 1]?.store;
|
|
19
|
+
if (!store)
|
|
20
|
+
throw new RokuHostError(
|
|
21
|
+
"C09_ROUTER_HOST",
|
|
22
|
+
"Expo navigation requires a mounted Roku router provider",
|
|
23
|
+
);
|
|
24
|
+
return store;
|
|
25
|
+
};
|
|
26
|
+
function createRouter(resolve: () => RouterStore) {
|
|
27
|
+
return Object.freeze({
|
|
28
|
+
push: (href: Href) => resolve().push(href),
|
|
29
|
+
replace: (href: Href) => resolve().replace(href),
|
|
30
|
+
navigate: (href: Href, options?: { dangerouslySingular?: boolean }) =>
|
|
31
|
+
resolve().navigate(href, options),
|
|
32
|
+
back: () => resolve().back(),
|
|
33
|
+
canGoBack: () => resolve().canGoBack(),
|
|
34
|
+
canDismiss: () => resolve().canGoBack(),
|
|
35
|
+
dismiss: (count?: number) => resolve().dismiss(count),
|
|
36
|
+
dismissTo: (href: Href) => resolve().dismissTo(href),
|
|
37
|
+
dismissAll: () => resolve().dismissAll(),
|
|
38
|
+
setParams: (params: Parameters<RouterStore["setParams"]>[0]) => resolve().setParams(params),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export const router = createRouter(required);
|
|
42
|
+
export function RokuRouterProvider({
|
|
43
|
+
store,
|
|
44
|
+
children,
|
|
45
|
+
}: {
|
|
46
|
+
readonly store: RouterStore;
|
|
47
|
+
readonly children: ReactNode;
|
|
48
|
+
}) {
|
|
49
|
+
useLayoutEffect(() => {
|
|
50
|
+
const registration = { store };
|
|
51
|
+
registrations.push(registration);
|
|
52
|
+
return () => {
|
|
53
|
+
const index = registrations.indexOf(registration);
|
|
54
|
+
if (index >= 0) registrations.splice(index, 1);
|
|
55
|
+
};
|
|
56
|
+
}, [store]);
|
|
57
|
+
return createElement(context.Provider, { value: store }, children);
|
|
58
|
+
}
|
|
59
|
+
function useStore() {
|
|
60
|
+
const store = useContext(context);
|
|
61
|
+
if (!store)
|
|
62
|
+
throw new RokuHostError("C09_ROUTER_HOST", "Navigation hooks require a Roku router provider");
|
|
63
|
+
return store;
|
|
64
|
+
}
|
|
65
|
+
function useLocation() {
|
|
66
|
+
const store = useStore();
|
|
67
|
+
return useSyncExternalStore(store.subscribe, store.getSnapshot);
|
|
68
|
+
}
|
|
69
|
+
export function useRouter() {
|
|
70
|
+
const store = useStore();
|
|
71
|
+
return useMemo(() => createRouter(() => store), [store]);
|
|
72
|
+
}
|
|
73
|
+
export function usePathname(): string {
|
|
74
|
+
return useLocation().pathname;
|
|
75
|
+
}
|
|
76
|
+
export function useSegments(): readonly string[] {
|
|
77
|
+
return useLocation().segments;
|
|
78
|
+
}
|
|
79
|
+
export function useLocalSearchParams<T = RouteParams>(): T {
|
|
80
|
+
return useLocation().params as T;
|
|
81
|
+
}
|
|
82
|
+
export function useGlobalSearchParams<T = RouteParams>(): T {
|
|
83
|
+
return useLocalSearchParams<T>();
|
|
84
|
+
}
|
|
85
|
+
export function Redirect({ href }: { readonly href: Href }) {
|
|
86
|
+
const store = useStore();
|
|
87
|
+
useEffect(() => {
|
|
88
|
+
store.replace(href);
|
|
89
|
+
}, [store, href]);
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function unsupported(name: string) {
|
|
94
|
+
return function UnsupportedRouterSurface(): never {
|
|
95
|
+
throw new RokuHostError(
|
|
96
|
+
"C09_ROUTER_SURFACE",
|
|
97
|
+
`${name} is not admitted; the Roku application uses the canonical persistent TV shell`,
|
|
98
|
+
);
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
export const Stack = Object.assign(unsupported("Stack"), {
|
|
102
|
+
Screen: unsupported("Stack.Screen"),
|
|
103
|
+
});
|
|
104
|
+
export const Tabs = Object.assign(unsupported("Tabs"), {
|
|
105
|
+
Screen: unsupported("Tabs.Screen"),
|
|
106
|
+
});
|
|
107
|
+
export const Slot = unsupported("Slot");
|
|
108
|
+
export const SplashScreen = Object.freeze({
|
|
109
|
+
preventAutoHideAsync: async (): Promise<never> => {
|
|
110
|
+
throw new RokuHostError("C09_SPLASH", "The native splash controller has not been connected");
|
|
111
|
+
},
|
|
112
|
+
hideAsync: async (): Promise<never> => {
|
|
113
|
+
throw new RokuHostError("C09_SPLASH", "The native splash controller has not been connected");
|
|
114
|
+
},
|
|
115
|
+
});
|