@shopify/react-native-skia 0.1.154 → 0.1.156

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.
Files changed (167) hide show
  1. package/android/cpp/jni/JniLoad.cpp +5 -5
  2. package/android/cpp/jni/JniPlatformContext.cpp +107 -119
  3. package/android/cpp/jni/JniSkiaManager.cpp +18 -20
  4. package/android/cpp/jni/include/JniPlatformContext.h +41 -45
  5. package/android/cpp/jni/include/JniSkiaBaseView.h +52 -55
  6. package/android/cpp/jni/include/JniSkiaDrawView.h +72 -77
  7. package/android/cpp/jni/include/JniSkiaManager.h +51 -53
  8. package/android/cpp/jni/include/JniSkiaPictureView.h +74 -78
  9. package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +36 -45
  10. package/android/cpp/rnskia-android/RNSkAndroidView.h +87 -92
  11. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +62 -65
  12. package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +20 -17
  13. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.cpp +257 -313
  14. package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h +107 -110
  15. package/cpp/api/JsiSkApi.h +66 -62
  16. package/cpp/api/JsiSkCanvas.h +38 -30
  17. package/cpp/api/JsiSkColor.h +58 -56
  18. package/cpp/api/JsiSkColorFilter.h +5 -3
  19. package/cpp/api/JsiSkColorFilterFactory.h +23 -21
  20. package/cpp/api/JsiSkContourMeasure.h +74 -85
  21. package/cpp/api/JsiSkContourMeasureIter.h +68 -75
  22. package/cpp/api/JsiSkData.h +16 -22
  23. package/cpp/api/JsiSkDataFactory.h +86 -79
  24. package/cpp/api/JsiSkFont.h +286 -311
  25. package/cpp/api/JsiSkHostObjects.h +15 -16
  26. package/cpp/api/JsiSkImage.h +107 -103
  27. package/cpp/api/JsiSkImageFactory.h +34 -36
  28. package/cpp/api/JsiSkImageFilter.h +5 -3
  29. package/cpp/api/JsiSkImageFilterFactory.h +71 -68
  30. package/cpp/api/JsiSkImageInfo.h +41 -38
  31. package/cpp/api/JsiSkMaskFilter.h +5 -3
  32. package/cpp/api/JsiSkMaskFilterFactory.h +2 -3
  33. package/cpp/api/JsiSkMatrix.h +26 -36
  34. package/cpp/api/JsiSkPaint.h +20 -13
  35. package/cpp/api/JsiSkPath.h +70 -85
  36. package/cpp/api/JsiSkPathEffect.h +5 -3
  37. package/cpp/api/JsiSkPathEffectFactory.h +33 -28
  38. package/cpp/api/JsiSkPathFactory.h +68 -67
  39. package/cpp/api/JsiSkPicture.h +28 -22
  40. package/cpp/api/JsiSkPictureFactory.h +13 -12
  41. package/cpp/api/JsiSkPictureRecorder.h +21 -19
  42. package/cpp/api/JsiSkPoint.h +6 -8
  43. package/cpp/api/JsiSkRRect.h +11 -7
  44. package/cpp/api/JsiSkRSXform.h +82 -85
  45. package/cpp/api/JsiSkRect.h +9 -9
  46. package/cpp/api/JsiSkRuntimeEffect.h +182 -186
  47. package/cpp/api/JsiSkRuntimeEffectFactory.h +10 -11
  48. package/cpp/api/JsiSkRuntimeShaderBuilder.h +64 -61
  49. package/cpp/api/JsiSkSVG.h +4 -5
  50. package/cpp/api/JsiSkSVGFactory.h +28 -27
  51. package/cpp/api/JsiSkShader.h +3 -2
  52. package/cpp/api/JsiSkShaderFactory.h +37 -25
  53. package/cpp/api/JsiSkSurface.h +44 -40
  54. package/cpp/api/JsiSkSurfaceFactory.h +22 -22
  55. package/cpp/api/JsiSkTextBlob.h +28 -31
  56. package/cpp/api/JsiSkTextBlobFactory.h +88 -87
  57. package/cpp/api/JsiSkTypeface.h +6 -5
  58. package/cpp/api/JsiSkTypefaceFactory.h +22 -21
  59. package/cpp/api/JsiSkVertices.h +137 -124
  60. package/cpp/api/third_party/CSSColorParser.cpp +161 -174
  61. package/cpp/api/third_party/CSSColorParser.h +172 -96
  62. package/cpp/jsi/JsiHostObject.cpp +11 -9
  63. package/cpp/jsi/JsiHostObject.h +31 -24
  64. package/cpp/jsi/JsiSimpleValueWrapper.h +74 -83
  65. package/cpp/jsi/JsiValueWrapper.h +52 -54
  66. package/cpp/rnskia/RNSkAnimation.h +26 -29
  67. package/cpp/rnskia/RNSkDispatchQueue.cpp +50 -61
  68. package/cpp/rnskia/RNSkDispatchQueue.h +3 -1
  69. package/cpp/rnskia/RNSkInfoParameter.h +12 -12
  70. package/cpp/rnskia/RNSkJsView.cpp +82 -81
  71. package/cpp/rnskia/RNSkJsView.h +45 -41
  72. package/cpp/rnskia/RNSkJsiViewApi.h +99 -89
  73. package/cpp/rnskia/RNSkManager.cpp +8 -7
  74. package/cpp/rnskia/RNSkManager.h +8 -6
  75. package/cpp/rnskia/RNSkPictureView.h +44 -37
  76. package/cpp/rnskia/RNSkPlatformContext.h +39 -28
  77. package/cpp/rnskia/RNSkValueApi.h +33 -34
  78. package/cpp/rnskia/RNSkView.h +108 -93
  79. package/cpp/rnskia/values/RNSkClockValue.h +63 -64
  80. package/cpp/rnskia/values/RNSkComputedValue.h +32 -30
  81. package/cpp/rnskia/values/RNSkReadonlyValue.h +60 -59
  82. package/cpp/rnskia/values/RNSkValue.h +38 -40
  83. package/cpp/utils/RNSkLog.h +9 -7
  84. package/cpp/utils/RNSkMeasureTime.h +7 -7
  85. package/cpp/utils/RNSkTimingInfo.h +27 -24
  86. package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +8 -9
  87. package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +24 -23
  88. package/ios/RNSkia-iOS/RNSkiOSView.h +16 -13
  89. package/ios/RNSkia-iOS/SkiaUIView.h +9 -8
  90. package/lib/commonjs/dom/nodes/JsiSkDOM.js +6 -0
  91. package/lib/commonjs/dom/nodes/JsiSkDOM.js.map +1 -1
  92. package/lib/commonjs/dom/nodes/LayerNode.js +43 -0
  93. package/lib/commonjs/dom/nodes/LayerNode.js.map +1 -0
  94. package/lib/commonjs/dom/nodes/RenderNode.js +1 -1
  95. package/lib/commonjs/dom/nodes/RenderNode.js.map +1 -1
  96. package/lib/commonjs/dom/types/NodeType.js +1 -0
  97. package/lib/commonjs/dom/types/NodeType.js.map +1 -1
  98. package/lib/commonjs/dom/types/SkDOM.js.map +1 -1
  99. package/lib/commonjs/renderer/HostComponents.js +3 -0
  100. package/lib/commonjs/renderer/HostComponents.js.map +1 -1
  101. package/lib/commonjs/renderer/components/Group.js +19 -4
  102. package/lib/commonjs/renderer/components/Group.js.map +1 -1
  103. package/lib/commonjs/renderer/components/Paint.js +6 -1
  104. package/lib/commonjs/renderer/components/Paint.js.map +1 -1
  105. package/lib/commonjs/views/SkiaPictureView.js +3 -2
  106. package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
  107. package/lib/commonjs/views/SkiaView.js +6 -3
  108. package/lib/commonjs/views/SkiaView.js.map +1 -1
  109. package/lib/module/dom/nodes/JsiSkDOM.js +5 -0
  110. package/lib/module/dom/nodes/JsiSkDOM.js.map +1 -1
  111. package/lib/module/dom/nodes/LayerNode.js +32 -0
  112. package/lib/module/dom/nodes/LayerNode.js.map +1 -0
  113. package/lib/module/dom/nodes/RenderNode.js +1 -1
  114. package/lib/module/dom/nodes/RenderNode.js.map +1 -1
  115. package/lib/module/dom/types/NodeType.js +1 -0
  116. package/lib/module/dom/types/NodeType.js.map +1 -1
  117. package/lib/module/dom/types/SkDOM.js.map +1 -1
  118. package/lib/module/renderer/HostComponents.js +3 -0
  119. package/lib/module/renderer/HostComponents.js.map +1 -1
  120. package/lib/module/renderer/components/Group.js +16 -3
  121. package/lib/module/renderer/components/Group.js.map +1 -1
  122. package/lib/module/renderer/components/Paint.js +7 -1
  123. package/lib/module/renderer/components/Paint.js.map +1 -1
  124. package/lib/module/views/SkiaPictureView.js +2 -2
  125. package/lib/module/views/SkiaPictureView.js.map +1 -1
  126. package/lib/module/views/SkiaView.js +4 -2
  127. package/lib/module/views/SkiaView.js.map +1 -1
  128. package/lib/typescript/src/dom/nodes/JsiSkDOM.d.ts +2 -0
  129. package/lib/typescript/src/dom/nodes/LayerNode.d.ts +8 -0
  130. package/lib/typescript/src/dom/types/NodeType.d.ts +1 -0
  131. package/lib/typescript/src/dom/types/SkDOM.d.ts +1 -0
  132. package/lib/typescript/src/renderer/HostComponents.d.ts +2 -1
  133. package/lib/typescript/src/renderer/components/Group.d.ts +5 -1
  134. package/lib/typescript/src/views/SkiaView.d.ts +3 -0
  135. package/libs/android/arm64-v8a/libskottie.a +0 -0
  136. package/libs/android/arm64-v8a/libsksg.a +0 -0
  137. package/libs/android/armeabi-v7a/libskottie.a +0 -0
  138. package/libs/android/armeabi-v7a/libsksg.a +0 -0
  139. package/libs/android/x86/libskottie.a +0 -0
  140. package/libs/android/x86/libsksg.a +0 -0
  141. package/libs/android/x86_64/libskottie.a +0 -0
  142. package/libs/android/x86_64/libsksg.a +0 -0
  143. package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
  144. package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
  145. package/libs/ios/libskottie.xcframework/Info.plist +42 -0
  146. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
  147. package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
  148. package/libs/ios/libsksg.xcframework/Info.plist +42 -0
  149. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
  150. package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
  151. package/libs/ios/libskshaper.xcframework/Info.plist +5 -5
  152. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
  153. package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
  154. package/libs/ios/libsvg.xcframework/Info.plist +5 -5
  155. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
  156. package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
  157. package/package.json +3 -1
  158. package/src/dom/nodes/JsiSkDOM.ts +5 -0
  159. package/src/dom/nodes/LayerNode.ts +35 -0
  160. package/src/dom/nodes/RenderNode.ts +4 -3
  161. package/src/dom/types/NodeType.ts +1 -0
  162. package/src/dom/types/SkDOM.ts +1 -0
  163. package/src/renderer/HostComponents.ts +3 -0
  164. package/src/renderer/components/Group.tsx +16 -3
  165. package/src/renderer/components/Paint.tsx +7 -1
  166. package/src/views/SkiaPictureView.tsx +2 -3
  167. package/src/views/SkiaView.tsx +2 -2
@@ -6,126 +6,125 @@
6
6
  #include <jsi/jsi.h>
7
7
 
8
8
  #include <algorithm>
9
- #include <functional>
10
9
  #include <chrono>
10
+ #include <functional>
11
+ #include <memory>
11
12
 
12
- namespace RNSkia
13
- {
14
- using namespace facebook;
13
+ namespace RNSkia {
14
+ namespace jsi = facebook::jsi;
15
15
  /**
16
- Implements a readonly Value that is updated every time the screen is redrawn. Its value will be the
17
- number of milliseconds since the animation value was started.
16
+ Implements a readonly Value that is updated every time the screen is redrawn.
17
+ Its value will be the number of milliseconds since the animation value was
18
+ started.
18
19
  */
19
- class RNSkClockValue : public RNSkReadonlyValue
20
- {
21
- enum RNSkClockState {
22
- NotStarted = 0,
23
- Running = 1,
24
- Stopped = 2
25
- };
26
-
20
+ class RNSkClockValue : public RNSkReadonlyValue {
21
+ enum RNSkClockState { NotStarted = 0, Running = 1, Stopped = 2 };
22
+
27
23
  public:
28
24
  RNSkClockValue(std::shared_ptr<RNSkPlatformContext> platformContext,
29
- size_t identifier,
30
- jsi::Runtime& runtime,
31
- const jsi::Value *arguments,
32
- size_t count) : RNSkReadonlyValue(platformContext),
33
- _runtime(runtime),
25
+ size_t identifier, jsi::Runtime &runtime,
26
+ const jsi::Value *arguments, size_t count)
27
+ : RNSkReadonlyValue(platformContext), _runtime(runtime),
34
28
  _identifier(identifier) {
35
29
  // Start by updating to zero (start value)
36
30
  update(_runtime, static_cast<double>(0));
37
31
  }
38
-
39
- virtual ~RNSkClockValue() {
40
- stopClock();
41
- }
42
-
32
+
33
+ virtual ~RNSkClockValue() { stopClock(); }
34
+
43
35
  JSI_HOST_FUNCTION(start) {
44
36
  startClock();
45
37
  return jsi::Value::undefined();
46
38
  }
47
-
39
+
48
40
  JSI_HOST_FUNCTION(stop) {
49
41
  stopClock();
50
42
  return jsi::Value::undefined();
51
43
  }
52
-
44
+
53
45
  JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(RNSkReadonlyValue, addListener),
54
46
  JSI_EXPORT_FUNC(RNSkClockValue, start),
55
47
  JSI_EXPORT_FUNC(RNSkClockValue, stop))
56
-
57
-
48
+
58
49
  virtual void startClock() {
59
- if(_state == RNSkClockState::Running) {
50
+ if (_state == RNSkClockState::Running) {
60
51
  return;
61
52
  }
62
-
53
+
63
54
  auto now = std::chrono::high_resolution_clock::now();
64
- if(_state == RNSkClockState::NotStarted) {
55
+ if (_state == RNSkClockState::NotStarted) {
65
56
  _start = now;
66
57
  _stop = now;
67
58
  }
68
-
59
+
69
60
  // Subtract pause time from start
70
61
  auto timeSinceStop = now - _stop;
71
62
  _start += timeSinceStop;
72
-
63
+
73
64
  _state = RNSkClockState::Running;
74
-
75
- getContext()->beginDrawLoop(_identifier, [weakSelf = weak_from_this()](bool invalidated){
76
- auto self = weakSelf.lock();
77
- if(self) {
78
- std::dynamic_pointer_cast<RNSkClockValue>(self)->notifyUpdate(invalidated);
79
- }
80
- });
65
+
66
+ getContext()->beginDrawLoop(
67
+ _identifier, [weakSelf = weak_from_this()](bool invalidated) {
68
+ auto self = weakSelf.lock();
69
+ if (self) {
70
+ std::dynamic_pointer_cast<RNSkClockValue>(self)->notifyUpdate(
71
+ invalidated);
72
+ }
73
+ });
81
74
  }
82
-
75
+
83
76
  virtual void stopClock() {
84
- if(_state == RNSkClockState::Running) {
77
+ if (_state == RNSkClockState::Running) {
85
78
  _state = RNSkClockState::Stopped;
86
79
  _stop = std::chrono::high_resolution_clock::now();
87
80
  getContext()->endDrawLoop(_identifier);
88
81
  }
89
82
  }
90
-
83
+
91
84
  protected:
92
85
  virtual void tick(jsi::Runtime &runtime, const jsi::Value &value) {
93
86
  RNSkClockValue::update(runtime, value);
94
87
  }
95
-
88
+
96
89
  void notifyUpdate(bool invalidated) {
97
- if(invalidated) {
90
+ if (invalidated) {
98
91
  stopClock();
99
92
  return;
100
93
  }
101
-
102
- if(_state != RNSkClockState::Running) {
94
+
95
+ if (_state != RNSkClockState::Running) {
103
96
  return;
104
97
  }
105
-
98
+
106
99
  // Ensure we call any updates from the draw loop on the javascript thread
107
100
  getContext()->runOnJavascriptThread(
108
- // To ensure that this shared_ptr instance is not deallocated before we are done
109
- // running the update lambda we pass a shared from this to the lambda scope.
110
- [weakSelf = weak_from_this()]() {
111
- auto self = weakSelf.lock();
112
- if(self) {
113
- auto selfClockValue = std::dynamic_pointer_cast<RNSkClockValue>(self);
114
- if(selfClockValue->getState() == RNSkClockState::Running) {
115
- auto now = std::chrono::high_resolution_clock::now();
116
- auto deltaFromStart = std::chrono::duration_cast<std::chrono::milliseconds>(now - selfClockValue->_start).count();
117
- selfClockValue->tick(selfClockValue->_runtime, static_cast<double>(deltaFromStart));
101
+ // To ensure that this shared_ptr instance is not deallocated before we
102
+ // are done running the update lambda we pass a shared from this to the
103
+ // lambda scope.
104
+ [weakSelf = weak_from_this()]() {
105
+ auto self = weakSelf.lock();
106
+ if (self) {
107
+ auto selfClockValue =
108
+ std::dynamic_pointer_cast<RNSkClockValue>(self);
109
+ if (selfClockValue->getState() == RNSkClockState::Running) {
110
+ auto now = std::chrono::high_resolution_clock::now();
111
+ auto deltaFromStart =
112
+ std::chrono::duration_cast<std::chrono::milliseconds>(
113
+ now - selfClockValue->_start)
114
+ .count();
115
+ selfClockValue->tick(selfClockValue->_runtime,
116
+ static_cast<double>(deltaFromStart));
117
+ }
118
118
  }
119
- }
120
- });
119
+ });
121
120
  }
122
-
121
+
123
122
  /**
124
123
  Returns the draw identifier for the clock. This identifier is used
125
124
  for the draw loop.
126
125
  */
127
126
  size_t getIdentifier() { return _identifier; }
128
-
127
+
129
128
  /**
130
129
  Returns the state of the clock
131
130
  */
@@ -135,7 +134,7 @@ protected:
135
134
  size_t _identifier;
136
135
  std::chrono::time_point<std::chrono::steady_clock> _start;
137
136
  std::chrono::time_point<std::chrono::steady_clock> _stop;
138
- std::atomic<RNSkClockState> _state = { RNSkClockState::NotStarted };
137
+ std::atomic<RNSkClockState> _state = {RNSkClockState::NotStarted};
139
138
  };
140
139
 
141
- }
140
+ } // namespace RNSkia
@@ -6,54 +6,55 @@
6
6
  #include <jsi/jsi.h>
7
7
 
8
8
  #include <algorithm>
9
- #include <functional>
10
9
  #include <chrono>
10
+ #include <functional>
11
11
  #include <memory>
12
12
  #include <vector>
13
13
 
14
- namespace RNSkia
15
- {
16
- using namespace facebook;
14
+ namespace RNSkia {
15
+ namespace jsi = facebook::jsi;
17
16
 
18
17
  /**
19
- Creates a readonly value that depends on one or more other values. The derived value has a callback
20
- function that is used to calculate the new value when any of the dependencies change.
18
+ Creates a readonly value that depends on one or more other values. The derived
19
+ value has a callback function that is used to calculate the new value when any
20
+ of the dependencies change.
21
21
  */
22
- class RNSkComputedValue : public RNSkReadonlyValue
23
- {
22
+ class RNSkComputedValue : public RNSkReadonlyValue {
24
23
  public:
25
24
  RNSkComputedValue(std::shared_ptr<RNSkPlatformContext> platformContext,
26
- jsi::Runtime &runtime,
27
- const jsi::Value *arguments,
28
- size_t count
29
- )
25
+ jsi::Runtime &runtime, const jsi::Value *arguments,
26
+ size_t count)
30
27
  : RNSkReadonlyValue(platformContext) {
31
28
  // Verify input
32
- if(!arguments[0].isObject() ||
33
- !arguments[0].asObject(runtime).isFunction(runtime)) {
34
- throw jsi::JSError(runtime, "Expected callback function as first parameter");
29
+ if (!arguments[0].isObject() ||
30
+ !arguments[0].asObject(runtime).isFunction(runtime)) {
31
+ throw jsi::JSError(runtime,
32
+ "Expected callback function as first parameter");
35
33
  }
36
34
 
37
- if(!arguments[1].isObject() ||
38
- !arguments[1].asObject(runtime).isArray(runtime)) {
39
- throw jsi::JSError(runtime, "Expected array of dependencies as second parameter");
35
+ if (!arguments[1].isObject() ||
36
+ !arguments[1].asObject(runtime).isArray(runtime)) {
37
+ throw jsi::JSError(runtime,
38
+ "Expected array of dependencies as second parameter");
40
39
  }
41
40
 
42
41
  // Get callback for calculating result
43
- _callback = std::make_shared<jsi::Function>(arguments[0].asObject(runtime).asFunction(runtime));
42
+ _callback = std::make_shared<jsi::Function>(
43
+ arguments[0].asObject(runtime).asFunction(runtime));
44
44
  }
45
45
 
46
46
  void invalidate() override {
47
47
  RNSkReadonlyValue::invalidate();
48
48
 
49
49
  // Unregister listeners
50
- for(const auto &unsubscribe: _unsubscribers) {
50
+ for (const auto &unsubscribe : _unsubscribers) {
51
51
  unsubscribe();
52
52
  }
53
53
  _unsubscribers.clear();
54
54
  }
55
55
 
56
- void initializeDependencies(jsi::Runtime &runtime, const jsi::Value *arguments, size_t count) {
56
+ void initializeDependencies(jsi::Runtime &runtime,
57
+ const jsi::Value *arguments, size_t count) {
57
58
  // Save dependencies
58
59
  std::vector<std::shared_ptr<RNSkReadonlyValue>> dependencies;
59
60
 
@@ -61,14 +62,14 @@ public:
61
62
  auto deps = arguments[1].asObject(runtime).asArray(runtime);
62
63
  const std::size_t size = deps.size(runtime);
63
64
  dependencies.reserve(size);
64
- for(size_t i=0; i<size; ++i) {
65
+ for (size_t i = 0; i < size; ++i) {
65
66
  auto dep = deps.getValueAtIndex(runtime, i);
66
- if(!dep.isObject() ||
67
- !dep.asObject(runtime).isHostObject(runtime)) {
67
+ if (!dep.isObject() || !dep.asObject(runtime).isHostObject(runtime)) {
68
68
  continue;
69
69
  }
70
- auto value = dep.asObject(runtime).asHostObject<RNSkReadonlyValue>(runtime);
71
- if(value == nullptr) {
70
+ auto value =
71
+ dep.asObject(runtime).asHostObject<RNSkReadonlyValue>(runtime);
72
+ if (value == nullptr) {
72
73
  continue;
73
74
  }
74
75
  dependencies.push_back(value);
@@ -76,10 +77,11 @@ public:
76
77
 
77
78
  // register change handler on dependencies
78
79
  _unsubscribers.reserve(_unsubscribers.size() + size);
79
- for(const auto &dep: dependencies) {
80
- _unsubscribers.push_back(dep->addListener([weakSelf = weak_from_this()](jsi::Runtime& runtime) {
80
+ for (const auto &dep : dependencies) {
81
+ _unsubscribers.push_back(dep->addListener([weakSelf = weak_from_this()](
82
+ jsi::Runtime &runtime) {
81
83
  auto self = weakSelf.lock();
82
- if(self) {
84
+ if (self) {
83
85
  auto selfAsThis = std::dynamic_pointer_cast<RNSkComputedValue>(self);
84
86
  selfAsThis->dependencyUpdated(runtime);
85
87
  }
@@ -99,4 +101,4 @@ private:
99
101
  std::shared_ptr<jsi::Function> _callback;
100
102
  std::vector<std::function<void()>> _unsubscribers;
101
103
  };
102
- }
104
+ } // namespace RNSkia
@@ -2,76 +2,76 @@
2
2
  #pragma once
3
3
 
4
4
  #include <algorithm>
5
- #include <functional>
6
5
  #include <chrono>
7
- #include <unordered_map>
6
+ #include <functional>
8
7
  #include <memory>
8
+ #include <unordered_map>
9
+ #include <utility>
9
10
 
10
11
  #include <jsi/jsi.h>
11
12
 
13
+ #include <JsiSimpleValueWrapper.h>
12
14
  #include <JsiSkHostObjects.h>
13
15
  #include <RNSkPlatformContext.h>
14
- #include <JsiSimpleValueWrapper.h>
15
16
 
16
- namespace RNSkia
17
- {
18
- using namespace facebook;
17
+ namespace RNSkia {
18
+ namespace jsi = facebook::jsi;
19
19
 
20
20
  /**
21
- Implements a readonly Value that is updated every time the screen is redrawn. Its value will be the
22
- number of milliseconds since the animation value was started.
21
+ Implements a readonly Value that is updated every time the screen is redrawn.
22
+ Its value will be the number of milliseconds since the animation value was
23
+ started.
23
24
  */
24
- class RNSkReadonlyValue : public JsiSkHostObject,
25
- public std::enable_shared_from_this<RNSkReadonlyValue>
26
- {
25
+ class RNSkReadonlyValue
26
+ : public JsiSkHostObject,
27
+ public std::enable_shared_from_this<RNSkReadonlyValue> {
27
28
  public:
28
- RNSkReadonlyValue(std::shared_ptr<RNSkPlatformContext> platformContext)
29
+ explicit RNSkReadonlyValue(
30
+ std::shared_ptr<RNSkPlatformContext> platformContext)
29
31
  : JsiSkHostObject(platformContext),
30
- _valueHolder(std::make_unique<JsiSimpleValueWrapper>(*platformContext->getJsRuntime()))
31
- { }
32
+ _valueHolder(std::make_unique<RNJsi::JsiSimpleValueWrapper>(
33
+ *platformContext->getJsRuntime())) {}
32
34
 
33
- virtual ~RNSkReadonlyValue() {
34
- invalidate();
35
- }
35
+ virtual ~RNSkReadonlyValue() { invalidate(); }
36
36
 
37
37
  JSI_PROPERTY_GET(__typename__) {
38
38
  return jsi::String::createFromUtf8(runtime, "RNSkValue");
39
39
  }
40
40
 
41
- JSI_PROPERTY_GET(current) {
42
- return getCurrent(runtime);
43
- }
41
+ JSI_PROPERTY_GET(current) { return getCurrent(runtime); }
44
42
 
45
- JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(RNSkReadonlyValue, __typename__),
43
+ JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(RNSkReadonlyValue,
44
+ __typename__),
46
45
  JSI_EXPORT_PROP_GET(RNSkReadonlyValue, current))
47
46
 
48
47
  JSI_HOST_FUNCTION(addListener) {
49
- if(!arguments[0].isObject() || !arguments[0].asObject(runtime).isFunction(runtime)) {
48
+ if (!arguments[0].isObject() ||
49
+ !arguments[0].asObject(runtime).isFunction(runtime)) {
50
50
  throw jsi::JSError(runtime, "Expected function as first parameter.");
51
51
  return jsi::Value::undefined();
52
52
  }
53
- auto callback = std::make_shared<jsi::Function>(arguments[0].asObject(runtime).asFunction(runtime));
54
-
55
- auto unsubscribe = addListener([weakSelf = weak_from_this(),
56
- callback = std::move(callback)](jsi::Runtime& runtime){
57
- auto self = weakSelf.lock();
58
- if(self) {
59
- auto selfReadonlyValue = std::dynamic_pointer_cast<RNSkReadonlyValue>(self);
60
- callback->call(runtime, selfReadonlyValue->get_current(runtime));
61
- }
62
- });
63
-
64
- return jsi::Function::createFromHostFunction(runtime,
65
- jsi::PropNameID::forUtf8(runtime, "unsubscribe"),
66
- 0,
67
- JSI_HOST_FUNCTION_LAMBDA {
68
- unsubscribe();
69
- return jsi::Value::undefined();
70
- });
53
+ auto callback = std::make_shared<jsi::Function>(
54
+ arguments[0].asObject(runtime).asFunction(runtime));
55
+
56
+ auto unsubscribe =
57
+ addListener([weakSelf = weak_from_this(),
58
+ callback = std::move(callback)](jsi::Runtime &runtime) {
59
+ auto self = weakSelf.lock();
60
+ if (self) {
61
+ auto selfReadonlyValue =
62
+ std::dynamic_pointer_cast<RNSkReadonlyValue>(self);
63
+ callback->call(runtime, selfReadonlyValue->get_current(runtime));
64
+ }
65
+ });
66
+
67
+ return jsi::Function::createFromHostFunction(
68
+ runtime, jsi::PropNameID::forUtf8(runtime, "unsubscribe"), 0,
69
+ JSI_HOST_FUNCTION_LAMBDA {
70
+ unsubscribe();
71
+ return jsi::Value::undefined();
72
+ });
71
73
  }
72
74
 
73
-
74
-
75
75
  JSI_HOST_FUNCTION(__invalidate) {
76
76
  invalidate();
77
77
  return jsi::Value::undefined();
@@ -81,17 +81,18 @@ public:
81
81
  JSI_EXPORT_FUNC(RNSkReadonlyValue, __invalidate))
82
82
 
83
83
  /**
84
- * Adds a callback that will be called whenever the value changes
85
- * @param cb Callback
86
- * @return unsubscribe function
87
- */
88
- const std::function<void()> addListener(const std::function<void(jsi::Runtime&)> cb) {
84
+ * Adds a callback that will be called whenever the value changes
85
+ * @param cb Callback
86
+ * @return unsubscribe function
87
+ */
88
+ const std::function<void()>
89
+ addListener(const std::function<void(jsi::Runtime &)> cb) {
89
90
  std::lock_guard<std::mutex> lock(_mutex);
90
91
  auto listenerId = _listenerId++;
91
92
  _listeners.emplace(listenerId, cb);
92
93
  return [weakSelf = weak_from_this(), listenerId]() {
93
94
  auto self = weakSelf.lock();
94
- if(self) {
95
+ if (self) {
95
96
  self->removeListener(listenerId);
96
97
  }
97
98
  };
@@ -99,22 +100,23 @@ public:
99
100
 
100
101
  /**
101
102
  Updates the underlying value and notifies all listeners about the change.
102
- Listeners are only notified if the value was actually changed for numeric, boolean and string
103
- values. For all other values listeners are notified without comparison.
103
+ Listeners are only notified if the value was actually changed for numeric,
104
+ boolean and string values. For all other values listeners are notified
105
+ without comparison.
104
106
  @param runtime Current JS Runtime
105
107
  @param value Next value
106
108
  */
107
109
  virtual void update(jsi::Runtime &runtime, const jsi::Value &value) {
108
110
  auto equal = _valueHolder->equals(runtime, value);
109
111
  _valueHolder->setCurrent(runtime, value);
110
- if(!equal) {
112
+ if (!equal) {
111
113
  notifyListeners(runtime);
112
114
  }
113
115
  }
114
116
 
115
117
  /**
116
- Override to implement invalidation logic for the value. In the base class this function
117
- clears all subscribers.
118
+ Override to implement invalidation logic for the value. In the base class
119
+ this function clears all subscribers.
118
120
  */
119
121
  virtual void invalidate() {
120
122
  std::lock_guard<std::mutex> lock(_mutex);
@@ -126,18 +128,17 @@ public:
126
128
  }
127
129
 
128
130
  protected:
129
-
130
131
  /**
131
132
  Notifies listeners about changes
132
133
  @param runtime Current JS Runtime
133
134
  */
134
135
  void notifyListeners(jsi::Runtime &runtime) {
135
- std::unordered_map<long, std::function<void(jsi::Runtime&)>> tmp;
136
+ std::unordered_map<long, std::function<void(jsi::Runtime &)>> tmp;
136
137
  {
137
138
  std::lock_guard<std::mutex> lock(_mutex);
138
139
  tmp.insert(_listeners.cbegin(), _listeners.cend());
139
140
  }
140
- for(const auto &listener: tmp) {
141
+ for (const auto &listener : tmp) {
141
142
  listener.second(runtime);
142
143
  }
143
144
  }
@@ -152,10 +153,10 @@ protected:
152
153
  }
153
154
 
154
155
  private:
155
- std::unique_ptr<JsiSimpleValueWrapper> _valueHolder;
156
+ std::unique_ptr<RNJsi::JsiSimpleValueWrapper> _valueHolder;
156
157
 
157
158
  long _listenerId = 0;
158
- std::unordered_map<long, std::function<void(jsi::Runtime&)>> _listeners;
159
+ std::unordered_map<long, std::function<void(jsi::Runtime &)>> _listeners;
159
160
  std::mutex _mutex;
160
161
  };
161
- }
162
+ } // namespace RNSkia
@@ -4,33 +4,29 @@
4
4
  #include <memory>
5
5
 
6
6
  #include <JsiHostObject.h>
7
+ #include <RNSkAnimation.h>
7
8
  #include <RNSkPlatformContext.h>
8
9
  #include <RNSkReadonlyValue.h>
9
- #include <RNSkAnimation.h>
10
10
  #include <jsi/jsi.h>
11
11
 
12
- namespace RNSkia
13
- {
14
- using namespace facebook;
12
+ namespace RNSkia {
13
+ namespace jsi = facebook::jsi;
15
14
  /**
16
- Implements a Value that can be both read and written to. It inherits from the ReadonlyValue with
17
- functionailty for subscribing to changes.
15
+ Implements a Value that can be both read and written to. It inherits from the
16
+ ReadonlyValue with functionailty for subscribing to changes.
18
17
  */
19
- class RNSkValue : public RNSkReadonlyValue
20
- {
18
+ class RNSkValue : public RNSkReadonlyValue {
21
19
  public:
22
20
  RNSkValue(std::shared_ptr<RNSkPlatformContext> platformContext,
23
- jsi::Runtime& runtime, const jsi::Value *arguments, size_t count)
21
+ jsi::Runtime &runtime, const jsi::Value *arguments, size_t count)
24
22
  : RNSkReadonlyValue(platformContext) {
25
- if(count == 1) {
26
- update(runtime, arguments[0]);
27
- }
28
- }
29
-
30
- ~RNSkValue() {
31
- unsubscribe();
23
+ if (count == 1) {
24
+ update(runtime, arguments[0]);
25
+ }
32
26
  }
33
27
 
28
+ ~RNSkValue() { unsubscribe(); }
29
+
34
30
  JSI_PROPERTY_SET(current) {
35
31
  // When someone else is setting the value we need to stop any ongoing
36
32
  // animations
@@ -43,13 +39,15 @@ public:
43
39
  unsubscribe();
44
40
 
45
41
  // Verify input
46
- if(value.isObject() && value.asObject(runtime).isHostObject<RNSkAnimation>(runtime)) {
47
- auto animation = value.asObject(runtime).getHostObject<RNSkAnimation>(runtime);
48
- if(animation != nullptr) {
42
+ if (value.isObject() &&
43
+ value.asObject(runtime).isHostObject<RNSkAnimation>(runtime)) {
44
+ auto animation =
45
+ value.asObject(runtime).getHostObject<RNSkAnimation>(runtime);
46
+ if (animation != nullptr) {
49
47
  // Now we have a value animation - let us connect and start
50
48
  subscribe(animation);
51
49
  }
52
- } else if(value.isUndefined() || value.isNull()) {
50
+ } else if (value.isUndefined() || value.isNull()) {
53
51
  // Do nothing - we've already unsubscribed
54
52
  } else {
55
53
  throw jsi::JSError(runtime, "Animation expected.");
@@ -57,7 +55,7 @@ public:
57
55
  }
58
56
 
59
57
  JSI_PROPERTY_GET(animation) {
60
- if(_animation != nullptr) {
58
+ if (_animation != nullptr) {
61
59
  return jsi::Object::createFromHostObject(runtime, _animation);
62
60
  }
63
61
  return jsi::Value::undefined();
@@ -66,46 +64,46 @@ public:
66
64
  JSI_EXPORT_PROPERTY_SETTERS(JSI_EXPORT_PROP_SET(RNSkValue, current),
67
65
  JSI_EXPORT_PROP_SET(RNSkValue, animation))
68
66
 
69
- JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(RNSkReadonlyValue, __typename__),
67
+ JSI_EXPORT_PROPERTY_GETTERS(JSI_EXPORT_PROP_GET(RNSkReadonlyValue,
68
+ __typename__),
70
69
  JSI_EXPORT_PROP_GET(RNSkValue, current),
71
70
  JSI_EXPORT_PROP_GET(RNSkValue, animation))
72
71
 
73
- JSI_EXPORT_FUNCTIONS(
74
- JSI_EXPORT_FUNC(RNSkValue, addListener),
75
- JSI_EXPORT_FUNC(RNSkReadonlyValue, __invalidate)
76
- )
72
+ JSI_EXPORT_FUNCTIONS(JSI_EXPORT_FUNC(RNSkValue, addListener),
73
+ JSI_EXPORT_FUNC(RNSkReadonlyValue, __invalidate))
77
74
 
78
75
  private:
79
76
  void subscribe(std::shared_ptr<RNSkAnimation> animation) {
80
- if(animation != nullptr) {
77
+ if (animation != nullptr) {
81
78
  _animation = animation;
82
- _unsubscribe = std::make_shared<std::function<void()>>(
83
- _animation->addListener([weakSelf = weak_from_this()](jsi::Runtime &runtime) {
84
- auto self = weakSelf.lock();
85
- if(self) {
86
- auto selfAsThis = std::dynamic_pointer_cast<RNSkValue>(self);
87
- selfAsThis->animationDidUpdate(runtime);
88
- }
89
- }));
79
+ _unsubscribe =
80
+ std::make_shared<std::function<void()>>(_animation->addListener(
81
+ [weakSelf = weak_from_this()](jsi::Runtime &runtime) {
82
+ auto self = weakSelf.lock();
83
+ if (self) {
84
+ auto selfAsThis = std::dynamic_pointer_cast<RNSkValue>(self);
85
+ selfAsThis->animationDidUpdate(runtime);
86
+ }
87
+ }));
90
88
  // Start the animation
91
89
  _animation->startClock();
92
90
  }
93
91
  }
94
92
 
95
- void animationDidUpdate(jsi::Runtime& runtime) {
96
- if(_animation != nullptr) {
93
+ void animationDidUpdate(jsi::Runtime &runtime) {
94
+ if (_animation != nullptr) {
97
95
  // Update ourselves from the current animation value
98
96
  update(runtime, _animation->get_current(runtime));
99
97
  }
100
98
  }
101
99
 
102
100
  void unsubscribe() {
103
- if(_unsubscribe != nullptr) {
101
+ if (_unsubscribe != nullptr) {
104
102
  (*_unsubscribe)();
105
103
  _unsubscribe = nullptr;
106
104
  }
107
105
 
108
- if(_animation != nullptr) {
106
+ if (_animation != nullptr) {
109
107
  _animation->stopClock();
110
108
  _animation = nullptr;
111
109
  }
@@ -115,4 +113,4 @@ private:
115
113
  std::shared_ptr<std::function<void()>> _unsubscribe;
116
114
  };
117
115
 
118
- }
116
+ } // namespace RNSkia