Skip to content

Commit

Permalink
Fix api
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte committed Nov 8, 2024
1 parent 2ee64f0 commit 8b75131
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ public final class io/ktor/server/sse/SSEKt {
}

public final class io/ktor/server/sse/SSEServerContent : io/ktor/http/content/OutgoingContent$WriteChannelContent {
public fun <init> (Lio/ktor/server/application/ApplicationCall;Lkotlin/jvm/functions/Function2;)V
public fun <init> (Lio/ktor/server/application/ApplicationCall;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)V
public synthetic fun <init> (Lio/ktor/server/application/ApplicationCall;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun getCall ()Lio/ktor/server/application/ApplicationCall;
public fun getContentType ()Lio/ktor/http/ContentType;
public final fun getHandle ()Lkotlin/jvm/functions/Function2;
Expand Down Expand Up @@ -41,3 +43,23 @@ public final class io/ktor/server/sse/SSESessionWithSerialization$DefaultImpls {
public static fun send (Lio/ktor/server/sse/SSESessionWithSerialization;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public abstract interface class io/ktor/server/sse/ServerSSESession : kotlinx/coroutines/CoroutineScope {
public abstract fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun getCall ()Lio/ktor/server/application/ApplicationCall;
public abstract fun send (Lio/ktor/sse/ServerSentEvent;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public abstract fun send (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

public final class io/ktor/server/sse/ServerSSESession$DefaultImpls {
public static fun send (Lio/ktor/server/sse/ServerSSESession;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun send$default (Lio/ktor/server/sse/ServerSSESession;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public abstract interface class io/ktor/server/sse/ServerSSESessionWithSerialization : io/ktor/server/sse/ServerSSESession {
public abstract fun getSerializer ()Lkotlin/jvm/functions/Function2;
}

public final class io/ktor/server/sse/ServerSSESessionWithSerialization$DefaultImpls {
public static fun send (Lio/ktor/server/sse/ServerSSESessionWithSerialization;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/Long;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@
// - Show declarations: true

// Library unique name: <io.ktor:ktor-server-sse>
abstract interface io.ktor.server.sse/SSESession : kotlinx.coroutines/CoroutineScope { // io.ktor.server.sse/SSESession|null[0]
abstract val call // io.ktor.server.sse/SSESession.call|{}call[0]
abstract fun <get-call>(): io.ktor.server.application/ApplicationCall // io.ktor.server.sse/SSESession.call.<get-call>|<get-call>(){}[0]
abstract interface io.ktor.server.sse/ServerSSESession : kotlinx.coroutines/CoroutineScope { // io.ktor.server.sse/ServerSSESession|null[0]
abstract val call // io.ktor.server.sse/ServerSSESession.call|{}call[0]
abstract fun <get-call>(): io.ktor.server.application/ApplicationCall // io.ktor.server.sse/ServerSSESession.call.<get-call>|<get-call>(){}[0]

abstract suspend fun close() // io.ktor.server.sse/SSESession.close|close(){}[0]
abstract suspend fun send(io.ktor.sse/ServerSentEvent) // io.ktor.server.sse/SSESession.send|send(io.ktor.sse.ServerSentEvent){}[0]
open suspend fun send(kotlin/String? = ..., kotlin/String? = ..., kotlin/String? = ..., kotlin/Long? = ..., kotlin/String? = ...) // io.ktor.server.sse/SSESession.send|send(kotlin.String?;kotlin.String?;kotlin.String?;kotlin.Long?;kotlin.String?){}[0]
abstract suspend fun close() // io.ktor.server.sse/ServerSSESession.close|close(){}[0]
abstract suspend fun send(io.ktor.sse/ServerSentEvent) // io.ktor.server.sse/ServerSSESession.send|send(io.ktor.sse.ServerSentEvent){}[0]
open suspend fun send(kotlin/String? = ..., kotlin/String? = ..., kotlin/String? = ..., kotlin/Long? = ..., kotlin/String? = ...) // io.ktor.server.sse/ServerSSESession.send|send(kotlin.String?;kotlin.String?;kotlin.String?;kotlin.Long?;kotlin.String?){}[0]
}

abstract interface io.ktor.server.sse/SSESessionWithSerialization : io.ktor.server.sse/SSESession { // io.ktor.server.sse/SSESessionWithSerialization|null[0]
abstract val serializer // io.ktor.server.sse/SSESessionWithSerialization.serializer|{}serializer[0]
abstract fun <get-serializer>(): kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String> // io.ktor.server.sse/SSESessionWithSerialization.serializer.<get-serializer>|<get-serializer>(){}[0]
abstract interface io.ktor.server.sse/ServerSSESessionWithSerialization : io.ktor.server.sse/ServerSSESession { // io.ktor.server.sse/ServerSSESessionWithSerialization|null[0]
abstract val serializer // io.ktor.server.sse/ServerSSESessionWithSerialization.serializer|{}serializer[0]
abstract fun <get-serializer>(): kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String> // io.ktor.server.sse/ServerSSESessionWithSerialization.serializer.<get-serializer>|<get-serializer>(){}[0]
}

final class <#A: io.ktor.server.sse/SSESession> io.ktor.server.sse/SSEServerContent : io.ktor.http.content/OutgoingContent.WriteChannelContent { // io.ktor.server.sse/SSEServerContent|null[0]
constructor <init>(io.ktor.server.application/ApplicationCall, kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String>?, kotlin.coroutines/SuspendFunction1<#A, kotlin/Unit>) // io.ktor.server.sse/SSEServerContent.<init>|<init>(io.ktor.server.application.ApplicationCall;kotlin.Function2<io.ktor.util.reflect.TypeInfo,kotlin.Any,kotlin.String>?;kotlin.coroutines.SuspendFunction1<1:0,kotlin.Unit>){}[0]
final class io.ktor.server.sse/SSEServerContent : io.ktor.http.content/OutgoingContent.WriteChannelContent { // io.ktor.server.sse/SSEServerContent|null[0]
constructor <init>(io.ktor.server.application/ApplicationCall, kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESession, kotlin/Unit>) // io.ktor.server.sse/SSEServerContent.<init>|<init>(io.ktor.server.application.ApplicationCall;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.ServerSSESession,kotlin.Unit>){}[0]
constructor <init>(io.ktor.server.application/ApplicationCall, kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESession, kotlin/Unit>, kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String>? = ...) // io.ktor.server.sse/SSEServerContent.<init>|<init>(io.ktor.server.application.ApplicationCall;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.ServerSSESession,kotlin.Unit>;kotlin.Function2<io.ktor.util.reflect.TypeInfo,kotlin.Any,kotlin.String>?){}[0]

final val call // io.ktor.server.sse/SSEServerContent.call|{}call[0]
final fun <get-call>(): io.ktor.server.application/ApplicationCall // io.ktor.server.sse/SSEServerContent.call.<get-call>|<get-call>(){}[0]
final val contentType // io.ktor.server.sse/SSEServerContent.contentType|{}contentType[0]
final fun <get-contentType>(): io.ktor.http/ContentType // io.ktor.server.sse/SSEServerContent.contentType.<get-contentType>|<get-contentType>(){}[0]
final val handle // io.ktor.server.sse/SSEServerContent.handle|{}handle[0]
final fun <get-handle>(): kotlin.coroutines/SuspendFunction1<#A, kotlin/Unit> // io.ktor.server.sse/SSEServerContent.handle.<get-handle>|<get-handle>(){}[0]
final fun <get-handle>(): kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESession, kotlin/Unit> // io.ktor.server.sse/SSEServerContent.handle.<get-handle>|<get-handle>(){}[0]
final val serialize // io.ktor.server.sse/SSEServerContent.serialize|{}serialize[0]
final fun <get-serialize>(): kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String>? // io.ktor.server.sse/SSEServerContent.serialize.<get-serialize>|<get-serialize>(){}[0]

Expand All @@ -39,10 +40,10 @@ final class <#A: io.ktor.server.sse/SSESession> io.ktor.server.sse/SSEServerCont
final val io.ktor.server.sse/SSE // io.ktor.server.sse/SSE|{}SSE[0]
final fun <get-SSE>(): io.ktor.server.application/ApplicationPlugin<kotlin/Unit> // io.ktor.server.sse/SSE.<get-SSE>|<get-SSE>(){}[0]

final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/SSESession, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.SSESession,kotlin.Unit>){}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String> = ..., kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/SSESessionWithSerialization, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.Function2<io.ktor.util.reflect.TypeInfo,kotlin.Any,kotlin.String>;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.SSESessionWithSerialization,kotlin.Unit>){}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin/String, kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/SSESession, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.String;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.SSESession,kotlin.Unit>){}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin/String, kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String> = ..., kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/SSESessionWithSerialization, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.String;kotlin.Function2<io.ktor.util.reflect.TypeInfo,kotlin.Any,kotlin.String>;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.SSESessionWithSerialization,kotlin.Unit>){}[0]
final suspend inline fun <#A: reified kotlin/Any> (io.ktor.server.sse/SSESessionWithSerialization).io.ktor.server.sse/send(#A) // io.ktor.server.sse/send|[email protected].SSESessionWithSerialization(0:0){0§<kotlin.Any>}[0]
final suspend inline fun <#A: reified kotlin/Any> (io.ktor.server.sse/SSESessionWithSerialization).io.ktor.server.sse/send(#A? = ..., kotlin/String? = ..., kotlin/String? = ..., kotlin/Long? = ..., kotlin/String? = ...) // io.ktor.server.sse/send|[email protected].SSESessionWithSerialization(0:0?;kotlin.String?;kotlin.String?;kotlin.Long?;kotlin.String?){0§<kotlin.Any>}[0]
final suspend inline fun <#A: reified kotlin/Any> (io.ktor.server.sse/SSESessionWithSerialization).io.ktor.server.sse/send(io.ktor.sse/TypedServerSentEvent<#A>) // io.ktor.server.sse/send|[email protected].SSESessionWithSerialization(io.ktor.sse.TypedServerSentEvent<0:0>){0§<kotlin.Any>}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESession, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.ServerSSESession,kotlin.Unit>){}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String> = ..., kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESessionWithSerialization, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.Function2<io.ktor.util.reflect.TypeInfo,kotlin.Any,kotlin.String>;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.ServerSSESessionWithSerialization,kotlin.Unit>){}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin/String, kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESession, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.String;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.ServerSSESession,kotlin.Unit>){}[0]
final fun (io.ktor.server.routing/Route).io.ktor.server.sse/sse(kotlin/String, kotlin/Function2<io.ktor.util.reflect/TypeInfo, kotlin/Any, kotlin/String> = ..., kotlin.coroutines/SuspendFunction1<io.ktor.server.sse/ServerSSESessionWithSerialization, kotlin/Unit>) // io.ktor.server.sse/sse|[email protected](kotlin.String;kotlin.Function2<io.ktor.util.reflect.TypeInfo,kotlin.Any,kotlin.String>;kotlin.coroutines.SuspendFunction1<io.ktor.server.sse.ServerSSESessionWithSerialization,kotlin.Unit>){}[0]
final suspend inline fun <#A: reified kotlin/Any> (io.ktor.server.sse/ServerSSESessionWithSerialization).io.ktor.server.sse/send(#A) // io.ktor.server.sse/send|[email protected].ServerSSESessionWithSerialization(0:0){0§<kotlin.Any>}[0]
final suspend inline fun <#A: reified kotlin/Any> (io.ktor.server.sse/ServerSSESessionWithSerialization).io.ktor.server.sse/send(#A? = ..., kotlin/String? = ..., kotlin/String? = ..., kotlin/Long? = ..., kotlin/String? = ...) // io.ktor.server.sse/send|[email protected].ServerSSESessionWithSerialization(0:0?;kotlin.String?;kotlin.String?;kotlin.Long?;kotlin.String?){0§<kotlin.Any>}[0]
final suspend inline fun <#A: reified kotlin/Any> (io.ktor.server.sse/ServerSSESessionWithSerialization).io.ktor.server.sse/send(io.ktor.sse/TypedServerSentEvent<#A>) // io.ktor.server.sse/send|[email protected].ServerSSESessionWithSerialization(io.ktor.sse.TypedServerSentEvent<0:0>){0§<kotlin.Any>}[0]
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal class DefaultServerSSESession(
private val output: ByteWriteChannel,
override val call: ApplicationCall,
override val coroutineContext: CoroutineContext
) : SSESession {
) : ServerSSESession {
private val mutex = Mutex()

override suspend fun send(event: ServerSentEvent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import io.ktor.util.reflect.*
*
* @param path A URL path at which to handle Server-Sent Events (SSE) requests.
* @param handler A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE
* endpoint. Inside the handler, you can use the functions provided by [SSESessionWithSerialization]
* endpoint. Inside the handler, you can use the functions provided by [ServerSSESessionWithSerialization]
* to send events to the connected clients.
*
* Example of usage:
Expand All @@ -33,9 +33,9 @@ import io.ktor.util.reflect.*
* To learn more, see [the SSE](https://en.wikipedia.org/wiki/Server-sent_events)
* and [the SSE specification](https://html.spec.whatwg.org/multipage/server-sent-events.html).
*
* @see SSESession
* @see ServerSSESession
*/
public fun Route.sse(path: String, handler: suspend SSESession.() -> Unit) {
public fun Route.sse(path: String, handler: suspend ServerSSESession.() -> Unit) {
route(path, HttpMethod.Get) {
sse(handler)
}
Expand All @@ -46,7 +46,7 @@ public fun Route.sse(path: String, handler: suspend SSESession.() -> Unit) {
* Requires [SSE] plugin to be installed.
*
* @param handler A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE
* endpoint. Inside the handler, you can use the functions provided by [SSESessionWithSerialization]
* endpoint. Inside the handler, you can use the functions provided by [ServerSSESessionWithSerialization]
* to send events to the connected clients.
*
* Example of usage:
Expand All @@ -64,9 +64,9 @@ public fun Route.sse(path: String, handler: suspend SSESession.() -> Unit) {
* To learn more, see [the SSE](https://en.wikipedia.org/wiki/Server-sent_events)
* and [the SSE specification](https://html.spec.whatwg.org/multipage/server-sent-events.html).
*
* @see SSESession
* @see ServerSSESession
*/
public fun Route.sse(handler: suspend SSESession.() -> Unit): Unit = processSSE(null, handler)
public fun Route.sse(handler: suspend ServerSSESession.() -> Unit): Unit = processSSEWithoutSerialization(null, handler)

/**
* Adds a route to handle Server-Sent Events (SSE) at the specified [path] using the provided [handler].
Expand All @@ -75,7 +75,7 @@ public fun Route.sse(handler: suspend SSESession.() -> Unit): Unit = processSSE(
* @param path A URL path at which to handle Server-Sent Events (SSE) requests.
* @param serialize A function to serialize data objects into the `data` field of a `ServerSentEvent`.
* @param handler A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE
* endpoint. Inside the handler, you can use the functions provided by [SSESessionWithSerialization]
* endpoint. Inside the handler, you can use the functions provided by [ServerSSESessionWithSerialization]
* to send events to the connected clients.
*
* Example of usage:
Expand All @@ -95,12 +95,12 @@ public fun Route.sse(handler: suspend SSESession.() -> Unit): Unit = processSSE(
* To learn more, see [the SSE](https://en.wikipedia.org/wiki/Server-sent_events)
* and [the SSE specification](https://html.spec.whatwg.org/multipage/server-sent-events.html).
*
* @see SSESessionWithSerialization
* @see ServerSSESessionWithSerialization
*/
public fun Route.sse(
path: String,
serialize: (TypeInfo, Any) -> String = { _, it -> it.toString() },
handler: suspend SSESessionWithSerialization.() -> Unit
handler: suspend ServerSSESessionWithSerialization.() -> Unit
) {
route(path, HttpMethod.Get) {
sse(serialize, handler)
Expand All @@ -113,7 +113,7 @@ public fun Route.sse(
*
* @param serialize A function to serialize data objects into the `data` field of a `ServerSentEvent`.
* @param handler A function that defines the behavior of the SSE session. It is invoked when a client connects to the SSE
* endpoint. Inside the handler, you can use the functions provided by [SSESessionWithSerialization]
* endpoint. Inside the handler, you can use the functions provided by [ServerSSESessionWithSerialization]
* to send events to the connected clients.
*
* Example of usage:
Expand All @@ -133,16 +133,35 @@ public fun Route.sse(
* To learn more, see [the SSE](https://en.wikipedia.org/wiki/Server-sent_events)
* and [the SSE specification](https://html.spec.whatwg.org/multipage/server-sent-events.html).
*
* @see SSESessionWithSerialization
* @see ServerSSESessionWithSerialization
*/
public fun Route.sse(
serialize: (TypeInfo, Any) -> String = { _, it -> it.toString() },
handler: suspend SSESessionWithSerialization.() -> Unit
): Unit = processSSE(serialize, handler)
handler: suspend ServerSSESessionWithSerialization.() -> Unit
): Unit = processSSEWithSerialization(serialize, handler)

private fun Route.processSSEWithoutSerialization(
serialize: ((TypeInfo, Any) -> String)?,
handler: suspend ServerSSESession.() -> Unit
) = processSSE(serialize, handler)

private fun Route.processSSEWithSerialization(
serialize: ((TypeInfo, Any) -> String)?,
handler: suspend ServerSSESessionWithSerialization.() -> Unit
) {
val sessionHandler: suspend ServerSSESession.() -> Unit = {
if (this is ServerSSESessionWithSerialization) {
handler()
} else {
throw IllegalStateException("ServerSSESessionWithSerialization is required.")
}
}
processSSE(serialize, sessionHandler)
}

private fun Route.processSSE(
serialize: ((TypeInfo, Any) -> String)?,
handler: suspend SSESessionWithSerialization.() -> Unit
handler: suspend ServerSSESession.() -> Unit
) {
plugin(SSE)

Expand All @@ -151,6 +170,6 @@ private fun Route.processSSE(
call.response.header(HttpHeaders.CacheControl, "no-store")
call.response.header(HttpHeaders.Connection, "keep-alive")
call.response.header("X-Accel-Buffering", "no")
call.respond(SSEServerContent(call, serialize, handler))
call.respond(SSEServerContent(call, handler, serialize))
}
}
Loading

0 comments on commit 8b75131

Please sign in to comment.