Skip to content

Commit

Permalink
Add AnyPureLoadable and AnyLoadable
Browse files Browse the repository at this point in the history
  • Loading branch information
aleh committed Oct 31, 2022
1 parent b33086f commit da4a53e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion MMMLoadable.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
Pod::Spec.new do |s|

s.name = "MMMLoadable"
s.version = "1.8.0"
s.version = "1.9.0"
s.summary = "A simple model for async calculations"
s.description = "#{s.summary}."
s.homepage = "https://github.com/mediamonks/#{s.name}"
Expand Down
48 changes: 48 additions & 0 deletions Sources/MMMLoadable/MMMLoadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,51 @@ extension MMMPureLoadableProtocol {
}
}
}

/// Forwards all calls in `MMMPureLoadableProtocol` to another object.
/// ("Type-erases" objects conforming `MMMPureLoadableProtocol`, if you want to be fancy.)
///
/// This is used as a base for public models where we want to hide internal methods that, although being public
/// technically, are only meant to be called internally, mostly by the superclass (e.g. `doSync`).
///
/// The actual implementation can inherit from all those base classes with exposed overridable methods but then it
/// is embedded into another wrapper that uses this "box" as its base to forward only the calls of public protocols.
open class AnyPureLoadable: NSObject, MMMPureLoadableProtocol {

private let object: MMMPureLoadableProtocol

public init(_ original: MMMPureLoadableProtocol) {
self.object = original
}

public var loadableState: MMMLoadableState { object.loadableState }
public var error: Error? { object.error }
public var isContentsAvailable: Bool { object.isContentsAvailable }
public func addObserver(_ observer: MMMLoadableObserverProtocol) { object.addObserver(observer) }
public func removeObserver(_ observer: MMMLoadableObserverProtocol) { object.removeObserver(observer) }
}

/// Forwards all calls in `MMMLoadableProtocol` to another object.
/// ("Type-erases" objects conforming `MMMLoadableProtocol`, if you want to be fancy.)
///
/// See `AnyPureLoadable`.
open class AnyLoadable: NSObject, MMMLoadableProtocol {

private let object: MMMLoadableProtocol

public init(_ original: MMMLoadableProtocol) {
self.object = original
}

// Don't want to inherit from the "pure" version of this helper to simplify the hierarchy.

public var loadableState: MMMLoadableState { object.loadableState }
public var error: Error? { object.error }
public var isContentsAvailable: Bool { object.isContentsAvailable }
public func addObserver(_ observer: MMMLoadableObserverProtocol) { object.addObserver(observer) }
public func removeObserver(_ observer: MMMLoadableObserverProtocol) { object.removeObserver(observer) }

public func sync() { object.sync() }
public var needsSync: Bool { object.needsSync }
public func syncIfNeeded() { object.syncIfNeeded() }
}

0 comments on commit da4a53e

Please sign in to comment.