Я пытаюсь перенести этот класс в swift на Kotlin. Существует ли Kotlin способ достижения расширений и уменьшения состояния (получения состояния)
public final class MutateNode {
public var onSubscribe: (() -> Void)?
public var onPublish: ((State) -> Void)?
public var leafs: [Node]
public init(leafs: [Node], onSubscribe: (() -> Void)? = nil, onPublish: ((State) -> Void)? = nil) {
self.leafs = leafs
self.onPublish = onPublish
self.onSubscribe = onSubscribe
}
}
extension MutateNode: ControlNode {
public var state: AnyPublisher<State, Never> {
guard !leafs.isEmpty else {
return Just(.failure)
.eraseToAnyPublisher()
}
return leafs.reduce(into: Just(.failure).eraseToAnyPublisher()) { (resultPublisher, nextChild) in
resultPublisher = resultPublisher.flatMap { state in
state == .success ? Just(state).eraseToAnyPublisher() : nextChild.state.eraseToAnyPublisher()
}.eraseToAnyPublisher()
}
.handleEvents(
receiveSubscription: { [weak self] _ in self?.onSubscribe?() },
receiveOutput: { [weak self] state in self?.onPublish?(state) }
)
.eraseToAnyPublisher()
}
}