-
Notifications
You must be signed in to change notification settings - Fork 25.1k
refactor: migrate DynamicFromMap to Kotlin #50597
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
Closed
artus9033
wants to merge
4
commits into
facebook:main
from
artus9033:refactor/dynamicfrommap-kotlin-rewrite
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 0 additions & 119 deletions
119
...ges/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.java
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromMap.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.bridge | ||
|
|
||
| import androidx.core.util.Pools.SimplePool | ||
|
|
||
| /** Implementation of Dynamic wrapping a ReadableMap. */ | ||
| internal class DynamicFromMap | ||
| // This is a pools object. Hide the constructor. | ||
| private constructor() : Dynamic { | ||
| private var map: ReadableMap? = null | ||
| private var name: String? = null | ||
|
|
||
| override fun recycle() { | ||
| map = null | ||
| name = null | ||
| sPool.get()?.release(this) | ||
| } | ||
|
|
||
| override val isNull: Boolean | ||
| get() { | ||
| return accessMapSafely { map, name -> map.isNull(name) } | ||
| } | ||
|
|
||
| override fun asBoolean(): Boolean { | ||
| return accessMapSafely { map, name -> map.getBoolean(name) } | ||
| } | ||
|
|
||
| override fun asDouble(): Double { | ||
| return accessMapSafely { map, name -> map.getDouble(name) } | ||
| } | ||
|
|
||
| override fun asInt(): Int { | ||
| return accessMapSafely { map, name -> map.getInt(name) } | ||
| } | ||
|
|
||
| override fun asString(): String? { | ||
| return accessMapSafely { map, name -> map.getString(name) } | ||
| } | ||
|
|
||
| override fun asArray(): ReadableArray? { | ||
| return accessMapSafely { map, name -> map.getArray(name) } | ||
| } | ||
|
|
||
| override fun asMap(): ReadableMap? { | ||
| return accessMapSafely { map, name -> map.getMap(name) } | ||
| } | ||
|
|
||
| override val type: ReadableType | ||
| get() { | ||
| return accessMapSafely { map, name -> map.getType(name) } | ||
| } | ||
|
|
||
| /** | ||
| * Asserts that both map and name are non-null and invokes the lambda with | ||
| * | ||
| * @param executor the callback to be invoked with non-null-asserted prop values | ||
| * @return value returned by the executor | ||
| */ | ||
| private fun <T> accessMapSafely(executor: (map: ReadableMap, name: String) -> T): T { | ||
| val name = checkNotNull(name) { DYNAMIC_VALUE_RECYCLED_FAILURE_MESSAGE } | ||
| val map = checkNotNull(map) { DYNAMIC_VALUE_RECYCLED_FAILURE_MESSAGE } | ||
|
|
||
| return executor(map, name) | ||
| } | ||
|
|
||
| companion object { | ||
| private val sPool: ThreadLocal<SimplePool<DynamicFromMap>> = | ||
| ThreadLocal.withInitial { SimplePool(10) } | ||
|
|
||
| private const val DYNAMIC_VALUE_RECYCLED_FAILURE_MESSAGE = | ||
| "This dynamic value has been recycled" | ||
|
|
||
| fun create(map: ReadableMap, name: String): DynamicFromMap { | ||
| val dynamic = sPool.get()?.acquire() ?: DynamicFromMap() | ||
|
|
||
| return dynamic.apply { | ||
| this.map = map | ||
| this.name = name | ||
| } | ||
| } | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
...s/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/DynamicFromMapTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| package com.facebook.react.bridge | ||
|
|
||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.assertj.core.api.Assertions.assertThatThrownBy | ||
| import org.junit.Test | ||
|
|
||
| /** Tests for [DynamicFromMap] */ | ||
| class DynamicFromMapTest { | ||
| @Test | ||
| fun testGetDynamic() { | ||
| assertThat(getDynamicFromMap("int").asInt()).isEqualTo(1) | ||
| assertThat(getDynamicFromMap("double").asDouble()).isEqualTo(2.0) | ||
| assertThat(getDynamicFromMap("string").asString()).isEqualTo("str") | ||
| assertThat(getDynamicFromMap("boolean").asBoolean()).isEqualTo(false) | ||
| assertThat(getDynamicFromMap("array").asArray()).isEqualTo(testSource.getArray("array")) | ||
| assertThat(getDynamicFromMap("map").asMap()).isEqualTo(testSource.getMap("map")) | ||
| assertThat(getDynamicFromMap("null").isNull).isEqualTo(true) | ||
| assertThat(getDynamicFromMap("int").type).isEqualTo(ReadableType.Number) | ||
| } | ||
|
|
||
| @Test | ||
| fun testGetFromRecycledDynamic() { | ||
| assertThatThrownBy { getRecycledDynamicFromMap("int").asInt() } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("double").asDouble() } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("string").asString() } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("boolean").asMap() } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("array").asArray() } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("map").asBoolean() } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("null").isNull } | ||
| assertThatThrownBy { getRecycledDynamicFromMap("anything").type } | ||
| } | ||
|
|
||
| private fun getDynamicFromMap(key: String): Dynamic { | ||
| return DynamicFromMap.create(testSource, key) | ||
| } | ||
|
|
||
| private fun getRecycledDynamicFromMap(key: String): Dynamic { | ||
| return getDynamicFromMap(key).apply { recycle() } | ||
| } | ||
|
|
||
| companion object { | ||
| private val testSource = | ||
| JavaOnlyMap.of( | ||
| "boolean", | ||
| false, | ||
| "array", | ||
| JavaOnlyArray.of(), | ||
| "map", | ||
| JavaOnlyMap.of(), | ||
| "null", | ||
| null, | ||
| "int", | ||
| 1, | ||
| "double", | ||
| 2.0, | ||
| "string", | ||
| "str", | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.