Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare DependencyKeys namespace #5

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
E189A4AE2776179D0090EC9F /* GlobalSchedulersClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E189A4AD2776179D0090EC9F /* GlobalSchedulersClient.swift */; };
E189A4AF2776179D0090EC9F /* GlobalSchedulersClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = E189A4AD2776179D0090EC9F /* GlobalSchedulersClient.swift */; };
E189A4B1277619040090EC9F /* NoOptionsScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E189A4B0277619040090EC9F /* NoOptionsScheduler.swift */; };
E189A4B2277619040090EC9F /* NoOptionsScheduler.swift in Sources */ = {isa = PBXBuildFile; fileRef = E189A4B0277619040090EC9F /* NoOptionsScheduler.swift */; };
E189A4B427761B9F0090EC9F /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = E189A4B327761B9F0090EC9F /* Helpers.swift */; };
E189A4B527761B9F0090EC9F /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = E189A4B327761B9F0090EC9F /* Helpers.swift */; };
E972F1FD26A2B74A00504D39 /* ExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E972F1EA26A2B74A00504D39 /* ExampleApp.swift */; };
E972F1FE26A2B74B00504D39 /* ExampleApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = E972F1EA26A2B74A00504D39 /* ExampleApp.swift */; };
E972F20126A2B74B00504D39 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E972F1EC26A2B74A00504D39 /* Assets.xcassets */; };
Expand All @@ -24,6 +30,9 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
E189A4AD2776179D0090EC9F /* GlobalSchedulersClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalSchedulersClient.swift; sourceTree = "<group>"; };
E189A4B0277619040090EC9F /* NoOptionsScheduler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NoOptionsScheduler.swift; sourceTree = "<group>"; };
E189A4B327761B9F0090EC9F /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
E972F1EA26A2B74A00504D39 /* ExampleApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExampleApp.swift; sourceTree = "<group>"; };
E972F1EC26A2B74A00504D39 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
E972F1F126A2B74A00504D39 /* Example.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Example.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -75,6 +84,9 @@
E972F21B26A2B9B900504D39 /* Level1Feature.swift */,
E972F21E26A2BA8400504D39 /* Level2Feature.swift */,
E972F22126A2C72200504D39 /* SharedDependencies.swift */,
E189A4AD2776179D0090EC9F /* GlobalSchedulersClient.swift */,
E189A4B0277619040090EC9F /* NoOptionsScheduler.swift */,
E189A4B327761B9F0090EC9F /* Helpers.swift */,
E972F1EA26A2B74A00504D39 /* ExampleApp.swift */,
E972F1EC26A2B74A00504D39 /* Assets.xcassets */,
);
Expand Down Expand Up @@ -222,6 +234,9 @@
E972F21C26A2B9B900504D39 /* Level1Feature.swift in Sources */,
E972F21F26A2BA8400504D39 /* Level2Feature.swift in Sources */,
E972F21926A2B9A700504D39 /* Level0Feature.swift in Sources */,
E189A4B427761B9F0090EC9F /* Helpers.swift in Sources */,
E189A4B1277619040090EC9F /* NoOptionsScheduler.swift in Sources */,
E189A4AE2776179D0090EC9F /* GlobalSchedulersClient.swift in Sources */,
E972F22226A2C72200504D39 /* SharedDependencies.swift in Sources */,
E972F1FD26A2B74A00504D39 /* ExampleApp.swift in Sources */,
);
Expand All @@ -234,6 +249,9 @@
E972F21D26A2B9B900504D39 /* Level1Feature.swift in Sources */,
E972F22026A2BA8400504D39 /* Level2Feature.swift in Sources */,
E972F21A26A2B9A700504D39 /* Level0Feature.swift in Sources */,
E189A4B527761B9F0090EC9F /* Helpers.swift in Sources */,
E189A4B2277619040090EC9F /* NoOptionsScheduler.swift in Sources */,
E189A4AF2776179D0090EC9F /* GlobalSchedulersClient.swift in Sources */,
E972F22326A2C72200504D39 /* SharedDependencies.swift in Sources */,
E972F1FE26A2B74B00504D39 /* ExampleApp.swift in Sources */,
);
Expand Down
9 changes: 4 additions & 5 deletions Example/Shared/ExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import ComposableArchitecture
import ComposableEnvironment
import SwiftUI

let store = Store<Level0State, Level0Action>(
initialState:
.init(level1: .init(
let store = Store(
initialState: Level0State(level1: .init(
first: .init(randomNumber: nil),
second: .init(randomNumber: nil)
)),
reducer: level0Reducer,
environment: .init()
environment: Level0Environment()
)

@main
struct ExampleApp: App {
var body: some Scene {
WindowGroup {
Level0View(store: store)
Level0View(store)
}
}
}
24 changes: 24 additions & 0 deletions Example/Shared/GlobalSchedulersClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Combine
import Foundation

public struct GlobalSchedulersClient {
public init(
_ scheduler: @escaping (DispatchQoS.QoSClass) -> NoOptionsSchedulerOf<DispatchQueue>
) {
self.scheduler = scheduler
}

public var scheduler: (DispatchQoS.QoSClass) -> NoOptionsSchedulerOf<DispatchQueue>

public func callAsFunction(
qos: DispatchQoS.QoSClass = .default
) -> NoOptionsSchedulerOf<DispatchQueue> {
return scheduler(qos)
}
}

extension GlobalSchedulersClient {
public static let live: GlobalSchedulersClient = .init { qos in
return DispatchQueue.global(qos: qos).ignoreOptions()
}
}
6 changes: 6 additions & 0 deletions Example/Shared/Helpers.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// for some reason ReferenceWritableKeyPath is not converible to function via type-inference in pullback
func get<Object: AnyObject, Value>(
_ keyPath: ReferenceWritableKeyPath<Object, Value>
) -> (Object) -> Value {
return { $0[keyPath: keyPath] }
}
127 changes: 75 additions & 52 deletions Example/Shared/Level0Feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,83 @@ enum Level0Action {
}

class Level0Environment: ComposableEnvironment {
// Dependencies
@Dependency(\.mainQueue) var main
@Dependency(\.backgroundQueue) var background

// Derived Environments
@DerivedEnvironment<Level1Environment> var level1
// This style of using property-wrappers allows to
// find derived environment type a bit easier, then DerivedEnvironment<Type>
// and visual composition is a bit better, so we keep wrapper as a simple annotation
// and all of the information that is related to a property is on one line
@DerivedEnvironment
var schedulers: StoreSchedulers

@DerivedEnvironment
var level1: Level1Environment
}

let level0Reducer = Reducer<Level0State, Level0Action, Level0Environment>.combine(
level1Reducer.pullback(state: \.level1,
action: /Level0Action.level1,
environment: \.level1),
Reducer<Level0State, Level0Action, Level0Environment> {
state, action, environment in
// You may prefer to define type explicitly to avoid using `get` helper
//
// let level0Reducer = Reducer<
// Level0State,
// Level0Action,
// Level0Environment
// >.combine(
// level1Reducer.pullback(
// state: \.level1,
// action: /Level0Action.level1,
// environment: \.level1
// ),
// ...
// )

let level0Reducer = Reducer.combine(
level1Reducer.pullback(
state: \Level0State.level1,
action: /Level0Action.level1,
environment: get(\Level0Environment.level1)
),
Reducer { state, action, environment in
switch action {
case .isReady:
state.isReady = true
return .none

case .level1:
return .none

case .onAppear:
return Effect(value: .isReady)
.delay(for: 1, scheduler: environment.background) // Simulate something lengthy…
.receive(on: environment.main)
.delay(for: 1, scheduler: environment.schedulers.background()) // Simulate something lengthy…
.receive(on: environment.schedulers.main)
.eraseToEffect()

// Alternatively, we can directly tap into the environment's dependepencies using
// their global property name, meaning that we can even bypass declarations like
// `@Dependency(\.mainQueue) var main` in the environment to write:
//
// return Effect(value: .isReady)
// .delay(for: 1, scheduler: environment.backgroundQueue)
// .receive(on: environment.mainQueue)
// .eraseToEffect()
}
}
)

// Alternatively, we can directly tap into the environment's dependepencies using
// their global property name, meaning that we can even bypass declarations like
// `@Dependency(\.mainQueue) var main` in the environment to write:
//
// return Effect(value: .isReady)
// .delay(for: 1, scheduler: environment.globalSchedulers(qos: .default))
// .receive(on: environment.mainQueue)
// .eraseToEffect()

struct Level0View: View {
let store: Store<Level0State, Level0Action>
init(store: Store<Level0State, Level0Action>) {

init(_ store: Store<Level0State, Level0Action>) {
self.store = store
}

var body: some View {
WithViewStore(store) { viewStore in
VStack {
Text("Random numbers")
.font(.title)
Level1View(store: store.scope(state: \.level1, action: Level0Action.level1))
.padding()
.disabled(!viewStore.isReady)
Level1View(store.scope(
state: \.level1,
action: Level0Action.level1
))
.padding()
.disabled(!viewStore.isReady)
}
.onAppear { viewStore.send(.onAppear) }
.fixedSize()
Expand All @@ -78,30 +103,28 @@ struct Level0View: View {

struct Level0View_Preview: PreviewProvider {
static var previews: some View {
Level0View(store:
.init(initialState:
.init(
level1: .init(
first: .init(randomNumber: 6),
second: .init(randomNumber: nil)
)
),
reducer: level0Reducer,
environment: Level0Environment() // Swift ≥ 5.4 can use .init()
.with(\.mainQueue, .immediate)
.with(\.backgroundQueue, .immediate)
// We can set the value of `rng` even if Level0Environment doesn't have a `rng` property:
.with(\.rng) { 4 })
)
Level0View(store:
.init(initialState:
.init(level1: .init(
first: .init(randomNumber: nil),
Level0View(Store(
initialState: .init(
level1: .init(
first: .init(randomNumber: 6),
second: .init(randomNumber: nil)
)),
reducer: level0Reducer,
// An environment default dependencies:
environment: .init())
)
)
),
reducer: level0Reducer,
environment: Level0Environment() // Swift ≥ 5.4 can use .init()
.with(\.eventHandlingScheduler, AnyScheduler.immediate.ignoreOptions())
.with(\.globalSchedulers, .init { _ in AnyScheduler.immediate.ignoreOptions() })
// We can set the value of `rng` even if Level0Environment doesn't have a `rng` property:
.with(\.rng) { 4 }
))
Level0View(Store(
initialState:.init(level1: .init(
first: .init(randomNumber: nil),
second: .init(randomNumber: nil)
)),
reducer: level0Reducer,
// An environment default dependencies:
environment: .init()
))
}
}
Loading