forked from plivesey/RocketData
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataHolder.swift
47 lines (42 loc) · 1.73 KB
/
DataHolder.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
41
42
43
44
45
46
47
// © 2016 LinkedIn Corp. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import Foundation
/**
This class encapsulates some data of a generic type and the time it was last updated.
This helps ensure that whenever the data is updated, the time is also updated.
For a CollectionDataProvider, T would be [T]. For a DataProvider, T would be T?.
*/
struct DataHolder<T> {
/// The data backed by this data holder
private(set) var data: T
/// The time the data was updated
private(set) var lastUpdated: ChangeTime
/**
Initialize with initial data as well as a specific change time.
The default ChangeTime is time zero since most of the time, you are initializing an 'empty' DataHolder and want all updates.
*/
init(data: T, changeTime: ChangeTime = ChangeTime.timeZero()) {
self.data = data
lastUpdated = changeTime
}
/**
This modifies the data in the data holder.
- parameter data: The new data.
- parameter changeTime: The new change time to set on lastUpdated.
- Returns: whether to successfully set the data
*/
@discardableResult mutating func setData(_ data: T, changeTime: ChangeTime) -> Bool {
if lastUpdated.after(changeTime) {
return false
}
self.data = data
lastUpdated = changeTime
return true
}
}