-
Notifications
You must be signed in to change notification settings - Fork 0
/
Desolated.swift
40 lines (34 loc) · 902 Bytes
/
Desolated.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// Desolated.swift
// Desolate
//
// Created by d-exclaimation on 8:30 PM.
// Copyright © 2021 d-exclaimation. All rights reserved.
//
import Foundation
@propertyWrapper
struct Desolated<Value> {
private var innerHook: Desolate<AsyncCapsule<Value>>
private var timeout: TimeInterval
private var fallback: () -> Value
var wrappedValue: Value {
get {
print("Getter called")
return innerHook.maybe() ?? fallback()
} set {
innerHook.set(newValue)
}
}
init(wrappedValue: Value) {
innerHook = hook { wrappedValue }
timeout = 5.0
fallback = {
fatalError("No value given")
}
}
init(wrappedValue: Value, timeout tm: TimeInterval, fallback fb: @escaping () -> Value) {
innerHook = hook { wrappedValue }
timeout = tm
fallback = fb
}
}