From a86c0384d43e83f3d71123a687d7113ef03a1622 Mon Sep 17 00:00:00 2001 From: Him188 Date: Mon, 16 Aug 2021 16:04:58 +0800 Subject: [PATCH] Add `AbstractPipelineContext` --- .../components/NoticeProcessorPipeline.kt | 57 +++++++++++-------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/mirai-core/src/commonMain/kotlin/network/components/NoticeProcessorPipeline.kt b/mirai-core/src/commonMain/kotlin/network/components/NoticeProcessorPipeline.kt index a81d7e08b..adecf988a 100644 --- a/mirai-core/src/commonMain/kotlin/network/components/NoticeProcessorPipeline.kt +++ b/mirai-core/src/commonMain/kotlin/network/components/NoticeProcessorPipeline.kt @@ -137,6 +137,36 @@ internal interface PipelineContext : BotAware { } } +internal abstract class AbstractPipelineContext( + override val bot: QQAndroidBot, override val attributes: TypeSafeMap, +) : PipelineContext { + private val consumers: Stack = Stack() + + override val isConsumed: Boolean get() = consumers.isNotEmpty() + override fun NoticeProcessor.markAsConsumed(marker: Any) { + consumers.push(marker) + } + + override fun NoticeProcessor.markNotConsumed(marker: Any) { + if (consumers.peek() === marker) { + consumers.pop() + } + } + + override val collected = MutableProcessResult(ConcurrentLinkedQueue()) + + override fun collect(packet: Packet) { + collected.data.add(packet) + } + + override fun collect(packets: Iterable) { + this.collected.data.addAll(packets) + } + + abstract override suspend fun processAlso(data: ProtocolStruct, attributes: TypeSafeMap): ProcessResult +} + + internal inline val PipelineContext.context get() = this internal open class NoticeProcessorPipelineImpl private constructor() : NoticeProcessorPipeline { @@ -151,31 +181,8 @@ internal open class NoticeProcessorPipelineImpl private constructor() : NoticePr inner class ContextImpl( - override val bot: QQAndroidBot, override val attributes: TypeSafeMap, - ) : PipelineContext { - private val consumers: Stack = Stack() - - override val isConsumed: Boolean get() = consumers.isNotEmpty() - override fun NoticeProcessor.markAsConsumed(marker: Any) { - consumers.push(marker) - } - - override fun NoticeProcessor.markNotConsumed(marker: Any) { - if (consumers.peek() === marker) { - consumers.pop() - } - } - - override val collected = MutableProcessResult(ConcurrentLinkedQueue()) - - override fun collect(packet: Packet) { - collected.data.add(packet) - } - - override fun collect(packets: Iterable) { - this.collected.data.addAll(packets) - } - + bot: QQAndroidBot, attributes: TypeSafeMap, + ) : AbstractPipelineContext(bot, attributes) { override suspend fun processAlso(data: ProtocolStruct, attributes: TypeSafeMap): ProcessResult { return process(bot, data, this.attributes + attributes) }