|
| 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