package de.kif.frontend

import org.w3c.dom.*
import kotlin.coroutines.Continuation
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.coroutines.startCoroutine


operator fun HTMLCollection.iterator() = object : Iterator<HTMLElement> {
    private var index = 0
    override fun hasNext(): Boolean {
        return index < this@iterator.length
    }

    override fun next(): HTMLElement {
        return this@iterator.get(index++) as HTMLElement
    }

}

operator fun NodeList.iterator() = object : Iterator<Node> {
    private var index = 0
    override fun hasNext(): Boolean {
        return index < this@iterator.length
    }

    override fun next(): Node {
        return this@iterator.get(index++)!!
    }

}

fun launch(context: CoroutineContext = EmptyCoroutineContext, block: suspend () -> Unit) =
    block.startCoroutine(Continuation(context) { result ->
        result.onFailure { exception ->
            console.error(exception)
        }
    })