@shopify/react-native-skia 0.1.155 → 0.1.156
Sign up to get free protection for your applications and to get access to all the features.
- package/android/cpp/jni/JniLoad.cpp +5 -5
- package/android/cpp/jni/JniPlatformContext.cpp +107 -119
- package/android/cpp/jni/JniSkiaManager.cpp +18 -20
- package/android/cpp/jni/include/JniPlatformContext.h +41 -45
- package/android/cpp/jni/include/JniSkiaBaseView.h +52 -55
- package/android/cpp/jni/include/JniSkiaDrawView.h +72 -77
- package/android/cpp/jni/include/JniSkiaManager.h +51 -53
- package/android/cpp/jni/include/JniSkiaPictureView.h +74 -78
- package/android/cpp/rnskia-android/RNSkAndroidPlatformContext.h +36 -45
- package/android/cpp/rnskia-android/RNSkAndroidView.h +87 -92
- package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.cpp +62 -65
- package/android/cpp/rnskia-android/RNSkOpenGLCanvasProvider.h +20 -17
- package/android/cpp/rnskia-android/SkiaOpenGLRenderer.cpp +257 -313
- package/android/cpp/rnskia-android/SkiaOpenGLRenderer.h +107 -110
- package/cpp/api/JsiSkApi.h +66 -62
- package/cpp/api/JsiSkCanvas.h +38 -30
- package/cpp/api/JsiSkColor.h +58 -56
- package/cpp/api/JsiSkColorFilter.h +5 -3
- package/cpp/api/JsiSkColorFilterFactory.h +23 -21
- package/cpp/api/JsiSkContourMeasure.h +74 -85
- package/cpp/api/JsiSkContourMeasureIter.h +68 -75
- package/cpp/api/JsiSkData.h +16 -22
- package/cpp/api/JsiSkDataFactory.h +86 -79
- package/cpp/api/JsiSkFont.h +286 -311
- package/cpp/api/JsiSkHostObjects.h +15 -16
- package/cpp/api/JsiSkImage.h +107 -103
- package/cpp/api/JsiSkImageFactory.h +34 -36
- package/cpp/api/JsiSkImageFilter.h +5 -3
- package/cpp/api/JsiSkImageFilterFactory.h +71 -68
- package/cpp/api/JsiSkImageInfo.h +41 -38
- package/cpp/api/JsiSkMaskFilter.h +5 -3
- package/cpp/api/JsiSkMaskFilterFactory.h +2 -3
- package/cpp/api/JsiSkMatrix.h +26 -36
- package/cpp/api/JsiSkPaint.h +20 -13
- package/cpp/api/JsiSkPath.h +70 -85
- package/cpp/api/JsiSkPathEffect.h +5 -3
- package/cpp/api/JsiSkPathEffectFactory.h +33 -28
- package/cpp/api/JsiSkPathFactory.h +68 -67
- package/cpp/api/JsiSkPicture.h +28 -22
- package/cpp/api/JsiSkPictureFactory.h +13 -12
- package/cpp/api/JsiSkPictureRecorder.h +21 -19
- package/cpp/api/JsiSkPoint.h +6 -8
- package/cpp/api/JsiSkRRect.h +11 -7
- package/cpp/api/JsiSkRSXform.h +82 -85
- package/cpp/api/JsiSkRect.h +9 -9
- package/cpp/api/JsiSkRuntimeEffect.h +182 -186
- package/cpp/api/JsiSkRuntimeEffectFactory.h +10 -11
- package/cpp/api/JsiSkRuntimeShaderBuilder.h +64 -61
- package/cpp/api/JsiSkSVG.h +4 -5
- package/cpp/api/JsiSkSVGFactory.h +28 -27
- package/cpp/api/JsiSkShader.h +3 -2
- package/cpp/api/JsiSkShaderFactory.h +37 -25
- package/cpp/api/JsiSkSurface.h +44 -40
- package/cpp/api/JsiSkSurfaceFactory.h +22 -22
- package/cpp/api/JsiSkTextBlob.h +28 -31
- package/cpp/api/JsiSkTextBlobFactory.h +88 -87
- package/cpp/api/JsiSkTypeface.h +6 -5
- package/cpp/api/JsiSkTypefaceFactory.h +22 -21
- package/cpp/api/JsiSkVertices.h +137 -124
- package/cpp/api/third_party/CSSColorParser.cpp +161 -174
- package/cpp/api/third_party/CSSColorParser.h +172 -96
- package/cpp/jsi/JsiHostObject.cpp +11 -9
- package/cpp/jsi/JsiHostObject.h +31 -24
- package/cpp/jsi/JsiSimpleValueWrapper.h +74 -83
- package/cpp/jsi/JsiValueWrapper.h +52 -54
- package/cpp/rnskia/RNSkAnimation.h +26 -29
- package/cpp/rnskia/RNSkDispatchQueue.cpp +50 -61
- package/cpp/rnskia/RNSkDispatchQueue.h +3 -1
- package/cpp/rnskia/RNSkInfoParameter.h +12 -12
- package/cpp/rnskia/RNSkJsView.cpp +82 -81
- package/cpp/rnskia/RNSkJsView.h +45 -41
- package/cpp/rnskia/RNSkJsiViewApi.h +99 -89
- package/cpp/rnskia/RNSkManager.cpp +8 -7
- package/cpp/rnskia/RNSkManager.h +8 -6
- package/cpp/rnskia/RNSkPictureView.h +44 -37
- package/cpp/rnskia/RNSkPlatformContext.h +39 -28
- package/cpp/rnskia/RNSkValueApi.h +33 -34
- package/cpp/rnskia/RNSkView.h +108 -93
- package/cpp/rnskia/values/RNSkClockValue.h +63 -64
- package/cpp/rnskia/values/RNSkComputedValue.h +32 -30
- package/cpp/rnskia/values/RNSkReadonlyValue.h +60 -59
- package/cpp/rnskia/values/RNSkValue.h +38 -40
- package/cpp/utils/RNSkLog.h +9 -7
- package/cpp/utils/RNSkMeasureTime.h +7 -7
- package/cpp/utils/RNSkTimingInfo.h +27 -24
- package/ios/RNSkia-iOS/RNSkMetalCanvasProvider.h +8 -9
- package/ios/RNSkia-iOS/RNSkiOSPlatformContext.h +24 -23
- package/ios/RNSkia-iOS/RNSkiOSView.h +16 -13
- package/ios/RNSkia-iOS/SkiaUIView.h +9 -8
- package/lib/commonjs/dom/nodes/JsiSkDOM.js +6 -0
- package/lib/commonjs/dom/nodes/JsiSkDOM.js.map +1 -1
- package/lib/commonjs/dom/nodes/LayerNode.js +43 -0
- package/lib/commonjs/dom/nodes/LayerNode.js.map +1 -0
- package/lib/commonjs/dom/types/NodeType.js +1 -0
- package/lib/commonjs/dom/types/NodeType.js.map +1 -1
- package/lib/commonjs/dom/types/SkDOM.js.map +1 -1
- package/lib/commonjs/renderer/HostComponents.js +3 -0
- package/lib/commonjs/renderer/HostComponents.js.map +1 -1
- package/lib/commonjs/renderer/components/Group.js +19 -4
- package/lib/commonjs/renderer/components/Group.js.map +1 -1
- package/lib/commonjs/renderer/components/Paint.js +6 -1
- package/lib/commonjs/renderer/components/Paint.js.map +1 -1
- package/lib/commonjs/views/SkiaPictureView.js +3 -2
- package/lib/commonjs/views/SkiaPictureView.js.map +1 -1
- package/lib/commonjs/views/SkiaView.js +6 -3
- package/lib/commonjs/views/SkiaView.js.map +1 -1
- package/lib/module/dom/nodes/JsiSkDOM.js +5 -0
- package/lib/module/dom/nodes/JsiSkDOM.js.map +1 -1
- package/lib/module/dom/nodes/LayerNode.js +32 -0
- package/lib/module/dom/nodes/LayerNode.js.map +1 -0
- package/lib/module/dom/types/NodeType.js +1 -0
- package/lib/module/dom/types/NodeType.js.map +1 -1
- package/lib/module/dom/types/SkDOM.js.map +1 -1
- package/lib/module/renderer/HostComponents.js +3 -0
- package/lib/module/renderer/HostComponents.js.map +1 -1
- package/lib/module/renderer/components/Group.js +16 -3
- package/lib/module/renderer/components/Group.js.map +1 -1
- package/lib/module/renderer/components/Paint.js +7 -1
- package/lib/module/renderer/components/Paint.js.map +1 -1
- package/lib/module/views/SkiaPictureView.js +2 -2
- package/lib/module/views/SkiaPictureView.js.map +1 -1
- package/lib/module/views/SkiaView.js +4 -2
- package/lib/module/views/SkiaView.js.map +1 -1
- package/lib/typescript/src/dom/nodes/JsiSkDOM.d.ts +2 -0
- package/lib/typescript/src/dom/nodes/LayerNode.d.ts +8 -0
- package/lib/typescript/src/dom/types/NodeType.d.ts +1 -0
- package/lib/typescript/src/dom/types/SkDOM.d.ts +1 -0
- package/lib/typescript/src/renderer/HostComponents.d.ts +2 -1
- package/lib/typescript/src/renderer/components/Group.d.ts +5 -1
- package/lib/typescript/src/views/SkiaView.d.ts +3 -0
- package/libs/android/arm64-v8a/libskottie.a +0 -0
- package/libs/android/arm64-v8a/libsksg.a +0 -0
- package/libs/android/armeabi-v7a/libskottie.a +0 -0
- package/libs/android/armeabi-v7a/libsksg.a +0 -0
- package/libs/android/x86/libskottie.a +0 -0
- package/libs/android/x86/libsksg.a +0 -0
- package/libs/android/x86_64/libskottie.a +0 -0
- package/libs/android/x86_64/libsksg.a +0 -0
- package/libs/ios/libskia.xcframework/ios-arm64_arm64e/libskia.a +0 -0
- package/libs/ios/libskia.xcframework/ios-arm64_arm64e_x86_64-simulator/libskia.a +0 -0
- package/libs/ios/libskottie.xcframework/Info.plist +42 -0
- package/libs/ios/libskottie.xcframework/ios-arm64_arm64e/libskottie.a +0 -0
- package/libs/ios/libskottie.xcframework/ios-arm64_arm64e_x86_64-simulator/libskottie.a +0 -0
- package/libs/ios/libsksg.xcframework/Info.plist +42 -0
- package/libs/ios/libsksg.xcframework/ios-arm64_arm64e/libsksg.a +0 -0
- package/libs/ios/libsksg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsksg.a +0 -0
- package/libs/ios/libskshaper.xcframework/Info.plist +5 -5
- package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e/libskshaper.a +0 -0
- package/libs/ios/libskshaper.xcframework/ios-arm64_arm64e_x86_64-simulator/libskshaper.a +0 -0
- package/libs/ios/libsvg.xcframework/Info.plist +5 -5
- package/libs/ios/libsvg.xcframework/ios-arm64_arm64e/libsvg.a +0 -0
- package/libs/ios/libsvg.xcframework/ios-arm64_arm64e_x86_64-simulator/libsvg.a +0 -0
- package/package.json +3 -1
- package/src/dom/nodes/JsiSkDOM.ts +5 -0
- package/src/dom/nodes/LayerNode.ts +35 -0
- package/src/dom/types/NodeType.ts +1 -0
- package/src/dom/types/SkDOM.ts +1 -0
- package/src/renderer/HostComponents.ts +3 -0
- package/src/renderer/components/Group.tsx +16 -3
- package/src/renderer/components/Paint.tsx +7 -1
- package/src/views/SkiaPictureView.tsx +2 -3
- 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.
|
17
|
-
number of milliseconds since the animation value was
|
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
|
-
|
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::
|
31
|
-
|
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
|
-
|
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(
|
76
|
-
|
77
|
-
|
78
|
-
|
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
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
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 = {
|
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
|
20
|
-
function that is used to calculate the new value when any
|
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
|
-
|
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
|
-
|
34
|
-
throw jsi::JSError(runtime,
|
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
|
-
|
39
|
-
throw jsi::JSError(runtime,
|
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>(
|
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,
|
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 =
|
71
|
-
|
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()](
|
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 <
|
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.
|
22
|
-
number of milliseconds since the animation value was
|
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
|
25
|
-
|
26
|
-
{
|
25
|
+
class RNSkReadonlyValue
|
26
|
+
: public JsiSkHostObject,
|
27
|
+
public std::enable_shared_from_this<RNSkReadonlyValue> {
|
27
28
|
public:
|
28
|
-
RNSkReadonlyValue(
|
29
|
+
explicit RNSkReadonlyValue(
|
30
|
+
std::shared_ptr<RNSkPlatformContext> platformContext)
|
29
31
|
: JsiSkHostObject(platformContext),
|
30
|
-
|
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,
|
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() ||
|
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>(
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
const std::function<void()>
|
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,
|
103
|
-
|
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
|
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
|
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&
|
21
|
+
jsi::Runtime &runtime, const jsi::Value *arguments, size_t count)
|
24
22
|
: RNSkReadonlyValue(platformContext) {
|
25
|
-
|
26
|
-
|
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() &&
|
47
|
-
|
48
|
-
|
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,
|
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
|
-
|
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 =
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
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&
|
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
|