Emcee

Форк
0
/
AutomaticTerminationPolicy.swift 
64 строки · 1.9 Кб
1
import Foundation
2

3
public enum AutomaticTerminationPolicy: Codable, CustomStringConvertible, Equatable {
4
    /// Will trigger termination after being idle for the given amout of time.
5
    case afterBeingIdle(period: TimeInterval)
6
    
7
    /// Will not trigger automatic termination.
8
    case stayAlive
9
    
10
    public var period: TimeInterval {
11
        switch self {
12
        case .afterBeingIdle(let period):
13
            return period
14
        case .stayAlive:
15
            return .infinity
16
        }
17
    }
18
    
19
    public var description: String {
20
        var items = ["\(type(of: self))"]
21
        
22
        switch self {
23
        case .afterBeingIdle(let period):
24
            items += ["after being idle for \(period) sec"]
25
        case .stayAlive:
26
            items += ["staying alive infinitely"]
27
        }
28
        
29
        return "<" + items.joined(separator: ", ") + ">"
30
    }
31
    
32
    private enum CodingKeys: String, CodingKey {
33
        case caseId
34
        case period
35
    }
36
    
37
    private enum CaseId: String, Codable {
38
        case afterBeingIdle
39
        case stayAlive
40
    }
41
    
42
    public init(from decoder: Decoder) throws {
43
        let container = try decoder.container(keyedBy: CodingKeys.self)
44
        let caseId = try container.decode(CaseId.self, forKey: .caseId)
45

46
        switch caseId {
47
        case .afterBeingIdle:
48
            self = .afterBeingIdle(period: try container.decode(TimeInterval.self, forKey: .period))
49
        case .stayAlive:
50
            self = .stayAlive
51
        }
52
    }
53
    
54
    public func encode(to encoder: Encoder) throws {
55
        var container = encoder.container(keyedBy: CodingKeys.self)
56
        switch self {
57
        case .afterBeingIdle(let period):
58
            try container.encode(CaseId.afterBeingIdle, forKey: .caseId)
59
            try container.encode(period, forKey: .period)
60
        case .stayAlive:
61
            try container.encode(CaseId.stayAlive, forKey: .caseId)
62
        }
63
    }
64
}
65

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.