Skip to content

Commit 74e8c78

Browse files
BogiKayfacebook-github-bot
authored andcommitted
refactor: migrate DynamicFromArray to Kotlin (#50602)
Summary: PR migrates DynamicFromArray class to Kotlin as part of #50513 work. ## Changelog: [ANDROID] [CHANGED] - Migrated DynamicFromArray to Kotlin Pull Request resolved: #50602 Test Plan: Run RN-Tester application and played around with it a bit. Reviewed By: rshest Differential Revision: D73179609 Pulled By: cortinico fbshipit-source-id: e304884fea9f57e152bca4926677d7338dc2403a
1 parent 95ca59e commit 74e8c78

File tree

2 files changed

+61
-105
lines changed

2 files changed

+61
-105
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/DynamicFromArray.java

-105
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.bridge
9+
10+
import androidx.core.util.Pools
11+
12+
/** Implementation of Dynamic wrapping a ReadableArray. */
13+
internal class DynamicFromArray private constructor() : Dynamic {
14+
private var array: ReadableArray? = null
15+
private var index: Int = -1
16+
17+
override fun recycle() {
18+
array = null
19+
index = -1
20+
pool.release(this)
21+
}
22+
23+
override val type: ReadableType
24+
get() =
25+
array?.getType(index) ?: throw IllegalStateException("This dynamic value has been recycled")
26+
27+
override val isNull: Boolean
28+
get() =
29+
array?.isNull(index) ?: throw IllegalStateException("This dynamic value has been recycled")
30+
31+
override fun asArray(): ReadableArray =
32+
array?.getArray(index) ?: throw IllegalStateException("This dynamic value has been recycled")
33+
34+
override fun asBoolean(): Boolean =
35+
array?.getBoolean(index)
36+
?: throw IllegalStateException("This dynamic value has been recycled")
37+
38+
override fun asDouble(): Double =
39+
array?.getDouble(index) ?: throw IllegalStateException("This dynamic value has been recycled")
40+
41+
override fun asInt(): Int =
42+
array?.getInt(index) ?: throw IllegalStateException("This dynamic value has been recycled")
43+
44+
override fun asMap(): ReadableMap =
45+
array?.getMap(index) ?: throw IllegalStateException("This dynamic value has been recycled")
46+
47+
override fun asString(): String =
48+
array?.getString(index) ?: throw IllegalStateException("This dynamic value has been recycled")
49+
50+
companion object {
51+
private val pool = Pools.SimplePool<DynamicFromArray>(10)
52+
53+
@JvmStatic
54+
fun create(array: ReadableArray, index: Int): DynamicFromArray {
55+
val dynamic = pool.acquire() ?: DynamicFromArray()
56+
dynamic.array = array
57+
dynamic.index = index
58+
return dynamic
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)