Skip to content

Commit

Permalink
feat: Add beforeUpdate() and afterUpdate()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Jan 16, 2025
1 parent d3b9e54 commit 8772df9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/nitrogen/src/syntax/swift/SwiftHybridObjectBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ export function createSwiftHybridObjectCxxBridge(
public final func getView() -> UnsafeMutableRawPointer {
return Unmanaged.passRetained(__implementation.view).toOpaque()
}
`.trim(),
`
public final func beforeUpdate() {
__implementation.beforeUpdate()
}
`.trim(),
`
public final func afterUpdate() {
__implementation.afterUpdate()
}
`.trim()
)
}
Expand Down
8 changes: 8 additions & 0 deletions packages/nitrogen/src/views/swift/SwiftHybridViewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ using namespace ${namespace}::views;
- (void) updateView {
// 1. Get Swift part
${swiftNamespace}::${HybridTSpecCxx}& swiftPart = _hybridView->getSwiftPart();
// 2. Get UIView*
void* viewUnsafe = swiftPart.getView();
UIView* view = (__bridge_transfer UIView*) viewUnsafe;
// 3. Update RCTViewComponentView's [contentView]
[self setContentView:view];
}
Expand All @@ -107,8 +109,14 @@ using namespace ${namespace}::views;
const auto& newViewPropsConst = *std::static_pointer_cast<${propsClassName} const>(props);
auto& newViewProps = const_cast<${propsClassName}&>(newViewPropsConst);
${swiftNamespace}::${HybridTSpecCxx}& swiftPart = _hybridView->getSwiftPart();
// 2. Update each prop
swiftPart.beforeUpdate();
${indent(propAssignments.join('\n'), ' ')}
swiftPart.afterUpdate();
// 3. Continue in base class
[super updateProps:props oldProps:oldProps];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ - (void) updateProps:(const react::Props::Shared&)props
const auto& newViewPropsConst = *std::static_pointer_cast<HybridTestViewProps const>(props);
auto& newViewProps = const_cast<HybridTestViewProps&>(newViewPropsConst);
NitroImage::HybridTestViewSpec_cxx& swiftPart = _hybridView->getSwiftPart();

// 2. Update each prop
swiftPart.beforeUpdate();

if (newViewProps.someProp.isDirty) {
swiftPart.setSomeProp(newViewProps.someProp.value);
newViewProps.someProp.isDirty = false;
Expand All @@ -75,6 +78,9 @@ - (void) updateProps:(const react::Props::Shared&)props
swiftPart.setSomeCallback(newViewProps.someCallback.value);
newViewProps.someCallback.isDirty = false;
}

swiftPart.afterUpdate();

// 3. Continue in base class
[super updateProps:props oldProps:oldProps];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,12 @@ public class HybridTestViewSpec_cxx {
public final func getView() -> UnsafeMutableRawPointer {
return Unmanaged.passRetained(__implementation.view).toOpaque()
}

public final func beforeUpdate() {
__implementation.beforeUpdate()
}

public final func afterUpdate() {
__implementation.afterUpdate()
}
}
16 changes: 16 additions & 0 deletions packages/react-native-nitro-modules/ios/views/HybridView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ public protocol HybridView: HybridObject {
* This value should not change during the lifetime of this ``HybridView``.
*/
var view: UIView { get }

/**
* Called right before updating props.
* React props are updated in a single batch/transaction.
*/
func beforeUpdate()
/**
* Called right after updating props.
* React props are updated in a single batch/transaction.
*/
func afterUpdate()
}

public extension HybridView {
func beforeUpdate() { /* noop */ }
func afterUpdate() { /* noop */ }
}

#endif

0 comments on commit 8772df9

Please sign in to comment.