在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):touchlab/Stately开源软件地址(OpenSource Url):https://github.com/touchlab/Stately开源编程语言(OpenSource Language):Kotlin 99.1%开源软件介绍(OpenSource Introduction):StatelyStately is a state utility library to facilitate state management in Kotlin Multiplatform. Kotlin JVM has the same rules around concurrency and state that Java has. In essence, multiple threads can access shared state in an unrestricted fashion, and it is up to the developer to ensure safe concurrency. Kotlin/Native, on the other hand, introduces new restrictions around concurrent state access (more info). Additionally, Kotlin/JS lives in the Javascript threading world, which means just the one thread. We publish all of our libraries to repositories {
mavenCentral()
} Stately provides various modules to facilitate writing shared code within these different worlds. stately-commonKotlin/Native state adds some concepts that don't exist in either the JVM or JS, as well as the annotation @Throws.
On native, these values delegate or are typealiased to their platform definitions. In JS and the JVM, they do nothing. ConfigcommonMain {
dependencies {
implementation 'co.touchlab:stately-common:1.2.0'
}
} stately-concurrency
Much of the functionality of this module is similar to atomic-fu. They differ in some ways, so while they both cover much of the same ground, Stately's version still has some use.
fun useRef(){
val threadRef = ThreadRef()
threadRef.same() // <- true
backgrundThread {
threadRef.same() // <- false
}
} ConfigcommonMain {
dependencies {
implementation 'co.touchlab:stately-concurrency:1.2.0'
}
} stately-isolate
The obvious use case is for collections. Example usage: fun usage(){
val cacheMap = IsolateState { mutableMapOf<String, String>() }
val key = "abc"
val value = "123"
cacheMap.access { it.put(key, value) }
val valueString = cacheMap.access { it.get(key) }
println(valueString) // <- will print '123'
} The You can create other instances of fun usage(){
val cacheMap = IsolateState { mutableMapOf<String, String>() }
val key = "abc"
val value = "123"
cacheMap.access { it.put(key, value) }
//Fork state
val entrySet = cacheMap.access { map ->
IsolateState(cacheMap.fork(map.entries))
}
val valueString = entrySet.access { entries -> entries.first().value }
println(valueString) // <- will print '123'
} You can create a class that extends class SimpleMap<K, V>: IsolateState<MutableMap<K, V>>({ mutableMapOf()})
{
fun put(key:K, value:V):V? = access { it.put(key, value) }
fun get(key: K):V? = access { it.get(key) }
}
You must dispose of fun usage(){
val cacheMap = IsolateState { mutableMapOf<String, String>() }
cacheMap.dispose()
} ConfigcommonMain {
dependencies {
implementation 'co.touchlab:stately-isolate:1.2.0'
}
} stately-iso-collectionsThis is a set of mutable collections implemented with ConfigcommonMain {
dependencies {
implementation 'co.touchlab:stately-iso-collections:1.2.0'
}
} stately-collectionsA set of collections that can be shared and accessed between threads. This is pretty much deprecated, but we have no plans to remove it as some production apps use it. However, we would strongly suggest you use UsageDependencies can be specified in the common source set, as shown above, if you have Gradle metadata enabled in enableFeaturePreview('GRADLE_METADATA') We're Hiring!Touchlab is looking for a Mobile Developer, with Android/Kotlin experience, who is eager to dive into Kotlin Multiplatform Mobile (KMM) development. Come join the remote-first team putting KMM in production. More info here. Primary MaintainerPing me on twitter @kpgalligan if you don't get a timely reply! -Kevin License
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论