Skip to content

Commit 60543c7

Browse files
committed
fix: discard inflight promises when js context invalidates
1 parent 957e0b5 commit 60543c7

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

packages/react-native/ReactCommon/react/nativemodule/samples/platform/android/SampleTurboModule.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,23 @@ public class SampleTurboModule(private val context: ReactApplicationContext) :
361361
toast?.show()
362362
}
363363

364-
override fun invalidate(): Unit = Unit
364+
override fun invalidate() {
365+
// Reject anything still in flight: the JS context that made these calls is going away, so the
366+
// results can never be delivered. Clearing the fields also lets the callbacks (which stay
367+
// registered until the launchers are unregistered) tolerate a late result harmlessly.
368+
pendingPermissionPromise?.reject(
369+
"E_MODULE_INVALIDATED", "Permission request cancelled: SampleTurboModule was invalidated")
370+
pendingPermissionPromise = null
371+
372+
pendingPickMediaPromise?.reject(
373+
"E_MODULE_INVALIDATED", "Media pick cancelled: SampleTurboModule was invalidated")
374+
pendingPickMediaPromise = null
375+
376+
pendingPickMultipleMediaPromise?.reject(
377+
"E_MODULE_INVALIDATED", "Multiple media pick cancelled: SampleTurboModule was invalidated")
378+
pendingPickMultipleMediaPromise = null
379+
super.invalidate()
380+
}
365381

366382
override fun getName(): String {
367383
return NAME

0 commit comments

Comments
 (0)