Skip to content

Bind SharedValues in handler config #3658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public NativeRNGestureHandlerModuleSpec(ReactApplicationContext reactContext) {

@ReactMethod
@DoNotStrip
public abstract void updateGestureHandler(double handlerTag, ReadableMap newConfig);
public abstract void setGestureHandlerConfig(double handlerTag, ReadableMap newConfig);

@ReactMethod
@DoNotStrip
public abstract void updateGestureHandlerConfig(double handlerTag, ReadableMap newConfig);

@ReactMethod
@DoNotStrip
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ class FlingGestureHandler : GestureHandler() {

override fun create(context: Context?): FlingGestureHandler = FlingGestureHandler()

override fun setConfig(handler: FlingGestureHandler, config: ReadableMap) {
super.setConfig(handler, config)
override fun updateConfig(handler: FlingGestureHandler, config: ReadableMap) {
super.updateConfig(handler, config)

if (config.hasKey(KEY_NUMBER_OF_POINTERS)) {
handler.numberOfPointersRequired = config.getInt(KEY_NUMBER_OF_POINTERS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -865,8 +865,12 @@ open class GestureHandler {

fun create(context: Context?, handlerTag: Int): T = create(context).also { it.tag = handlerTag }

open fun setConfig(handler: T, config: ReadableMap) {
fun setConfig(handler: T, config: ReadableMap) {
handler.resetConfig()
updateConfig(handler, config)
}

open fun updateConfig(handler: T, config: ReadableMap) {
if (config.hasKey(KEY_SHOULD_CANCEL_WHEN_OUTSIDE)) {
handler.shouldCancelWhenOutside = config.getBoolean(KEY_SHOULD_CANCEL_WHEN_OUTSIDE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ class LongPressGestureHandler(context: Context) : GestureHandler() {

override fun create(context: Context?): LongPressGestureHandler = LongPressGestureHandler((context)!!)

override fun setConfig(handler: LongPressGestureHandler, config: ReadableMap) {
super.setConfig(handler, config)
override fun updateConfig(handler: LongPressGestureHandler, config: ReadableMap) {
super.updateConfig(handler, config)
if (config.hasKey(KEY_MIN_DURATION_MS)) {
handler.minDurationMs = config.getInt(KEY_MIN_DURATION_MS).toLong()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ class NativeViewGestureHandler : GestureHandler() {

override fun create(context: Context?): NativeViewGestureHandler = NativeViewGestureHandler()

override fun setConfig(handler: NativeViewGestureHandler, config: ReadableMap) {
super.setConfig(handler, config)
override fun updateConfig(handler: NativeViewGestureHandler, config: ReadableMap) {
super.updateConfig(handler, config)
if (config.hasKey(KEY_SHOULD_ACTIVATE_ON_START)) {
handler.shouldActivateOnStart = config.getBoolean(KEY_SHOULD_ACTIVATE_ON_START)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ class PanGestureHandler(context: Context?) : GestureHandler() {

override fun create(context: Context?): PanGestureHandler = PanGestureHandler(context)

override fun setConfig(handler: PanGestureHandler, config: ReadableMap) {
super.setConfig(handler, config)
override fun updateConfig(handler: PanGestureHandler, config: ReadableMap) {
super.updateConfig(handler, config)
var hasCustomActivationCriteria = false
if (config.hasKey(KEY_ACTIVE_OFFSET_X_START)) {
handler.activeOffsetXStart =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ class TapGestureHandler : GestureHandler() {

override fun create(context: Context?): TapGestureHandler = TapGestureHandler()

override fun setConfig(handler: TapGestureHandler, config: ReadableMap) {
super.setConfig(handler, config)
override fun updateConfig(handler: TapGestureHandler, config: ReadableMap) {
super.updateConfig(handler, config)
if (config.hasKey(KEY_NUMBER_OF_TAPS)) {
handler.numberOfTaps = config.getInt(KEY_NUMBER_OF_TAPS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,36 @@ class RNGestureHandlerModule(reactContext: ReactApplicationContext?) :
}
}

private fun <T : GestureHandler> updateGestureHandlerHelper(handlerTag: Int, config: ReadableMap) {
private fun <T : GestureHandler> updateGestureHandlerHelper(
handlerTag: Int,
config: ReadableMap,
onlyUpdate: Boolean,
) {
val handler = registry.getHandler(handlerTag) ?: return
val factory = RNGestureHandlerFactoryUtil.findFactoryForHandler<GestureHandler>(handler) ?: return

if (onlyUpdate) {
factory.updateConfig(handler, config)
return
}

interactionManager.dropRelationsForHandlerWithTag(handlerTag)
interactionManager.configureInteractions(handler, config)
factory.setConfig(handler, config)
}

@ReactMethod
override fun updateGestureHandler(handlerTagDouble: Double, config: ReadableMap) {
override fun setGestureHandlerConfig(handlerTagDouble: Double, config: ReadableMap) {
val handlerTag = handlerTagDouble.toInt()

updateGestureHandlerHelper<GestureHandler>(handlerTag, config, false)
}

@ReactMethod
override fun updateGestureHandlerConfig(handlerTagDouble: Double, config: ReadableMap) {
val handlerTag = handlerTagDouble.toInt()

updateGestureHandlerHelper<GestureHandler>(handlerTag, config)
updateGestureHandlerHelper<GestureHandler>(handlerTag, config, true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think updateGestureHandlerHelper can be inlined in both places. Now that handlers are no longer generic classes, the helper doesn't add much, and the result should be easier to read.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, done in 50ffbcd

}

@ReactMethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,9 @@ - (void)resetConfig
#endif
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];
RNBetterSwipeGestureRecognizer *recognizer = (RNBetterSwipeGestureRecognizer *)_recognizer;

id prop = config[@"direction"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ - (void)resetConfig
recognizer.minForce = defaultMinForce;
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];
RNForceTouchGestureRecognizer *recognizer = (RNForceTouchGestureRecognizer *)_recognizer;

APPLY_FLOAT_PROP(maxForce);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ - (void)resetConfig
#endif
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];

#if CHECK_TARGET(13_4)
if (@available(iOS 13.4, *)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ - (void)resetConfig
recognizer.allowableMovement = 10;
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];
RNBetterLongPressGestureRecognizer *recognizer = (RNBetterLongPressGestureRecognizer *)_recognizer;

id prop = config[@"minDurationMs"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ - (instancetype)initWithTag:(NSNumber *)tag
return self;
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];
_shouldActivateOnStart = [RCTConvert BOOL:config[@"shouldActivateOnStart"]];
_disallowInterruption = [RCTConvert BOOL:config[@"disallowInterruption"]];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,9 +381,9 @@ - (void)resetConfig
recognizer.activateAfterLongPress = NAN;
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];
RNBetterPanGestureRecognizer *recognizer = (RNBetterPanGestureRecognizer *)_recognizer;

APPLY_FLOAT_PROP(minVelocityX);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ - (void)resetConfig
recognizer.maxDistSq = NAN;
}

- (void)configure:(NSDictionary *)config
- (void)updateConfig:(NSDictionary *)config
{
[super configure:config];
[super updateConfig:config];
RNBetterTapGestureRecognizer *recognizer = (RNBetterTapGestureRecognizer *)_recognizer;

APPLY_INT_PROP(numberOfTaps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
- (void)bindToView:(nonnull RNGHUIView *)view;
- (void)unbindFromView;
- (void)resetConfig NS_REQUIRES_SUPER;
- (void)configure:(nullable NSDictionary *)config NS_REQUIRES_SUPER;
- (void)setConfig:(nullable NSDictionary *)config NS_REQUIRES_SUPER;
- (void)updateConfig:(nullable NSDictionary *)config NS_REQUIRES_SUPER;
- (void)handleGesture:(nonnull id)recognizer;
- (void)handleGesture:(nonnull id)recognizer inState:(RNGestureHandlerState)state;
- (BOOL)containsPointInView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,14 @@ - (void)resetConfig
#endif
}

- (void)configure:(NSDictionary *)config
- (void)setConfig:(NSDictionary *)config
{
[self resetConfig];
[self updateConfig:config];
}

- (void)updateConfig:(NSDictionary *)config
{
_handlersToWaitFor = [RCTConvert NSNumberArray:config[@"waitFor"]];
_simultaneousHandlers = [RCTConvert NSNumberArray:config[@"simultaneousHandlers"]];
_handlersThatShouldWait = [RCTConvert NSNumberArray:config[@"blocksHandlers"]];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
toViewWithTag:(nonnull NSNumber *)viewTag
withActionType:(RNGestureHandlerActionType)actionType;

- (void)updateGestureHandler:(nonnull NSNumber *)handlerTag config:(nonnull NSDictionary *)config;
- (void)setGestureHandlerConfig:(nonnull NSNumber *)handlerTag config:(nonnull NSDictionary *)config;

- (void)updateGestureHandlerConfig:(nonnull NSNumber *)handlerTag config:(nonnull NSDictionary *)config;

- (void)dropGestureHandler:(nonnull NSNumber *)handlerTag;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ - (void)createGestureHandler:(NSString *)handlerName tag:(NSNumber *)handlerTag
}

RNGestureHandler *gestureHandler = [[nodeClass alloc] initWithTag:handlerTag];
[gestureHandler configure:config];
[gestureHandler setConfig:config];
[_registry registerGestureHandler:gestureHandler];

__weak id<RNGestureHandlerEventEmitter> emitter = self;
Expand Down Expand Up @@ -205,10 +205,16 @@ - (void)attachGestureHandler:(nonnull NSNumber *)handlerTag
[self registerViewWithGestureRecognizerAttachedIfNeeded:view];
}

- (void)updateGestureHandler:(NSNumber *)handlerTag config:(NSDictionary *)config
- (void)setGestureHandlerConfig:(NSNumber *)handlerTag config:(NSDictionary *)config
{
RNGestureHandler *handler = [_registry handlerWithTag:handlerTag];
[handler configure:config];
[handler setConfig:config];
}

- (void)updateGestureHandlerConfig:(NSNumber *)handlerTag config:(NSDictionary *)config
{
RNGestureHandler *handler = [_registry handlerWithTag:handlerTag];
[handler updateConfig:config];
}

- (void)dropGestureHandler:(NSNumber *)handlerTag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,19 @@ - (void)attachGestureHandler:(double)handlerTag newView:(double)viewTag actionTy
}];
}

- (void)updateGestureHandler:(double)handlerTag newConfig:(NSDictionary *)config
- (void)setGestureHandlerConfig:(double)handlerTag newConfig:(NSDictionary *)config
{
[self addOperationBlock:^(RNGestureHandlerManager *manager) {
[manager updateGestureHandler:[NSNumber numberWithDouble:handlerTag] config:config];
[manager setGestureHandlerConfig:[NSNumber numberWithDouble:handlerTag] config:config];
}];
}

- (void)updateGestureHandlerConfig:(double)handlerTag newConfig:(NSDictionary *)config
{
RNGestureHandlerManager *manager = [RNGestureHandlerModule handlerManagerForModuleId:_moduleId];
[manager updateGestureHandlerConfig:[NSNumber numberWithDouble:handlerTag] config:config];
Comment on lines +187 to +188
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't sure if this should be done like this, or like in setGestureHandlerConfig. However, if we use addOperationBlock we have to call flushOperations, so I think we can leave it as it is (cc @j-piasecki)

}

- (void)dropGestureHandler:(double)handlerTag
{
[self addOperationBlock:^(RNGestureHandlerManager *manager) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
NodeManager.getHandler(handlerTag),
config as unknown as Config
);
this.updateGestureHandler(handlerTag, config as unknown as Config);
this.setGestureHandlerConfig(handlerTag, config as unknown as Config);
},
attachGestureHandler(
handlerTag: number,
Expand All @@ -63,7 +63,7 @@
}

// @ts-ignore Types should be HTMLElement or React.Component
NodeManager.getHandler(handlerTag).init(newView, propsRef, actionType);

Check warning on line 66 in packages/react-native-gesture-handler/src/RNGestureHandlerModule.web.ts

View workflow job for this annotation

GitHub Actions / check

Unsafe call of an `any` typed value
},
detachGestureHandler(handlerTag: number) {
if (shouldPreventDrop) {
Expand All @@ -73,14 +73,17 @@

NodeManager.detachGestureHandler(handlerTag);
},
updateGestureHandler(handlerTag: number, newConfig: Config) {
setGestureHandlerConfig(handlerTag: number, newConfig: Config) {
NodeManager.getHandler(handlerTag).updateGestureConfig(newConfig);

InteractionManager.instance.configureInteractions(
NodeManager.getHandler(handlerTag),
newConfig
);
},
updateGestureHandlerConfig(_handlerTag: number, _newConfig: Config) {
// TODO: To be implemented
},
getGestureHandlerNode(handlerTag: number) {
return NodeManager.getHandler(handlerTag);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ export default {
) {
// NO-OP
},
updateGestureHandler(_handlerTag: number, _newConfig: Config) {
setGestureHandlerConfig(_handlerTag: number, _newConfig: Config) {
// NO-OP
},
updateGestureHandlerConfig(_handlerTag: number, _newConfig: Config) {
// NO-OP
},
getGestureHandlerNode(_handlerTag: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,15 @@ export default function createHandler<
});
};

private updateGestureHandler = (
private setGestureHandlerConfig = (
newConfig: Readonly<Record<string, unknown>>
) => {
this.config = newConfig;

RNGestureHandlerModule.updateGestureHandler(this.handlerTag, newConfig);
RNGestureHandlerModule.setGestureHandlerConfig(
this.handlerTag,
newConfig
);
scheduleFlushOperations();
};

Expand All @@ -426,7 +429,7 @@ export default function createHandler<
config
);
if (!deepEqual(this.config, newConfig)) {
this.updateGestureHandler(newConfig);
this.setGestureHandlerConfig(newConfig);
}
}
}
Expand All @@ -439,7 +442,7 @@ export default function createHandler<
[...allowedProps, ...customNativeProps],
config
);
this.updateGestureHandler(newConfig);
this.setGestureHandlerConfig(newConfig);
}

render() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function attachHandlers({
return;
}
for (const handler of gesturesToAttach) {
RNGestureHandlerModule.updateGestureHandler(
RNGestureHandlerModule.setGestureHandlerConfig(
handler.handlerTag,
filterConfig(
handler.config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function updateHandlers(
handler.config = newGestures[i].config;
handler.handlers = newGestures[i].handlers;

RNGestureHandlerModule.updateGestureHandler(
RNGestureHandlerModule.setGestureHandlerConfig(
handler.handlerTag,
filterConfig(
handler.config,
Expand Down
Loading
Loading