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,255 @@
|
|
|
1
|
+
import { createElement, forwardRef, type ComponentType } from "react";
|
|
2
|
+
import { makeMutable, type MutableValue } from "./sharedValue";
|
|
3
|
+
import { Easing, interpolate, useAnimatedStyle, withTiming } from "./reanimated";
|
|
4
|
+
import { View, Text } from "../react-native/primitives";
|
|
5
|
+
import { Image } from "../react-native/image";
|
|
6
|
+
import { ScrollView } from "../react-native/scrollView";
|
|
7
|
+
import { styleComponent } from "../react-native/styleInterop";
|
|
8
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
9
|
+
export { Easing };
|
|
10
|
+
let listenerID = 0,
|
|
11
|
+
warned = false;
|
|
12
|
+
class AnimatedNode {
|
|
13
|
+
constructor(readonly read: () => number) {}
|
|
14
|
+
interpolate(config: {
|
|
15
|
+
inputRange: number[];
|
|
16
|
+
outputRange: number[];
|
|
17
|
+
extrapolate?: "extend" | "clamp" | "identity";
|
|
18
|
+
}) {
|
|
19
|
+
return new AnimatedNode(() =>
|
|
20
|
+
interpolate(this.read(), config.inputRange, config.outputRange, config.extrapolate),
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
__getValue(): number {
|
|
24
|
+
return this.read();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
export class Value extends AnimatedNode {
|
|
28
|
+
readonly mutable: MutableValue<number>;
|
|
29
|
+
private readonly offset: MutableValue<number>;
|
|
30
|
+
private readonly listenerIDs = new Set<number>();
|
|
31
|
+
constructor(initial: number) {
|
|
32
|
+
const value = makeMutable(initial),
|
|
33
|
+
offset = makeMutable(0);
|
|
34
|
+
super(() => value.value + offset.value);
|
|
35
|
+
this.mutable = value;
|
|
36
|
+
this.offset = offset;
|
|
37
|
+
}
|
|
38
|
+
setValue(value: number): void {
|
|
39
|
+
this.mutable.value = value;
|
|
40
|
+
}
|
|
41
|
+
setOffset(value: number): void {
|
|
42
|
+
this.offset.value = value;
|
|
43
|
+
}
|
|
44
|
+
flattenOffset(): void {
|
|
45
|
+
this.mutable.value += this.offset.value;
|
|
46
|
+
this.offset.value = 0;
|
|
47
|
+
}
|
|
48
|
+
extractOffset(): void {
|
|
49
|
+
this.offset.value += this.mutable.value;
|
|
50
|
+
this.mutable.value = 0;
|
|
51
|
+
}
|
|
52
|
+
stopAnimation(callback?: (value: number) => void): void {
|
|
53
|
+
this.mutable.cancel();
|
|
54
|
+
callback?.(this.read());
|
|
55
|
+
}
|
|
56
|
+
addListener(callback: (event: { value: number }) => void): string {
|
|
57
|
+
const id = ++listenerID;
|
|
58
|
+
this.listenerIDs.add(id);
|
|
59
|
+
const listener = () => callback({ value: this.read() });
|
|
60
|
+
this.mutable.addListener(id, listener);
|
|
61
|
+
this.offset.addListener(id, listener);
|
|
62
|
+
return String(id);
|
|
63
|
+
}
|
|
64
|
+
removeListener(id: string): void {
|
|
65
|
+
this.mutable.removeListener(Number(id));
|
|
66
|
+
this.offset.removeListener(Number(id));
|
|
67
|
+
this.listenerIDs.delete(Number(id));
|
|
68
|
+
}
|
|
69
|
+
removeAllListeners(): void {
|
|
70
|
+
for (const id of this.listenerIDs) this.removeListener(String(id));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function resolve(value: unknown): unknown {
|
|
74
|
+
if (value instanceof AnimatedNode) return value.read();
|
|
75
|
+
if (Array.isArray(value)) return value.map(resolve);
|
|
76
|
+
if (value && typeof value === "object")
|
|
77
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, resolve(item)]));
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
function createAnimatedComponent<P extends object>(Component: ComponentType<P>) {
|
|
81
|
+
return forwardRef<unknown, P & { style?: unknown }>((props, ref) => {
|
|
82
|
+
const style = useAnimatedStyle(() => resolve(props.style) as object);
|
|
83
|
+
return createElement(styleComponent(Component), {
|
|
84
|
+
...props,
|
|
85
|
+
style,
|
|
86
|
+
ref,
|
|
87
|
+
} as P);
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
export interface CompositeAnimation {
|
|
91
|
+
start(callback?: (result: { finished: boolean }) => void): void;
|
|
92
|
+
stop(): void;
|
|
93
|
+
reset(): void;
|
|
94
|
+
}
|
|
95
|
+
function timing(
|
|
96
|
+
value: Value,
|
|
97
|
+
config: {
|
|
98
|
+
toValue: number;
|
|
99
|
+
duration?: number;
|
|
100
|
+
easing?: (value: number) => number;
|
|
101
|
+
useNativeDriver?: boolean;
|
|
102
|
+
},
|
|
103
|
+
): CompositeAnimation {
|
|
104
|
+
let initial = value.mutable.value;
|
|
105
|
+
return {
|
|
106
|
+
start(callback) {
|
|
107
|
+
initial = value.mutable.value;
|
|
108
|
+
if (config.useNativeDriver && !warned) {
|
|
109
|
+
warned = true;
|
|
110
|
+
console.warn(
|
|
111
|
+
"RNR_JS_ANIMATION_DRIVER",
|
|
112
|
+
"Native-thread animation performance is not qualified; using the Roku JS executor",
|
|
113
|
+
);
|
|
114
|
+
}
|
|
115
|
+
value.mutable.value = withTiming(config.toValue, config, (finished) =>
|
|
116
|
+
callback?.({ finished }),
|
|
117
|
+
);
|
|
118
|
+
},
|
|
119
|
+
stop() {
|
|
120
|
+
value.stopAnimation();
|
|
121
|
+
},
|
|
122
|
+
reset() {
|
|
123
|
+
value.stopAnimation();
|
|
124
|
+
value.setValue(initial);
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function parallel(
|
|
129
|
+
animations: CompositeAnimation[],
|
|
130
|
+
options: { stopTogether?: boolean } = {},
|
|
131
|
+
): CompositeAnimation {
|
|
132
|
+
let active = false,
|
|
133
|
+
remaining = 0,
|
|
134
|
+
finished = true,
|
|
135
|
+
callback: ((result: { finished: boolean }) => void) | undefined;
|
|
136
|
+
const complete = (result: { finished: boolean }) => {
|
|
137
|
+
if (!active) return;
|
|
138
|
+
finished &&= result.finished;
|
|
139
|
+
if (!result.finished && options.stopTogether !== false) {
|
|
140
|
+
active = false;
|
|
141
|
+
animations.forEach((animation) => animation.stop());
|
|
142
|
+
callback?.({ finished: false });
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
if (--remaining === 0) {
|
|
146
|
+
active = false;
|
|
147
|
+
callback?.({ finished });
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
return {
|
|
151
|
+
start(next) {
|
|
152
|
+
active = true;
|
|
153
|
+
remaining = animations.length;
|
|
154
|
+
finished = true;
|
|
155
|
+
callback = next;
|
|
156
|
+
if (!remaining) {
|
|
157
|
+
active = false;
|
|
158
|
+
next?.({ finished: true });
|
|
159
|
+
} else
|
|
160
|
+
animations.forEach((animation) => {
|
|
161
|
+
if (active) animation.start(complete);
|
|
162
|
+
});
|
|
163
|
+
},
|
|
164
|
+
stop() {
|
|
165
|
+
if (!active) return;
|
|
166
|
+
active = false;
|
|
167
|
+
animations.forEach((animation) => animation.stop());
|
|
168
|
+
callback?.({ finished: false });
|
|
169
|
+
},
|
|
170
|
+
reset() {
|
|
171
|
+
active = false;
|
|
172
|
+
animations.forEach((animation) => animation.reset());
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
function sequence(animations: CompositeAnimation[]): CompositeAnimation {
|
|
177
|
+
let index = 0,
|
|
178
|
+
active = false,
|
|
179
|
+
callback: ((result: { finished: boolean }) => void) | undefined;
|
|
180
|
+
const next = (result: { finished: boolean }) => {
|
|
181
|
+
if (!active) return;
|
|
182
|
+
if (!result.finished || index === animations.length) {
|
|
183
|
+
active = false;
|
|
184
|
+
callback?.(result);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
animations[index++].start(next);
|
|
188
|
+
};
|
|
189
|
+
return {
|
|
190
|
+
start(done) {
|
|
191
|
+
index = 0;
|
|
192
|
+
active = true;
|
|
193
|
+
callback = done;
|
|
194
|
+
next({ finished: true });
|
|
195
|
+
},
|
|
196
|
+
stop() {
|
|
197
|
+
if (!active) return;
|
|
198
|
+
active = false;
|
|
199
|
+
animations[index - 1]?.stop();
|
|
200
|
+
callback?.({ finished: false });
|
|
201
|
+
},
|
|
202
|
+
reset() {
|
|
203
|
+
active = false;
|
|
204
|
+
animations.forEach((animation) => animation.reset());
|
|
205
|
+
},
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const read = (value: AnimatedNode | number) =>
|
|
209
|
+
value instanceof AnimatedNode ? value.read() : value;
|
|
210
|
+
const binary = (
|
|
211
|
+
left: AnimatedNode | number,
|
|
212
|
+
right: AnimatedNode | number,
|
|
213
|
+
operation: (a: number, b: number) => number,
|
|
214
|
+
) => new AnimatedNode(() => operation(read(left), read(right)));
|
|
215
|
+
function event(mapping: unknown[], options: { listener?: (...args: unknown[]) => void } = {}) {
|
|
216
|
+
const assign = (map: unknown, value: unknown): void => {
|
|
217
|
+
if (map instanceof Value) {
|
|
218
|
+
if (typeof value !== "number")
|
|
219
|
+
throw new RokuHostError("C06_ANIMATED_EVENT", "Animated event values must be numeric");
|
|
220
|
+
map.setValue(value);
|
|
221
|
+
} else if (map && typeof map === "object")
|
|
222
|
+
for (const [key, child] of Object.entries(map))
|
|
223
|
+
assign(child, (value as Record<string, unknown> | null)?.[key]);
|
|
224
|
+
};
|
|
225
|
+
return (...values: unknown[]) => {
|
|
226
|
+
mapping.forEach((map, index) => assign(map, values[index]));
|
|
227
|
+
options.listener?.(...values);
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
const pending = (): never => {
|
|
231
|
+
throw new RokuHostError("C06_ANIMATION", "This legacy animation recipe needs a Roku executor");
|
|
232
|
+
};
|
|
233
|
+
export const Animated = Object.freeze({
|
|
234
|
+
Value,
|
|
235
|
+
createAnimatedComponent,
|
|
236
|
+
View: createAnimatedComponent(View),
|
|
237
|
+
Text: createAnimatedComponent(Text),
|
|
238
|
+
Image: createAnimatedComponent(Image),
|
|
239
|
+
ScrollView: createAnimatedComponent(ScrollView),
|
|
240
|
+
timing,
|
|
241
|
+
parallel,
|
|
242
|
+
sequence,
|
|
243
|
+
event,
|
|
244
|
+
add: (a: AnimatedNode | number, b: AnimatedNode | number) => binary(a, b, (x, y) => x + y),
|
|
245
|
+
subtract: (a: AnimatedNode | number, b: AnimatedNode | number) => binary(a, b, (x, y) => x - y),
|
|
246
|
+
multiply: (a: AnimatedNode | number, b: AnimatedNode | number) => binary(a, b, (x, y) => x * y),
|
|
247
|
+
divide: (a: AnimatedNode | number, b: AnimatedNode | number) =>
|
|
248
|
+
binary(a, b, (x, y) => {
|
|
249
|
+
if (!y) throw new RokuHostError("C06_ANIMATION_VALUE", "Animated division by zero");
|
|
250
|
+
return x / y;
|
|
251
|
+
}),
|
|
252
|
+
spring: pending,
|
|
253
|
+
decay: pending,
|
|
254
|
+
loop: pending,
|
|
255
|
+
});
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createElement,
|
|
3
|
+
forwardRef,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useMemo,
|
|
6
|
+
useRef,
|
|
7
|
+
useSyncExternalStore,
|
|
8
|
+
type ComponentType,
|
|
9
|
+
} from "react";
|
|
10
|
+
import { View, Text } from "../react-native/primitives";
|
|
11
|
+
import { Image } from "../react-native/image";
|
|
12
|
+
import { ScrollView } from "../react-native/scrollView";
|
|
13
|
+
import { styleComponent } from "../react-native/styleInterop";
|
|
14
|
+
import { createDerivedMapper } from "./derivedValue";
|
|
15
|
+
import { RokuHostError } from "../scene/sceneGraph.types";
|
|
16
|
+
import {
|
|
17
|
+
collectValues,
|
|
18
|
+
makeMutable,
|
|
19
|
+
type Animation,
|
|
20
|
+
type Animatable,
|
|
21
|
+
type MutableValue,
|
|
22
|
+
type SharedValue,
|
|
23
|
+
} from "./sharedValue";
|
|
24
|
+
export { makeMutable };
|
|
25
|
+
export type { SharedValue };
|
|
26
|
+
|
|
27
|
+
export const Easing = Object.freeze({
|
|
28
|
+
linear: (value: number) => value,
|
|
29
|
+
ease: (value: number) => value * value * (3 - 2 * value),
|
|
30
|
+
quad: (value: number) => value * value,
|
|
31
|
+
cubic: (value: number) => value * value * value,
|
|
32
|
+
in: (easing: (value: number) => number) => easing,
|
|
33
|
+
out: (easing: (value: number) => number) => (value: number) => 1 - easing(1 - value),
|
|
34
|
+
inOut: (easing: (value: number) => number) => (value: number) =>
|
|
35
|
+
value < 0.5 ? easing(value * 2) / 2 : 1 - easing((1 - value) * 2) / 2,
|
|
36
|
+
bezier(x1: number, y1: number, x2: number, y2: number) {
|
|
37
|
+
if (![x1, y1, x2, y2].every(Number.isFinite) || x1 < 0 || x1 > 1 || x2 < 0 || x2 > 1)
|
|
38
|
+
throw new RokuHostError(
|
|
39
|
+
"C06_EASING",
|
|
40
|
+
"Bezier control points require finite values and x within [0,1]",
|
|
41
|
+
);
|
|
42
|
+
const sample = (t: number, a: number, b: number) =>
|
|
43
|
+
3 * (1 - t) ** 2 * t * a + 3 * (1 - t) * t ** 2 * b + t ** 3;
|
|
44
|
+
return (value: number) => {
|
|
45
|
+
if (value <= 0 || value >= 1) return Math.max(0, Math.min(1, value));
|
|
46
|
+
let low = 0,
|
|
47
|
+
high = 1;
|
|
48
|
+
for (let i = 0; i < 24; i++) {
|
|
49
|
+
const middle = (low + high) / 2;
|
|
50
|
+
if (sample(middle, x1, x2) < value) low = middle;
|
|
51
|
+
else high = middle;
|
|
52
|
+
}
|
|
53
|
+
return sample((low + high) / 2, y1, y2);
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
export function withTiming(
|
|
58
|
+
to: Animatable,
|
|
59
|
+
options: {
|
|
60
|
+
duration?: number;
|
|
61
|
+
easing?: (value: number) => number;
|
|
62
|
+
reduceMotion?: unknown;
|
|
63
|
+
} = {},
|
|
64
|
+
callback?: (finished: boolean, current: Animatable) => void,
|
|
65
|
+
): Animation {
|
|
66
|
+
const duration = options.duration ?? 300;
|
|
67
|
+
if (!Number.isFinite(duration) || duration < 0)
|
|
68
|
+
throw new RokuHostError("C06_DURATION", "Animation duration must be finite and nonnegative");
|
|
69
|
+
return {
|
|
70
|
+
kind: "timing",
|
|
71
|
+
to,
|
|
72
|
+
duration,
|
|
73
|
+
easing: options.easing ?? Easing.inOut(Easing.quad),
|
|
74
|
+
callback,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export function withDelay(duration: number, animation: Animation): Animation {
|
|
78
|
+
if (!Number.isFinite(duration) || duration < 0)
|
|
79
|
+
throw new RokuHostError("C06_DURATION", "Animation delay must be finite and nonnegative");
|
|
80
|
+
return { kind: "delay", duration, animation };
|
|
81
|
+
}
|
|
82
|
+
export function withSequence(...animations: Animation[]): Animation {
|
|
83
|
+
return { kind: "sequence", animations };
|
|
84
|
+
}
|
|
85
|
+
export function withRepeat(
|
|
86
|
+
animation: Animation,
|
|
87
|
+
count = 2,
|
|
88
|
+
reverse = false,
|
|
89
|
+
callback?: (finished: boolean) => void,
|
|
90
|
+
): Animation {
|
|
91
|
+
if (!Number.isInteger(count))
|
|
92
|
+
throw new RokuHostError("C06_ANIMATION_REPEAT", "Repetition count must be an integer");
|
|
93
|
+
return { kind: "repeat", animation, count, reverse, callback };
|
|
94
|
+
}
|
|
95
|
+
export function cancelAnimation(value: SharedValue<unknown>): void {
|
|
96
|
+
(value as MutableValue<unknown>).cancel();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function useSharedValue<T>(initial: T): MutableValue<T> {
|
|
100
|
+
const value = useMemo(() => makeMutable(initial), []);
|
|
101
|
+
useLayoutEffect(() => () => value.cancel(), [value]);
|
|
102
|
+
return value;
|
|
103
|
+
}
|
|
104
|
+
function useReactive<T>(updater: () => T): T {
|
|
105
|
+
const evaluation = collectValues(updater),
|
|
106
|
+
dependencies = [...evaluation.dependencies];
|
|
107
|
+
const subscribe = (callback: () => void) => {
|
|
108
|
+
const releases = dependencies.map((value) => value.subscribe(callback));
|
|
109
|
+
return () => releases.forEach((release) => release());
|
|
110
|
+
};
|
|
111
|
+
useSyncExternalStore(subscribe, () => dependencies.map((value) => value.version).join(":"));
|
|
112
|
+
return evaluation.value;
|
|
113
|
+
}
|
|
114
|
+
export function useAnimatedStyle<T extends object>(
|
|
115
|
+
updater: () => T,
|
|
116
|
+
_dependencies?: readonly unknown[],
|
|
117
|
+
): T {
|
|
118
|
+
return useReactive(updater);
|
|
119
|
+
}
|
|
120
|
+
export function useDerivedValue<T extends Animatable>(
|
|
121
|
+
updater: () => T | Animation,
|
|
122
|
+
_dependencies?: readonly unknown[],
|
|
123
|
+
): MutableValue<T> {
|
|
124
|
+
const mapper = useMemo(() => createDerivedMapper(isTimingTarget(updater()) as T), []);
|
|
125
|
+
useLayoutEffect(() => mapper.mount(), [mapper]);
|
|
126
|
+
useLayoutEffect(() => mapper.commit(updater));
|
|
127
|
+
return mapper.value;
|
|
128
|
+
}
|
|
129
|
+
function isTimingTarget(value: Animatable | Animation): Animatable {
|
|
130
|
+
if (value && typeof value === "object" && "kind" in value) {
|
|
131
|
+
const animation = value as Animation;
|
|
132
|
+
if (animation.kind === "timing") return animation.to;
|
|
133
|
+
if (animation.kind === "delay" || animation.kind === "repeat")
|
|
134
|
+
return isTimingTarget(animation.animation);
|
|
135
|
+
const last = animation.animations[animation.animations.length - 1];
|
|
136
|
+
return last ? isTimingTarget(last) : 0;
|
|
137
|
+
}
|
|
138
|
+
return value as Animatable;
|
|
139
|
+
}
|
|
140
|
+
export function createAnimatedComponent<P extends object>(
|
|
141
|
+
Component: ComponentType<P>,
|
|
142
|
+
): ComponentType<P> {
|
|
143
|
+
// Animated styles subscribe through their hook; preserve refs and primitive identity here.
|
|
144
|
+
const AnimatedComponent = forwardRef<unknown, P>((props, ref) =>
|
|
145
|
+
createElement(styleComponent(Component), { ...props, ref } as P),
|
|
146
|
+
);
|
|
147
|
+
AnimatedComponent.displayName = `RokuAnimated(${Component.displayName ?? Component.name ?? "Component"})`;
|
|
148
|
+
return AnimatedComponent as unknown as ComponentType<P>;
|
|
149
|
+
}
|
|
150
|
+
export interface AnimatedRef<T> {
|
|
151
|
+
(value?: T | null): T | null;
|
|
152
|
+
current: T | null;
|
|
153
|
+
}
|
|
154
|
+
export function useAnimatedRef<T>(): AnimatedRef<T> {
|
|
155
|
+
return useMemo(() => {
|
|
156
|
+
const callback = ((value?: T | null) => {
|
|
157
|
+
if (value !== undefined) callback.current = value;
|
|
158
|
+
return callback.current;
|
|
159
|
+
}) as AnimatedRef<T>;
|
|
160
|
+
callback.current = null;
|
|
161
|
+
return callback;
|
|
162
|
+
}, []);
|
|
163
|
+
}
|
|
164
|
+
export function useEvent<T>(
|
|
165
|
+
handler: (event: T) => void,
|
|
166
|
+
_events?: readonly string[],
|
|
167
|
+
_rebuild?: boolean,
|
|
168
|
+
): (event: T | { nativeEvent: T }) => void {
|
|
169
|
+
const latest = useRef(handler);
|
|
170
|
+
latest.current = handler;
|
|
171
|
+
return useMemo(
|
|
172
|
+
() => (event) =>
|
|
173
|
+
latest.current(
|
|
174
|
+
event && typeof event === "object" && "nativeEvent" in event
|
|
175
|
+
? event.nativeEvent
|
|
176
|
+
: (event as T),
|
|
177
|
+
),
|
|
178
|
+
[],
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
export function dispatchCommand(
|
|
182
|
+
ref: { current?: unknown } | (() => unknown),
|
|
183
|
+
command: string,
|
|
184
|
+
args: unknown[] = [],
|
|
185
|
+
): void {
|
|
186
|
+
const target = typeof ref === "function" ? ref() : ref.current;
|
|
187
|
+
if (
|
|
188
|
+
!target ||
|
|
189
|
+
typeof target !== "object" ||
|
|
190
|
+
typeof (target as Record<string, unknown>)[command] !== "function"
|
|
191
|
+
)
|
|
192
|
+
throw new RokuHostError(
|
|
193
|
+
"C06_COMMAND",
|
|
194
|
+
`Native command ${command} is unavailable on this target`,
|
|
195
|
+
);
|
|
196
|
+
(target as Record<string, (...args: unknown[]) => void>)[command](...args);
|
|
197
|
+
}
|
|
198
|
+
export function interpolate(
|
|
199
|
+
value: number,
|
|
200
|
+
input: readonly number[],
|
|
201
|
+
output: readonly number[],
|
|
202
|
+
extrapolate:
|
|
203
|
+
| "clamp"
|
|
204
|
+
| "extend"
|
|
205
|
+
| "identity"
|
|
206
|
+
| { extrapolateLeft?: string; extrapolateRight?: string } = "extend",
|
|
207
|
+
): number {
|
|
208
|
+
if (
|
|
209
|
+
input.length < 2 ||
|
|
210
|
+
input.length !== output.length ||
|
|
211
|
+
input.some((item, index) => !Number.isFinite(item) || (index > 0 && item <= input[index - 1]))
|
|
212
|
+
)
|
|
213
|
+
throw new RokuHostError(
|
|
214
|
+
"C06_INTERPOLATE",
|
|
215
|
+
"Interpolation ranges must have matching length and increasing finite input",
|
|
216
|
+
);
|
|
217
|
+
const left =
|
|
218
|
+
typeof extrapolate === "string" ? extrapolate : (extrapolate.extrapolateLeft ?? "extend"),
|
|
219
|
+
right =
|
|
220
|
+
typeof extrapolate === "string" ? extrapolate : (extrapolate.extrapolateRight ?? "extend");
|
|
221
|
+
if (value < input[0]) {
|
|
222
|
+
if (left === "clamp") return output[0];
|
|
223
|
+
if (left === "identity") return value;
|
|
224
|
+
}
|
|
225
|
+
if (value > input[input.length - 1]) {
|
|
226
|
+
if (right === "clamp") return output[output.length - 1];
|
|
227
|
+
if (right === "identity") return value;
|
|
228
|
+
}
|
|
229
|
+
let segment = 0;
|
|
230
|
+
while (segment < input.length - 2 && value > input[segment + 1]) segment++;
|
|
231
|
+
return (
|
|
232
|
+
output[segment] +
|
|
233
|
+
((value - input[segment]) / (input[segment + 1] - input[segment])) *
|
|
234
|
+
(output[segment + 1] - output[segment])
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
export default Object.freeze({
|
|
238
|
+
createAnimatedComponent,
|
|
239
|
+
View: createAnimatedComponent(View),
|
|
240
|
+
Text: createAnimatedComponent(Text),
|
|
241
|
+
Image: createAnimatedComponent(Image),
|
|
242
|
+
ScrollView: createAnimatedComponent(ScrollView),
|
|
243
|
+
});
|