react-native-yoga-jsi 0.1.2 → 0.2.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.
@@ -14,60 +14,61 @@ export function createLayout<T extends string>(styles: NodeTree<T>) {
14
14
  return styles;
15
15
  }
16
16
 
17
- class Layout<T extends string> {
18
- layout: Record<T, Node>;
19
- rootKey: T;
20
-
21
- constructor(rootKey: T) {
22
- const config = Yoga.Config.create();
23
- config.setPointScaleFactor(0);
24
- const root = Yoga.Node.create(config);
25
- this.rootKey = rootKey;
26
- this.layout = {
27
- [rootKey]: root,
28
- } as Record<T, Node>;
29
- }
30
-
31
- addNodeTo(parentKey: T, key: T, index: number) {
32
- const parent = this.layout[parentKey];
33
- const child = Yoga.Node.create();
34
- parent.insertChild(child, index);
35
- this.layout[key] = child;
36
- return child;
37
- }
38
-
39
- getNode(key: T) {
40
- return this.layout[key];
41
- }
42
-
43
- free() {
44
- this.getNode(this.rootKey).freeRecursive();
45
- }
17
+ type TreeItem<T extends string> = NodeTree<T> | NodeTree<T>[];
46
18
 
47
- forEach(cb: (node: Node, key: T) => void) {
48
- for (const key in this.layout) {
49
- cb(this.layout[key], key);
50
- }
51
- }
19
+ function Layout<T extends string>(rootKey: T){
20
+ 'worklet';
21
+ const config = Yoga.Config.create();
22
+ config.setPointScaleFactor(0);
23
+ const root = Yoga.Node.create(config);
24
+ const layout = {
25
+ [rootKey]: root,
26
+ } as Record<T, Node>;
27
+ return {
28
+ layout,
29
+ rootKey,
30
+ getNode: (key: T) => {
31
+ 'worklet';
32
+ return layout[key];
33
+ },
34
+ addNodeTo: (parentKey: T, key: T, index: number) => {
35
+ 'worklet';
36
+ const parent = layout[parentKey];
37
+ const child = Yoga.Node.create();
38
+ parent.insertChild(child, index);
39
+ layout[key] = child;
40
+ return child;
41
+ },
42
+ free: () => {
43
+ 'worklet';
44
+ root.freeRecursive();
45
+ },
46
+ forEach: (cb: (node: Node, key: T) => void) => {
47
+ 'worklet';
48
+ for (const key in layout) {
49
+ cb(layout[key], key);
50
+ }
51
+ },
52
+ };
52
53
  }
53
54
 
54
- type TreeItem<T extends string> = NodeTree<T> | NodeTree<T>[];
55
-
56
55
  export function generateStyledLayout<T extends string>(layout: NodeTree<T>) {
57
- const layoutTree = new Layout(layout.key);
56
+ 'worklet';
57
+ const layoutTree = Layout(layout.key);
58
58
  applyStyle(layoutTree.layout[layout.key], layout.style);
59
- (function _parse(
60
- treeItem: TreeItem<T>,
59
+ function _parse<U extends T>(
60
+ treeItem: TreeItem<U>,
61
61
  index: number,
62
62
  isArray: boolean,
63
- parentKey: T | null,
63
+ parentKey: U | null
64
64
  ) {
65
+ 'worklet';
65
66
  if (isArray) {
66
- (treeItem as NodeTree<T>[]).forEach((o, i) => {
67
+ (treeItem as NodeTree<U>[]).forEach((o, i) => {
67
68
  _parse(o, i, false, parentKey);
68
69
  });
69
70
  } else {
70
- treeItem = treeItem as NodeTree<T>;
71
+ treeItem = treeItem as NodeTree<U>;
71
72
  if (parentKey !== null) {
72
73
  const addedNode = layoutTree.addNodeTo(parentKey, treeItem.key, index);
73
74
  applyStyle(addedNode, treeItem.style);
@@ -76,9 +77,11 @@ export function generateStyledLayout<T extends string>(layout: NodeTree<T>) {
76
77
  _parse(treeItem.children, 0, true, treeItem.key);
77
78
  }
78
79
  }
79
-
80
+
80
81
  return layoutTree;
81
- })(layout, 0, false, null);
82
+ }
83
+
84
+ _parse(layout, 0, false, null);
82
85
 
83
86
  return layoutTree;
84
87
  }
package/src/index.ts CHANGED
@@ -1,18 +1,16 @@
1
1
  import { NativeModules } from 'react-native';
2
- import type { Yoga } from './coreTypes';
3
- // import * as derived from './derived';
4
- // import * as coreTypes from './coreTypes';
2
+ import type { Yoga as YogaType } from './coreTypes';
5
3
 
6
4
  (function () {
7
5
  NativeModules.YogaJSI.install();
8
6
  })();
9
7
 
10
- //@ts-expect-error gotta fix it later
11
- const YogaModule: {
12
- Yoga: Yoga;
13
- } = global;
8
+ declare global {
9
+ const Yoga: YogaType;
10
+ }
14
11
 
15
- export default YogaModule.Yoga;
12
+
13
+ export default Yoga;
16
14
  export * from './derived';
17
15
  export * from './coreTypes';
18
16