Skip to content

Commit

Permalink
Fix rootEnvironment not merged with .defaultEnvironment (#461)
Browse files Browse the repository at this point in the history
* Make actual use of `rootEnvironment` passed into functions, falling back to `.defaultEnvironment`

* Add `.merge(_:)`/`.merging(_:)` to `EnvironmentValues`

* Merge `.defaultEnvironment` with `rootEnvironment`

* Add `@_spi(TokamakCore)` protection for `EnvironmentValues.merge(_:)`/`.merging(_:)`
  • Loading branch information
regexident authored Jan 3, 2022
1 parent 077c0cd commit a9addc8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Sources/TokamakCore/Environment/EnvironmentValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public struct EnvironmentValues: CustomStringConvertible {
values[bindable] = newValue
}
}

@_spi(TokamakCore)
public mutating func merge(_ other: Self?) {
if let other = other {
values.merge(other.values) { existing, new in
new
}
}
}

@_spi(TokamakCore)
public func merging(_ other: Self?) -> Self {
var merged = self
merged.merge(other)
return merged
}
}

struct IsEnabledKey: EnvironmentKey {
Expand Down
2 changes: 1 addition & 1 deletion Sources/TokamakDOM/DOMRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ final class DOMRenderer: Renderer {
reconciler = StackReconciler(
app: app,
target: DOMNode(ref),
environment: .defaultEnvironment,
environment: .defaultEnvironment.merging(rootEnvironment),
renderer: self
) { scheduler.schedule(options: nil, $0) }
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/TokamakGTK/GTKRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import CGTK
import Dispatch
import TokamakCore
@_spi(TokamakCore) import TokamakCore

extension EnvironmentValues {
/// Returns default settings for the GTK environment
Expand Down Expand Up @@ -56,7 +56,7 @@ final class GTKRenderer: Renderer {
self.reconciler = StackReconciler(
app: app,
target: Widget(window),
environment: .defaultEnvironment,
environment: .defaultEnvironment.merging(rootEnvironment),
renderer: self,
scheduler: { next in
DispatchQueue.main.async {
Expand Down
6 changes: 3 additions & 3 deletions Sources/TokamakStaticHTML/StaticHTMLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Created by Carson Katri on 7/20/20.
//

import TokamakCore
@_spi(TokamakCore) import TokamakCore

extension EnvironmentValues {
/// Returns default settings for the static HTML environment
Expand Down Expand Up @@ -90,7 +90,7 @@ public final class StaticHTMLRenderer: Renderer {
reconciler = StackReconciler(
view: view,
target: rootTarget,
environment: .defaultEnvironment,
environment: .defaultEnvironment.merging(rootEnvironment),
renderer: self,
scheduler: { _ in
fatalError("Stateful apps cannot be created with TokamakStaticHTML")
Expand All @@ -104,7 +104,7 @@ public final class StaticHTMLRenderer: Renderer {
reconciler = StackReconciler(
app: app,
target: rootTarget,
environment: .defaultEnvironment,
environment: .defaultEnvironment.merging(rootEnvironment),
renderer: self,
scheduler: { _ in
fatalError("Stateful apps cannot be created with TokamakStaticHTML")
Expand Down

0 comments on commit a9addc8

Please sign in to comment.