This file contains hidden or 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
private func setupLaunchSteps() { | |
// Steps are arranged in proper dependency order - DO NOT REORDER without checking dependencies! | |
// Foundation Layer (no dependencies) | |
launchSteps.append(AppConfigStep()) | |
launchSteps.append(LoggingConfigurationStep()) | |
launchSteps.append(NetworkMonitoringStep()) | |
launchSteps.append(SecureStorageStep()) | |
launchSteps.append(UserDefaultsStep()) | |
launchSteps.append(AudioSessionStep()) |
This file contains hidden or 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
func application( | |
_ application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? | |
) -> Bool { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
LaunchOrchestrator.shared.executeLaunchSteps() // 🚀 | |
let totalTime = CFAbsoluteTimeGetCurrent() - startTime | |
print("Total launch time: \(String(format: "%.2f", totalTime))s") |
This file contains hidden or 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
// swift-tools-version: 6.1 | |
import PackageDescription | |
let package = Package( | |
name: "SwiftScriptDemos", | |
platforms: [ | |
.macOS(.v13) | |
], | |
dependencies: [ | |
.package(url: "https://github.com/swiftlang/swift-subprocess.git", exact: "0.1.0"), |
This file contains hidden or 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
#!/usr/bin/swift | |
import Subprocess | |
let result = try await run( | |
.name("ls"), | |
arguments: ["-1"], | |
output: .string(limit: 1 << 20) | |
) | |
print(result.standardOutput ?? "") |
This file contains hidden or 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
while (true) { | |
char* command = read_input(); | |
pid_t pid = fork(); | |
if (pid == 0) { | |
exec(command); // execute child process | |
} else { | |
wait(pid); // parent waits for child to finish | |
} | |
} |
This file contains hidden or 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
@MainActor | |
func setupUI() async { | |
await someAsyncWork() | |
} | |
@concurrent | |
func someAsyncWork() async { | |
// with @concurrent attribute, this runs on the background thread | |
try? await Task.sleep(for: .milliseconds(100)) | |
} |
This file contains hidden or 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
@MainActor | |
func setupUI() async { | |
await someAsyncWork() | |
} | |
func someAsyncWork() async { | |
// Add breakpoint to test this at runtime - it's on the main thread! | |
try? await Task.sleep(for: .milliseconds(100)) | |
} |
This file contains hidden or 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
actor AuthServiceImpl: AuthService { | |
var tokenTask: Task<String, Error>? | |
func getBearerToken() async throws -> String { | |
if tokenTask == nil { | |
tokenTask = Task { try await fetchValidAuthToken() } | |
} | |
defer { tokenTask = nil } | |
This file contains hidden or 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
actor AuthServiceImpl: AuthService { | |
func getBearerToken() async throws -> String { | |
if cachedToken.isValid { | |
return cachedToken | |
} | |
return try await authAPI.refreshCredentials() <-- suspension point | |
} | |
} |
This file contains hidden or 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
var purgeCacheTask: Task<Void, Never>? | |
func setupCacheTTL() { | |
purgeCacheTask?.cancel() | |
purgeCacheTask = Task(priority: .low) { | |
try? await Task.sleep(for: .seconds(300)) | |
guard !Task.isCancelled else { return } | |
cache.clear() | |
} | |
} |