From 219fab20573ddf97fff49ebdfab900c823eaff4d Mon Sep 17 00:00:00 2001 From: Manuel Burghard Date: Thu, 29 Feb 2024 11:13:55 +0100 Subject: [PATCH] Add EmptyDisposable --- .../Source/Disposables/EmptyDisposable.swift | 30 +++++++++++++++++++ .../EffectHandlers/EffectRouterDSL.swift | 4 +-- .../CompositeEventSourceBuilder.swift | 2 +- MobiusCore/Source/Mobius.swift | 2 +- .../EffectHandlers/EffectHandlerTests.swift | 2 +- .../EffectHandlers/EffectRouterDSLTests.swift | 2 +- .../EffectHandlers/EffectRouterTests.swift | 6 ++-- MobiusCore/Test/NonReentrancyTests.swift | 2 +- .../Test/EventSource+ExtensionsTests.swift | 2 +- 9 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 MobiusCore/Source/Disposables/EmptyDisposable.swift diff --git a/MobiusCore/Source/Disposables/EmptyDisposable.swift b/MobiusCore/Source/Disposables/EmptyDisposable.swift new file mode 100644 index 00000000..ceba8514 --- /dev/null +++ b/MobiusCore/Source/Disposables/EmptyDisposable.swift @@ -0,0 +1,30 @@ +// Copyright 2019-2024 Spotify AB. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import Foundation + +/// The `EmptyDisposable` class implements a `Disposable` type for when you don't have anything to dispose of. +public final class EmptyDisposable: MobiusCore.Disposable { + + /// Create an `EmptyDisposable` + public init() {} + + public func dispose() { + // No-op + } +} + +public extension Disposable where Self == EmptyDisposable { + static var empty: EmptyDisposable { EmptyDisposable() } +} diff --git a/MobiusCore/Source/EffectHandlers/EffectRouterDSL.swift b/MobiusCore/Source/EffectHandlers/EffectRouterDSL.swift index ce6998ef..4b92b9d2 100644 --- a/MobiusCore/Source/EffectHandlers/EffectRouterDSL.swift +++ b/MobiusCore/Source/EffectHandlers/EffectRouterDSL.swift @@ -42,7 +42,7 @@ public extension _PartialEffectRouter { return to { parameters, callback in fireAndForget(parameters) callback.end() - return AnonymousDisposable {} + return .empty } } @@ -58,7 +58,7 @@ public extension _PartialEffectRouter { callback.send(event) } callback.end() - return AnonymousDisposable {} + return .empty } } } diff --git a/MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift b/MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift index 1662deb5..37cebd04 100644 --- a/MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift +++ b/MobiusCore/Source/EventSources/CompositeEventSourceBuilder.swift @@ -40,7 +40,7 @@ public struct CompositeEventSourceBuilder { public func build() -> AnyEventSource { switch eventSources.count { case 0: - return AnyEventSource { _ in AnonymousDisposable {} } + return AnyEventSource { _ in .empty } case 1: return eventSources[0] default: diff --git a/MobiusCore/Source/Mobius.swift b/MobiusCore/Source/Mobius.swift index 11c21af4..23ade8f6 100644 --- a/MobiusCore/Source/Mobius.swift +++ b/MobiusCore/Source/Mobius.swift @@ -74,7 +74,7 @@ public enum Mobius { return Builder( update: update, effectHandler: effectHandler, - eventSource: AnyEventSource({ _ in AnonymousDisposable(disposer: {}) }), + eventSource: AnyEventSource({ _ in .empty }), eventConsumerTransformer: { $0 }, logger: AnyMobiusLogger(NoopLogger()) ) diff --git a/MobiusCore/Test/EffectHandlers/EffectHandlerTests.swift b/MobiusCore/Test/EffectHandlers/EffectHandlerTests.swift index a76ab783..121e09f9 100644 --- a/MobiusCore/Test/EffectHandlers/EffectHandlerTests.swift +++ b/MobiusCore/Test/EffectHandlers/EffectHandlerTests.swift @@ -83,5 +83,5 @@ private func handleEffect(effect: Effect, callback: EffectCallback) -> Di callback.send(.eventForEffect1) } callback.end() - return AnonymousDisposable {} + return .empty } diff --git a/MobiusCore/Test/EffectHandlers/EffectRouterDSLTests.swift b/MobiusCore/Test/EffectHandlers/EffectRouterDSLTests.swift index 32ab2891..be22b42f 100644 --- a/MobiusCore/Test/EffectHandlers/EffectRouterDSLTests.swift +++ b/MobiusCore/Test/EffectHandlers/EffectRouterDSLTests.swift @@ -167,7 +167,7 @@ class EffectRouterDSLTests: QuickSpec { expect(effect).to(equal(.effect1)) callback.send(.eventForEffect1) callback.end() - return AnonymousDisposable {} + return .empty } .asConnectable .connect { events.append($0) } diff --git a/MobiusCore/Test/EffectHandlers/EffectRouterTests.swift b/MobiusCore/Test/EffectHandlers/EffectRouterTests.swift index 4616069e..60e1c66c 100644 --- a/MobiusCore/Test/EffectHandlers/EffectRouterTests.swift +++ b/MobiusCore/Test/EffectHandlers/EffectRouterTests.swift @@ -98,7 +98,7 @@ class EffectRouterTests: QuickSpec { .to { _, callback in callback.send(.eventForEffect2) callback.end() - return AnonymousDisposable {} + return .empty } .asConnectable @@ -119,7 +119,7 @@ class EffectRouterTests: QuickSpec { beforeEach { let handler = AnyEffectHandler { _, _ in - AnonymousDisposable {} + .empty } let invalidRouter = EffectRouter() .routeEffects(equalTo: .multipleHandlersForThisEffect).to(handler) @@ -163,7 +163,7 @@ class EffectRouterTests: QuickSpec { .routeEffects(equalTo: .effect2) .to { _, callback in callback.end() - return AnonymousDisposable {} + return .empty } .asConnectable diff --git a/MobiusCore/Test/NonReentrancyTests.swift b/MobiusCore/Test/NonReentrancyTests.swift index 4c090c8b..31c5c434 100644 --- a/MobiusCore/Test/NonReentrancyTests.swift +++ b/MobiusCore/Test/NonReentrancyTests.swift @@ -67,7 +67,7 @@ class NonReentrancyTests: QuickSpec { let testEffectHandler = AnyEffectHandler { handleEffect($0, $1) - return AnonymousDisposable {} + return .empty } let effectConnectable = EffectRouter() diff --git a/MobiusExtras/Test/EventSource+ExtensionsTests.swift b/MobiusExtras/Test/EventSource+ExtensionsTests.swift index 8b2a2e76..fb1990e7 100644 --- a/MobiusExtras/Test/EventSource+ExtensionsTests.swift +++ b/MobiusExtras/Test/EventSource+ExtensionsTests.swift @@ -27,7 +27,7 @@ class EventSourceExtensionsTests: QuickSpec { beforeEach { intEventSource = AnyEventSource { (consumer: @escaping (Int) -> Void) in subscribedIntConsumer = consumer - return AnonymousDisposable {} + return .empty } }