Skip to content

Commit a2749b1

Browse files
committed
Lazy and Provider as @propertyWrapper
1 parent b7a6518 commit a2749b1

9 files changed

Lines changed: 67 additions & 55 deletions

File tree

Source/PropertyWrapper/SharedPropertyWrapper/InjectLazy.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
public struct InjectLazy<Value> {
1010
public private(set) var wrappedValue: Value {
1111
get {
12-
return projectedValue.instance
12+
return projectedValue.wrappedValue
1313
}
1414
set {
1515
assertionFailure("<Inject> setter is unavailable")

Source/PropertyWrapper/SharedPropertyWrapper/InjectProvider.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Foundation
99
public struct InjectProvider<Value> {
1010
public private(set) var wrappedValue: Value {
1111
get {
12-
return projectedValue.instance
12+
return projectedValue.wrappedValue
1313
}
1414
set {
1515
assertionFailure("<Inject> setter is unavailable")

Source/Wrappers/Lazy.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import Threading
55
/// it will request instance from the container ONCE and return existing one stored inside wrapper.
66
///
77
/// - Note: It's useful when you need to create a new instance of the object every time to solve cyclic dependencies.
8+
@propertyWrapper
89
public final class Lazy<Wrapped>: InstanceWrapper {
910
@AtomicValue
1011
private var factory: (() -> Wrapped)!
1112

12-
public private(set) lazy var instance: Wrapped = {
13+
public private(set) lazy var wrappedValue: Wrapped = {
1314
let factory = $factory.syncUnchecked { factory in
1415
defer {
1516
factory = nil
@@ -19,6 +20,11 @@ public final class Lazy<Wrapped>: InstanceWrapper {
1920
return factory!()
2021
}()
2122

23+
@available(*, deprecated, renamed: "wrappedValue")
24+
public var instance: Wrapped {
25+
wrappedValue
26+
}
27+
2228
public init(with factory: @escaping () -> Wrapped) {
2329
self.factory = factory
2430
}

Source/Wrappers/Provider.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ import Threading
55
/// it will request instance from the container and it will create a new instance or return existing one based on registration options.
66
///
77
/// - Note: It's useful when you need to create a new instance of the object every time to solve cyclic dependencies.
8+
@propertyWrapper
89
public final class Provider<Wrapped>: InstanceWrapper {
910
@AtomicValue
1011
private var factory: () -> Wrapped
1112

12-
public var instance: Wrapped {
13+
public var wrappedValue: Wrapped {
1314
return $factory.syncUnchecked { factory in
1415
return factory()
1516
}
1617
}
1718

19+
@available(*, deprecated, renamed: "wrappedValue")
20+
public var instance: Wrapped {
21+
wrappedValue
22+
}
23+
1824
public init(with factory: @escaping () -> Wrapped) {
1925
self.factory = factory
2026
}

Tests/ContainerThreadSafeTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class ThreadSafeResolutionTests: XCTestCase {
1313
container = nil
1414
}
1515

16-
func test_thread_safe_resolution() async throws {
16+
func test_thread_safe_resolution() async {
1717
// Register different types with different scopes
1818
container.register(Instance.self, options: .container) {
1919
return Instance(id: 1)
@@ -61,7 +61,7 @@ final class ThreadSafeResolutionTests: XCTestCase {
6161
await fulfillment(of: [expectation], timeout: 5.0)
6262
}
6363

64-
func test_thread_safe_transient_resolution() async throws {
64+
func test_thread_safe_transient_resolution() async {
6565
// Register with transient scope
6666
container.register(Instance.self, options: .transient) {
6767
return Instance(id: Int.random(in: 1...1000))
@@ -85,7 +85,7 @@ final class ThreadSafeResolutionTests: XCTestCase {
8585
await fulfillment(of: [expectation], timeout: 5.0)
8686
}
8787

88-
func test_thread_safe_weak_resolution() async throws {
88+
func test_thread_safe_weak_resolution() async {
8989
// Register with weak scope
9090
container.register(Instance.self, options: .weak) {
9191
return Instance(id: 1)

Tests/SharedPropertyWrapper/InstanceLazyTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ final class InstanceLazyTests: XCTestCase {
2121
var subject: Lazy<Instance> = container.resolveWrapped()
2222
XCTAssertEqual(resolvingCounter, 0)
2323

24-
var i1: Instance? = subject.instance
24+
var i1: Instance? = subject.wrappedValue
2525
XCTAssertEqual(resolvingCounter, 1)
2626
XCTAssertNotNil(i1)
2727

28-
var i2: Instance? = subject.instance
28+
var i2: Instance? = subject.wrappedValue
2929
XCTAssertEqual(resolvingCounter, 1)
3030
XCTAssertNotNil(i2)
3131
XCTAssertEqual(i1, i2)
@@ -36,11 +36,11 @@ final class InstanceLazyTests: XCTestCase {
3636

3737
subject = container.resolveWrapped()
3838

39-
i1 = subject.instance
39+
i1 = subject.wrappedValue
4040
XCTAssertEqual(resolvingCounter, 2) // retained by wrapper
4141
XCTAssertNotNil(i1)
4242

43-
i2 = subject.instance
43+
i2 = subject.wrappedValue
4444
XCTAssertEqual(resolvingCounter, 2)
4545
XCTAssertNotNil(i2)
4646
XCTAssertEqual(i1, i2)
@@ -52,11 +52,11 @@ final class InstanceLazyTests: XCTestCase {
5252
var subject: Lazy<Instance> = container.resolveWrapped()
5353
XCTAssertEqual(resolvingCounter, 0)
5454

55-
var i1: Instance? = subject.instance
55+
var i1: Instance? = subject.wrappedValue
5656
XCTAssertEqual(resolvingCounter, 1)
5757
XCTAssertNotNil(i1)
5858

59-
var i2: Instance? = subject.instance
59+
var i2: Instance? = subject.wrappedValue
6060
XCTAssertEqual(resolvingCounter, 1)
6161
XCTAssertNotNil(i2)
6262
XCTAssertEqual(i1, i2)
@@ -67,11 +67,11 @@ final class InstanceLazyTests: XCTestCase {
6767

6868
subject = container.resolveWrapped()
6969

70-
i1 = subject.instance
70+
i1 = subject.wrappedValue
7171
XCTAssertEqual(resolvingCounter, 2) // retained by wrapper
7272
XCTAssertNotNil(i1)
7373

74-
i2 = subject.instance
74+
i2 = subject.wrappedValue
7575
XCTAssertEqual(resolvingCounter, 2)
7676
XCTAssertNotNil(i2)
7777
XCTAssertEqual(i1, i2)
@@ -83,11 +83,11 @@ final class InstanceLazyTests: XCTestCase {
8383
var subject: Lazy<Instance> = container.resolveWrapped()
8484
XCTAssertEqual(resolvingCounter, 0)
8585

86-
var i1: Instance? = subject.instance
86+
var i1: Instance? = subject.wrappedValue
8787
XCTAssertEqual(resolvingCounter, 1)
8888
XCTAssertNotNil(i1)
8989

90-
var i2: Instance? = subject.instance
90+
var i2: Instance? = subject.wrappedValue
9191
XCTAssertEqual(resolvingCounter, 1)
9292
XCTAssertNotNil(i2)
9393
XCTAssertEqual(i1, i2)
@@ -98,11 +98,11 @@ final class InstanceLazyTests: XCTestCase {
9898

9999
subject = container.resolveWrapped()
100100

101-
i1 = subject.instance
101+
i1 = subject.wrappedValue
102102
XCTAssertEqual(resolvingCounter, 1) // retained by container
103103
XCTAssertNotNil(i1)
104104

105-
i2 = subject.instance
105+
i2 = subject.wrappedValue
106106
XCTAssertEqual(resolvingCounter, 1)
107107
XCTAssertNotNil(i2)
108108
XCTAssertEqual(i1, i2)

Tests/SharedPropertyWrapper/InstanceProviderTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ final class InstanceProviderTests: XCTestCase {
2222
var subject: Provider<T> = container.resolveWrapped()
2323
XCTAssertEqual(resolvingCounter, 0)
2424

25-
var i1: T? = subject.instance
25+
var i1: T? = subject.wrappedValue
2626
XCTAssertEqual(resolvingCounter, 1)
2727
XCTAssertNotNil(i1)
2828

29-
var i2: T? = subject.instance
29+
var i2: T? = subject.wrappedValue
3030
XCTAssertEqual(resolvingCounter, 1)
3131
XCTAssertNotNil(i2)
3232
XCTAssertEqual(i1, i2)
@@ -37,11 +37,11 @@ final class InstanceProviderTests: XCTestCase {
3737

3838
subject = container.resolveWrapped()
3939

40-
i1 = subject.instance
40+
i1 = subject.wrappedValue
4141
XCTAssertEqual(resolvingCounter, 2)
4242
XCTAssertNotNil(i1)
4343

44-
i2 = subject.instance
44+
i2 = subject.wrappedValue
4545
XCTAssertEqual(resolvingCounter, 2)
4646
XCTAssertNotNil(i2)
4747
XCTAssertEqual(i1, i2)
@@ -53,11 +53,11 @@ final class InstanceProviderTests: XCTestCase {
5353
var subject: Provider<T> = container.resolveWrapped()
5454
XCTAssertEqual(resolvingCounter, 0)
5555

56-
var i1: T? = subject.instance
56+
var i1: T? = subject.wrappedValue
5757
XCTAssertEqual(resolvingCounter, 1)
5858
XCTAssertNotNil(i1)
5959

60-
var i2: T? = subject.instance
60+
var i2: T? = subject.wrappedValue
6161
XCTAssertEqual(resolvingCounter, 2)
6262
XCTAssertNotNil(i2)
6363
XCTAssertNotEqual(i1, i2)
@@ -68,11 +68,11 @@ final class InstanceProviderTests: XCTestCase {
6868

6969
subject = container.resolveWrapped()
7070

71-
i1 = subject.instance
71+
i1 = subject.wrappedValue
7272
XCTAssertEqual(resolvingCounter, 3)
7373
XCTAssertNotNil(i1)
7474

75-
i2 = subject.instance
75+
i2 = subject.wrappedValue
7676
XCTAssertEqual(resolvingCounter, 4)
7777
XCTAssertNotNil(i2)
7878
XCTAssertNotEqual(i1, i2)
@@ -84,11 +84,11 @@ final class InstanceProviderTests: XCTestCase {
8484
var subject: Provider<T> = container.resolveWrapped()
8585
XCTAssertEqual(resolvingCounter, 0)
8686

87-
var i1: T? = subject.instance
87+
var i1: T? = subject.wrappedValue
8888
XCTAssertEqual(resolvingCounter, 1)
8989
XCTAssertNotNil(i1)
9090

91-
var i2: T? = subject.instance
91+
var i2: T? = subject.wrappedValue
9292
XCTAssertEqual(resolvingCounter, 1)
9393
XCTAssertNotNil(i2)
9494
XCTAssertEqual(i1, i2)
@@ -99,11 +99,11 @@ final class InstanceProviderTests: XCTestCase {
9999

100100
subject = container.resolveWrapped()
101101

102-
i1 = subject.instance
102+
i1 = subject.wrappedValue
103103
XCTAssertEqual(resolvingCounter, 1) // retained by container
104104
XCTAssertNotNil(i1)
105105

106-
i2 = subject.instance
106+
i2 = subject.wrappedValue
107107
XCTAssertEqual(resolvingCounter, 1)
108108
XCTAssertNotNil(i2)
109109
XCTAssertEqual(i1, i2)

Tests/SharedPropertyWrapper/ValueLazyTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ final class ValueLazyTests: XCTestCase {
2121
var subject: Lazy<Value> = container.resolveWrapped()
2222
XCTAssertEqual(resolvingCounter, 0)
2323

24-
var i1: Value? = subject.instance
24+
var i1: Value? = subject.wrappedValue
2525
XCTAssertEqual(resolvingCounter, 1)
2626
XCTAssertNotNil(i1)
2727

28-
var i2: Value? = subject.instance
28+
var i2: Value? = subject.wrappedValue
2929
XCTAssertEqual(resolvingCounter, 1)
3030
XCTAssertNotNil(i2)
3131
XCTAssertEqual(i1, i2)
@@ -36,11 +36,11 @@ final class ValueLazyTests: XCTestCase {
3636

3737
subject = container.resolveWrapped()
3838

39-
i1 = subject.instance
39+
i1 = subject.wrappedValue
4040
XCTAssertEqual(resolvingCounter, 2) // retained by wrapper
4141
XCTAssertNotNil(i1)
4242

43-
i2 = subject.instance
43+
i2 = subject.wrappedValue
4444
XCTAssertEqual(resolvingCounter, 2)
4545
XCTAssertNotNil(i2)
4646
XCTAssertEqual(i1, i2)
@@ -52,11 +52,11 @@ final class ValueLazyTests: XCTestCase {
5252
var subject: Lazy<Value> = container.resolveWrapped()
5353
XCTAssertEqual(resolvingCounter, 0)
5454

55-
var i1: Value? = subject.instance
55+
var i1: Value? = subject.wrappedValue
5656
XCTAssertEqual(resolvingCounter, 1)
5757
XCTAssertNotNil(i1)
5858

59-
var i2: Value? = subject.instance
59+
var i2: Value? = subject.wrappedValue
6060
XCTAssertEqual(resolvingCounter, 1)
6161
XCTAssertNotNil(i2)
6262
XCTAssertEqual(i1, i2)
@@ -67,11 +67,11 @@ final class ValueLazyTests: XCTestCase {
6767

6868
subject = container.resolveWrapped()
6969

70-
i1 = subject.instance
70+
i1 = subject.wrappedValue
7171
XCTAssertEqual(resolvingCounter, 2) // retained by wrapper
7272
XCTAssertNotNil(i1)
7373

74-
i2 = subject.instance
74+
i2 = subject.wrappedValue
7575
XCTAssertEqual(resolvingCounter, 2)
7676
XCTAssertNotNil(i2)
7777
XCTAssertEqual(i1, i2)
@@ -83,11 +83,11 @@ final class ValueLazyTests: XCTestCase {
8383
var subject: Lazy<Value> = container.resolveWrapped()
8484
XCTAssertEqual(resolvingCounter, 0)
8585

86-
var i1: Value? = subject.instance
86+
var i1: Value? = subject.wrappedValue
8787
XCTAssertEqual(resolvingCounter, 1)
8888
XCTAssertNotNil(i1)
8989

90-
var i2: Value? = subject.instance
90+
var i2: Value? = subject.wrappedValue
9191
XCTAssertEqual(resolvingCounter, 1)
9292
XCTAssertNotNil(i2)
9393
XCTAssertEqual(i1, i2)
@@ -98,11 +98,11 @@ final class ValueLazyTests: XCTestCase {
9898

9999
subject = container.resolveWrapped()
100100

101-
i1 = subject.instance
101+
i1 = subject.wrappedValue
102102
XCTAssertEqual(resolvingCounter, 1) // retained by container
103103
XCTAssertNotNil(i1)
104104

105-
i2 = subject.instance
105+
i2 = subject.wrappedValue
106106
XCTAssertEqual(resolvingCounter, 1)
107107
XCTAssertNotNil(i2)
108108
XCTAssertEqual(i1, i2)

0 commit comments

Comments
 (0)