Skip to content

Commit

Permalink
Fix runBlocking
Browse files Browse the repository at this point in the history
  • Loading branch information
bootstraponline committed Oct 29, 2018
1 parent 31df4fc commit a795319
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 86 deletions.
8 changes: 8 additions & 0 deletions mv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

rm -rf "../ios/ios-frameworks/run_blocking.framework/"
rm -rf "../ios/ios-frameworks/run_blocking.framework.dSYM"

cp -R "./build/bin/ios/main/debug/framework/run_blocking.framework" "../ios/ios-frameworks/run_blocking.framework/"

cp -R "./build/bin/ios/main/debug/framework/run_blocking.framework.dSYM" "../ios/ios-frameworks/run_blocking.framework.dSYM/"
16 changes: 16 additions & 0 deletions src/commonMain/kotlin/example/Common.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example

import io.ktor.client.HttpClient
import io.ktor.client.call.call
import io.ktor.client.response.readText
import kotlinx.io.core.use

expect fun <T> runBlocking(block: suspend () -> T): T

fun healthCheck(): String {
return runBlocking {
HttpClient().use {
it.call("http:https://this.is.a.test.instructure.com/health_check").response.readText()
}
}
}
17 changes: 0 additions & 17 deletions src/commonMain/kotlin/example/GitHub.kt

This file was deleted.

12 changes: 12 additions & 0 deletions src/commonTest/kotlin/example/CommonTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package example

import kotlin.test.Test
import kotlin.test.assertEquals

class CommonTest {

@Test
fun testHealthCheck() {
assertEquals("canvas ok", healthCheck())
}
}
13 changes: 0 additions & 13 deletions src/commonTest/kotlin/example/GitHubTest.kt

This file was deleted.

29 changes: 29 additions & 0 deletions src/iosMain/kotlin/example/Expectation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package example

import platform.Foundation.NSDate
import platform.Foundation.NSRunLoop
import platform.Foundation.addTimeInterval
import platform.Foundation.runUntilDate

class Expectation<T> {
private var waiting = true
private var result: T? = null

fun fulfill(result: T?) {
waiting = false
this.result = result
}

fun wait(): T? {
while (waiting) {
advanceRunLoop()
}

return result
}
}

private fun advanceRunLoop() {
val date = NSDate().addTimeInterval(1.0) as NSDate
NSRunLoop.mainRunLoop.runUntilDate(date)
}
21 changes: 0 additions & 21 deletions src/iosMain/kotlin/example/GitHubIos.kt

This file was deleted.

27 changes: 27 additions & 0 deletions src/iosMain/kotlin/example/RunBlocking.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package example

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.launch
import platform.Foundation.NSRunLoop
import platform.Foundation.performBlock
import kotlin.coroutines.CoroutineContext

actual fun <T> runBlocking(block: suspend () -> T): T {
val expectation = Expectation<T>()

GlobalScope.launch(MainRunLoopDispatcher) {
expectation.fulfill(block.invoke())
}

return expectation.wait() ?: throw RuntimeException("runBlocking failed")
}

private object MainRunLoopDispatcher : CoroutineDispatcher() {
override fun dispatch(context: CoroutineContext, block: Runnable) {
NSRunLoop.mainRunLoop().performBlock {
block.run()
}
}
}
14 changes: 0 additions & 14 deletions src/iosTest/kotlin/sample/GitHubTestIos.kt

This file was deleted.

8 changes: 0 additions & 8 deletions src/jvmMain/kotlin/example/GitHubJvm.kt

This file was deleted.

5 changes: 5 additions & 0 deletions src/jvmMain/kotlin/example/Jvm.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package example

import kotlinx.coroutines.runBlocking

actual fun <T> runBlocking(block: suspend () -> T): T = runBlocking { block.invoke() }
13 changes: 0 additions & 13 deletions src/jvmTest/kotlin/example/GitHubTestJvm.kt

This file was deleted.

0 comments on commit a795319

Please sign in to comment.