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.7k
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.2k
Test multiple APKs with Robolectric
wakwak3125
0
640
Clip, Elevation and ViewOutlineProvider
wakwak3125
1
1.1k
WebView as Fancy and effective View
wakwak3125
1
1.4k
ViewPager2をちょっとさわってみよう
wakwak3125
0
1.5k
年末だし、振り返るKotlin
wakwak3125
1
930
社内ライブラリのアップデートフロー
wakwak3125
4
3.5k
Wantedly Peopleのリリースフロー
wakwak3125
1
4.3k
KOINかわいいよ、KOIN
wakwak3125
0
810
Other Decks in Programming
See All in Programming
もう実家に手頃な情シス娘は不要!Bedrockでもう一人の娘を作る
komakichi
0
120
How to debug Xdebug... or any other weird bug in PHP
dunglas
2
1.2k
RDBの世界をぬりかえていくモデルグラフDB〜truncus graphによるモデルファースト開発〜
jurabi
0
170
Vue :: Better Testing 2024
up1
1
410
Micro Frontends for Java Microservices - dev2next 2024
mraible
PRO
0
210
XP2024 っていう国際会議に行ってきたよの記
bonotake
4
240
Beyond Laravel Octane - Hyperf for Laravel Artisans
albertcht
1
140
色んなオートローダーを覗き見る #phpcon_okinawa
o0h
PRO
5
410
UnJSで簡単に始めるCLIツール開発 / cli-tool-development-with-unjs
aoseyuu
2
340
実践Dash - 手を抜きながら本気で作るデータApplicationの基本と応用 / Dash for Python and Baseball
shinyorke
2
640
Go製CLIツールGatling Commanderによる負荷試験実施の自動化
okmtz
3
710
"noncopyable types" の使いどころについて考えてみた
andpad
0
160
Featured
See All Featured
What's new in Ruby 2.0
geeforr
341
31k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.9k
Side Projects
sachag
452
42k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.5k
What the flash - Photography Introduction
edds
67
11k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.8k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
What's in a price? How to price your products and services
michaelherold
243
11k
Unsuck your backbone
ammeep
668
57k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2k
Making Projects Easy
brettharned
115
5.9k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ