Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add empty AnonymousDisposable initializer #1

Open
wants to merge 1 commit into
base: add-EmptyDisposable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions MobiusCore/Source/Disposables/AnonymousDisposable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation

/// The `AnonymousDisposable` class implements a `Disposable` type that disposes of resources via a closure.
public final class AnonymousDisposable: MobiusCore.Disposable {
/// The closure which disposes of the object.
private var disposer: (() -> Void)?
private let lock = DispatchQueue(label: "Mobius.AnonymousDisposable")
public final class AnonymousDisposable: Disposable {
private let disposable: Synchronized<(() -> Void)?>?

/// Creates a type-erased `AnonymousDisposable` that wraps the given instance.
public convenience init<Disposable: MobiusCore.Disposable>(_ base: Disposable) {
Expand All @@ -35,17 +31,17 @@ public final class AnonymousDisposable: MobiusCore.Disposable {
///
/// - Parameter disposer: The code which disposes of the resources.
public init(disposer: @escaping () -> Void) {
self.disposer = disposer
disposable = Synchronized(value: disposer)
}

public func dispose() {
var disposer: (() -> Void)?
public init() {
disposable = nil
}

lock.sync {
disposer = self.disposer
self.disposer = nil
public func dispose() {
disposable?.mutate { disposer in
disposer?()
disposer = nil
}

disposer?()
}
}
26 changes: 0 additions & 26 deletions MobiusCore/Source/Disposables/EmptyDisposable.swift

This file was deleted.

4 changes: 2 additions & 2 deletions MobiusCore/Source/EffectHandlers/EffectRouterDSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public extension _PartialEffectRouter {
return to { parameters, callback in
fireAndForget(parameters)
callback.end()
return EmptyDisposable()
return AnonymousDisposable()
}
}

Expand All @@ -58,7 +58,7 @@ public extension _PartialEffectRouter {
callback.send(event)
}
callback.end()
return EmptyDisposable()
return AnonymousDisposable()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct CompositeEventSourceBuilder<Event> {
public func build() -> AnyEventSource<Event> {
switch eventSources.count {
case 0:
return AnyEventSource { _ in EmptyDisposable() }
return AnyEventSource { _ in AnonymousDisposable() }
case 1:
return eventSources[0]
default:
Expand Down
2 changes: 1 addition & 1 deletion MobiusCore/Source/Mobius.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public enum Mobius {
return Builder(
update: update,
effectHandler: effectHandler,
eventSource: AnyEventSource({ _ in EmptyDisposable() }),
eventSource: AnyEventSource({ _ in AnonymousDisposable() }),
eventConsumerTransformer: { $0 },
logger: AnyMobiusLogger(NoopLogger())
)
Expand Down
2 changes: 1 addition & 1 deletion MobiusCore/Test/EffectHandlers/EffectHandlerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ private func handleEffect(effect: Effect, callback: EffectCallback<Event>) -> Di
callback.send(.eventForEffect1)
}
callback.end()
return EmptyDisposable()
return AnonymousDisposable()
}
2 changes: 1 addition & 1 deletion MobiusCore/Test/EffectHandlers/EffectRouterDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class EffectRouterDSLTests: QuickSpec {
expect(effect).to(equal(.effect1))
callback.send(.eventForEffect1)
callback.end()
return EmptyDisposable()
return AnonymousDisposable()
}
.asConnectable
.connect { events.append($0) }
Expand Down
6 changes: 3 additions & 3 deletions MobiusCore/Test/EffectHandlers/EffectRouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class EffectRouterTests: QuickSpec {
.to { _, callback in
callback.send(.eventForEffect2)
callback.end()
return EmptyDisposable()
return AnonymousDisposable()
}
.asConnectable

Expand All @@ -119,7 +119,7 @@ class EffectRouterTests: QuickSpec {

beforeEach {
let handler = AnyEffectHandler<Effect, Event> { _, _ in
EmptyDisposable()
AnonymousDisposable()
}
let invalidRouter = EffectRouter<Effect, Event>()
.routeEffects(equalTo: .multipleHandlersForThisEffect).to(handler)
Expand Down Expand Up @@ -163,7 +163,7 @@ class EffectRouterTests: QuickSpec {
.routeEffects(equalTo: .effect2)
.to { _, callback in
callback.end()
return EmptyDisposable()
return AnonymousDisposable()
}
.asConnectable

Expand Down
2 changes: 1 addition & 1 deletion MobiusCore/Test/NonReentrancyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class NonReentrancyTests: QuickSpec {

let testEffectHandler = AnyEffectHandler<Effect, Event> {
handleEffect($0, $1)
return EmptyDisposable()
return AnonymousDisposable()
}

let effectConnectable = EffectRouter<Effect, Event>()
Expand Down
2 changes: 1 addition & 1 deletion MobiusExtras/Test/EventSource+ExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EventSourceExtensionsTests: QuickSpec {
beforeEach {
intEventSource = AnyEventSource { (consumer: @escaping (Int) -> Void) in
subscribedIntConsumer = consumer
return EmptyDisposable()
return AnonymousDisposable()
}
}

Expand Down