Share
Embed
Coroutinesから紐解くKtorの仕組み / Revealing Ktor with coroutines
Kotlin Fest 2019 で行った「Coroutinesから紐解くKtorの仕組み」の資料です。
Yuji Koyano
August 24, 2019
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
158 slides
-
Coroutines͔Βඥղ͘ KtorͷΈ Kotlin Fest 2019 08/24 (Sat.) Recruit
Lifestyle. Yuji Koyano -
༷ʑͳѪͰํ w "OESPJE w J04 ,PUMJO/BUJWF w +BWB4DSJQU w
4FWFS4JEF,PUMJO -
͜ͷηογϣϯͷత w ,UPSͷઃܭ"1* w $PSPVUJOFͷ Ϩϕϧͳ"1*ͷհ w 4FSWFS4JEF,UPS w ࣮ફతͳ,UPSͷ5JQTӡ༻ࣄྫ
w $PSPVUJOFͷ Ұൠతͳ͍ࣝํ w $MJFOU4JEF,UPS ͢͜ͱ ͞ͳ͍͜ͱ -
fun Application.main() { routing { get("/kotline_fest") { call.respondText { "Kotlin
Fest 2019!” } } } } Featureͷinstallྫ w จࣈྻΛฦ͢࠷খͷΞϓϦέʔγϣϯͷίʔυ -
fun Application.main() { install(DefaultHeaders) install(CallLogging) routing { get("/kotline_fest") { call.respondText
{ "Kotlin Fest 2019!” } } } } Featureͷinstallྫ w JOTUBMMϝιουΛ༻ͯ͠ΞϓϦέʔγϣϯʹػೳՃ -
1IBTF 1IBTF 1IBTF 1IBTF { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } ύΠϓϥΠϯػߏͷΠϝʔδ -
{ setHeader() } { getAuth(userID) } { logging(response) } {
saveProfile( userID, profile ) } w ,UPSϦΫΤετΛड͚ΔͱίϧʔνϯΛ։࢝ͯ͠ ܭࢉύΠϓϥΠϯʹׂΓͯΒΕͨॲཧΛ࣮ߦ͍ͯ͘͠ 1IBTF 1IBTF 1IBTF 1IBTF -
{ asyncFun(context) } { getAuth(userID) } { logging(response) } {
saveProfile( userID, profile ) } w ,UPSϦΫΤετΛϋϯυϦϯά͢ΔͱίϧʔνϯΛ։࢝ͯ͠ ܭࢉ1JQFMJOFʹׂΓͯΒΕͨॲཧΛ࣮ߦ͍ͯ͘͠ 1IBTF 1IBTF 1IBTF 1IBTF override fun handle(ɾɾɾ) { try { ɾɾɾ launch(dispatcher + JettyCallHandlerCoroutineName) ɹɹɹɹɹɹɹɾɾɾ try { pipeline().execute(call) } catch (cancelled: CancellationException) { ɹɹɹɹɹɹɹɹɹɹɾɾɾ } } } catch (ex: Throwable) { ɹɹɹɹɹɹɹɾɾɾ } } -
{ asyncFun(context) } { getAuth(userID) } { logging(response) } {
saveProfile( userID, profile ) } w ,UPSϦΫΤετΛϋϯυϦϯά͢ΔͱίϧʔνϯΛ։࢝ͯ͠ ܭࢉ1JQFMJOFʹׂΓͯΒΕͨॲཧΛ࣮ߦ͍ͯ͘͠ 1IBTF 1IBTF 1IBTF 1IBTF override fun handle(ɾɾɾ) { try { ɾɾɾ launch(dispatcher + JettyCallHandlerCoroutineName) ɹɹɹɹɹɹɹɾɾɾ try { pipeline().execute(call) } catch (cancelled: CancellationException) { ɹɹɹɹɹɹɹɹɹɹɾɾɾ } } } catch (ex: Throwable) { ɹɹɹɹɹɹɹɾɾɾ } } -
{ setHeader() } { getAuth(userID) } { logging(response) } {
saveProfile( userID, profile ) } w ܭࢉύΠϓϥΠϯ1JQFMJOF1IBTFʹΑͬͯॲཧͷॱংΛఆٛ 1JQFMJOF1IBTFຖʹҙͷॲཧΛׂΓ͍ͯͯ͘͜ͱʹΑͬͯ ύΠϓϥΠϯશମͷॲཧΛܾఆ͢Δ͜ͱ͕Ͱ͖Δ 1IBTF 1IBTF 1IBTF 1IBTF -
{ setHeader() } { getAuth(userID) } { logging(response) } {
saveProfile( userID, profile ) } w 1JQFMJOF1IBTFطʹఆٛ͞Ε͍ͯΔͷ͋Ε ࣗͰ৽͘͠ҙͷॱ൪ͰՃ͢Δ͜ͱͰ͖Δ ҎԼ1JQFMJOF1IBTFͷྫ 4FUVQ .POJUPSJOH $BMM 4FOE -
4FUVQ $BMM 4FOE .POJUPSJOH %FGBVMU)FBEFS $BMM-PHHJOH 'FBUVSF 3PVUJOH JOTUBMM w
1JQFMJOF1IBTFͷॲཧͷׂΓͯ 'FBUVSFͷJOTUBMM3PVUJOHͷఆٛʹΑͬͯߦΘΕΔ ˞ͻͱͭͷϑΣΠζʹෳͷॲཧΛׂΓͯΔ͜ͱՄೳ -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ saveProfile( userID, profile ) } w 1JQFMJOF1IBTFͷॲཧͷׂΓͯ 'FBUVSFͷJOTUBMM3PVUJOHͷఆٛʹΑͬͯߦΘΕΔ -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } "QQMJDBUJPO$BMM w 1JQFMJOF1IBTFؒͰ"QQMJDBUJPO$BMMͱ͍͏ ΞϓϦέʔγϣϯϦΫΤετɾϨεϙϯεʹؔ͢ΔใΛ อ࣋ͨ͠NVUBCMFͳΦϒδΣΫτΛڞ༗͍ͯ͠Δ -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶃͰϦΫΤετΛϋϯυϦϯάͯ͠ 5ISFBEᶄ্ͰίϧʔνϯΛ։࢝ ͦͷ··5ISFBEᶄ্ͷίϧʔνϯͰ1JQFMJOFΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶃͷεϨουϊϯϒϩοΩϯάঢ়ଶͳͷͰ ଞͷϦΫΤετΛͭ͜ͱ͕Ͱ͖Δ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕։࢝ { setHeader() } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্ͷίϧʔνϯ͕ผͷίϧʔνϯΛಉεϨουͰ։࢝ ᶃ4FUVQϑΣΠζʹׂΓͯΒΕͨϥϜμࣜΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕։࢝ { logging(response) } {
saveProfile( userID, profile ) } { getAuth(userID) } { setHeader() } w ᶃ4FUVQϑΣΠζΛ࣮ߦ͢Δίϧʔνϯ͕ྃ ᶄ.POJUPSJOHϑΣΠζͷॲཧΛ࣮ߦ͢ΔผͷίϧʔνϯΛ։࢝ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕։࢝ { setHeader() } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ w ᶄ.POJUPSJOHϑΣΠζΛ࣮ߦ͢Δίϧʔνϯ͕ྃ ᶅ$BMMϑΣΠζͰผͷॲཧΛ࣮ߦ͢ΔผͷίϧʔνϯΛ։࢝ -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕։࢝ w ᶅ$BMMϑΣΠζΛ࣮ߦ͢Δίϧʔνϯ͕ྃ ᶆ4FOEϑΣΠζͰผͷॲཧΛ࣮ߦ͢ΔผͷίϧʔνϯΛ։࢝ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { setHeader()
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 4FOEϑΣΠζͷॲཧ͕ྃͨ͠Β5ISFBEᶄ্ͷίϧʔνϯ͕ ऴྃ͠ɺݺͼग़͠ݩͷ5ISFBEᶃͷͬͯϋϯυϦϯά͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্Ͱ։࢝ͨ͠࠷ॳͷίϧʔνϯ͕ ֤1JQFMJOF1IBTFຖʹίϧʔνϯΛੜ͍ͯ͠Δ ͦΕΒશͯ5ISFBEᶄ্Ͱಈ࡞͢Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH { setHeader() } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { logging(response) }
{ saveProfile( userID, profile ) } { getAuth(userID) } Ұ࣌தஅ͢Δؔ 1 : suspendؔʹΑͬͯҰ࣌தஅ͢Δྫ -
4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶃͰϦΫΤετΛϋϯυϦϯάͯ͠ 5ISFBEᶄ্ͰίϧʔνϯΛ։࢝ ͦͷ··5ISFBEᶄ্ͷίϧʔνϯͰ1JQFMJOFΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH w 5ISFBEᶄ্Ͱίϧʔνϯ͕4FUVQϑΣΠζͷॲཧΛ࣮ߦ ॲཧΛ࣮ߦ { delay(1000L) }
{ getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧΛ࣮ߦ { delay(1000L) } {
logging(response) } { saveProfile( userID, profile ) } { getAuth(userID) } w 4FUVQϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕.POJUPSJOHϑΣΠζͷॲཧΛ࣮ߦ Ұ࣌தஅ͢Δؔ -
4FUVQ $BMM 4FOE .POJUPSJOH w ॲཧΛҰ࣌தஅͯ͠ɺॲཧΛ࠶։͢Δ·Ͱίϧʔνϯ্Ͱػ 5ISFBEᶄผͷϦΫΤετͷॲཧΛड͚͚ΒΕΔΑ͏ʹͳΔ ॲཧΛҰ࣌தஅ { delay(1000L)
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ Ұ࣌தஅ͢Δؔ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࠶։ { delay(1000L) } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ w தஅॲཧ͕ྃͨ͠Β5ISFBEᶄͰ.POJUPSJOHϑΣΠζͷ ΓͷॲཧΛ࣮ߦ ҧ͏5ISFBEͷՄೳੑ͕͋Δ͜ͱʹҙ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { delay(1000L) } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ w .POJUPSJOHϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕$BMMϑΣΠζͷॲཧΛ࣮ߦ ॲཧ͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { delay(1000L) } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ w $BMMϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕4FOEϑΣΠζͷॲཧΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { delay(1000L)
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 4FOEϑΣΠζͷॲཧ͕ྃͨ͠Β5ISFBEᶄ্ͷίϧʔνϯ͕ ऴྃ͠ɺݺͼग़͠ݩͷ5ISFBEᶃͷͬͯϋϯυϦϯά͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH { launch { delay(1000L) } logging(request)
} { logging(response) } { saveProfile( userID, profile ) } { getAuth(userID) } ผεϨουͰ ॲཧΛߦ͏ϥϜμ 2 : launchؔͷྫ -
4FUVQ $BMM 4FOE .POJUPSJOH { launch { delay(1000L) } logging(request)
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶃͰϦΫΤετΛϋϯυϦϯάͯ͠ 5ISFBEᶄ্ͰίϧʔνϯΛ։࢝ ͦͷ··5ISFBEᶄ্ͷίϧʔνϯͰ1JQFMJOFΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH w 5ISFBEᶄ্Ͱίϧʔνϯ͕4FUVQϑΣΠζͷॲཧΛ࣮ߦ ॲཧΛ࣮ߦ { launch {
delay(1000L) } logging(request) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧΛ࣮ߦ { logging(response) } {
saveProfile( userID, profile ) } { getAuth(userID) } w 4FUVQϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕.POJUPSJOHϑΣΠζͷॲཧΛ࣮ߦ { launch { delay(1000L) } logging(request) } ผεϨουͰ ॲཧΛߦ͏ϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH w MBVODIؔʹΑͬͯ5ISFBEᶅ্ͰίϧʔνϯΛ։࢝ 5ISFBEᶄͱ5ISFBEᶅͷͦΕͧΕͰॲཧΛ࣮ߦ MBVODIؔΛ࣮ߦ { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ { launch { delay(1000L) } logging(request) } ผεϨουͰ ॲཧΛߦ͏ϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH w MBVODIؔʹΑͬͯ5ISFBEᶅ্ͰίϧʔνϯΛ։࢝ 5ISFBEᶄͱ5ISFBEᶅͷͦΕͧΕͰॲཧΛ࣮ߦ { launch {
delay(1000L) } logging(request) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ΓͷॲཧΛ࣮ߦ 5ISFBEᶅͰ࣮ߦத ผεϨουͰ ॲཧΛߦ͏ϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { launch { delay(1000L) }
logging(request) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ w .POJUPSJOHϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕$BMMϑΣΠζͷॲཧΛ࣮ߦ ॲཧ͕ऴྃ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { launch { delay(1000L) }
logging(request) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ w $BMMϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕4FOEϑΣΠζͷॲཧΛ࣮ߦ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { launch
{ delay(1000L) } logging(request) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 4FOEϑΣΠζͷॲཧ͕ྃͨ͠Β5ISFBEᶄ্ͷίϧʔνϯ 5ISFBEᶅͷίϧʔνϯ͕ऴྃ͢Δ·Ͱͭ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { launch
{ delay(1000L) } logging(request) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } 5ISFBEᶅͰ ॲཧ͕ऴྃ w 5ISFBEᶅͷίϧʔνϯ͕ऴྃͨ͠Β5ISFBEᶄ্ͷίϧʔνϯ ͕ऴྃ͠ɺݺͼग़͠ݩͷ5ISFBEᶃͷͬͯϋϯυϦϯά͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH { async { delay(1000L) “Finish” }.await()
} { logging(response) } { saveProfile( userID, profile ) } { getAuth(userID) } ผεϨουͰॲཧΛߦͬͯ ྃΛͭϥϜμ 3 : asyncؔͷྫ -
4FUVQ $BMM 4FOE .POJUPSJOH { async { delay(1000L) “Finish” }.await()
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶃͰϦΫΤετΛϋϯυϦϯάͯ͠ 5ISFBEᶄ্ͰίϧʔνϯΛ։࢝ ͦͷ··5ISFBEᶄ্ͷίϧʔνϯͰ1JQFMJOFΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH w 5ISFBEᶄ্Ͱίϧʔνϯ͕4FUVQϑΣΠζͷॲཧΛ࣮ߦ ॲཧΛ࣮ߦ { async {
delay(1000L) “Finish” }.await() } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧΛ࣮ߦ { logging(response) } {
saveProfile( userID, profile ) } { getAuth(userID) } w 4FUVQϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕.POJUPSJOHϑΣΠζͷॲཧΛ࣮ߦ { async { delay(1000L) “Finish” }.await() } ผεϨουͰॲཧΛߦͬͯ ྃΛͭϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH w BTZODؔʹΑͬͯ5ISFBEᶅ্ͰίϧʔνϯΛ։࢝ 5ISFBEᶄͱ5ISFBEᶅͷͦΕͧΕͰॲཧΛ࣮ߦ BTZODؔΛ࣮ߦ { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ { async { delay(1000L) “Finish” }.await() } ผεϨουͰॲཧΛߦͬͯ ྃΛͭϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH w BTZODؔʹΑͬͯ5ISFBEᶅ্ͰίϧʔνϯΛ։࢝ 5ISFBEᶄͱ5ISFBEᶅͷͦΕͧΕͰॲཧΛ࣮ߦ { async {
delay(1000L) “Finish” }.await() } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ΓͷॲཧΛ࣮ߦ ผεϨουͰॲཧΛߦͬͯ ྃΛͭϥϜμ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH { async { delay(1000L) “Finish” }.await()
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ 5ISFBEᶅ͕ ऴΘΔ·Ͱػ 5ISFBEᶅͰ࣮ߦத ผεϨουͰॲཧΛߦͬͯ ྃΛͭϥϜμ w 5ISFBEᶅ্ͷίϧʔνϯͷྃΛػ͍ͯ͠ΔͷͰ 5ISFBEᶄͷ.POJUPSJOHϑΣΠζͷॲཧྃ͠ͳ͍·· -
4FUVQ $BMM 4FOE .POJUPSJOH { async { delay(1000L) “Finish” }.await()
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ΓͷॲཧΛ࣮ߦ ผεϨουͰॲཧΛߦͬͯ ྃΛͭϥϜμ w 5ISFBEᶅ্ͷίϧʔνϯͷྃͷ݁ՌΛड͚ͯ 5ISFBEᶄͷ.POJUPSJOHϑΣΠζͷॲཧΛ࠶։ 5ISFBEᶅͰॲཧ͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { async { delay(1000L) “Finish”
}.await() } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ w 5ISFBEᶄͷ.POJUPSJOHϑΣΠζͷॲཧ͕ऴྃͯ͠ ͬͱ$BMMϑΣΠζͷ࣮ߦΛ։࢝Ͱ͖Δ 5ISFBEᶅͰॲཧ͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { asyncFun(context) } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ w 5ISFBEᶄͷ.POJUPSJOHϑΣΠζͷॲཧ͕ऴྃͯ͠ ͬͱ$BMMϑΣΠζͷ࣮ߦΛ։࢝Ͱ͖Δ ͜ͷผεϨουͷॲཧͪͷ࣌ؒΛ ༗ޮʹ͏ͨΊͷ"1*͕ ,UPS͔Βఏڙ͞Ε͍ͯ·͢ʂ -
4FUVQ $BMM 4FOE .POJUPSJOH { val id = async {
delay(1000L) “Finish” } proceed() logging(id.await()) } { logging(response) } { saveProfile( userID, profile ) } { getAuth(userID) } QSPDFFE Λ࣮ߦ͢ΔϥϜμ 4 : proceedϝιουͱasyncؔͷྫ -
4FUVQ $BMM 4FOE .POJUPSJOH { val id = async {
delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } QSPDFFE Λ࣮ߦ͢ΔϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH { val id = async {
delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶃͰϦΫΤετΛϋϯυϦϯάͯ͠ 5ISFBEᶄ্ͰίϧʔνϯΛ։࢝ ͦͷ··5ISFBEᶄ্ͷίϧʔνϯͰ1JQFMJOFΛ࣮ߦ -
4FUVQ $BMM 4FOE .POJUPSJOH w 5ISFBEᶄ্Ͱίϧʔνϯ͕4FUVQϑΣΠζͷॲཧΛ࣮ߦ ॲཧΛ࣮ߦ { val id
= async { delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧΛ࣮ߦ { logging(response) } {
saveProfile( userID, profile ) } { getAuth(userID) } w 4FUVQϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕.POJUPSJOHϑΣΠζͷॲཧΛ࣮ߦ { val id = async { delay(1000L) “Finish” } proceed() logging(id.await()) } QSPDFFE Λ࣮ߦ͢ΔϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH w BTZODؔʹΑͬͯ5ISFBEᶅ্ͰίϧʔνϯΛ։࢝ 5ISFBEᶄͱ5ISFBEᶅͷͦΕͧΕͰॲཧΛ࣮ߦ BTZODؔΛ࣮ߦ { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ { val id = async { delay(1000L) “Finish” } proceed() logging(id.await()) } -
4FUVQ $BMM 4FOE .POJUPSJOH w BTZODؔʹΑͬͯ5ISFBEᶅ্ͰίϧʔνϯΛ։࢝ 5ISFBEᶄͱ5ISFBEᶅͷͦΕͧΕͰॲཧΛ࣮ߦ { val id
= async { delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ΓͷॲཧΛ࣮ߦ 5ISFBEᶅͰ࣮ߦத QSPDFFE Λ࣮ߦ͢ΔϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH { val id = async {
delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ w 5ISFBEᶄ্ͰQSPDFFEΛ࣮ߦ͢Δͱ5ISFBEᶅ্ͷίϧʔνϯͷ ྃΛͨͣʹ.POJUPSJOHϑΣΠζΛҰ࣌தஅͰ͖Δ ॲཧΛҰ࣌தஅ QSPDFFE Λ࣮ߦ͢ΔϥϜμ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { val id = async
{ delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ w 5ISFBEᶄͷ.POJUPSJOHϑΣΠζΛҰ࣌தஅͯ͠ ͦͷ··5ISFBEᶄ্Ͱ$BMMϑΣΠζͷ࣮ߦΛ։࢝ ॲཧΛҰ࣌தஅ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { val id = async
{ delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ w $BMMϑΣΠζͷॲཧ͕ྃͨ͠Β࣍ʹ5ISFBEᶄ্Ͱ ίϧʔνϯ͕4FOEϑΣΠζͷॲཧΛ࣮ߦ ॲཧΛҰ࣌தஅ 5ISFBEᶅͰ࣮ߦத -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { val id = async
{ delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ ॲཧ͕ऴྃ w 5ISFBEᶄ্Ͱ4FOEϑΣΠζͷॲཧΛ࣮ߦ͍ͯ͠Δؒʹ 5ISFBEᶅ্ͷίϧʔνϯͷॲཧ͕ྃ ॲཧΛҰ࣌தஅ 5ISFBEᶅͰॲཧ͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { val id
= async { delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 4FOEϑΣΠζͷॲཧ͕ྃͨ͠Β5ISFBEᶄ্ͷίϧʔνϯ .POJUPSJOHϑΣΠζͷॲཧΛ࠶։͢Δ ॲཧΛ࠶։ 5ISFBEᶅͰॲཧ͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { val id
= async { delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 4FOEϑΣΠζͷॲཧ͕ྃͨ͠Β5ISFBEᶄ্ͷίϧʔνϯ .POJUPSJOHϑΣΠζͷॲཧΛ࠶։͢Δ ΓͷॲཧΛ࣮ߦ 5ISFBEᶅͰॲཧ͕ऴྃ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ ॲཧ͕ऴྃ { val
id = async { delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্ͷίϧʔνϯ͕ऴྃͨ͠Β ݺͼग़͠ݩͷ5ISFBEᶃͷͬͯϋϯυϦϯά͕ऴྃ 5ISFBEᶅͰॲཧ͕ऴྃ -
4 : proceedϝιουͱasyncؔͷྫ w ,UPS͔Βఏڙ͞Ε͍ͯΔQSPDFFEϝιουΛ͏͜ͱͰ ผεϨουͷίϧʔνϯ͔ΒͷฦΓΛ͍ͬͯΔؒʹ ޙଓͷผͷύΠϓϥΠϯͷॲཧΛ࣮ߦ͢Δ͜ͱ͕Ͱ͖Δ w ֤ύΠϓϥΠϯϑΣΠζͰͷϥϜμࣜΛ ผͷ'FBUVSFͰಠཱʹఆٛ͠ͳ͕Β
ύΠϓϥΠϯϑΣΠζΛލ͍ͰඇಉظॲཧΛ࣮ߦ͢Δ͜ͱͰ ΑΓޮతʹܭࢉΛߦ͏͜ͱ͕Ͱ͖Δ -
Continuation (ܧଓ) w TVTQFOEؔͰதஅ͞Εͨίϧʔνϯͷঢ়ଶ w தஅ͞Εͨ෦Ҏ߱ͷΓͷॲཧ ίʔϧόοΫ Λද͢ w SFTVNF8JUIϝιουྃίʔϧόοΫͰ͋Γ
ίϧʔνϯͷྃ࣌ʹޭɾࣦഊͷ͍ͣΕ͔Λฦ͢ public interface Continuation<in T> { public val context: CoroutineContext public fun resumeWith(result: Result<T>) } -
suspendؔ suspend fun susFuncA() { delay(1000L) } val block: suspend
() -> Unit = {} w ؔͰଞͷTVTQFOEؔΛݺͼग़͢͜ͱͰ ࣮ߦεϨουΛϒϩοΫͤͣʹ ίʔυ࣮ߦΛதஅ͢ΔՄೳੑͷ͋Δؔ w TVTQFOEؔଞͷTVTQFOE͔ؔΒͷΈݺͼग़͢͜ͱ͕Մೳ TVTQFOEؔϥϜμ fun normalFunc() { susFuncA(1000L) } ௨ৗͷ͔ؔΒͷݺͼग़͠ -
suspendؔ suspend fun susFuncA() { delay(1000L) } val block: suspend
() -> Unit = {} w ؔͰଞͷTVTQFOEؔΛݺͼग़͢͜ͱͰ ࣮ߦεϨουΛϒϩοΫͤͣʹ ίʔυ࣮ߦΛதஅ͢ΔՄೳੑͷ͋Δؔ w TVTQFOEؔଞͷTVTQFOE͔ؔΒͷΈݺͼग़͢͜ͱ͕Մೳ TVTQFOEؔϥϜμ fun normalFunc() { susFuncA(1000L) } ௨ৗͷ͔ؔΒͷݺͼग़͠ ͳͥTVTQFOEؔ ௨ৗͷ͔ؔΒݺͼग़͢͜ͱ͕ग़དྷͳ͍ͷ͔ʁ -
suspendؔ suspend fun susFunc( time: Long ) : String TVTQFOEؔ
fun susFunc( time: Long, continuation: Continuation ) : Any? ੜ͞ΕΔίʔυ ٙࣅίʔυ w TVTQFOEम০ࢠίϯύΠϧ࣌ʹผͷؔʹม͞ΕΔ ˞࣮ࡍTVTQFOEؔͷίʔυγάχνϟ͕มߋ͞ΕͨΓ ঢ়ଶΫϥε͕ੜ͞ΕͨΓ͍ͯ͠Δʹҙɻ -
suspend fun susFunc( time: Long ) : String { delay(time)
return "Kotlin Fest!" } TVTQFOEؔ -
suspend fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ -
(suspend) fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ -
(suspend) fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ ᶃҾʹ$POUJOVBUJPO͕Ճ͞Ε͍ͯΔ ᶄฦΓ͕"OZ ܕʹͳ͍ͬͯΔ -
(suspend) fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ ᶅ෦Ͱݺͼग़͍ͯ͠ΔTVTQFOEؔͷҾʹ $POUJOVBUJPO͕Ճ͞Ε͍ͯͯ ͕ࣗड͚औͬͨ$POUJOVBUJPOΛ͍ͯ͠Δ -
(suspend) fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ ௨ৗͷ͔ؔΒTVTQFOEؔΛݺͼग़ͦ͏ͱͯ͠ $POUJOVBUJPOΛҾͱͯ͢͜͠ͱ͕Ͱ͖ͳ͍ -
(suspend) fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ -
(suspend) fun susFunc( time: Long, continuation: Continuation ) : Any?
{ val call_back = when(continuation){ is ... -> continuation else -> ... } when(call_back.lable){ 1 -> { call_back.lable = 2 return delay(time, call_back) } 2 -> { return "Kotlin Fest!" } } } suspend fun susFunc( time: Long ) : String { delay(time) return "Kotlin Fest!" } TVTQFOEؔ ੜ͞ΕΔίʔυ ٙࣅίʔυ ͰͲ͏ͬͯ࠷ॳͷTVTQFOEؔΛ ී௨ͷ͔ؔΒݺͼग़͢ʁ ίϧʔνϯΛ։࢝͢Δʁ -
startCoroutine w ίϧʔνϯΛ࡞ͯ͠ݱࡏͷεϨουͰ ϨγʔόʔͰ͋ΔTVTQFOEϥϜμΛ࣮ߦ͢Δ w MBVODI BTZODͳͲͷίϧʔνϯϏϧμʔ ෦తʹݺͼग़ͯ͠ίϧʔνϯΛ࡞͍ͯ͠Δ public fun
<T> (suspend () -> T).startCoroutine( completion: kotlin.coroutines.Continuation<T> ): kotlin.Unit { /* compiled code */ } -
val continuation = object : Continuation<Unit> { override val context:
CoroutineContext = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>){ ɾɾɾ } } suspend { delay(1000L) }.startCoroutine(continuation) w TVTQFOEϥϜμͷ֦ுؔͱͯ͠ఆٛ͞Ε͍ͯͯ TVTQFOEؔҎ֎ͷ௨ৗͷ͔ؔΒݺͼग़͕͠Մೳ w $POUJOVBUJPOࣗલͰ༻ҙ͢Δ͔ طʹ։͍࢝ͯ͠Δίϧʔνϯ͔Βऔಘ͢Δ -
val continuation = object : Continuation<Unit> { override val context:
CoroutineContext = EmptyCoroutineContext override fun resumeWith(result: Result<Unit>){ ɾɾɾ } } suspend { delay(1000L) }.startCoroutine(continuation) w TVTQFOEϥϜμͷ֦ுؔͱͯ͠ఆٛ͞Ε͍ͯͯ TVTQFOEؔҎ֎ͷ௨ৗͷ͔ؔΒݺͼग़͕͠Մೳ w $POUJOVBUJPOࣗલͰ༻ҙ͢Δ͔ طʹ։͍࢝ͯ͠Δίϧʔνϯ͔Βऔಘ͢Δ -
inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn( completion: Continuation<T> ):
Any? = (this as Function1<Continuation<T>, Any?>).invoke(completion) public fun <T> (suspend () -> T).startCoroutine( completion: kotlin.coroutines.Continuation<T> ): kotlin.Unit { /* compiled code */ } startCoroutineUninterceptedOrReturn w TUBSU$PSPVUJOFͱҧ͍ɺฦΓΛड͚औΔ w ࣮ߦͨ͠TVTQFOE͕ؔ෦ͷதஅͰதஅ͞ΕΔͱ $03065*/&@4641&/%&%ϚʔΧʔΛฦ͢ -
inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn( completion: Continuation<T> ):
Any? = (this as Function1<Continuation<T>, Any?>).invoke(completion) public fun <T> (suspend () -> T).startCoroutine( completion: kotlin.coroutines.Continuation<T> ): kotlin.Unit { /* compiled code */ } startCoroutineUninterceptedOrReturn w TUBSU$PSPVUJOFͱҧ͍ɺฦΓΛड͚औΔ w ࣮ߦͨ͠TVTQFOE͕ؔ෦ͷதஅͰதஅ͞ΕΔͱ $03065*/&@4641&/%&%ϚʔΧʔΛฦ͢ -
inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn( completion: Continuation<T> ):
Any? = (this as Function1<Continuation<T>, Any?>).invoke(completion) internal enum class CoroutineSingletons { COROUTINE_SUSPENDED, UNDECIDED, RESUMED } startCoroutineUninterceptedOrReturn w TUBSU$PSPVUJOFͱҧ͍ɺฦΓΛड͚औΔ w ࣮ߦͨ͠TVTQFOE͕ؔ෦ͷதஅͰதஅ͞ΕΔͱ $03065*/&@4641&/%&%ϚʔΧʔΛฦ͢ -
inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn( completion: Continuation<T> ):
kotlin.Any? { /* compiled code */ } internal enum class CoroutineSingletons { COROUTINE_SUSPENDED, UNDECIDED, RESUMED } startCoroutineUninterceptedOrReturn w TUBSU$PSPVUJOFͱҧ͍ɺฦΓΛड͚औΔ w ࣮ߦͨ͠TVTQFOE͕ؔ෦ͷதஅͰதஅ͞ΕΔͱ $03065*/&@4641&/%&%ϚʔΧʔΛฦ͢ ίϧʔνϯʹ͞Ε͍ͯΔ ContinuationͲ͏ͬͯऔಘͰ͖Δʁ -
suspendCoroutineUninterceptedOrReturn w ίϧʔνϯͰݺͿ͜ͱͰίϧʔνϯ͕தஅͨ͠Γͷঢ়ଶΛ $POUJOVBUJPOΠϯελϯεͱͯ͠औಘͰ͖Δ"1* w ϥϜμࣜͰ$POUJOVBUJPOΠϯελϯεΛࢀর͠ ίϧʔνϯͷதஅɾྃͱ͍ͬͨঢ়ଶΛ੍ޚͰ͖Δ suspend inline fun
<T> suspendCoroutineUninterceptedOrReturn( crossinline block: (Continuation<T>) -> Any? ): T -
w ίϧʔνϯΛதஅঢ়ଶͱͯ͠ҡ࣋͢Δ߹ $03065*/&@4641&/%&%ϚʔΧʔΛฦ͢ w ίϧʔνϯΛྃ͢Δ߹ ݁ՌΛSFTVNF8JUIϝιουʹ࣮ͯ͠ߦ suspend inline fun <T>
suspendCoroutineUninterceptedOrReturn( crossinline block: (Continuation<T>) -> Any? ): T suspendCoroutineUninterceptedOrReturn -
ύϑΥʔϚϯεΛߟྀͨ͠Coroutineͷੜ 4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { getAuth(userID)
} { logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্Ͱ։࢝ͨ͠࠷ॳͷίϧʔνϯ͕ ֤1JQFMJOF1IBTFຖʹίϧʔνϯΛੜ͍ͯ͠Δ ͦΕΒશͯ5ISFBEᶄ্Ͱಈ࡞͢Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্Ͱ։࢝ͨ͠࠷ॳͷίϧʔνϯ͕ ֤1JQFMJOF1IBTFຖʹίϧʔνϯΛੜ͍ͯ͠Δ ͦΕΒશͯ5ISFBEᶄ্Ͱಈ࡞͢Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% 1IBTFຖʹίϧʔνϯΛ ੜ͢Δ͜ͱʹΑͬͯ 1IBTFຖʹதஅॲཧΩϟϯηϧΛ ੍ޚ͢Δ͜ͱ͕Ͱ͖Δ -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্Ͱ։࢝ͨ͠࠷ॳͷίϧʔνϯ͕ ֤1JQFMJOF1IBTFຖʹίϧʔνϯΛੜ͢Δ͍ͯ͠Δ ͦΕΒશͯ5ISFBEᶄ্Ͱಈ࡞͢Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% ܰྔͱ͍͑ ϦΫΤετͷʹେྔʹ ίϧʔνϯΛੜ͍ͯͯ͠ ύϑΥʔϚϯεʹӨڹͳ͍ͷʁ -
4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w 5ISFBEᶄ্Ͱ։࢝ͨ͠࠷ॳͷίϧʔνϯ͕ ֤1JQFMJOF1IBTFຖʹίϧʔνϯΛੜ͍ͯ͠Δ ͦΕΒશͯ5ISFBEᶄ্Ͱಈ࡞͢Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w ࠷ॳͷίϧʔνϯ֤1IBTFίϧʔνϯΛ࡞͢Δࡍʹ ৽ͨʹ$POUJOVBUJPOΛ࡞Βͣɺࣗͷ$POUJOVBUJPOΛ Ҿ͖͍ͯ͠Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% suspend inline fun <T> suspendCoroutineUninterceptedOrReturn( crossinline block: (Continuation<T>) -> Any? ): T w TVTQFOE$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOؔΛ͏ͱ ࣮ߦதͷίϧʔνϯͷ$POUJOVBUJPOΛऔΓग़ͯ͠ ҾͷϥϜμࣜͷதͰѻ͏͜ͱ͕Ͱ͖Δ -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w TVTQFOE$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOؔΛ͏ͱ ࣮ߦதͷίϧʔνϯͷ$POUJOVBUJPOΛऔΓग़ͯ͠ ҾͷϥϜμࣜͷதͰѻ͏͜ͱ͕Ͱ͖Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% suspend inline fun <T> suspendCoroutineUninterceptedOrReturn( crossinline block: (Continuation<T>) -> Any? ): T -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOؔͷҾʹ $POUJOVBUJPOΛ͢ͱ৽ͨʹ$POUJOVBUJPOΛ ࡞ͤͣʹίϧʔνϯΛ։࢝Ͱ͖Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn( completion: Continuation<T> ): kotlin.Any? -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% inline fun <T> (suspend () -> T).startCoroutineUninterceptedOrReturn( completion: Continuation<T> ): kotlin.Any? w TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOؔͷҾʹ $POUJOVBUJPOΛ͢ͱ৽ͨʹ$POUJOVBUJPOΛ ࡞ͤͣʹίϧʔνϯΛ։࢝Ͱ͖Δ -
suspendCoroutineUninterceptedOrReturn { continuation -> do { val rc = PhaseʹׂΓͯΒΕͨϥϜμࣜ.
startCoroutineUninterceptedOrReturn(ɾɾɾ) ɾɾɾ } while (true) COROUTINE_SUSPENDED } w TVTQFOE$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOͱ TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOΛΈ߹ΘͤΔ͜ͱͰ $POUJOVBUJPOΛڞ༗͠ͳ͕ΒίϧʔνϯΛ࡞͍ͯ͠Δ -
4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w $POUJOVBUJPOΛڞ༗ͯ͠ίϧʔνϯͷੜίετΛ͑Δ͜ͱͰ ύΠϓϥΠϯͷ࣮ߦͷύϑΥʔϚϯεΛ্͍ͯ͠Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w $POUJOVBUJPOΛڞ༗ͯ͠ίϧʔνϯͷੜίετΛ͑Δ͜ͱͰ ύΠϓϥΠϯͷ࣮ߦͷύϑΥʔϚϯεΛ্͍ͯ͠Δ ίϧʔνϯ" ίϧʔνϯ# ίϧʔνϯ$ ίϧʔνϯ% TVTQFOE$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOͱ TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOΛ Έ߹ΘͤͰίϧʔνϯͷੜίετΛ͑Δํ๏ (FOFSBUPSTͷZJFMEؔͷ෦࣮ͰΘΕ͍ͯΔ -
proceedؔͷ࣮ 4FUVQ $BMM 4FOE .POJUPSJOH { val id = async
{ delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } QSPDFFE Λ࣮ߦ͢ΔϥϜμ -
4FUVQ $BMM 4FOE .POJUPSJOH ॲཧΛ࣮ߦ { val id = async
{ delay(1000L) “Finish” } proceed() logging(id.await()) } { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } ॲཧ͕ऴྃ w QSPDFFEϝιουΛ࣮ߦ͢Δͱ֘ϑΣΠζͷॲཧΛதஅͯ͠ ޙଓͷύΠϓϥΠϯͷॲཧΛ࣮ߦͰ͖Δ ॲཧΛҰ࣌தஅ 5ISFBEᶅͰ࣮ߦத -
suspendCoroutineUninterceptedOrReturn { continuation -> do { ɾɾɾ val rc =
PhaseʹׂΓͯΒΕͨϥϜμࣜ. startCoroutineUninterceptedOrReturn(ɾɾɾ) ɾɾɾ } while (true) COROUTINE_SUSPENDED } w QSPDFFEϝιουΛ࣮ߦ͢Δͱ$POUJOVBUJPOΛ৽ͨʹ࡞ͯ͠ ผεϨουͰಈ࡞͢ΔίϧʔνϯΛ։࢝͢Δ -
private val continuation: Continuation<Unit> = object : Continuation<Unit>, CoroutineStackFrame {
ɾɾɾ override val context: CoroutineContext get () { val cont = rootContinuation return when (cont) { null -> throw IllegalStateException("Not started") is Continuation<*> -> cont.context is List<*> -> (cont as List<Continuation<*>>).last().context else -> throw IllegalStateException("e") } } } -
private val continuation: Continuation<Unit> = object : Continuation<Unit>, CoroutineStackFrame {
ɾɾɾ override val context: CoroutineContext get () { val cont = rootContinuation return when (cont) { null -> throw IllegalStateException("Not started") is Continuation<*> -> cont.context is List<*> -> (cont as List<Continuation<*>>).last().context else -> throw IllegalStateException("e") } } } -
ಠࣗͷCoroutineStart 4FUVQ $BMM 4FOE .POJUPSJOH { delay(1000L) } { delay(1000L)
} { getAuth(userID) } { logging(response) } { saveProfile( userID, profile ) } -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w ΞϓϦέʔγϣϯϦΫΤετɺϨεϙϯεͷใΛ ϛϡʔλϒϧʹอ࣋ͨ͠"QQMJDBUJPO$BMMΦϒδΣΫτΛ ֤1JQFMJOF1IBTFؒͰڞ༗͍ͯ͠Δ { asyncFun(context) } { getAuth(userID) } { saveProfile( userID, profile ) } "QQMJDBUJPO$BMM -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w "QQMJDBUJPO$BMMύΠϓϥΠϯʹάϩʔόϧͳมͱͯ͠ ڞ༗͞ΕΔͷͰͳ͘ɺ1JQFMJOF1IBTFຖͷίϧʔνϯੜ࣌ʹ $POUJOVBUJPOͱҰॹʹҾ͖͍ͯ͠Δ "QQMJDBUJPO$BMM -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } w "QQMJDBUJPO$BMMύΠϓϥΠϯʹάϩʔόϧͳมͱͯ͠ ڞ༗͞ΕΔͷͰͳ͘ɺ1JQFMJOF1IBTFຖͷίϧʔνϯੜ࣌ʹ $POUJOVBUJPOͱҰॹʹҾ͖͍ͯ͠Δ "QQMJDBUJPO$BMM ίϧʔνϯΛ։࢝͢Δͱ͖ʹ $POUJOVBUJPOҎ֎ͷΦϒδΣΫτΛ Ͳ͏ͬͯ͢ͷ͔ʁ -
public inline fun <T> (suspend () -> T). startCoroutineUninterceptedOrReturn( completion:
kotlin.coroutines.Continuation<T> ): kotlin.Any? { /* compiled code */ } public inline fun <R, T> (suspend R.() -> T). startCoroutineUninterceptedOrReturn( receiver: R, completion: kotlin.coroutines.Continuation<T> ): kotlin.Any? { /* compiled code */ } w TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOΛ͏ͱ ৽ͨʹίϧʔνϯΛੜ͢Δͱ͖ʹ$POUJOVBUJPOΛҾͱͯ͠ ͢͜ͱͰίϧʔνϯؒͰ$POUJOVBUJPOΛڞ༗Ͱ͖Δ -
public inline fun <T> (suspend () -> T). startCoroutineUninterceptedOrReturn( completion:
kotlin.coroutines.Continuation<T> ): kotlin.Any? { /* compiled code */ } public inline fun <R, T> (suspend R.() -> T). startCoroutineUninterceptedOrReturn( receiver: R, completion: kotlin.coroutines.Continuation<T> ): kotlin.Any? { /* compiled code */ } w $POUJOVBUJPOΛίϧʔνϯʹ͢"1*͔͠ެ։͞Ε͍ͯͳ͍ ͦͷͨΊίϧʔνϯ։࢝࣌ʹ"QQMJDBUJPO$BMMҾ͖͢͜ͱ ௨ৗෆՄೳ -
internal actual inline fun <R, A> (suspend R.(A) -> Unit).startCoroutineUninterceptedOrReturn3(
receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } w ,UPSͰ$POUJOVBUJPOʹՃ͑ͯҙͷܕͷΦϒδΣΫτΛ Ҿ͖ͯ͠৽ͨʹίϧʔνϯΛ։࢝͢ΔؔΛಠ࣮͍ࣗͯ͠Δ w ΈࠐΈؔͱ͍ۙॲཧΛҎԼͷؔͰ࣮ݱ͍ͯ͠Δ -
public inline fun <R, T> (suspend R.() -> T). startCoroutineUninterceptedOrReturn(
receiver: R, completion: kotlin.coroutines.Continuation<T> ): kotlin.Any? { /* compiled code */ } internal actual inline fun <R, A> (suspend R.(A) -> Unit).startCoroutineUninterceptedOrReturn3( receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } ϥΠϒϥϦ͔Βఏڙ͞Ε͍ͯΔؔ ,UPSͰಠ࣮͍ࣗͯ͠Δίϧʔνϯ։࢝"1* -
public inline fun <R, T> (suspend R.() -> T). startCoroutineUninterceptedOrReturn(
receiver: R, completion: kotlin.coroutines.Continuation<T> ): kotlin.Any? { /* compiled code */ } internal actual inline fun <R, A> (suspend R.(A) -> Unit).startCoroutineUninterceptedOrReturn3( receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } ,UPSͰಠ࣮͍ࣗͯ͠Δίϧʔνϯ։࢝"1* ϥΠϒϥϦ͔Βఏڙ͞Ε͍ͯΔؔ -
internal actual inline fun <R, A> (suspend R.(A) -> Unit).startCoroutineUninterceptedOrReturn3(
receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } w ҾΛ"ܕ͔͠ड͚औΒͳ͍ͣͷϥϜμࣜ UIJT ͕Կނ͔ Ҿͱͯ͠"ܕɾ$POUJOVBUJPOͷ̎ͭΛड͚औΔ'VODUJPOܕʹ Ωϟετ͞Ε͔ͯΒ࣮ߦ͞Ε͍ͯΔ -
internal actual inline fun <R, A> ((suspend) R.(A, Continuation<Unit>) ->
Unit). startCoroutineUninterceptedOrReturn3( receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } w ࣮ϥϜμࣜ UIJT TVTQFOEϥϜμͳͷͰίϯύΠϧ࣌ʹ Ҿͱͯ͠$POUJOVBUJPOड͚औΔϥϜμࣜʹม͞ΕΔʂ w ͕࣮ͨͬͯ͠ߦ࣌ͷΩϟετޭ͢Δ ੜ͞ΕΔίʔυ ٙࣅίʔυ -
internal actual inline fun <R, A> ((suspend) R.(A, Continuation<Unit>) ->
Unit). startCoroutineUninterceptedOrReturn3( receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } w ࣮ϥϜμࣜ UIJT TVTQFOEϥϜμͳͷͰίϯύΠϧ࣌ʹ Ҿͱͯ͠$POUJOVBUJPOड͚औΔϥϜμࣜʹม͞ΕΔʂ w ͕࣮ͨͬͯ͠ߦ࣌ͷΩϟετޭ͢Δ ੜ͞ΕΔίʔυ ٙࣅίʔυ ίϯύΠϧޙͷίʔυͷγάωνϟΛ ༧ଌͯ͠ڧ੍Ωϟετ͍ͤͯ͞Δ -
internal actual inline fun <R, A> ((suspend) R.(A, Continuation<Unit>) ->
Unit). startCoroutineUninterceptedOrReturn3( receiver: R, arg: A, continuation: Continuation<Unit> ): Any? { val function = (this as Function3<R, A, Continuation<Unit>, Any?>) return function.invoke(receiver, arg, continuation) } w ࣮ϥϜμࣜ UIJT TVTQFOEϥϜμͳͷͰίϯύΠϧ࣌ʹ Ҿͱͯ͠$POUJOVBUJPOड͚औΔϥϜμࣜʹม͞ΕΔʂ w ͕࣮ͨͬͯ͠ߦ࣌ͷΩϟετޭ͢Δ ੜ͞ΕΔίʔυ ٙࣅίʔυ ࣮TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSOͷ ࣮Ͱಉ͜͡ͱ͕ߦΘΕ͍ͯΔ ͜ͷํ๏Λࢀߟʹ͢Δͱ ҙͷͷҾΛίϧʔνϯ։࢝࣌ʹ ͢Α͏ͳϨϕϧ"1*Λ ࣗͰ࣮͢Δ͜ͱ͕Ͱ͖Δ -
4FUVQ $BMM 4FOE .POJUPSJOH { asyncFun(context) } { getAuth(userID) }
{ logging(response) } { saveProfile( userID, profile ) } -
CoroutinesͷϨϕϧAPI w ίϧʔνϯͰߴ͍ύϑΥʔϚϯεͳͲΛٻΊΔࡍʹ ॏཁʹͳͬͯ͘ΔΑ͏ͳϨϕϧͷ"1*Λհ w $POUJOVBUJPOTVTQFOEؔͷࣗಈมܗ w TUBSU$PSPVUJOF TUBSU$PSPVUJOF6OJOUFSDFQUFE0S3FUVSO w
TVTQFOE$PSPVUJOF6OJOUFSDFQUFE0S3FUVSO