@shopify/react-native-skia 0.1.133 → 0.1.134

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. package/android/CMakeLists.txt +1 -1
  2. package/android/build.gradle +17 -2
  3. package/cpp/rnskia/RNSkValueApi.h +6 -6
  4. package/cpp/rnskia/values/{RNSkDerivedValue.h → RNSkComputedValue.h} +7 -7
  5. package/ios/RNSkia-iOS/SkiaManager.mm +1 -1
  6. package/lib/commonjs/mock/index.js +190 -0
  7. package/lib/commonjs/mock/index.js.map +1 -0
  8. package/lib/commonjs/skia/core/Data.js +33 -43
  9. package/lib/commonjs/skia/core/Data.js.map +1 -1
  10. package/lib/commonjs/skia/core/Typeface.js +1 -1
  11. package/lib/commonjs/skia/core/Typeface.js.map +1 -1
  12. package/lib/commonjs/skia/web/JsiSkia.js.map +1 -1
  13. package/lib/commonjs/values/api.js +3 -3
  14. package/lib/commonjs/values/api.js.map +1 -1
  15. package/lib/commonjs/values/api.web.js +3 -3
  16. package/lib/commonjs/values/api.web.js.map +1 -1
  17. package/lib/commonjs/values/hooks/index.js +4 -4
  18. package/lib/commonjs/values/hooks/index.js.map +1 -1
  19. package/lib/commonjs/values/hooks/useComputedValue.js +32 -0
  20. package/lib/commonjs/values/hooks/useComputedValue.js.map +1 -0
  21. package/lib/commonjs/values/web/{RNSkDerivedValue.js → RNSkComputedValue.js} +4 -4
  22. package/lib/commonjs/values/web/RNSkComputedValue.js.map +1 -0
  23. package/lib/commonjs/values/web/api.js +3 -3
  24. package/lib/commonjs/values/web/api.js.map +1 -1
  25. package/lib/module/mock/index.js +152 -0
  26. package/lib/module/mock/index.js.map +1 -0
  27. package/lib/module/skia/core/Data.js +32 -41
  28. package/lib/module/skia/core/Data.js.map +1 -1
  29. package/lib/module/skia/core/Typeface.js +1 -1
  30. package/lib/module/skia/core/Typeface.js.map +1 -1
  31. package/lib/module/skia/web/JsiSkia.js.map +1 -1
  32. package/lib/module/values/api.js +1 -1
  33. package/lib/module/values/api.js.map +1 -1
  34. package/lib/module/values/api.web.js +1 -1
  35. package/lib/module/values/api.web.js.map +1 -1
  36. package/lib/module/values/hooks/index.js +1 -1
  37. package/lib/module/values/hooks/index.js.map +1 -1
  38. package/lib/module/values/hooks/useComputedValue.js +18 -0
  39. package/lib/module/values/hooks/useComputedValue.js.map +1 -0
  40. package/lib/module/values/web/{RNSkDerivedValue.js → RNSkComputedValue.js} +2 -2
  41. package/lib/module/values/web/RNSkComputedValue.js.map +1 -0
  42. package/lib/module/values/web/api.js +3 -3
  43. package/lib/module/values/web/api.js.map +1 -1
  44. package/lib/typescript/src/mock/index.d.ts +47 -0
  45. package/lib/typescript/src/skia/core/Data.d.ts +3 -3
  46. package/lib/typescript/src/skia/types/Skia.d.ts +1 -1
  47. package/lib/typescript/src/values/api.d.ts +1 -1
  48. package/lib/typescript/src/values/api.web.d.ts +1 -1
  49. package/lib/typescript/src/values/hooks/index.d.ts +1 -1
  50. package/lib/typescript/src/values/hooks/{useDerivedValue.d.ts → useComputedValue.d.ts} +2 -1
  51. package/lib/typescript/src/values/types.d.ts +2 -2
  52. package/lib/typescript/src/values/web/{RNSkDerivedValue.d.ts → RNSkComputedValue.d.ts} +1 -1
  53. package/package.json +2 -1
  54. package/src/mock/index.ts +156 -0
  55. package/src/skia/core/Data.ts +67 -50
  56. package/src/skia/core/Typeface.ts +6 -1
  57. package/src/skia/types/Skia.ts +1 -1
  58. package/src/skia/web/JsiSkia.ts +1 -1
  59. package/src/values/api.ts +1 -1
  60. package/src/values/api.web.ts +1 -1
  61. package/src/values/hooks/index.ts +1 -1
  62. package/src/values/hooks/{useDerivedValue.ts → useComputedValue.ts} +8 -3
  63. package/src/values/types.ts +2 -2
  64. package/src/values/web/{RNSkDerivedValue.ts → RNSkComputedValue.ts} +1 -1
  65. package/src/values/web/api.ts +3 -3
  66. package/lib/commonjs/values/hooks/useDerivedValue.js +0 -25
  67. package/lib/commonjs/values/hooks/useDerivedValue.js.map +0 -1
  68. package/lib/commonjs/values/web/RNSkDerivedValue.js.map +0 -1
  69. package/lib/module/values/hooks/useDerivedValue.js +0 -14
  70. package/lib/module/values/hooks/useDerivedValue.js.map +0 -1
  71. package/lib/module/values/web/RNSkDerivedValue.js.map +0 -1
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Mock implementation for test runners.
3
+ *
4
+ * Example:
5
+ *
6
+ * ```js
7
+ * jest.mock('@shopify/react-native-skia', () => require('@shopify/react-native-skia/lib/commonjs/mock'));
8
+ * ```
9
+ */
10
+ import type { Skia as SkiaApi, SkRect } from "../skia/types";
11
+ export declare const Skia: SkiaApi;
12
+ export declare const vec: (x: number, y: number) => {
13
+ x: number;
14
+ y: number;
15
+ };
16
+ export declare const rect: (x: number, y: number, width: number, height: number) => {
17
+ x: number;
18
+ y: number;
19
+ width: number;
20
+ height: number;
21
+ };
22
+ export declare const rrect: (r: SkRect, rx: number, ry: number) => {
23
+ rect: SkRect;
24
+ rx: number;
25
+ ry: number;
26
+ };
27
+ export declare const useTouchHandler: () => any;
28
+ export declare const useComputedValue: () => any;
29
+ export declare const useValue: () => any;
30
+ export declare const useClockValue: () => any;
31
+ export declare const useValueEffect: () => any;
32
+ export declare const useTiming: () => any;
33
+ export declare const runTiming: () => any;
34
+ export declare const timing: () => any;
35
+ export declare const useSpring: () => any;
36
+ export declare const runSpring: () => any;
37
+ export declare const spring: () => any;
38
+ export declare const runDecay: () => any;
39
+ export declare const decay: () => any;
40
+ export declare const useSharedValueEffect: () => any;
41
+ export declare const useData: () => any;
42
+ export declare const useDataCollection: () => any;
43
+ export declare const useFont: () => any;
44
+ export declare const useImage: () => any;
45
+ export declare const usePicture: () => any;
46
+ export declare const useSVG: () => any;
47
+ export declare const useTypeface: () => any;
@@ -1,5 +1,5 @@
1
1
  import type { DependencyList } from "react";
2
2
  import type { SkData, DataSource } from "../types";
3
- export declare const useDataCollection: <T>(sources: DataSource[], factory: (data: SkData[]) => T, deps?: DependencyList) => T | null;
4
- export declare const useRawData: <T>(source: DataSource | null | undefined, factory: (data: SkData) => T, onError?: ((err: Error) => void) | undefined) => T | null;
5
- export declare const useData: (source: DataSource | null | undefined, onError?: ((err: Error) => void) | undefined) => SkData | null;
3
+ export declare const useDataCollection: <T>(sources: DataSource[], factory: (data: SkData) => T, onError?: ((err: Error) => void) | undefined, deps?: DependencyList | undefined) => (T | null)[] | null;
4
+ export declare const useRawData: <T>(source: DataSource | null | undefined, factory: (data: SkData) => T, onError?: ((err: Error) => void) | undefined, deps?: DependencyList | undefined) => T | null;
5
+ export declare const useData: (source: DataSource | null | undefined, onError?: ((err: Error) => void) | undefined, deps?: DependencyList | undefined) => SkData | null;
@@ -39,7 +39,7 @@ export interface Skia {
39
39
  PictureRecorder: () => SkPictureRecorder;
40
40
  Picture: PictureFactory;
41
41
  Path: PathFactory;
42
- Matrix: (matrix?: number[]) => SkMatrix;
42
+ Matrix: (matrix?: readonly number[]) => SkMatrix;
43
43
  ColorFilter: ColorFilterFactory;
44
44
  Font: (typeface?: SkTypeface, size?: number) => SkFont;
45
45
  Typeface: TypefaceFactory;
@@ -3,4 +3,4 @@ declare global {
3
3
  var SkiaValueApi: ISkiaValueApi;
4
4
  }
5
5
  export declare const ValueApi: ISkiaValueApi;
6
- export declare const createValue: <T>(initialValue: T) => import("./types").SkiaMutableValue<T>, createDerivedValue: <R>(cb: () => R, values: import("./types").SkiaValue<unknown>[]) => import("./types").SkiaValue<R>;
6
+ export declare const createValue: <T>(initialValue: T) => import("./types").SkiaMutableValue<T>, createComputedValue: <R>(cb: () => R, values: import("./types").SkiaValue<unknown>[]) => import("./types").SkiaValue<R>;
@@ -1,2 +1,2 @@
1
1
  export declare const ValueApi: import("./types").ISkiaValueApi;
2
- export declare const createValue: <T>(initialValue: T) => import("./types").SkiaMutableValue<T>, createDerivedValue: <R>(cb: () => R, values: import("./types").SkiaValue<unknown>[]) => import("./types").SkiaValue<R>;
2
+ export declare const createValue: <T>(initialValue: T) => import("./types").SkiaMutableValue<T>, createComputedValue: <R>(cb: () => R, values: import("./types").SkiaValue<unknown>[]) => import("./types").SkiaValue<R>;
@@ -1,4 +1,4 @@
1
1
  export * from "./useClockValue";
2
- export * from "./useDerivedValue";
2
+ export * from "./useComputedValue";
3
3
  export * from "./useValue";
4
4
  export * from "./useValueEffect";
@@ -1,8 +1,9 @@
1
1
  /**
2
- * Creates a new derived value - a value that will calculate its value depending
2
+ * Creates a new computed value - a value that will calculate its value depending
3
3
  * on other values.
4
4
  * @param cb Callback to calculate new value
5
5
  * @param values Dependant values
6
6
  * @returns A readonly value
7
7
  */
8
+ export declare const useComputedValue: <R>(cb: () => R, values: unknown[]) => import("..").SkiaValue<R>;
8
9
  export declare const useDerivedValue: <R>(cb: () => R, values: unknown[]) => import("..").SkiaValue<R>;
@@ -41,10 +41,10 @@ export interface ISkiaValueApi {
41
41
  */
42
42
  createValue: <T>(initialValue: T) => SkiaMutableValue<T>;
43
43
  /**
44
- * Creates a derived value. This is a calculated value that returns the result of
44
+ * Creates a computed value. This is a calculated value that returns the result of
45
45
  * a function that is called with the values of the dependencies.
46
46
  */
47
- createDerivedValue: <R>(cb: () => R, values: Array<SkiaValue<unknown>>) => SkiaValue<R>;
47
+ createComputedValue: <R>(cb: () => R, values: Array<SkiaValue<unknown>>) => SkiaValue<R>;
48
48
  /**
49
49
  * Creates a clock value where the value is the number of milliseconds elapsed
50
50
  * since the clock was created
@@ -1,6 +1,6 @@
1
1
  import type { DependencyList } from "react";
2
2
  import { RNSkReadonlyValue } from "./RNSkReadonlyValue";
3
- export declare class RNSkDerivedValue<T> extends RNSkReadonlyValue<T> {
3
+ export declare class RNSkComputedValue<T> extends RNSkReadonlyValue<T> {
4
4
  constructor(callback: () => T, dependencies: DependencyList);
5
5
  private dependecyUpdated;
6
6
  private _callback;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "access": "public"
5
5
  },
6
6
  "title": "React Native Skia",
7
- "version": "0.1.133",
7
+ "version": "0.1.134",
8
8
  "description": "High-performance React Native Graphics using Skia",
9
9
  "main": "lib/module/index.js",
10
10
  "files": [
@@ -70,6 +70,7 @@
70
70
  "@types/react-reconciler": "^0.26.4",
71
71
  "eslint": "7.32.0",
72
72
  "eslint-config-react-native-wcandillon": "^3.7.2",
73
+ "eslint-plugin-reanimated": "^1.2.6",
73
74
  "jest": "^27.4.3",
74
75
  "patch-package": "^6.4.7",
75
76
  "react": "17.0.2",
@@ -0,0 +1,156 @@
1
+ /**
2
+ * Mock implementation for test runners.
3
+ *
4
+ * Example:
5
+ *
6
+ * ```js
7
+ * jest.mock('@shopify/react-native-skia', () => require('@shopify/react-native-skia/lib/commonjs/mock'));
8
+ * ```
9
+ */
10
+
11
+ import type { Skia as SkiaApi, SkRect } from "../skia/types";
12
+
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ const MockJSIInstance: any = {};
15
+ const Noop = () => MockJSIInstance;
16
+
17
+ export const Skia: SkiaApi = {
18
+ Point: Noop,
19
+ XYWHRect: Noop,
20
+ RuntimeShaderBuilder: Noop,
21
+ RRectXY: Noop,
22
+ RSXform: Noop,
23
+ Color: Noop,
24
+ ContourMeasureIter: Noop,
25
+ Paint: Noop,
26
+ PictureRecorder: Noop,
27
+ Picture: {
28
+ MakePicture: Noop,
29
+ },
30
+ Path: {
31
+ Make: Noop,
32
+ MakeFromSVGString: Noop,
33
+ MakeFromOp: Noop,
34
+ MakeFromCmds: Noop,
35
+ MakeFromText: Noop,
36
+ },
37
+ Matrix: Noop,
38
+ ColorFilter: {
39
+ MakeMatrix: Noop,
40
+ MakeBlend: Noop,
41
+ MakeCompose: Noop,
42
+ MakeLerp: Noop,
43
+ MakeLinearToSRGBGamma: Noop,
44
+ MakeSRGBToLinearGamma: Noop,
45
+ MakeLumaColorFilter: Noop,
46
+ },
47
+ Font: Noop,
48
+ Typeface: {
49
+ MakeFreeTypeFaceFromData: Noop,
50
+ },
51
+ MaskFilter: {
52
+ MakeBlur: Noop,
53
+ },
54
+ RuntimeEffect: {
55
+ Make: Noop,
56
+ },
57
+ ImageFilter: {
58
+ MakeOffset: Noop,
59
+ MakeDisplacementMap: Noop,
60
+ MakeShader: Noop,
61
+ MakeBlur: Noop,
62
+ MakeColorFilter: Noop,
63
+ MakeCompose: Noop,
64
+ MakeDropShadow: Noop,
65
+ MakeDropShadowOnly: Noop,
66
+ MakeErode: Noop,
67
+ MakeDilate: Noop,
68
+ MakeBlend: Noop,
69
+ MakeRuntimeShader: Noop,
70
+ },
71
+ Shader: {
72
+ MakeLinearGradient: Noop,
73
+ MakeRadialGradient: Noop,
74
+ MakeTwoPointConicalGradient: Noop,
75
+ MakeSweepGradient: Noop,
76
+ MakeTurbulence: Noop,
77
+ MakeFractalNoise: Noop,
78
+ MakeBlend: Noop,
79
+ MakeColor: Noop,
80
+ },
81
+ PathEffect: {
82
+ MakeCorner: Noop,
83
+ MakeDash: Noop,
84
+ MakeDiscrete: Noop,
85
+ MakeCompose: Noop,
86
+ MakeSum: Noop,
87
+ MakeLine2D: Noop,
88
+ MakePath1D: Noop,
89
+ MakePath2D: Noop,
90
+ },
91
+ MakeVertices: Noop,
92
+ Data: {
93
+ fromURI: Noop,
94
+ fromBytes: Noop,
95
+ fromBase64: Noop,
96
+ },
97
+ Image: {
98
+ MakeImageFromEncoded: Noop,
99
+ MakeImage: Noop,
100
+ },
101
+ SVG: {
102
+ MakeFromData: Noop,
103
+ MakeFromString: Noop,
104
+ },
105
+ FontMgr: {
106
+ RefDefault: Noop,
107
+ },
108
+ TextBlob: {
109
+ MakeFromText: Noop,
110
+ MakeFromGlyphs: Noop,
111
+ MakeFromRSXform: Noop,
112
+ MakeFromRSXformGlyphs: Noop,
113
+ },
114
+ Surface: {
115
+ Make: Noop,
116
+ },
117
+ };
118
+
119
+ export const vec = (x: number, y: number) => ({ x, y });
120
+
121
+ export const rect = (x: number, y: number, width: number, height: number) => ({
122
+ x,
123
+ y,
124
+ width,
125
+ height,
126
+ });
127
+
128
+ export const rrect = (r: SkRect, rx: number, ry: number) => ({
129
+ rect: r,
130
+ rx,
131
+ ry,
132
+ });
133
+
134
+ export const useTouchHandler = Noop;
135
+ export const useComputedValue = Noop;
136
+ export const useValue = Noop;
137
+ export const useClockValue = Noop;
138
+ export const useValueEffect = Noop;
139
+ export const useTiming = Noop;
140
+ export const runTiming = Noop;
141
+ export const timing = Noop;
142
+ export const useSpring = Noop;
143
+ export const runSpring = Noop;
144
+ export const spring = Noop;
145
+ export const runDecay = Noop;
146
+ export const decay = Noop;
147
+
148
+ export const useSharedValueEffect = Noop;
149
+
150
+ export const useData = Noop;
151
+ export const useDataCollection = Noop;
152
+ export const useFont = Noop;
153
+ export const useImage = Noop;
154
+ export const usePicture = Noop;
155
+ export const useSVG = Noop;
156
+ export const useTypeface = Noop;
@@ -11,71 +11,88 @@ const resolveAsset = (source: ReturnType<typeof require>) => {
11
11
  : Image.resolveAssetSource(source).uri;
12
12
  };
13
13
 
14
- export const useDataCollection = <T>(
15
- sources: DataSource[],
16
- factory: (data: SkData[]) => T,
17
- deps: DependencyList = []
14
+ const factoryWrapper = <T>(
15
+ data2: SkData,
16
+ factory: (data: SkData) => T,
17
+ onError?: (err: Error) => void
18
18
  ) => {
19
- const [data, setData] = useState<T | null>(null);
20
- useEffect(() => {
21
- const bytesOrURIs = sources.map((source) => {
22
- if (source instanceof Uint8Array) {
23
- return source;
24
- }
25
- return typeof source === "string" ? source : resolveAsset(source);
26
- });
27
- Promise.all(
28
- bytesOrURIs.map((bytesOrURI) =>
29
- bytesOrURI instanceof Uint8Array
30
- ? Skia.Data.fromBytes(bytesOrURI)
31
- : Skia.Data.fromURI(bytesOrURI)
32
- )
33
- ).then((d) => setData(factory(d)));
34
- // eslint-disable-next-line react-hooks/exhaustive-deps
35
- }, deps);
36
- return data;
19
+ const factoryResult = factory(data2);
20
+ if (factoryResult === null) {
21
+ onError && onError(new Error("Could not load data"));
22
+ return null;
23
+ } else {
24
+ return factoryResult;
25
+ }
37
26
  };
38
27
 
39
- export const useRawData = <T>(
40
- source: DataSource | null | undefined,
28
+ const loadDataCollection = <T>(
29
+ sources: DataSource[],
30
+ factory: (data: SkData) => T,
31
+ onError?: (err: Error) => void
32
+ ): Promise<(T | null)[]> =>
33
+ Promise.all(sources.map((source) => loadData(source, factory, onError)));
34
+
35
+ const loadData = <T>(
36
+ source: DataSource,
41
37
  factory: (data: SkData) => T,
42
38
  onError?: (err: Error) => void
39
+ ): Promise<T | null> => {
40
+ if (source instanceof Uint8Array) {
41
+ return new Promise((resolve) =>
42
+ resolve(factoryWrapper(Skia.Data.fromBytes(source), factory, onError))
43
+ );
44
+ } else {
45
+ const uri = typeof source === "string" ? source : resolveAsset(source);
46
+ return Skia.Data.fromURI(uri).then((d) =>
47
+ factoryWrapper(d, factory, onError)
48
+ );
49
+ }
50
+ };
51
+
52
+ type Source = DataSource | null | undefined;
53
+
54
+ const useLoading = <T>(
55
+ source: Source,
56
+ loader: () => Promise<T | null>,
57
+ deps: DependencyList = []
43
58
  ) => {
44
59
  const [data, setData] = useState<T | null>(null);
45
- const prevSourceRef = useRef<DataSource | null | undefined>();
60
+ const prevSourceRef = useRef<Source>();
46
61
  useEffect(() => {
47
- // Track to avoid re-fetching the same data
48
62
  if (prevSourceRef.current !== source) {
49
63
  prevSourceRef.current = source;
50
- if (source !== null && source !== undefined) {
51
- const factoryWrapper = (data2: SkData) => {
52
- const factoryResult = factory(data2);
53
- if (factoryResult === null) {
54
- onError && onError(new Error("Could not load data"));
55
- setData(null);
56
- } else {
57
- setData(factoryResult);
58
- }
59
- };
60
- if (source instanceof Uint8Array) {
61
- factoryWrapper(Skia.Data.fromBytes(source));
62
- } else {
63
- const uri =
64
- typeof source === "string" ? source : resolveAsset(source);
65
- Skia.Data.fromURI(uri).then((d) => factoryWrapper(d));
66
- }
67
- } else {
68
- // new source is null or undefined -> remove cached data
69
- setData(null);
70
- }
64
+ loader().then(setData);
65
+ } else {
66
+ setData(null);
71
67
  }
72
- }, [factory, onError, source]);
68
+ // eslint-disable-next-line react-hooks/exhaustive-deps
69
+ }, deps);
73
70
  return data;
74
71
  };
75
72
 
73
+ export const useDataCollection = <T>(
74
+ sources: DataSource[],
75
+ factory: (data: SkData) => T,
76
+ onError?: (err: Error) => void,
77
+ deps?: DependencyList
78
+ ) =>
79
+ useLoading(
80
+ sources,
81
+ () => loadDataCollection(sources, factory, onError),
82
+ deps
83
+ );
84
+
85
+ export const useRawData = <T>(
86
+ source: DataSource | null | undefined,
87
+ factory: (data: SkData) => T,
88
+ onError?: (err: Error) => void,
89
+ deps?: DependencyList
90
+ ) => useLoading(source, () => loadData(source, factory, onError), deps);
91
+
76
92
  const identity = (data: SkData) => data;
77
93
 
78
94
  export const useData = (
79
95
  source: DataSource | null | undefined,
80
- onError?: (err: Error) => void
81
- ) => useRawData(source, identity, onError);
96
+ onError?: (err: Error) => void,
97
+ deps?: DependencyList
98
+ ) => useRawData(source, identity, onError, deps);
@@ -8,4 +8,9 @@ import { useRawData } from "./Data";
8
8
  export const useTypeface = (
9
9
  source: DataSource | null | undefined,
10
10
  onError?: (err: Error) => void
11
- ) => useRawData(source, Skia.Typeface.MakeFreeTypeFaceFromData, onError);
11
+ ) =>
12
+ useRawData(
13
+ source,
14
+ Skia.Typeface.MakeFreeTypeFaceFromData.bind(Skia.Typeface),
15
+ onError
16
+ );
@@ -48,7 +48,7 @@ export interface Skia {
48
48
  PictureRecorder: () => SkPictureRecorder;
49
49
  Picture: PictureFactory;
50
50
  Path: PathFactory;
51
- Matrix: (matrix?: number[]) => SkMatrix;
51
+ Matrix: (matrix?: readonly number[]) => SkMatrix;
52
52
  ColorFilter: ColorFilterFactory;
53
53
  Font: (typeface?: SkTypeface, size?: number) => SkFont;
54
54
  Typeface: TypefaceFactory;
@@ -63,7 +63,7 @@ export const JsiSkApi = (CanvasKit: CanvasKit): Skia => ({
63
63
  new JsiSkPictureRecorder(CanvasKit, new CanvasKit.PictureRecorder()),
64
64
  Picture: new JsiSkPictureFactory(CanvasKit),
65
65
  Path: new JsiSkPathFactory(CanvasKit),
66
- Matrix: (matrix?: number[]) =>
66
+ Matrix: (matrix?: readonly number[]) =>
67
67
  new JsiSkMatrix(
68
68
  CanvasKit,
69
69
  matrix
package/src/values/api.ts CHANGED
@@ -7,4 +7,4 @@ declare global {
7
7
  const { SkiaValueApi } = global;
8
8
  export const ValueApi = SkiaValueApi;
9
9
 
10
- export const { createValue, createDerivedValue } = ValueApi;
10
+ export const { createValue, createComputedValue } = ValueApi;
@@ -2,4 +2,4 @@ import { ValueApi as ValueApiWeb } from "./web";
2
2
 
3
3
  export const ValueApi = ValueApiWeb;
4
4
 
5
- export const { createValue, createDerivedValue } = ValueApi;
5
+ export const { createValue, createComputedValue } = ValueApi;
@@ -1,4 +1,4 @@
1
1
  export * from "./useClockValue";
2
- export * from "./useDerivedValue";
2
+ export * from "./useComputedValue";
3
3
  export * from "./useValue";
4
4
  export * from "./useValueEffect";
@@ -4,15 +4,20 @@ import { ValueApi } from "../api";
4
4
  import { isValue } from "../../renderer/processors";
5
5
 
6
6
  /**
7
- * Creates a new derived value - a value that will calculate its value depending
7
+ * Creates a new computed value - a value that will calculate its value depending
8
8
  * on other values.
9
9
  * @param cb Callback to calculate new value
10
10
  * @param values Dependant values
11
11
  * @returns A readonly value
12
12
  */
13
- export const useDerivedValue = <R>(cb: () => R, values: unknown[]) =>
13
+ export const useComputedValue = <R>(cb: () => R, values: unknown[]) =>
14
14
  useMemo(
15
- () => ValueApi.createDerivedValue<R>(cb, values.filter(isValue)),
15
+ () => ValueApi.createComputedValue<R>(cb, values.filter(isValue)),
16
16
  // eslint-disable-next-line react-hooks/exhaustive-deps
17
17
  values
18
18
  );
19
+
20
+ export const useDerivedValue = <R>(cb: () => R, values: unknown[]) => {
21
+ console.warn("useDerivedValue is deprecated. Use useComputedValue instead.");
22
+ return useComputedValue(cb, values);
23
+ };
@@ -46,10 +46,10 @@ export interface ISkiaValueApi {
46
46
  */
47
47
  createValue: <T>(initialValue: T) => SkiaMutableValue<T>;
48
48
  /**
49
- * Creates a derived value. This is a calculated value that returns the result of
49
+ * Creates a computed value. This is a calculated value that returns the result of
50
50
  * a function that is called with the values of the dependencies.
51
51
  */
52
- createDerivedValue: <R>(
52
+ createComputedValue: <R>(
53
53
  cb: () => R,
54
54
  values: Array<SkiaValue<unknown>>
55
55
  ) => SkiaValue<R>;
@@ -2,7 +2,7 @@ import type { DependencyList } from "react";
2
2
 
3
3
  import { RNSkReadonlyValue } from "./RNSkReadonlyValue";
4
4
 
5
- export class RNSkDerivedValue<T> extends RNSkReadonlyValue<T> {
5
+ export class RNSkComputedValue<T> extends RNSkReadonlyValue<T> {
6
6
  constructor(callback: () => T, dependencies: DependencyList) {
7
7
  // Initialize dependencies - we can't call this yet, since
8
8
  // super if not called and it requires a start value to be set.
@@ -9,18 +9,18 @@ import type {
9
9
 
10
10
  import { RNSkAnimation } from "./RNSkAnimation";
11
11
  import { RNSkClockValue } from "./RNSkClockValue";
12
- import { RNSkDerivedValue } from "./RNSkDerivedValue";
12
+ import { RNSkComputedValue } from "./RNSkComputedValue";
13
13
  import { RNSkValue } from "./RNSkValue";
14
14
 
15
15
  export const ValueApi: ISkiaValueApi = {
16
16
  createValue: function <T>(initialValue: T): SkiaMutableValue<T> {
17
17
  return new RNSkValue(initialValue);
18
18
  },
19
- createDerivedValue: function <R>(
19
+ createComputedValue: function <R>(
20
20
  cb: () => R,
21
21
  values: SkiaValue<unknown>[]
22
22
  ): SkiaValue<R> {
23
- return new RNSkDerivedValue(cb, values);
23
+ return new RNSkComputedValue(cb, values);
24
24
  },
25
25
  createClockValue: function (): SkiaClockValue {
26
26
  return new RNSkClockValue(requestAnimationFrame.bind(window));
@@ -1,25 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useDerivedValue = void 0;
7
-
8
- var _react = require("react");
9
-
10
- var _api = require("../api");
11
-
12
- var _processors = require("../../renderer/processors");
13
-
14
- /**
15
- * Creates a new derived value - a value that will calculate its value depending
16
- * on other values.
17
- * @param cb Callback to calculate new value
18
- * @param values Dependant values
19
- * @returns A readonly value
20
- */
21
- const useDerivedValue = (cb, values) => (0, _react.useMemo)(() => _api.ValueApi.createDerivedValue(cb, values.filter(_processors.isValue)), // eslint-disable-next-line react-hooks/exhaustive-deps
22
- values);
23
-
24
- exports.useDerivedValue = useDerivedValue;
25
- //# sourceMappingURL=useDerivedValue.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["useDerivedValue.ts"],"names":["useDerivedValue","cb","values","ValueApi","createDerivedValue","filter","isValue"],"mappings":";;;;;;;AAAA;;AAEA;;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,eAAe,GAAG,CAAIC,EAAJ,EAAiBC,MAAjB,KAC7B,oBACE,MAAMC,cAASC,kBAAT,CAA+BH,EAA/B,EAAmCC,MAAM,CAACG,MAAP,CAAcC,mBAAd,CAAnC,CADR,EAEE;AACAJ,MAHF,CADK","sourcesContent":["import { useMemo } from \"react\";\n\nimport { ValueApi } from \"../api\";\nimport { isValue } from \"../../renderer/processors\";\n\n/**\n * Creates a new derived value - a value that will calculate its value depending\n * on other values.\n * @param cb Callback to calculate new value\n * @param values Dependant values\n * @returns A readonly value\n */\nexport const useDerivedValue = <R>(cb: () => R, values: unknown[]) =>\n useMemo(\n () => ValueApi.createDerivedValue<R>(cb, values.filter(isValue)),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n values\n );\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["RNSkDerivedValue.ts"],"names":["RNSkDerivedValue","RNSkReadonlyValue","constructor","callback","dependencies","unsubscribers","notifyUpdateRef","current","undefined","forEach","dep","push","addListener","_unsubscribers","dependecyUpdated","bind","_callback","update","unsubscribe"],"mappings":";;;;;;;AAEA;;;;AAEO,MAAMA,gBAAN,SAAkCC,oCAAlC,CAAuD;AAC5DC,EAAAA,WAAW,CAACC,QAAD,EAAoBC,YAApB,EAAkD;AAC3D;AACA;AACA,UAAMC,aAAgC,GAAG,EAAzC;AACA,UAAMC,eAAsD,GAAG;AAC7DC,MAAAA,OAAO,EAAEC;AADoD,KAA/D;AAGAJ,IAAAA,YAAY,CAACK,OAAb,CAAsBC,GAAD,IAAS;AAC5B,UAAI,kBAAkBA,GAAlB,IAAyB,iBAAiBA,GAA9C,EAAmD;AACjDL,QAAAA,aAAa,CAACM,IAAd,CACGD,GAAD,CAAoCE,WAApC,CAAgD;AAAA;;AAAA,0CAC9CN,eAAe,CAACC,OAD8B,0DAC9C,2BAAAD,eAAe,CAD+B;AAAA,SAAhD,CADF;AAKD;AACF,KARD;AASA,UAAMH,QAAQ,EAAd;;AAhB2D;;AAAA;;AAiB3D,SAAKU,cAAL,GAAsBR,aAAtB;AACAC,IAAAA,eAAe,CAACC,OAAhB,GAA0B,KAAKO,gBAAL,CAAsBC,IAAtB,CAA2B,IAA3B,CAA1B;AACA,SAAKC,SAAL,GAAiBb,QAAjB;AACD;;AAEOW,EAAAA,gBAAgB,GAAG;AACzB,SAAKG,MAAL,CAAY,KAAKD,SAAL,EAAZ;AACD;;AAKME,EAAAA,WAAW,GAAG;AACnB,SAAKL,cAAL,CAAoBJ,OAApB,CAA6BS,WAAD,IAAiBA,WAAW,EAAxD;AACD;;AAhC2D","sourcesContent":["import type { DependencyList } from \"react\";\n\nimport { RNSkReadonlyValue } from \"./RNSkReadonlyValue\";\n\nexport class RNSkDerivedValue<T> extends RNSkReadonlyValue<T> {\n constructor(callback: () => T, dependencies: DependencyList) {\n // Initialize dependencies - we can't call this yet, since\n // super if not called and it requires a start value to be set.\n const unsubscribers: Array<() => void> = [];\n const notifyUpdateRef: { current: (() => void) | undefined } = {\n current: undefined,\n };\n dependencies.forEach((dep) => {\n if (\"__typename__\" in dep && \"addListener\" in dep) {\n unsubscribers.push(\n (dep as RNSkReadonlyValue<unknown>).addListener(() =>\n notifyUpdateRef.current?.()\n )\n );\n }\n });\n super(callback());\n this._unsubscribers = unsubscribers;\n notifyUpdateRef.current = this.dependecyUpdated.bind(this);\n this._callback = callback;\n }\n\n private dependecyUpdated() {\n this.update(this._callback());\n }\n\n private _callback: () => T;\n private _unsubscribers: Array<() => void>;\n\n public unsubscribe() {\n this._unsubscribers.forEach((unsubscribe) => unsubscribe());\n }\n}\n"]}
@@ -1,14 +0,0 @@
1
- import { useMemo } from "react";
2
- import { ValueApi } from "../api";
3
- import { isValue } from "../../renderer/processors";
4
- /**
5
- * Creates a new derived value - a value that will calculate its value depending
6
- * on other values.
7
- * @param cb Callback to calculate new value
8
- * @param values Dependant values
9
- * @returns A readonly value
10
- */
11
-
12
- export const useDerivedValue = (cb, values) => useMemo(() => ValueApi.createDerivedValue(cb, values.filter(isValue)), // eslint-disable-next-line react-hooks/exhaustive-deps
13
- values);
14
- //# sourceMappingURL=useDerivedValue.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["useDerivedValue.ts"],"names":["useMemo","ValueApi","isValue","useDerivedValue","cb","values","createDerivedValue","filter"],"mappings":"AAAA,SAASA,OAAT,QAAwB,OAAxB;AAEA,SAASC,QAAT,QAAyB,QAAzB;AACA,SAASC,OAAT,QAAwB,2BAAxB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,MAAMC,eAAe,GAAG,CAAIC,EAAJ,EAAiBC,MAAjB,KAC7BL,OAAO,CACL,MAAMC,QAAQ,CAACK,kBAAT,CAA+BF,EAA/B,EAAmCC,MAAM,CAACE,MAAP,CAAcL,OAAd,CAAnC,CADD,EAEL;AACAG,MAHK,CADF","sourcesContent":["import { useMemo } from \"react\";\n\nimport { ValueApi } from \"../api\";\nimport { isValue } from \"../../renderer/processors\";\n\n/**\n * Creates a new derived value - a value that will calculate its value depending\n * on other values.\n * @param cb Callback to calculate new value\n * @param values Dependant values\n * @returns A readonly value\n */\nexport const useDerivedValue = <R>(cb: () => R, values: unknown[]) =>\n useMemo(\n () => ValueApi.createDerivedValue<R>(cb, values.filter(isValue)),\n // eslint-disable-next-line react-hooks/exhaustive-deps\n values\n );\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["RNSkDerivedValue.ts"],"names":["RNSkReadonlyValue","RNSkDerivedValue","constructor","callback","dependencies","unsubscribers","notifyUpdateRef","current","undefined","forEach","dep","push","addListener","_unsubscribers","dependecyUpdated","bind","_callback","update","unsubscribe"],"mappings":";;AAEA,SAASA,iBAAT,QAAkC,qBAAlC;AAEA,OAAO,MAAMC,gBAAN,SAAkCD,iBAAlC,CAAuD;AAC5DE,EAAAA,WAAW,CAACC,QAAD,EAAoBC,YAApB,EAAkD;AAC3D;AACA;AACA,UAAMC,aAAgC,GAAG,EAAzC;AACA,UAAMC,eAAsD,GAAG;AAC7DC,MAAAA,OAAO,EAAEC;AADoD,KAA/D;AAGAJ,IAAAA,YAAY,CAACK,OAAb,CAAsBC,GAAD,IAAS;AAC5B,UAAI,kBAAkBA,GAAlB,IAAyB,iBAAiBA,GAA9C,EAAmD;AACjDL,QAAAA,aAAa,CAACM,IAAd,CACGD,GAAD,CAAoCE,WAApC,CAAgD;AAAA;;AAAA,0CAC9CN,eAAe,CAACC,OAD8B,0DAC9C,2BAAAD,eAAe,CAD+B;AAAA,SAAhD,CADF;AAKD;AACF,KARD;AASA,UAAMH,QAAQ,EAAd;;AAhB2D;;AAAA;;AAiB3D,SAAKU,cAAL,GAAsBR,aAAtB;AACAC,IAAAA,eAAe,CAACC,OAAhB,GAA0B,KAAKO,gBAAL,CAAsBC,IAAtB,CAA2B,IAA3B,CAA1B;AACA,SAAKC,SAAL,GAAiBb,QAAjB;AACD;;AAEOW,EAAAA,gBAAgB,GAAG;AACzB,SAAKG,MAAL,CAAY,KAAKD,SAAL,EAAZ;AACD;;AAKME,EAAAA,WAAW,GAAG;AACnB,SAAKL,cAAL,CAAoBJ,OAApB,CAA6BS,WAAD,IAAiBA,WAAW,EAAxD;AACD;;AAhC2D","sourcesContent":["import type { DependencyList } from \"react\";\n\nimport { RNSkReadonlyValue } from \"./RNSkReadonlyValue\";\n\nexport class RNSkDerivedValue<T> extends RNSkReadonlyValue<T> {\n constructor(callback: () => T, dependencies: DependencyList) {\n // Initialize dependencies - we can't call this yet, since\n // super if not called and it requires a start value to be set.\n const unsubscribers: Array<() => void> = [];\n const notifyUpdateRef: { current: (() => void) | undefined } = {\n current: undefined,\n };\n dependencies.forEach((dep) => {\n if (\"__typename__\" in dep && \"addListener\" in dep) {\n unsubscribers.push(\n (dep as RNSkReadonlyValue<unknown>).addListener(() =>\n notifyUpdateRef.current?.()\n )\n );\n }\n });\n super(callback());\n this._unsubscribers = unsubscribers;\n notifyUpdateRef.current = this.dependecyUpdated.bind(this);\n this._callback = callback;\n }\n\n private dependecyUpdated() {\n this.update(this._callback());\n }\n\n private _callback: () => T;\n private _unsubscribers: Array<() => void>;\n\n public unsubscribe() {\n this._unsubscribers.forEach((unsubscribe) => unsubscribe());\n }\n}\n"]}