Skip to content

Commit

Permalink
StylingCache
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Apr 20, 2024
1 parent cf881d3 commit d5f6797
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/ThemePark/Style.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public struct Variant: Hashable, Sendable {
}
}

public struct Query {
public struct Query: Hashable, Sendable {
public enum Key : Hashable, Sendable {
case editorBackground

Expand Down
25 changes: 25 additions & 0 deletions Sources/ThemePark/StylingCache.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import Foundation

///Very simple type that will cache the result of style queries.
public final class StylingCache {
private var cache = [Query: Style]()
private let styler: any Styling

public init(styler: any Styling) {
self.styler = styler
}
}

extension StylingCache: Styling {
public func style(for query: Query) -> Style? {
if let style = cache[query] {
return style
}

let style = styler.style(for: query)

self.cache[query] = style

return style
}
}

0 comments on commit d5f6797

Please sign in to comment.