Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
KotlinJSでもCoroutines
Search
Ryo Sakaguchi
July 03, 2018
Programming
1
1.5k
KotlinJSでもCoroutines
2018.07.03 Kotlin愛好会 vol2
create-react-kotlin-appでサンプルプロジェクトを作り、その上でcoroutinesを動かしてみます。
Ryo Sakaguchi
July 03, 2018
Tweet
Share
More Decks by Ryo Sakaguchi
See All by Ryo Sakaguchi
Android Architecture Componentsを使って、改善・効率化するAndroidアプリ開発
wakwak3125
0
1.1k
Test multiple APKs with Robolectric
wakwak3125
0
560
Clip, Elevation and ViewOutlineProvider
wakwak3125
1
970
WebView as Fancy and effective View
wakwak3125
1
1.3k
ViewPager2をちょっとさわってみよう
wakwak3125
0
1.4k
年末だし、振り返るKotlin
wakwak3125
1
830
社内ライブラリのアップデートフロー
wakwak3125
4
3.3k
Wantedly Peopleのリリースフロー
wakwak3125
1
4.1k
KOINかわいいよ、KOIN
wakwak3125
0
770
Other Decks in Programming
See All in Programming
PHPでOfficeファイルを取り扱う! PHP Officeライブラリを プロダクトに組み込んだ話
hirobe1999
0
840
DocC Tutorial と TCA におけるテスト機能の紹介
kalupas226
1
330
Crafting a Own PHP - ウキウキ手作りミニマリストPHP
uzulla
4
1.1k
品質とスピードを両立: TypeScriptの柔軟な型システムをバックエンドで活用する
kosui
4
870
品質が高いコードって何?Rev2.1
ickx
1
490
SwiftUI, Jetpack Composeの導入で変化した「家族アルバム みてね」のアプリ開発体験
hicka04
6
400
ファイル先頭の use の意味、説明できますか? 〜PHP の namespace と autoloading の関係を正しく理解しよう〜 / namespace and autoloading in php
okashoi
2
470
もうすぐ新年度、Babylon.jsがお勧めな3個の理由
hideg
0
160
Why 1 + 1 = 2 in Swift?
1plus4
1
240
Swiftの型推論を学ぼう | Let's Learn About Type Inference in Swift
omochi
2
520
オブジェクト指向コードレビューの新しいアプローチ
akkie76
3
1.5k
上手な探索的テストとその上達方法について
matsu802
4
650
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
455
32k
The Art of Programming - Codeland 2020
erikaheidi
40
12k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
18
1.7k
Designing with Data
zakiwarfel
94
4.8k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
950
Into the Great Unknown - MozCon
thekraken
10
830
Product Roadmaps are Hard
iamctodd
43
9.6k
Why Our Code Smells
bkeepers
PRO
330
56k
Happy Clients
brianwarren
91
6.3k
WebSockets: Embracing the real-time Web
robhawkes
59
6.9k
KATA
mclloyd
14
11k
Mobile First: as difficult as doing things right
swwweet
215
8.5k
Transcript
KotlinJSͰ Coroutines 2018.07.03 KotlinѪձ vol2 Ryo Sakaguchi(@wakwak3125)
About me • Ryo Sakaguchi (@wakwak3125) • Wantedly People •
AndroidͷਓͰ͢ • Snowboard/Guitar/Beer
WHAT IS COROUTINES?
These posts are useful! • Coroutines - Kotlin Programming Language
• https://kotlinlang.org/docs/reference/coroutines.html • શʹཧղͨ͠ؾʹͳΔKotlin Coroutines by @takahirom • https://qiita.com/takahirom/items/ 2ba221a8d0c32cf701ba • ೖKotlin coroutines @k-kagurazaka • https://qiita.com/k-kagurazaka@github/items/
KotlinJSͰCoroutines͕͑Δʂ ಛʹ͍͜͠ͱΛҙࣝ͢Δ͜ͱͳ͑ͯ͘͠·ͬͨ!
SET UP KotlinJSͰ࡞ΒΕͨReactϕʔεͷΣϒΞϓϦͰ
create-react-kotlin-app • JetBrains/create-react-kotlin-app • https://github.com/JetBrains/create-react-kotlin-app • ҰൃͰKotlinͰॻ͔ΕͨReactΞϓϦ͕࡞ΕΔཔΕΔͭ
͔͍͔ͭͨ • $ npm install -g create-react-kotlin-app • $ create-react-kotlin-app
my-app • done!! • $ yarn start͢ΔͱlocalhostͰಈ࡞֬ೝͰ͖Δ
͜Μͳײ͡
App.kt class App : RComponent<RProps, RState>() { override fun RBuilder.render()
{ div("App-header") { logo() h2 { +"Welcome to React with Kotlin" } } p("App-intro") { +"To get started, edit " code { +"app/App.kt" } +" and save to reload." } p("App-ticker") { ticker() } } } fun RBuilder.app() = child(App::class) {}
Ticker.kt class Ticker(props: TickerProps) : RComponent<TickerProps, TickerState>(props) { override fun
TickerState.init(props: TickerProps) { secondsElapsed = props.startFrom } var timerID: Int? = null override fun componentDidMount() { timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } override fun componentWillUnmount() { window.clearInterval(timerID!!) } override fun RBuilder.render() { +"This app has been running for ${state.secondsElapsed} seconds." } } fun RBuilder.ticker(startFrom: Int = 0) = child(Ticker::class) { attrs.startFrom = startFrom }
TickerͷStateͱProps interface TickerProps : RProps { var startFrom: Int }
interface TickerState : RState { var secondsElapsed: Int }
ΊͬͬͬͪΌKotlin
Tickerͷ࣮ setIntervalͰ1ඵ͝ͱʹΧϯτΞοϓ var timerId: Int? = null override fun componentDidMount()
{ timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) }
Tickerͷ࣮ setIntervalͰ1ඵ͝ͱʹΧϯτΞοϓ var timerId: Int? = null override fun componentDidMount()
{ timerID = window.setInterval({ setState { secondsElapsed += 1 } }, 1000) } => CoroutineԽͯ͠ΈΑ͏
kotlinx-coroutines-core • kotlinx.coroutines/js/kotlinx-coroutines-core-js • https://github.com/Kotlin/kotlinx.coroutines/tree/master/ js/kotlinx-coroutines-core-js • Install the dependency
$ yarn add kotlinx-coroutines-core • done!
CoroutineԽͯ͠ΈΔ var job: Job? = null override fun componentDidMount() {
job = launch { while (true) { delay(1000) setState { secondsElapsed += 1 } } } } => done!
fetch • fetchͪΖΜ͑Δ // ίʔυงғؾ suspend fun getFoo(): Any =
suspendCoroutine { cont -> window .fetch("https://foo.com/api/foo") .then({ response -> cont.resume(response.ok.toString()) },{ throwable -> cont.resumeWithException(throwable) }) } // ී௨ʹ͏ launce { val foo = getFoo() }
ΊͬͬͬͪΌCoroutines
͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ