Skip to content

Commit 3fc60a9

Browse files
committed
feat: starting framework for JS rerenders
1 parent 77724e1 commit 3fc60a9

File tree

7 files changed

+40
-7
lines changed

7 files changed

+40
-7
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22

33
`rect-native-css` ported to C++ for performance. This is a prototype and not production ready. Ideally this eventually be `react-native-css@4.0.0`
44

5+
## Description
6+
7+
This library is a port of `react-native-css` to C++ for performance. It will eventually be a drop in replacement for `react-native-css` and they will share the same compiler.
8+
9+
Unlike `react-native-css`, the majority of the processing is done off thread in C++. When styles are updated they are directly applied to the Shadow Tree nodes.
10+
11+
There are two exceptions where styles are applied via a React re-render:
12+
13+
- A non-style prop is changed (e.g `caretColor`)
14+
- The component is animated (the component has a transition or animation style)
15+
516
## Progress
617

718
These are the features that are "done". Only basic testing as been performed.
819

920
- [x] Dynamic styles - shadow tree
10-
- [ ] Dynamic styles - JS rerender
21+
- [x] Dynamic styles - JS rerender
1122
- [x] Style hot reload - shadow tree
12-
- [ ] Style hot reload- JS rerender
23+
- [x] Style hot reload- JS rerender
1324
- [x] Web
1425
- [x] Multiple style rules
1526
- [x] Specificity sorting
@@ -30,7 +41,7 @@ These are the features that are "done". Only basic testing as been performed.
3041
- [x] Upgrading elements
3142
- [ ] Upgrading elements warning
3243
- [ ] Animations
33-
- [ ] Transitions
44+
- [x] Transitions
3445
- [ ] Important styles
3546
- [ ] Important props
3647
- [ ] Shorthand runtime styles

cpp/HybridStyleRegistry.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ namespace margelo::nitro::cssnitro {
174174
// Build computed Styled via factory
175175
auto computed = ::margelo::nitro::cssnitro::makeStyledComputed(styleRuleMap_, classNames,
176176
componentId,
177+
rerender,
177178
*shadowUpdates_,
178179
variableScope,
179180
containerScope);

cpp/ShadowTreeUpdateManager.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ namespace margelo::nitro::cssnitro {
106106
if (it != component_links_.end()) component_links_.erase(it);
107107
}
108108

109+
bool ShadowTreeUpdateManager::hasComponent(const std::string &componentId) {
110+
auto it = component_links_.find(componentId);
111+
return (it == component_links_.end());
112+
}
113+
109114
void ShadowTreeUpdateManager::addUpdates(
110115
const std::string &componentId,
111116
const std::shared_ptr<::margelo::nitro::AnyMap> &styleMap) {

cpp/ShadowTreeUpdateManager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ namespace margelo::nitro::cssnitro {
3939
const std::string &componentId,
4040
facebook::react::Tag tag);
4141

42+
bool hasComponent(const std::string &componentId);
43+
4244
void unlinkComponent(const std::string &componentId);
4345

4446
void addUpdates(const std::string &componentId,

cpp/StyledComputedFactory.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ namespace margelo::nitro::cssnitro {
2222
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> &styleRuleMap,
2323
const std::string &classNames,
2424
const std::string &componentId,
25+
const std::function<void()> &rerender,
2526
ShadowTreeUpdateManager &shadowUpdates,
2627
const std::string &variableScope,
2728
const std::string &containerScope) {
2829
auto computed = reactnativecss::Computed<Styled>::create(
29-
[&styleRuleMap, classNames, componentId, &shadowUpdates, variableScope, containerScope](
30+
[&styleRuleMap, classNames, componentId, &rerender, &shadowUpdates, variableScope, containerScope](
3031
const Styled &prev,
3132
typename reactnativecss::Effect::GetProxy &get) {
3233
(void) prev;
@@ -158,8 +159,12 @@ namespace margelo::nitro::cssnitro {
158159
next.style = anyMap;
159160
}
160161

161-
// Notify ShadowTreeUpdateManager with the resolved style
162+
if (shouldRerender(next)) {
163+
(void) rerender();
164+
}
165+
162166
if (next.style.has_value()) {
167+
// Notify ShadowTreeUpdateManager with the resolved style
163168
shadowUpdates.addUpdates(componentId, next.style.value());
164169
}
165170

@@ -170,4 +175,9 @@ namespace margelo::nitro::cssnitro {
170175
return computed;
171176
}
172177

178+
bool shouldRerender(const Styled &styled) {
179+
// Check if props has a value - if it does, we should rerender
180+
return styled.props.has_value();
181+
}
182+
173183
} // namespace margelo::nitro::cssnitro

cpp/StyledComputedFactory.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66

77
#include "Styled.hpp"
88
#include "HybridStyleRule.hpp"
9+
#include "ShadowTreeUpdateManager.hpp"
910
#include "Observable.hpp"
1011
#include "Computed.hpp"
1112

1213
namespace margelo::nitro::cssnitro {
1314

14-
class ShadowTreeUpdateManager;
15+
class StyledComputedFactory;
1516

1617
// Build a Computed<Styled> that resolves styles from classNames against the styleRuleMap
1718
// and notifies ShadowTreeUpdateManager with the value of next.style for the given componentId.
1819
std::shared_ptr<reactnativecss::Computed<Styled>> makeStyledComputed(
1920
const std::unordered_map<std::string, std::shared_ptr<reactnativecss::Observable<std::vector<HybridStyleRule>>>> &styleRuleMap,
2021
const std::string &classNames,
2122
const std::string &componentId,
23+
const std::function<void()> &rerender,
2224
ShadowTreeUpdateManager &shadowUpdates,
2325
const std::string &variableScope,
2426
const std::string &containerScope);
2527

28+
bool shouldRerender(const Styled &styled);
29+
2630
} // namespace margelo::nitro::cssnitro

src/components/View/View.native.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export const View = copyComponentProperties(
1212
(originalProps: ViewProps) => {
1313
let p = originalProps as Record<string, any>;
1414
const componentId = useId();
15-
const ref = useRef(componentId, p.ref);
1615
const next = useStyledProps(componentId, p.className, p);
16+
const ref = useRef(componentId, p.ref);
1717

1818
if (p.style) {
1919
StyleRegistry.updateComponentInlineStyleKeys(

0 commit comments

Comments
 (0)