-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extract classes needed to be shared (#408)
Co-authored-by: Muhammad Rehan <mrehan.official@gmail.com>
- Loading branch information
Showing
46 changed files
with
3,688 additions
and
3,459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import Foundation | ||
|
||
public class DIGraphShared: DIManager { | ||
public static let shared: DIGraphShared = .init() | ||
|
||
public var singletons: [String: Any] = [:] | ||
public var overrides: [String: Any] = [:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import Foundation | ||
|
||
public protocol DIManager: AnyObject { | ||
var overrides: [String: Any] { get set } | ||
var singletons: [String: Any] { get set } | ||
|
||
func override<T: Any>(value: T, forType type: T.Type) | ||
func getOverriddenInstance<T: Any>() -> T? | ||
func reset() | ||
} | ||
|
||
public extension DIManager { | ||
/** | ||
Designed to be used only in test classes to override dependencies. | ||
|
||
``` | ||
let mockOffRoadWheels = // make a mock of OffRoadWheels class | ||
DIGraph.shared.override(mockOffRoadWheels, OffRoadWheels.self) | ||
``` | ||
*/ | ||
func override<T>(value: T, forType type: T.Type) { | ||
overrides[String(describing: type)] = value | ||
} | ||
|
||
// Retrieves an overridden instance of a specified type from the `overrides` dictionary. | ||
// If an overridden instance exists and can be cast to the specified type, it is returned; otherwise, nil is returned. | ||
func getOverriddenInstance<T: Any>() -> T? { | ||
// Get the type name as the key for the dictionary. | ||
let typeName = String(describing: T.self) | ||
|
||
guard overrides[typeName] != nil else { | ||
return nil // no override set. Quit early. | ||
} | ||
|
||
// Get and cast the overridden instance from the dictionary. | ||
guard let overriddenInstance = overrides[typeName] as? T else { | ||
fatalError("Failed to cast overridden instance to type '\(typeName)'.") | ||
} | ||
|
||
return overriddenInstance | ||
} | ||
|
||
// Reset the DI graph (useful for testing) | ||
func reset() { | ||
singletons = [:] | ||
overrides = [:] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Foundation | ||
|
||
// Base class meant to be subclassed by top-level classes such as `MessagingPush` and `MessagingInApp`. Provides some | ||
// boilerplate logic to creating an implementation object | ||
// once the SDK has finally been initialized. | ||
// | ||
// Top-level class meaning it contains public facing SDK functions called by customers. | ||
// There isn't a constructor populated via dependency injection. It's at the top node | ||
// of dependencies. | ||
// TODO: [CDP] Remove class if no longer needed | ||
open class ModuleTopLevelObject<ImplementationClass> { | ||
private(set) var alreadyCreatedImplementation: ImplementationClass? | ||
public var implementation: ImplementationClass? { | ||
alreadyCreatedImplementation ?? createAndSetImplementationInstance() | ||
} | ||
|
||
// for writing tests | ||
public init(implementation: ImplementationClass?) { | ||
self.alreadyCreatedImplementation = implementation | ||
} | ||
|
||
// singleton constructor | ||
public init() { | ||
} | ||
|
||
private func createAndSetImplementationInstance() -> ImplementationClass? { | ||
let newInstance = getImplementationInstance() | ||
alreadyCreatedImplementation = newInstance | ||
return newInstance | ||
} | ||
|
||
// We want each top level module to have an initialize function so that features get setup as early as possible | ||
open func inititlizeModule() { | ||
fatalError("forgot to override in subclass") | ||
} | ||
|
||
open func getImplementationInstance() -> ImplementationClass { | ||
fatalError("forgot to override in subclass") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.