Skip to content

Commit

Permalink
Add autoclosure resolver on EnvironmentValuseResolver (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
hainayanda authored Mar 17, 2024
1 parent b490b26 commit de6f846
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
35 changes: 31 additions & 4 deletions Sources/SwiftEnvironment/EnvironmentValuesResolver.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// EnvironmentValuesResolver.swift
//
//
//
// Created by Nayanda Haberty on 14/3/24.
//
Expand Down Expand Up @@ -37,28 +37,55 @@ public class EnvironmentValuesResolver {
public func environment<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
_ value: @escaping () -> V) -> EnvironmentValuesResolver {
_ value: @autoclosure @escaping () -> V) -> EnvironmentValuesResolver {
resolvers[keyPath] = SingletonInstanceResolver(queue: queue, resolver: value)
return self
}

@discardableResult
public func environment<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
resolver: @escaping () -> V) -> EnvironmentValuesResolver {
resolvers[keyPath] = SingletonInstanceResolver(queue: queue, resolver: resolver)
return self
}

@discardableResult
public func transient<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
_ value: @escaping () -> V) -> EnvironmentValuesResolver {
_ value: @autoclosure @escaping () -> V) -> EnvironmentValuesResolver {
resolvers[keyPath] = TransientInstanceResolver(queue: queue, resolver: value)
return self
}

@discardableResult
public func transient<V>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
resolver: @escaping () -> V) -> EnvironmentValuesResolver {
resolvers[keyPath] = TransientInstanceResolver(queue: queue, resolver: resolver)
return self
}

@discardableResult
public func weak<V: AnyObject>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
_ value: @escaping () -> V) -> EnvironmentValuesResolver {
_ value: @autoclosure @escaping () -> V) -> EnvironmentValuesResolver {
resolvers[keyPath] = WeakInstanceResolver(queue: queue, resolver: value)
return self
}

@discardableResult
public func weak<V: AnyObject>(
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
resolver: @escaping () -> V) -> EnvironmentValuesResolver {
resolvers[keyPath] = WeakInstanceResolver(queue: queue, resolver: resolver)
return self
}
}

extension EnvironmentValuesResolver {
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftEnvironment/GlobalResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public struct GlobalResolver {
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
resolver: @escaping () -> V) -> EnvironmentValuesResolver {
EnvironmentValuesResolver.global.environment(keyPath, resolveOn: queue, resolver)
EnvironmentValuesResolver.global.environment(keyPath, resolveOn: queue, resolver: resolver)
}

@inlinable
Expand All @@ -64,7 +64,7 @@ public struct GlobalResolver {
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
resolver: @escaping () -> V) -> EnvironmentValuesResolver {
EnvironmentValuesResolver.global.transient(keyPath, resolveOn: queue, resolver)
EnvironmentValuesResolver.global.transient(keyPath, resolveOn: queue, resolver: resolver)
}

@inlinable
Expand All @@ -81,6 +81,6 @@ public struct GlobalResolver {
_ keyPath: WritableKeyPath<EnvironmentValues, V>,
resolveOn queue: DispatchQueue? = nil,
resolver: @escaping () -> V) -> EnvironmentValuesResolver {
EnvironmentValuesResolver.global.weak(keyPath, resolveOn: queue, resolver)
EnvironmentValuesResolver.global.weak(keyPath, resolveOn: queue, resolver: resolver)
}
}

0 comments on commit de6f846

Please sign in to comment.