@@ -445,20 +445,21 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
445445 val propertyTypeEnum = RNPropertyType .mapToRNPropertyType(propertyType)
446446
447447 try {
448+ val viewModelInstance = getViewModelInstance() ? : return
448449 val property = when (propertyTypeEnum) {
449- RNPropertyType .String -> getViewModelInstance()? .getStringProperty(path)
450- RNPropertyType .Boolean -> getViewModelInstance()? .getBooleanProperty(path)
451- RNPropertyType .Number -> getViewModelInstance()? .getNumberProperty(path)
452- RNPropertyType .Color -> getViewModelInstance()? .getColorProperty(path)
453- RNPropertyType .Enum -> getViewModelInstance()? .getEnumProperty(path)
454- RNPropertyType .Trigger -> getViewModelInstance()? .getTriggerProperty(path)
450+ RNPropertyType .String -> viewModelInstance .getStringProperty(path)
451+ RNPropertyType .Boolean -> viewModelInstance .getBooleanProperty(path)
452+ RNPropertyType .Number -> viewModelInstance .getNumberProperty(path)
453+ RNPropertyType .Color -> viewModelInstance .getColorProperty(path)
454+ RNPropertyType .Enum -> viewModelInstance .getEnumProperty(path)
455+ RNPropertyType .Trigger -> viewModelInstance .getTriggerProperty(path)
455456 } ? : return
456457 val job = scope.launch {
457458 property.valueFlow.collect { value ->
458459 sendEvent(key, value)
459460 }
460461 }
461- propertyListeners[key] = PropertyListener (path, propertyType, job)
462+ propertyListeners[key] = PropertyListener (viewModelInstance, path, propertyType, job)
462463 } catch (ex: RiveException ) {
463464 handleRiveException(ex)
464465 } catch (ex: Exception ) {
@@ -488,6 +489,15 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
488489 fun bindInstance (instance : ViewModelInstance ) {
489490 riveAnimationView?.controller?.stateMachines?.first()?.viewModelInstance = instance
490491 riveAnimationView?.controller?.activeArtboard?.viewModelInstance = instance
492+
493+ // Re-register the listener if the listener wasn't added on this view model instance.
494+ // As calling registerPropertyListener from JS may have been done before/after/during
495+ // this configuration.
496+ propertyListeners.toList().forEach { (_, listener) ->
497+ if (listener.instance != instance) {
498+ registerPropertyListener(listener.path, listener.propertyType)
499+ }
500+ }
491501 }
492502
493503 when (val config = dataBindingConfig) {
@@ -496,7 +506,7 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
496506 // The whole view needs to be reloaded
497507 shouldBeReloaded = true
498508 }
499-
509+
500510 is DataBindingConfig .Index -> {
501511 bindInstance(viewModel.createInstanceFromIndex(config.index))
502512 }
@@ -511,13 +521,6 @@ class RiveReactNativeView(private val context: ThemedReactContext) : FrameLayout
511521
512522 null -> {}
513523 }
514-
515- // Re-add the listeners, as calling registerPropertyListener from JS may have been done
516- // at a different time than this configuration, and we need to add the listeners to the
517- // current bound instance
518- propertyListeners.toList().forEach { (_, listener) ->
519- registerPropertyListener(listener.path, listener.propertyType)
520- }
521524 } catch (ex: RiveException ) {
522525 handleRiveException(ex)
523526 } catch (ex: Exception ) {
@@ -1199,6 +1202,7 @@ sealed class DataBindingConfig {
11991202}
12001203
12011204data class PropertyListener (
1205+ val instance : ViewModelInstance ,
12021206 val path : String ,
12031207 val propertyType : String ,
12041208 val job : Job
0 commit comments