@shopify/react-native-skia 0.1.176 → 0.1.178

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. package/android/CMakeLists.txt +2 -0
  2. package/cpp/jsi/JsiHostObject.cpp +13 -7
  3. package/cpp/jsi/JsiHostObject.h +3 -1
  4. package/cpp/jsi/RuntimeAwareCache.cpp +9 -0
  5. package/cpp/jsi/RuntimeAwareCache.h +101 -0
  6. package/cpp/jsi/RuntimeLifecycleMonitor.cpp +57 -0
  7. package/cpp/jsi/RuntimeLifecycleMonitor.h +32 -0
  8. package/cpp/rnskia/RNSkManager.cpp +4 -0
  9. package/cpp/rnskia/dom/base/JsiDomNode.h +22 -2
  10. package/lib/commonjs/external/reanimated/index.d.ts +1 -0
  11. package/lib/commonjs/external/reanimated/index.js +13 -0
  12. package/lib/commonjs/external/reanimated/index.js.map +1 -1
  13. package/lib/commonjs/external/reanimated/moduleWrapper.d.ts +8 -0
  14. package/lib/commonjs/external/reanimated/moduleWrapper.js +72 -0
  15. package/lib/commonjs/external/reanimated/moduleWrapper.js.map +1 -0
  16. package/lib/commonjs/external/reanimated/renderHelpers.d.ts +5 -0
  17. package/lib/commonjs/external/reanimated/renderHelpers.js +85 -0
  18. package/lib/commonjs/external/reanimated/renderHelpers.js.map +1 -0
  19. package/lib/commonjs/external/reanimated/useSharedValueEffect.d.ts +2 -5
  20. package/lib/commonjs/external/reanimated/useSharedValueEffect.js +10 -25
  21. package/lib/commonjs/external/reanimated/useSharedValueEffect.js.map +1 -1
  22. package/lib/commonjs/headless/index.d.ts +3 -0
  23. package/lib/commonjs/headless/index.js +57 -0
  24. package/lib/commonjs/headless/index.js.map +1 -0
  25. package/lib/commonjs/mock/index.d.ts +1 -2
  26. package/lib/commonjs/mock/index.js +0 -4
  27. package/lib/commonjs/mock/index.js.map +1 -1
  28. package/lib/commonjs/renderer/Canvas.js +7 -1
  29. package/lib/commonjs/renderer/Canvas.js.map +1 -1
  30. package/lib/commonjs/renderer/Container.d.ts +2 -1
  31. package/lib/commonjs/renderer/Container.js +2 -0
  32. package/lib/commonjs/renderer/Container.js.map +1 -1
  33. package/lib/commonjs/renderer/HostConfig.js +6 -4
  34. package/lib/commonjs/renderer/HostConfig.js.map +1 -1
  35. package/lib/commonjs/renderer/Reconciler.d.ts +1 -1
  36. package/lib/commonjs/renderer/Reconciler.js +2 -1
  37. package/lib/commonjs/renderer/Reconciler.js.map +1 -1
  38. package/lib/commonjs/renderer/processors/Animations/Animations.d.ts +4 -1
  39. package/lib/commonjs/renderer/processors/Animations/Animations.js.map +1 -1
  40. package/lib/module/external/reanimated/index.d.ts +1 -0
  41. package/lib/module/external/reanimated/index.js +1 -0
  42. package/lib/module/external/reanimated/index.js.map +1 -1
  43. package/lib/module/external/reanimated/moduleWrapper.d.ts +8 -0
  44. package/lib/module/external/reanimated/moduleWrapper.js +51 -0
  45. package/lib/module/external/reanimated/moduleWrapper.js.map +1 -0
  46. package/lib/module/external/reanimated/renderHelpers.d.ts +5 -0
  47. package/lib/module/external/reanimated/renderHelpers.js +76 -0
  48. package/lib/module/external/reanimated/renderHelpers.js.map +1 -0
  49. package/lib/module/external/reanimated/useSharedValueEffect.d.ts +2 -5
  50. package/lib/module/external/reanimated/useSharedValueEffect.js +7 -22
  51. package/lib/module/external/reanimated/useSharedValueEffect.js.map +1 -1
  52. package/lib/module/headless/index.d.ts +3 -0
  53. package/lib/module/headless/index.js +29 -0
  54. package/lib/module/headless/index.js.map +1 -0
  55. package/lib/module/mock/index.d.ts +1 -2
  56. package/lib/module/mock/index.js +0 -3
  57. package/lib/module/mock/index.js.map +1 -1
  58. package/lib/module/renderer/Canvas.js +7 -1
  59. package/lib/module/renderer/Canvas.js.map +1 -1
  60. package/lib/module/renderer/Container.d.ts +2 -1
  61. package/lib/module/renderer/Container.js +2 -0
  62. package/lib/module/renderer/Container.js.map +1 -1
  63. package/lib/module/renderer/HostConfig.js +5 -4
  64. package/lib/module/renderer/HostConfig.js.map +1 -1
  65. package/lib/module/renderer/Reconciler.d.ts +1 -1
  66. package/lib/module/renderer/Reconciler.js +2 -1
  67. package/lib/module/renderer/Reconciler.js.map +1 -1
  68. package/lib/module/renderer/processors/Animations/Animations.d.ts +4 -1
  69. package/lib/module/renderer/processors/Animations/Animations.js.map +1 -1
  70. package/lib/typescript/src/external/reanimated/index.d.ts +1 -0
  71. package/lib/typescript/src/external/reanimated/moduleWrapper.d.ts +8 -0
  72. package/lib/typescript/src/external/reanimated/renderHelpers.d.ts +5 -0
  73. package/lib/typescript/src/external/reanimated/useSharedValueEffect.d.ts +2 -5
  74. package/lib/typescript/src/headless/index.d.ts +3 -0
  75. package/lib/typescript/src/mock/index.d.ts +1 -2
  76. package/lib/typescript/src/renderer/Container.d.ts +2 -1
  77. package/lib/typescript/src/renderer/Reconciler.d.ts +1 -1
  78. package/lib/typescript/src/renderer/processors/Animations/Animations.d.ts +4 -1
  79. package/package.json +1 -1
  80. package/src/external/reanimated/index.ts +1 -0
  81. package/src/external/reanimated/moduleWrapper.ts +62 -0
  82. package/src/external/reanimated/renderHelpers.ts +74 -0
  83. package/src/external/reanimated/useSharedValueEffect.ts +17 -23
  84. package/src/headless/index.ts +31 -0
  85. package/src/mock/index.ts +0 -5
  86. package/src/renderer/Canvas.tsx +6 -2
  87. package/src/renderer/Container.tsx +2 -1
  88. package/src/renderer/HostConfig.ts +8 -2
  89. package/src/renderer/Reconciler.tsx +3 -2
  90. package/src/renderer/processors/Animations/Animations.ts +9 -1
@@ -0,0 +1,62 @@
1
+ import { useMemo } from "react";
2
+
3
+ import type { SharedValueType } from "../../renderer/processors/Animations";
4
+
5
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6
+ let Reanimated: any;
7
+
8
+ try {
9
+ Reanimated = require("react-native-reanimated");
10
+ } catch (e) {
11
+ // Ignore
12
+ }
13
+
14
+ export const HAS_REANIMATED = !!Reanimated;
15
+
16
+ function throwOnMissingReanimated() {
17
+ throw new Error(
18
+ "Reanimated was not found, make sure react-native-reanimated package is installed if you want to use \
19
+ react-naitve-skia's integration layer API."
20
+ );
21
+ }
22
+
23
+ let ReanimatedVersionTested = false;
24
+
25
+ export function throwOnIncompatibleReanimatedVersion() {
26
+ if (ReanimatedVersionTested) {
27
+ // we avoid testing version more than once as it won't change and we throw
28
+ // an error when version is incompatible
29
+ return;
30
+ }
31
+ ReanimatedVersionTested = true;
32
+ const reanimatedVersion =
33
+ // eslint-disable-next-line import/extensions
34
+ require("react-native-reanimated/package.json").version;
35
+ // The first compatible version is 3.0.0 but we need to exclude 3.0.0 pre-releases
36
+ // as they have limited support for the used API.
37
+ if (
38
+ !reanimatedVersion ||
39
+ reanimatedVersion < "3.0.0" ||
40
+ reanimatedVersion.includes("3.0.0-")
41
+ ) {
42
+ throw new Error(
43
+ `Reanimated version ${reanimatedVersion} is not supported, please upgrade to 3.0.0 or newer.`
44
+ );
45
+ }
46
+ }
47
+
48
+ export const useSharedValue =
49
+ Reanimated?.useSharedValue ||
50
+ ((value: number) => useMemo(() => ({ value }), [value]));
51
+
52
+ export const startMapper = Reanimated?.startMapper || throwOnMissingReanimated;
53
+ export const stopMapper = Reanimated?.stopMapper || throwOnMissingReanimated;
54
+ export const runOnJS = Reanimated?.runOnJS || throwOnMissingReanimated;
55
+ export const isSharedValue = <T>(
56
+ value: unknown
57
+ ): value is SharedValueType<T> => {
58
+ if (!Reanimated) {
59
+ throwOnMissingReanimated();
60
+ }
61
+ return !!value && Reanimated.isSharedValue(value);
62
+ };
@@ -0,0 +1,74 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ /* eslint-disable reanimated/js-function-in-worklet */
3
+ import type { Container } from "../../renderer/Container";
4
+ import type { AnimatedProps } from "../../renderer/processors";
5
+ import type { Node } from "../../dom/types";
6
+
7
+ import {
8
+ startMapper,
9
+ stopMapper,
10
+ isSharedValue,
11
+ throwOnIncompatibleReanimatedVersion,
12
+ HAS_REANIMATED,
13
+ } from "./moduleWrapper";
14
+
15
+ const _bindings = new WeakMap<Node<unknown>, unknown>();
16
+
17
+ export function extractReanimatedProps(props: AnimatedProps<any>) {
18
+ if (!HAS_REANIMATED) {
19
+ return [props, {}];
20
+ }
21
+ const reanimatedProps = {} as AnimatedProps<any>;
22
+ const otherProps = {} as AnimatedProps<any>;
23
+ for (const propName in props) {
24
+ if (propName === "children") {
25
+ continue;
26
+ }
27
+ const propValue = props[propName];
28
+ if (isSharedValue(propValue)) {
29
+ reanimatedProps[propName] = propValue;
30
+ otherProps[propName] = propValue.value;
31
+ } else {
32
+ otherProps[propName] = propValue;
33
+ }
34
+ }
35
+ return [otherProps, reanimatedProps];
36
+ }
37
+
38
+ export function bindReanimatedProps(
39
+ container: Container,
40
+ node: Node<any>,
41
+ reanimatedProps: AnimatedProps<any>
42
+ ) {
43
+ if (!HAS_REANIMATED) {
44
+ return;
45
+ }
46
+ if (__DEV__) {
47
+ throwOnIncompatibleReanimatedVersion();
48
+ }
49
+ const sharedValues = Object.values(reanimatedProps);
50
+ const previousMapperId = _bindings.get(node);
51
+ if (previousMapperId !== undefined) {
52
+ stopMapper(previousMapperId);
53
+ }
54
+ if (sharedValues.length > 0) {
55
+ const viewId = container.getNativeId();
56
+ const { SkiaViewApi } = global;
57
+ const mapperId = startMapper(() => {
58
+ "worklet";
59
+ for (const propName in reanimatedProps) {
60
+ node && node.setProp(propName, reanimatedProps[propName].value);
61
+ }
62
+ // On React Native we use the SkiaViewApi to redraw because it can
63
+ // run on the worklet thread (container.redraw can't)
64
+ // if SkiaViewApi is undefined, we are on web and container.redraw()
65
+ // can safely be invoked
66
+ if (SkiaViewApi) {
67
+ SkiaViewApi.requestRedraw(viewId);
68
+ } else {
69
+ container.redraw();
70
+ }
71
+ }, sharedValues);
72
+ _bindings.set(node, mapperId);
73
+ }
74
+ }
@@ -1,22 +1,14 @@
1
- import { useEffect, useMemo } from "react";
1
+ import { useEffect } from "react";
2
2
 
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- let Reanimated: any;
3
+ import type { SharedValueType } from "../../renderer/processors/Animations";
5
4
 
6
- try {
7
- Reanimated = require("react-native-reanimated");
8
- } catch (e) {
9
- // Ignore
10
- }
11
-
12
- type SharedValueTypeWrapper<T = number> = {
13
- value: T;
14
- };
15
-
16
- const useSharedValueWrapper =
17
- Reanimated?.useSharedValue === undefined
18
- ? (value: number) => useMemo(() => ({ value }), [value])
19
- : Reanimated.useSharedValue;
5
+ import {
6
+ HAS_REANIMATED,
7
+ useSharedValue,
8
+ runOnJS,
9
+ startMapper,
10
+ stopMapper,
11
+ } from "./moduleWrapper";
20
12
 
21
13
  /**
22
14
  * Connects a shared value from reanimated to a SkiaView or Canvas
@@ -26,19 +18,21 @@ const useSharedValueWrapper =
26
18
  */
27
19
  export const useSharedValueEffect = <T = number>(
28
20
  cb: () => void,
29
- value: SharedValueTypeWrapper<T>,
30
- ...values: SharedValueTypeWrapper<T>[]
21
+ value: SharedValueType<T>,
22
+ ...values: SharedValueType<T>[]
31
23
  ) => {
32
- const input = useSharedValueWrapper(0);
24
+ console.warn(
25
+ `useSharedValueEffect() is now deprecated, you can use Reanimated values directly.
26
+ Learn more at https://shopify.github.io/react-native-skia/.`
27
+ );
28
+ const input = useSharedValue(0);
33
29
 
34
30
  useEffect(() => {
35
- if (!Reanimated) {
31
+ if (!HAS_REANIMATED) {
36
32
  console.warn(
37
33
  "Reanimated was not found and the useSharedValueEffect hook will have no effect."
38
34
  );
39
35
  } else {
40
- const { runOnJS, startMapper, stopMapper } = Reanimated;
41
-
42
36
  // Start a mapper in Reanimated
43
37
  const mapperId = startMapper(
44
38
  () => {
@@ -0,0 +1,31 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
+ // @ts-ignore
3
+ import CanvasKitInit from "canvaskit-wasm/bin/full/canvaskit";
4
+ import type { ReactNode } from "react";
5
+
6
+ import { JsiSkApi } from "../skia/web";
7
+ import { SkiaRoot } from "../renderer/Reconciler";
8
+ import { JsiDrawingContext } from "../dom/types";
9
+
10
+ export * from "../renderer/components";
11
+
12
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
13
+ let Skia: any;
14
+
15
+ export const draw = (element: ReactNode, width: number, height: number) => {
16
+ if (!Skia) {
17
+ Skia = JsiSkApi(CanvasKit);
18
+ }
19
+ const surface = Skia.Surface.MakeOffscreen(width, height);
20
+ if (surface === null) {
21
+ throw new Error("Couldn't create surface!");
22
+ }
23
+ const root = new SkiaRoot(Skia);
24
+ root.render(element);
25
+ const canvas = surface.getCanvas();
26
+ const ctx = new JsiDrawingContext(Skia, canvas);
27
+ root.dom.render(ctx);
28
+ surface.flush();
29
+ const image = surface.makeImageSnapshot();
30
+ return image;
31
+ };
package/src/mock/index.ts CHANGED
@@ -6,10 +6,8 @@ import * as ValuesHooks from "../values/hooks";
6
6
  import { Selector } from "../values/selector";
7
7
  import * as BaseSkia from "../skia/types";
8
8
  import type * as SkiaExports from "../skia";
9
- import type * as ExternalExports from "../external";
10
9
  import type * as ValueExports from "../values";
11
10
  import type * as AnimationExports from "../animation";
12
- import { useSharedValueEffect } from "../external/reanimated/useSharedValueEffect";
13
11
  import * as timingFunctions from "../animation/timing";
14
12
  import * as springFunctions from "../animation/spring";
15
13
  import * as decayFunctions from "../animation/decay";
@@ -35,7 +33,6 @@ export const Skia: SkiaApi = new Stub() as any;
35
33
  export const vec = (x?: number, y?: number) => ({ x: x ?? 0, y: y ?? x ?? 0 });
36
34
 
37
35
  export const Mock: typeof SkiaExports &
38
- typeof ExternalExports &
39
36
  typeof ValueExports &
40
37
  typeof AnimationExports & {
41
38
  createDrawing: () => any;
@@ -82,8 +79,6 @@ export const Mock: typeof SkiaExports &
82
79
  bottomRight: Noop,
83
80
  center: Noop,
84
81
  processTransform2d: Noop,
85
- // ExternalExports
86
- useSharedValueEffect,
87
82
  // ValueExports
88
83
  ...Values,
89
84
  ...ValuesHooks,
@@ -37,6 +37,10 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
37
37
  const redraw = useCallback(() => {
38
38
  innerRef.current?.redraw();
39
39
  }, [innerRef]);
40
+ const getNativeId = useCallback(() => {
41
+ const id = innerRef.current?.nativeId ?? -1;
42
+ return id;
43
+ }, [innerRef]);
40
44
 
41
45
  const registerValues = useCallback(
42
46
  (values: Array<SkiaValue<unknown>>) => {
@@ -48,8 +52,8 @@ export const Canvas = forwardRef<SkiaDomView, CanvasProps>(
48
52
  [ref]
49
53
  );
50
54
  const root = useMemo(
51
- () => new SkiaRoot(Skia, registerValues, redraw),
52
- [redraw, registerValues]
55
+ () => new SkiaRoot(Skia, registerValues, redraw, getNativeId),
56
+ [redraw, registerValues, getNativeId]
53
57
  );
54
58
 
55
59
  // Render effect
@@ -15,7 +15,8 @@ export class Container {
15
15
  constructor(
16
16
  Skia: Skia,
17
17
  public depMgr: DependencyManager,
18
- public redraw: () => void = () => {}
18
+ public redraw: () => void = () => {},
19
+ public getNativeId: () => number = () => 0
19
20
  ) {
20
21
  this.Sk = new JsiSkDOM({ Skia, depMgr });
21
22
  this._root = this.Sk.Group();
@@ -4,6 +4,10 @@ import { DefaultEventPriority } from "react-reconciler/constants";
4
4
 
5
5
  import type { NodeType, Node } from "../dom/types";
6
6
  import type { SkiaValue } from "../values";
7
+ import {
8
+ bindReanimatedProps,
9
+ extractReanimatedProps,
10
+ } from "../external/reanimated";
7
11
 
8
12
  import type { Container } from "./Container";
9
13
  import { createNode } from "./HostComponents";
@@ -121,8 +125,9 @@ export const skHostConfig: SkiaHostConfig = {
121
125
  _internalInstanceHandle
122
126
  ) {
123
127
  debug("createInstance", type);
124
- const props = { ...pristineProps };
128
+ const [props, reanimatedProps] = extractReanimatedProps(pristineProps);
125
129
  const node = createNode(container, type, materialize(props));
130
+ bindReanimatedProps(container, node, reanimatedProps);
126
131
  container.depMgr.subscribeNode(node, props);
127
132
  return node;
128
133
  },
@@ -193,9 +198,10 @@ export const skHostConfig: SkiaHostConfig = {
193
198
  if (shallowEq(prevProps, nextProps)) {
194
199
  return;
195
200
  }
196
- const props = { ...nextProps };
201
+ const [props, reanimatedProps] = extractReanimatedProps(nextProps);
197
202
  updatePayload.depMgr.unsubscribeNode(instance);
198
203
  instance.setProps(materialize(props));
204
+ bindReanimatedProps(updatePayload, instance, reanimatedProps);
199
205
  updatePayload.depMgr.subscribeNode(instance, props);
200
206
  },
201
207
 
@@ -31,10 +31,11 @@ export class SkiaRoot {
31
31
  constructor(
32
32
  Skia: Skia,
33
33
  registerValues: RegisterValues = () => () => {},
34
- redraw: () => void = () => {}
34
+ redraw: () => void = () => {},
35
+ getNativeId: () => number = () => 0
35
36
  ) {
36
37
  const depMgr = createDependencyManager(registerValues);
37
- this.container = new Container(Skia, depMgr, redraw);
38
+ this.container = new Container(Skia, depMgr, redraw, getNativeId);
38
39
  this.root = skiaReconciler.createContainer(
39
40
  this.container,
40
41
  0,
@@ -1,5 +1,9 @@
1
1
  import type { SkiaSelector, SkiaValue } from "../../../values";
2
2
 
3
+ export type SharedValueType<T = number> = {
4
+ value: T;
5
+ };
6
+
3
7
  export const isValue = (value: unknown): value is SkiaValue<unknown> => {
4
8
  if (value === undefined || value === null) {
5
9
  return false;
@@ -44,7 +48,11 @@ export const isAnimated = <T>(props: AnimatedProps<T>) => {
44
48
  };
45
49
 
46
50
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
47
- export type AnimatedProp<T, P = any> = T | SkiaValue<T> | SkiaSelector<T, P>;
51
+ export type AnimatedProp<T, P = any> =
52
+ | T
53
+ | SkiaValue<T>
54
+ | SkiaSelector<T, P>
55
+ | SharedValueType<T>;
48
56
 
49
57
  export type AnimatedProps<T, O extends keyof T | never = never> = {
50
58
  [K in keyof T]: K extends "children"