Skip to content

Commit

Permalink
API refinements
Browse files Browse the repository at this point in the history
  • Loading branch information
Alkenso committed Oct 4, 2021
1 parent 3466f71 commit 8511879
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions Sources/SwiftConvenience/Misc/Transformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ public class Transformer<Input, Transformed, Output> {
}


// MARK: Register
// MARK: Subscription

public func register(on queue: DispatchQueue = .global(), transform: @escaping AsyncTransform) -> TransformerSubscription {
public func subscribe(on queue: DispatchQueue = .global(), transform: @escaping AsyncTransform) -> TransformerSubscription {
let subscription = DeinitAction {}
let id = ObjectIdentifier(subscription)
_transforms.writeAsync {
$0[id] = (transform, queue)
}
subscription.replaceCleanup { [weak self] in self?.unregister(id) }
subscription.replaceCleanup { [weak self] in self?.unsubscribe(id) }
return subscription
}

public func register(on queue: DispatchQueue = .global(), transform: @escaping SyncTransform) -> TransformerSubscription {
register(on: queue) { $1(transform($0)) }
public func subscribe(on queue: DispatchQueue = .global(), transform: @escaping SyncTransform) -> TransformerSubscription {
subscribe(on: queue) { $1(transform($0)) }
}

private func unregister(_ id: ObjectIdentifier) {
private func unsubscribe(_ id: ObjectIdentifier) {
_transforms.writeAsync {
$0.removeValue(forKey: id)
}
Expand Down Expand Up @@ -133,7 +133,7 @@ extension Notifier where Transformed == Void, Output == Void {
public var publisher: AnyPublisher<Input, Never> {
let subject = PassthroughSubject<Input, Never>()
var proxy = NotificationChainSubject(proxy: subject.eraseToAnyPublisher())
proxy.chainSubscription = register(on: .global(), transform: subject.send)
proxy.chainSubscription = subscribe(on: .global(), transform: subject.send)
return proxy.eraseToAnyPublisher()
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftConvenienceTests/TransformerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ final class TransformerTests: XCTestCase {
var disposables: [Any] = []
let postedEvent = "some event"
let transformer = TransformerOneToMany<String, Int>()
disposables.append(transformer.register { event in
disposables.append(transformer.subscribe { event in
XCTAssertEqual(event, postedEvent)
return 1
})
disposables.append(transformer.register { event, completion in
disposables.append(transformer.subscribe { event, completion in
XCTAssertEqual(event, postedEvent)
completion(2)
})
Expand Down

0 comments on commit 8511879

Please sign in to comment.