|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.rpc |
| 6 | + |
| 7 | +import kotlinx.coroutines.CoroutineScope |
| 8 | +import kotlinx.coroutines.Deferred |
| 9 | +import kotlinx.coroutines.flow.Flow |
| 10 | +import kotlinx.coroutines.flow.SharedFlow |
| 11 | +import kotlinx.coroutines.flow.StateFlow |
| 12 | +import kotlinx.rpc.descriptor.RpcServiceDescriptor |
| 13 | +import kotlinx.rpc.internal.FieldDataObject |
| 14 | +import kotlinx.rpc.internal.RpcFlow |
| 15 | + |
| 16 | +/** |
| 17 | + * Registers Flow<T> field of the interface. Sends initialization request, subscribes to emitted values |
| 18 | + * and returns the instance of the flow to be consumed |
| 19 | + * |
| 20 | + * @param T type parameter for Flow |
| 21 | + * @param serviceScope Service's coroutine scope |
| 22 | + * @param fieldName the name of the field. |
| 23 | + * @param descriptor descriptor of the service, that made the call |
| 24 | + * that is used to be mapped to the corresponding field on a server. |
| 25 | + * @param serviceId id of the service, that made the call |
| 26 | + * @return Flow instance to be consumed. |
| 27 | + */ |
| 28 | +public fun <T> RpcClient.registerPlainFlowField( |
| 29 | + serviceScope: CoroutineScope, |
| 30 | + fieldName: String, |
| 31 | + descriptor: RpcServiceDescriptor<*>, |
| 32 | + serviceId: Long, |
| 33 | +): Flow<T> { |
| 34 | + return RpcFlow.Plain(descriptor.fqName, initializeFlowField(serviceScope, fieldName, descriptor, serviceId)) |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Registers SharedFlow<T> field of the interface. Sends initialization request, subscribes to emitted values |
| 39 | + * and returns the instance of the flow to be consumed |
| 40 | + * |
| 41 | + * @param T type parameter for SharedFlow |
| 42 | + * @param serviceScope Service's coroutine scope |
| 43 | + * @param fieldName the name of the field. |
| 44 | + * @param descriptor descriptor of the service, that made the call |
| 45 | + * that is used to be mapped to the corresponding field on a server. |
| 46 | + * @param serviceId id of the service, that made the call |
| 47 | + * @return SharedFlow instance to be consumed. |
| 48 | + */ |
| 49 | +public fun <T> RpcClient.registerSharedFlowField( |
| 50 | + serviceScope: CoroutineScope, |
| 51 | + fieldName: String, |
| 52 | + descriptor: RpcServiceDescriptor<*>, |
| 53 | + serviceId: Long, |
| 54 | +): SharedFlow<T> { |
| 55 | + return RpcFlow.Shared(descriptor.fqName, initializeFlowField(serviceScope, fieldName, descriptor, serviceId)) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Registers StateFlow<T> field of the interface. Sends initialization request, subscribes to emitted values |
| 60 | + * and returns the instance of the flow to be consumed |
| 61 | + * |
| 62 | + * @param T type parameter for StateFlow |
| 63 | + * @param serviceScope Service's coroutine scope |
| 64 | + * @param fieldName the name of the field. |
| 65 | + * @param descriptor descriptor of the service, that made the call |
| 66 | + * that is used to be mapped to the corresponding field on a server. |
| 67 | + * @param serviceId id of the service, that made the call |
| 68 | + * @return StateFlow instance to be consumed. |
| 69 | + */ |
| 70 | +public fun <T> RpcClient.registerStateFlowField( |
| 71 | + serviceScope: CoroutineScope, |
| 72 | + fieldName: String, |
| 73 | + descriptor: RpcServiceDescriptor<*>, |
| 74 | + serviceId: Long, |
| 75 | +): StateFlow<T> { |
| 76 | + return RpcFlow.State(descriptor.fqName, initializeFlowField(serviceScope, fieldName, descriptor, serviceId)) |
| 77 | +} |
| 78 | + |
| 79 | +private fun <T, FlowT : Flow<T>> RpcClient.initializeFlowField( |
| 80 | + serviceScope: CoroutineScope, |
| 81 | + fieldName: String, |
| 82 | + descriptor: RpcServiceDescriptor<*>, |
| 83 | + serviceId: Long, |
| 84 | +): Deferred<FlowT> { |
| 85 | + return callSync(serviceScope, RpcCall(descriptor, fieldName, FieldDataObject, serviceId)) |
| 86 | +} |
0 commit comments