What is the idiom to write (the important aspect i...
# webassembly
d
What is the idiom to write (the important aspect is I have a static constant variable value for the handler, to
removeEventListener
and
addEventListener
are working on exact same handle)
Copy code
@JsFun("() => { return undefined }")
external fun jsUndefined(): JsAny = definedExternally

val myHandler: EventListener = fun(event: Event): JsAny {  /// this is the problem part the setup
  return jsUndefined()
}

/// tried
val myHandler: EventListener = (Event): JsAny -> { event ->
  // ...
}
val myHandler: EventListener = EventListener { event ->
  // ...
}
val myHandler: EventListener = objectFactory.eventListener { event ->  // I make this up, but maybe a cure to this idiom
  // ...
}
Copy code
val myHandler = { event: Event ->
   // return true   // this errors now
}

//////
ele.removeEventListener(EV_click, myHandler)
ele.addEventListener(EV_click, myHandler)

/// This compiles without error, but has not return value possible, is this a good idiom ?
Copy code
fun myHandler(event: Event): Boolean {
  // do stuff
  return false // this work here
}

val myHandlerCB = { event: Event ->
   // return true   // this errors now
    myHandler(event)  // this compiles but how does a return work here
}

//////
ele.removeEventListener(EV_click, myHandlerCB)
ele.addEventListener(EV_click, myHandlerCB)

/// This compiles without error now and I get to use control flow that can use `return`
/// to return early.
/// But is myHandlerCB passing on a return value ?
I am expecting the
myHandlerDB
is a constant value for the lifetime of the WASM. Does WASM provide a lifecycle event or maybe a calling convention
fun preDestroy() {}
like
fun main() {}
is a entrypoint convention ? In the case it is unloaded but the context it was loaded into is not being destroyed, like the webpage.
Hmm why is the prototype for EventListener
((Event) -> Unit)
? Surly it should be returning
Any?
or
JsAny?
?