• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

touchlab/Stately: Kotlin Multiplatform State Library

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

touchlab/Stately

开源软件地址(OpenSource Url):

https://github.com/touchlab/Stately

开源编程语言(OpenSource Language):

Kotlin 99.1%

开源软件介绍(OpenSource Introduction):

Stately

Stately 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 mavenCentral(), so make sure to add that repository.

repositories {
   mavenCentral()
}

Stately provides various modules to facilitate writing shared code within these different worlds.

stately-common

Kotlin/Native state adds some concepts that don't exist in either the JVM or JS, as well as the annotation @Throws.

stately-common is very simple. It includes common definitions for freeze(), isFrozen, and ensureNeverFrozen(), and as mentioned @Throws. These definitions, 2 functions, a val, and an annotation, are often source-copied into other libraries. stately-common's sole purpose is to define these as minimally as possible, to be included in apps or other libraries.

On native, these values delegate or are typealiased to their platform definitions. In JS and the JVM, they do nothing.

Config

commonMain {
    dependencies {
        implementation 'co.touchlab:stately-common:1.2.0'
    }
}

stately-concurrency

stately-concurrency includes some concurrency support classes. These include a set of Atomicxxx classes, a Lock, a ThreadLocal container, and a class ThreadRef that allows you to hold a thread id.

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.

ThreadRef is unique to Stately. It allows you to capture a reference to the id of the thread in which it was created, and ask if the current thread is the same. Just FYI, it does not keep a reference to the actual thread. Just an id. Usage looks like this:

fun useRef(){
  val threadRef = ThreadRef()
  threadRef.same() // <- true
  backgrundThread {
    threadRef.same() // <- false
  }
}

Config

commonMain {
    dependencies {
        implementation 'co.touchlab:stately-concurrency:1.2.0'
    }
}

stately-isolate

stately-isolate creates mutable state in a special state thread, and restricts access to that state from the same thread. This allows the state held by an instance of IsolateState to remain mutable. State coming in and going out must be frozen, but the guarded state can change.

Read more about the design in this blog post.

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 cacheMap above can be called from multiple threads. The lambda passed to the access method will be invoked on the same thread that the state was created on. Because it is a single thread, access is serialized and thread-safe.

You can create other instances of IsolateState by forking the parent instance.

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 IsolateState and provides for simpler access.

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

stately-iso-collections implements collections by extending IsolateState in this manner.

You must dispose of IsolateState instances to avoid memory leaks.

fun usage(){
    val cacheMap = IsolateState { mutableMapOf<String, String>() }
    cacheMap.dispose()
}

Config

commonMain {
    dependencies {
        implementation 'co.touchlab:stately-isolate:1.2.0'
    }
}

stately-iso-collections

This is a set of mutable collections implemented with IsolateState. The set currently includes a MutableSet, MutableList, MutableMap, and an implementation of ArrayDeque that is being added to the Kotlin stdlib in 1.3.70.

Config

commonMain {
    dependencies {
        implementation 'co.touchlab:stately-iso-collections:1.2.0'
    }
}

stately-collections

A 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 stately-isolate and stately-iso-collections instead. Collections implemented with stately-isolate are far more flexible and absolutely CRUSH the original stately-collections in performance benchmarks. See blog post.

Usage

Dependencies can be specified in the common source set, as shown above, if you have Gradle metadata enabled in settings.gradle.

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 Maintainer

Kevin Galligan

Image of Kevin

Ping me on twitter @kpgalligan if you don't get a timely reply! -Kevin

License

Copyright 2020 Touchlab, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
rstudio/bookdown: Authoring Books and Technical Documents with R Markdown发布时间:2022-07-09
下一篇:
patacrep/patacrep: Engine for LaTeX songbooks发布时间:2022-07-09
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap