Skip to content

Commit c788ad6

Browse files
authored
feat: add async API compat layer for experimental migration (#183)
Adds async equivalents for all sync methods and marks the sync versions as `@deprecated`. This lets users migrate to the new async API while still on the legacy backend, before switching to experimental. No behavior changes — all new async methods are thin `Promise.async {}` wrappers around existing sync calls.
1 parent eb6d4d6 commit c788ad6

File tree

97 files changed

+1814
-102
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

97 files changed

+1814
-102
lines changed

android/src/main/java/com/margelo/nitro/rive/HybridRiveFile.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.File
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67
import java.lang.ref.WeakReference
78
import kotlinx.coroutines.CoroutineScope
89
import kotlinx.coroutines.Dispatchers
@@ -85,6 +86,38 @@ class HybridRiveFile : HybridRiveFileSpec() {
8586
}
8687
}
8788

89+
override fun getViewModelNamesAsync(): Promise<Array<String>> {
90+
return Promise.async {
91+
val file = riveFile ?: return@async emptyArray()
92+
val count = file.viewModelCount
93+
val names = mutableListOf<String>()
94+
for (i in 0 until count) {
95+
try {
96+
val vm = file.getViewModelByIndex(i)
97+
names.add(vm.name)
98+
} catch (_: Exception) {
99+
}
100+
}
101+
names.toTypedArray()
102+
}
103+
}
104+
105+
override fun viewModelByNameAsync(name: String, validate: Boolean?): Promise<HybridViewModelSpec?> {
106+
return Promise.async { viewModelByName(name) }
107+
}
108+
109+
override fun defaultArtboardViewModelAsync(artboardBy: ArtboardBy?): Promise<HybridViewModelSpec?> {
110+
return Promise.async { defaultArtboardViewModel(artboardBy) }
111+
}
112+
113+
override fun getArtboardCountAsync(): Promise<Double> {
114+
return Promise.async { artboardCount }
115+
}
116+
117+
override fun getArtboardNamesAsync(): Promise<Array<String>> {
118+
return Promise.async { artboardNames }
119+
}
120+
88121
override fun updateReferencedAssets(referencedAssets: ReferencedAssetsType) {
89122
val assetsData = referencedAssets.data ?: return
90123
val cache = referencedAssetCache ?: return

android/src/main/java/com/margelo/nitro/rive/HybridViewModel.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModel
55
import app.rive.runtime.kotlin.core.errors.ViewModelException
66
import com.facebook.proguard.annotations.DoNotStrip
7+
import com.margelo.nitro.core.Promise
78

89
@Keep
910
@DoNotStrip
@@ -51,4 +52,16 @@ class HybridViewModel(private val viewModel: ViewModel) : HybridViewModelSpec()
5152
return null
5253
}
5354
}
55+
56+
override fun createInstanceByNameAsync(name: String): Promise<HybridViewModelInstanceSpec?> {
57+
return Promise.async { createInstanceByName(name) }
58+
}
59+
60+
override fun createDefaultInstanceAsync(): Promise<HybridViewModelInstanceSpec?> {
61+
return Promise.async { createDefaultInstance() }
62+
}
63+
64+
override fun createBlankInstanceAsync(): Promise<HybridViewModelInstanceSpec?> {
65+
return Promise.async { createInstance() }
66+
}
5467
}

android/src/main/java/com/margelo/nitro/rive/HybridViewModelBooleanProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelBooleanProperty
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67

78
@Keep
89
@DoNotStrip
@@ -15,6 +16,14 @@ class HybridViewModelBooleanProperty(private val viewModelBoolean: ViewModelBool
1516
viewModelBoolean.value = value
1617
}
1718

19+
override fun getValueAsync(): Promise<Boolean> {
20+
return Promise.async { value }
21+
}
22+
23+
override fun set(value: Boolean) {
24+
viewModelBoolean.value = value
25+
}
26+
1827
override fun addListener(onChanged: (value: Boolean) -> Unit): () -> Unit {
1928
val remover = addListenerInternal(onChanged)
2029
ensureValueListenerJob(viewModelBoolean.valueFlow)

android/src/main/java/com/margelo/nitro/rive/HybridViewModelColorProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelColorProperty
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67

78
@Keep
89
@DoNotStrip
@@ -15,6 +16,14 @@ class HybridViewModelColorProperty(private val viewModelColor: ViewModelColorPro
1516
viewModelColor.value = value.toLong().toInt()
1617
}
1718

19+
override fun getValueAsync(): Promise<Double> {
20+
return Promise.async { value }
21+
}
22+
23+
override fun set(value: Double) {
24+
viewModelColor.value = value.toLong().toInt()
25+
}
26+
1827
override fun addListener(onChanged: (value: Double) -> Unit): () -> Unit {
1928
val remover = addListenerInternal { intValue: Int -> onChanged(intValue.toDouble()) }
2029
ensureValueListenerJob(viewModelColor.valueFlow)

android/src/main/java/com/margelo/nitro/rive/HybridViewModelEnumProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelEnumProperty
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67

78
@Keep
89
@DoNotStrip
@@ -15,6 +16,14 @@ class HybridViewModelEnumProperty(private val viewModelEnum: ViewModelEnumProper
1516
viewModelEnum.value = value
1617
}
1718

19+
override fun getValueAsync(): Promise<String> {
20+
return Promise.async { value }
21+
}
22+
23+
override fun set(value: String) {
24+
viewModelEnum.value = value
25+
}
26+
1827
override fun addListener(onChanged: (value: String) -> Unit): () -> Unit {
1928
val remover = addListenerInternal(onChanged)
2029
ensureValueListenerJob(viewModelEnum.valueFlow)

android/src/main/java/com/margelo/nitro/rive/HybridViewModelInstance.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelInstance
55
import app.rive.runtime.kotlin.core.errors.ViewModelException
66
import com.facebook.proguard.annotations.DoNotStrip
7+
import com.margelo.nitro.core.Promise
78

89
@Keep
910
@DoNotStrip
@@ -65,4 +66,8 @@ class HybridViewModelInstance(val viewModelInstance: ViewModelInstance) : Hybrid
6566
val nativeInstance = (instance as HybridViewModelInstance).viewModelInstance
6667
viewModelInstance.setInstanceProperty(path, nativeInstance)
6768
}
69+
70+
override fun viewModelAsync(path: String): Promise<HybridViewModelInstanceSpec?> {
71+
return Promise.async { viewModel(path) }
72+
}
6873
}

android/src/main/java/com/margelo/nitro/rive/HybridViewModelListProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelListProperty
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67
import kotlinx.coroutines.flow.map
78

89
@Keep
@@ -56,6 +57,14 @@ class HybridViewModelListProperty(private val listProperty: ViewModelListPropert
5657
return true
5758
}
5859

60+
override fun getLengthAsync(): Promise<Double> {
61+
return Promise.async { length }
62+
}
63+
64+
override fun getInstanceAtAsync(index: Double): Promise<HybridViewModelInstanceSpec?> {
65+
return Promise.async { getInstanceAt(index) }
66+
}
67+
5968
override fun addListener(onChanged: () -> Unit): () -> Unit {
6069
val remover = addListenerInternal { _ -> onChanged() }
6170
ensureValueListenerJob(listProperty.valueFlow.map { })

android/src/main/java/com/margelo/nitro/rive/HybridViewModelNumberProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelNumberProperty
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67
import kotlinx.coroutines.flow.map
78

89
@Keep
@@ -16,6 +17,14 @@ class HybridViewModelNumberProperty(private val viewModelNumber: ViewModelNumber
1617
viewModelNumber.value = value.toFloat()
1718
}
1819

20+
override fun getValueAsync(): Promise<Double> {
21+
return Promise.async { value }
22+
}
23+
24+
override fun set(value: Double) {
25+
viewModelNumber.value = value.toFloat()
26+
}
27+
1928
override fun addListener(onChanged: (value: Double) -> Unit): () -> Unit {
2029
val remover = addListenerInternal(onChanged)
2130
ensureValueListenerJob(viewModelNumber.valueFlow.map { it.toDouble() })

android/src/main/java/com/margelo/nitro/rive/HybridViewModelStringProperty.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.margelo.nitro.rive
33
import androidx.annotation.Keep
44
import app.rive.runtime.kotlin.core.ViewModelStringProperty
55
import com.facebook.proguard.annotations.DoNotStrip
6+
import com.margelo.nitro.core.Promise
67

78
@Keep
89
@DoNotStrip
@@ -15,6 +16,14 @@ class HybridViewModelStringProperty(private val viewModelString: ViewModelString
1516
viewModelString.value = value
1617
}
1718

19+
override fun getValueAsync(): Promise<String> {
20+
return Promise.async { value }
21+
}
22+
23+
override fun set(value: String) {
24+
viewModelString.value = value
25+
}
26+
1827
override fun addListener(onChanged: (value: String) -> Unit): () -> Unit {
1928
val remover = addListenerInternal(onChanged)
2029
ensureValueListenerJob(viewModelString.valueFlow)

ios/HybridRiveFile.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import NitroModules
12
import RiveRuntime
23

34
typealias ReferencedAssetCache = [String: RiveFileAsset]
@@ -88,6 +89,36 @@ class HybridRiveFile: HybridRiveFileSpec, RiveViewSource {
8889
return HybridBindableArtboard(bindableArtboard: bindable)
8990
}
9091

92+
func getViewModelNamesAsync() throws -> Promise<[String]> {
93+
return Promise.async {
94+
guard let file = self.riveFile else { return [] }
95+
let count = file.viewModelCount
96+
var names: [String] = []
97+
for i in 0..<count {
98+
if let vm = file.viewModel(at: UInt(i)) {
99+
names.append(vm.name)
100+
}
101+
}
102+
return names
103+
}
104+
}
105+
106+
func viewModelByNameAsync(name: String, validate: Bool?) throws -> Promise<(any HybridViewModelSpec)?> {
107+
return Promise.async { try self.viewModelByName(name: name) }
108+
}
109+
110+
func defaultArtboardViewModelAsync(artboardBy: ArtboardBy?) throws -> Promise<(any HybridViewModelSpec)?> {
111+
return Promise.async { try self.defaultArtboardViewModel(artboardBy: artboardBy) }
112+
}
113+
114+
func getArtboardCountAsync() throws -> Promise<Double> {
115+
return Promise.async { self.artboardCount }
116+
}
117+
118+
func getArtboardNamesAsync() throws -> Promise<[String]> {
119+
return Promise.async { self.artboardNames }
120+
}
121+
91122
func updateReferencedAssets(referencedAssets: ReferencedAssetsType) {
92123
guard let assetsData = referencedAssets.data,
93124
let cache = referencedAssetCache,

0 commit comments

Comments
 (0)