-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64d10e9
commit ad91b6d
Showing
6 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
0296-cross-platform-pt7/Counter/Sources/StorageClient/StorageClient.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import Foundation | ||
import Dependencies | ||
import DependenciesMacros | ||
|
||
@DependencyClient | ||
public struct StorageClient: Sendable { | ||
public var load: @Sendable (String) throws -> Data | ||
public var save: @Sendable (Data, _ to: String) throws -> Void | ||
} | ||
|
||
extension StorageClient: TestDependencyKey { | ||
public static let testValue = StorageClient() | ||
} |
42 changes: 42 additions & 0 deletions
42
0296-cross-platform-pt7/Counter/Sources/StorageClientLive/StorageClientLive.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Dependencies | ||
import Foundation | ||
import StorageClient | ||
|
||
#if os(WASI) | ||
import JavaScriptKit | ||
#endif | ||
|
||
extension StorageClient: DependencyKey { | ||
#if os(WASI) | ||
public static let liveValue = Self( | ||
load: { key in | ||
guard let value = JSObject.global.window.localStorage.getItem(key).string | ||
else { | ||
struct DataLoadingError: Error {} | ||
throw DataLoadingError() | ||
} | ||
return Data(value.utf8) | ||
}, | ||
save: { data, key in | ||
JSObject.global.window.localStorage.setItem( | ||
key, | ||
String(decoding: data, as: UTF8.self) | ||
) | ||
} | ||
) | ||
#else | ||
public static let liveValue = Self( | ||
load: { key in | ||
let url = URL.documentsDirectory | ||
.appendingPathComponent(key) | ||
.appendingPathExtension("json") | ||
return try Data(contentsOf: url) | ||
}, | ||
save: { data, key in | ||
try data.write(to: URL.documentsDirectory | ||
.appendingPathComponent(key) | ||
.appendingPathExtension("json")) | ||
} | ||
) | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters