orangy
class FlashSessionTransport(private val transport: SessionTransport) : SessionTransport {
companion object {
private val flashSessionKey = AttributeKey<Boolean>("FlashSessionReceived")
}
override fun receive(call: ApplicationCall): String? = transport.receive(call)?.also {
call.attributes.put(flashSessionKey, true)
}
override fun send(call: ApplicationCall, value: String) = if (call.attributes.getOrNull(flashSessionKey) == true)
transport.clear(call)
else
transport.send(call, value)
override fun clear(call: ApplicationCall) = transport.clear(call)
}
inline fun <S : Any> Sessions.Configuration.flash(name: String, sessionType: KClass<S>, block: CookieSessionBuilder<S>.() -> Unit) {
val builder = CookieSessionBuilder(sessionType).apply(block)
val transport = FlashSessionTransport(SessionTransportCookie(name, builder.cookie, builder.transformers))
val tracker = SessionTrackerByValue(sessionType, builder.serializer)
val provider = SessionProvider(name, sessionType, transport, tracker)
register(provider)
}