Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
bump coroutines lib version
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Mar 5, 2017
1 parent 70b69be commit a625da0
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'com.github.sanity'
version '0.0.27'
version '0.0.29'

buildscript {
ext.kotlin_version = '1.1.0'
Expand Down Expand Up @@ -70,8 +70,8 @@ dependencies {
compile group: 'commons-io', name: 'commons-io', version: '2.5'

compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.10-rc'
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:0.10-rc'
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.12'
compile 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:0.12'

compile 'io.github.microutils:kotlin-logging:1.4.2'

Expand Down
79 changes: 78 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,81 @@ asynchronicity largely invisible to the programmer.

#### What does it look like?

Check out the included [demos](https://github.com/sanity/kweb/tree/master/src/main/kotlin/com/github/sanity/kweb/demos).
Here we create a simple "todo" list app, note this is heavily commented, excluding comments it is
fewer than 40 lines of code.

```kotlin
import com.github.sanity.kweb.KWeb
import com.github.sanity.kweb.dom.element.creation.*
import com.github.sanity.kweb.dom.element.events.on
import com.github.sanity.kweb.dom.element.modification.addText
import com.github.sanity.kweb.dom.element.modification.delete
import kotlinx.coroutines.experimental.future.await
import kotlinx.coroutines.experimental.future.future

fun main(args: Array<String>) {
// Starts a web server listening on port 8091
KWeb(8091, debug = true) {
doc.body.apply {
// Add a header parent to the body, along with some simple instructions.
h1().addText("Simple KWeb demo - a to-do list")
p().addText("Edit the text box below and click the button to add the item. Click an item to remove it.")

// If you're unfamiliar with the `apply` function, read this:
// http://beust.com/weblog/2015/10/30/exploring-the-kotlin-standard-library/

// We create a <ul> parent, and then use apply() to add things to it
val ul = ul().apply {

// Add some initial items to the list
for (text in listOf("one", "two", "three")) {
// We define this below
newListItem(text)
}
}

// Next create an input parent
val inputElement = input(type = InputType.text, size = 20)

// And a button to add a new item
val button = button()
button.addText("Add Item")
// Here we register a callback, the code block will be called when the
// user clicks this button.
button.on.click {
// This looks simple, but it is deceptively cool, and in more complex applications is
// the key to hiding the client/server divide in a fairly efficient matter. It uses
// Kotlin 1.1's new coroutines functionality, see
// https://github.com/Kotlin/kotlinx.coroutines

// We start an async block, which will allow us to use `await` within the block
future {
// This is where async comes in. inputElement.getValue() sends a message to the
// browser asking for the `value` of inputElement. This will take time so
// inputElement.getValue() actually returns a future. `await()` then uses
// coroutines to effectively wait until the future comes back, but crucially,
// without tying up a thread (which would getString very inefficient very quickly).
val newItemText = inputElement.getValue().await()

// And now we add the new item using our custom function
ul.newListItem(newItemText)

// And finally reset the value of the inputElement parent.
inputElement.setValue("")
}
}
}
}
Thread.sleep(10000)
}

// Here we use an extension method which can be used on any <UL> parent to add a list item which will
// delete itself when clicked.
fun ULElement.newListItem(text: String) {
li().apply {
addText(text)
on.click { event ->
delete() }
}
}
```

0 comments on commit a625da0

Please sign in to comment.