init
This commit is contained in:
commit
6df6010d94
|
@ -0,0 +1,9 @@
|
|||
.DS_Store
|
||||
/.build
|
||||
/Packages
|
||||
/*.xcodeproj
|
||||
xcuserdata/
|
||||
DerivedData/
|
||||
.swiftpm/config/registries.json
|
||||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
|
||||
.netrc
|
|
@ -0,0 +1,41 @@
|
|||
{
|
||||
"pins" : [
|
||||
{
|
||||
"identity" : "c",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/0xOpenBytes/c",
|
||||
"state" : {
|
||||
"revision" : "9477c47d3802d983580fb0770c9869c9e9330a37",
|
||||
"version" : "0.3.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "flet",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/0xOpenBytes/FLet",
|
||||
"state" : {
|
||||
"revision" : "35a4a21a058661e59556c2870e5ed2351f58622f",
|
||||
"version" : "1.1.0"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "o",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/0xOpenBytes/o",
|
||||
"state" : {
|
||||
"revision" : "2721b5d5feab371ab6e6999a557f9c80f0426228",
|
||||
"version" : "0.2.1"
|
||||
}
|
||||
},
|
||||
{
|
||||
"identity" : "t",
|
||||
"kind" : "remoteSourceControl",
|
||||
"location" : "https://github.com/0xOpenBytes/t",
|
||||
"state" : {
|
||||
"revision" : "78294df737c524f40267bca42beb8e10ca28aa41",
|
||||
"version" : "0.2.0"
|
||||
}
|
||||
}
|
||||
],
|
||||
"version" : 2
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
// swift-tools-version: 5.6
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "CacheStore",
|
||||
platforms: [
|
||||
.iOS(.v14),
|
||||
.macOS(.v11),
|
||||
.watchOS(.v7)
|
||||
],
|
||||
products: [
|
||||
// Products define the executables and libraries a package produces, and make them visible to other packages.
|
||||
.library(
|
||||
name: "CacheStore",
|
||||
targets: ["CacheStore"]
|
||||
)
|
||||
],
|
||||
dependencies: [
|
||||
// Dependencies declare other packages that this package depends on.
|
||||
.package(
|
||||
url: "https://github.com/0xOpenBytes/FLet",
|
||||
from: "1.1.0"
|
||||
)
|
||||
],
|
||||
targets: [
|
||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
||||
.target(
|
||||
name: "CacheStore",
|
||||
dependencies: ["FLet"]
|
||||
),
|
||||
.testTarget(
|
||||
name: "CacheStoreTests",
|
||||
dependencies: ["CacheStore"]
|
||||
)
|
||||
]
|
||||
)
|
|
@ -0,0 +1,26 @@
|
|||
import c
|
||||
import SwiftUI
|
||||
|
||||
public class CacheStore<CacheKey: Hashable>: ObservableObject, Cacheable {
|
||||
private var lock: NSLock
|
||||
@Published private var cache: [CacheKey: Any]
|
||||
|
||||
required public init(initialValues: [CacheKey: Any]) {
|
||||
lock = NSLock()
|
||||
cache = initialValues
|
||||
}
|
||||
|
||||
public func get<Value>(_ key: CacheKey) -> Value? {
|
||||
lock.lock()
|
||||
defer { lock.unlock() }
|
||||
return cache[key] as? Value
|
||||
}
|
||||
|
||||
public func resolve<Value>(_ key: CacheKey) -> Value { get(key)! }
|
||||
|
||||
public func set<Value>(value: Value, forKey key: CacheKey) {
|
||||
lock.lock()
|
||||
cache[key] = value
|
||||
lock.unlock()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
import t
|
||||
import XCTest
|
||||
@testable import CacheStore
|
||||
|
||||
final class CacheStoreTests: XCTestCase {
|
||||
func testExample() throws {
|
||||
enum HashableKey: Hashable {
|
||||
case a, b, c
|
||||
}
|
||||
|
||||
let cache = CacheStore<HashableKey>(
|
||||
initialValues: [
|
||||
.a: "a"
|
||||
]
|
||||
)
|
||||
|
||||
XCTAssert(
|
||||
t.suite {
|
||||
try t.assert(cache.resolve(.a), isEqualTo: "a")
|
||||
}
|
||||
)
|
||||
|
||||
cache.set(value: "aa", forKey: .a)
|
||||
cache.set(value: "C", forKey: .c)
|
||||
|
||||
XCTAssert(
|
||||
t.suite {
|
||||
try t.assert(cache.resolve(.a), isEqualTo: "aa")
|
||||
try t.assert(cache.resolve(.c), isEqualTo: "C")
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue