Skip to content

Commit

Permalink
MainActor-ing everything and everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
NikSativa committed Oct 11, 2024
1 parent 337bb09 commit 2628e19
Show file tree
Hide file tree
Showing 38 changed files with 126 additions and 34 deletions.
1 change: 1 addition & 0 deletions .github/workflows/swift_macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- "main"
- "*"
paths:
- ".github/workflows/**"
- "Package.swift"
Expand Down
23 changes: 21 additions & 2 deletions Source/Arguments.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import Foundation

@MainActor
public struct Arguments {
private let elements: [Any]

@MainActor
public init(_ elements: [Any]) {
self.elements = elements
}

@MainActor
public init(_ elements: Any...) {
self.elements = elements
}

@MainActor
public init() {
self.elements = []
}
Expand Down Expand Up @@ -49,18 +53,22 @@ public struct Arguments {
}
}

#if swift(>=6.0)

// MARK: - Sequence

extension Arguments: Sequence {
extension Arguments: @preconcurrency Sequence {
public func makeIterator() -> some IteratorProtocol {
return ArgumentsIterator(self)
}
}

private final class ArgumentsIterator: IteratorProtocol {
@MainActor
private final class ArgumentsIterator: @preconcurrency IteratorProtocol {
let args: Arguments
var idx = 0

@MainActor
init(_ args: Arguments) {
self.args = args
}
Expand All @@ -79,8 +87,19 @@ private final class ArgumentsIterator: IteratorProtocol {

// MARK: - Arguments + ExpressibleByArrayLiteral

@MainActor
extension Arguments: @preconcurrency ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Any...) {
self.init(elements)
}
}
#else

// MARK: - Arguments + ExpressibleByArrayLiteral

extension Arguments: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Any...) {
self.init(elements)
}
}
#endif
1 change: 1 addition & 0 deletions Source/Assembly.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
public protocol Assembly {
/// unique identifier of the assembly that will be used to used to remove duplicates from the Container
/// - Warning: override it on your own risk
Expand Down
6 changes: 5 additions & 1 deletion Source/Container.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import Foundation

@MainActor
public final class Container {
private typealias Storyboardable = (Any, Resolver) -> Void
private var storages: [String: Storage] = [:]

@MainActor
public init(assemblies: [Assembly]) {
let allAssemblies = assemblies.flatMap(\.allDependencies).unified()
for assembly in allAssemblies {
assembly.assemble(with: self)
}
}

@MainActor
public convenience init(assemblies: Assembly...) {
self.init(assemblies: assemblies)
}
Expand Down Expand Up @@ -75,7 +78,7 @@ extension Container: Registrator {
@discardableResult
public func register<T>(_ type: T.Type,
options: Options,
entity: @escaping (Resolver, _ arguments: Arguments) -> T) -> Forwarding {
entity: @MainActor @escaping (Resolver, _ arguments: Arguments) -> T) -> Forwarding {
let key = key(type, name: options.name)

if let found = storages[key] {
Expand Down Expand Up @@ -174,6 +177,7 @@ private extension Optional {
}

private extension [Assembly] {
@MainActor
func unified() -> [Element] {
var keys: Set<String> = []
let unified = filter {
Expand Down
4 changes: 4 additions & 0 deletions Source/Forwarder.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
public protocol Forwarding {
@discardableResult
func implements<T>(_ type: T.Type, named: String?, accessLevel: Options.AccessLevel?) -> Self
Expand All @@ -12,14 +13,17 @@ public extension Forwarding {
}
}

@MainActor
protocol ForwardRegistrator: AnyObject {
func register<T>(_ type: T.Type, named: String?, storage: Storage)
}

@MainActor
struct Forwarder: Forwarding {
private unowned let container: ForwardRegistrator
private let storage: Storage

@MainActor
init(container: ForwardRegistrator,
storage: Storage) {
self.container = container
Expand Down
5 changes: 5 additions & 0 deletions Source/Options.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
public struct Options: Equatable {
public enum AccessLevel: Equatable {
case final
Expand Down Expand Up @@ -31,6 +32,7 @@ public struct Options: Equatable {
public let entityKind: EntityKind
public let name: String?

@MainActor
public init(accessLevel: AccessLevel = .final,
entityKind: EntityKind = .weak,
name: String? = nil) {
Expand All @@ -40,14 +42,17 @@ public struct Options: Equatable {
}
}

@MainActor
public func +(lhs: Options, rhs: Options.EntityKind) -> Options {
return .init(accessLevel: lhs.accessLevel, entityKind: rhs, name: lhs.name)
}

@MainActor
public func +(lhs: Options, rhs: Options.AccessLevel) -> Options {
return .init(accessLevel: rhs, entityKind: lhs.entityKind, name: lhs.name)
}

@MainActor
public func +(lhs: Options, rhs: String) -> Options {
return .init(accessLevel: lhs.accessLevel, entityKind: lhs.entityKind, name: rhs)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import Combine
import SwiftUI

@MainActor @propertyWrapper
@MainActor
@propertyWrapper
public struct DIObservedObject<Value: ObservableObject>: DynamicProperty {
@EnvironmentObject
private var container: ObservableResolver
Expand Down
4 changes: 3 additions & 1 deletion Source/PropertyWrapper/DIPropertyWrapper/DIProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
import Foundation
import SwiftUI

@MainActor
public struct DIProviderOptions {
public let name: String?
public let arguments: Arguments?
}

@MainActor @propertyWrapper
@MainActor
@propertyWrapper
public struct DIProvider<Value>: DynamicProperty {
@EnvironmentObject
private var container: ObservableResolver
Expand Down
3 changes: 2 additions & 1 deletion Source/PropertyWrapper/DIPropertyWrapper/DIState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import Combine
import SwiftUI

@MainActor @propertyWrapper
@MainActor
@propertyWrapper
public struct DIState<Value>: DynamicProperty {
@EnvironmentObject
private var container: ObservableResolver
Expand Down
3 changes: 2 additions & 1 deletion Source/PropertyWrapper/DIPropertyWrapper/DIStateObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import Combine
import SwiftUI

@MainActor @propertyWrapper
@MainActor
@propertyWrapper
public struct DIStateObject<Value: ObservableObject>: DynamicProperty {
@EnvironmentObject
private var container: ObservableResolver
Expand Down
1 change: 1 addition & 0 deletions Source/PropertyWrapper/EnvParametersHolder.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#if canImport(SwiftUI)
import Foundation

@MainActor
internal final class EnvParametersHolder {
var name: String?
var arguments: Arguments?
Expand Down
1 change: 1 addition & 0 deletions Source/PropertyWrapper/SharedPropertyWrapper/Inject.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
@propertyWrapper
public struct Inject<Value> {
public private(set) var wrappedValue: Value {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
@propertyWrapper
public struct InjectLazy<Value> {
public private(set) var wrappedValue: Value {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
@propertyWrapper
public struct InjectProvider<Value> {
public private(set) var wrappedValue: Value {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
@propertyWrapper
public struct InjectWrapped<Value: InstanceWrapper> {
public private(set) var wrappedValue: Value {
Expand Down
10 changes: 6 additions & 4 deletions Source/Registrator.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import Foundation

@MainActor
public protocol Registrator {
/// register classes
@discardableResult
func register<T>(_ type: T.Type,
options: Options,
entity: @escaping (_ resolver: Resolver, _ arguments: Arguments) -> T) -> Forwarding
entity: @MainActor @escaping (_ resolver: Resolver, _ arguments: Arguments) -> T) -> Forwarding

func registration<T>(for type: T.Type,
name: String?) -> Forwarding
}

@MainActor
public extension Registrator {
@discardableResult
func register<T>(_ type: T.Type = T.self,
options: Options = .default,

Check warning on line 19 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 19 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 19 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 19 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6
entity: @escaping (_ resolver: Resolver, _ arguments: Arguments) -> T) -> Forwarding {
entity: @MainActor @escaping (_ resolver: Resolver, _ arguments: Arguments) -> T) -> Forwarding {
return register(type,
options: options,
entity: entity)
Expand All @@ -24,7 +26,7 @@ public extension Registrator {
@discardableResult
func register<T>(_ type: T.Type = T.self,
options: Options = .default,

Check warning on line 28 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 28 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 28 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6
entity: @escaping (_ resolver: Resolver) -> T) -> Forwarding {
entity: @MainActor @escaping (_ resolver: Resolver) -> T) -> Forwarding {
return register(type,
options: options,
entity: { resolver, _ in
Expand All @@ -35,7 +37,7 @@ public extension Registrator {
@discardableResult
func register<T>(_ type: T.Type = T.self,
options: Options = .default,

Check warning on line 39 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 39 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6

Check warning on line 39 in Source/Registrator.swift

View workflow job for this annotation

GitHub Actions / macOS Xcode_15.4 5.10

main actor-isolated static property 'default' can not be referenced from a non-isolated context; this is an error in Swift 6
entity: @escaping () -> T) -> Forwarding {
entity: @MainActor @escaping () -> T) -> Forwarding {
return register(type,
options: options,
entity: { _, _ in
Expand Down
1 change: 1 addition & 0 deletions Source/Resolver.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
public protocol Resolver {
func optionalResolve<T>(_ type: T.Type, named: String?, with arguments: Arguments) -> T?
func resolve<T>(_ type: T.Type, named: String?, with arguments: Arguments?) -> T
Expand Down
5 changes: 5 additions & 0 deletions Source/SelfInjectable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Foundation
import ObjectiveC

@MainActor
public protocol SelfInjectable {
func resolveDependncies(with resolver: Resolver)
}
Expand All @@ -14,6 +15,7 @@ extension NSObject {
static let dipTag: StaticString = "DIKit.dipTag"
}

@MainActor
@objc
private var isInitializedFromDI: Bool {
get {
Expand All @@ -30,6 +32,7 @@ extension NSObject {
}
}

@MainActor
@objc
private(set) var dipTag: String? {
get {
Expand All @@ -50,13 +53,15 @@ extension NSObject {
}
}

@MainActor
private func isDependenciesInitializationNeeded() -> Bool {
defer {
isInitializedFromDI = true
}
return !isInitializedFromDI
}

@MainActor
public func resolveDependnciesIfNeeded(with resolver: Resolver) {
if isDependenciesInitializationNeeded(),
let selfInjectable = self as? SelfInjectable {
Expand Down
1 change: 1 addition & 0 deletions Source/Storages/ContainerStorage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
final class ContainerStorage: Storage {
private var entity: Any?
private let generator: Generator
Expand Down
1 change: 1 addition & 0 deletions Source/Storages/ForwardingStorage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
final class ForwardingStorage: Storage {
let accessLevel: Options.AccessLevel
private let storage: Storage
Expand Down
1 change: 1 addition & 0 deletions Source/Storages/Storage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
protocol Storage {
typealias Entity = Any
typealias Generator = (Resolver, _ arguments: Arguments) -> Entity
Expand Down
1 change: 1 addition & 0 deletions Source/Storages/TransientStorage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
final class TransientStorage: Storage {
private let generator: Generator
let accessLevel: Options.AccessLevel
Expand Down
1 change: 1 addition & 0 deletions Source/Storages/WeakStorage.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Foundation

@MainActor
final class WeakStorage: Storage {
private weak var entity: AnyObject?
private let generator: Generator
Expand Down
Loading

0 comments on commit 2628e19

Please sign in to comment.