aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Source/Core/FSTView.h
blob: fc6ceadada44394f39bef06a1c2784d1c07fd628 (plain)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright 2017 Google
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <Foundation/Foundation.h>

#import "Firestore/Source/Core/FSTTypes.h"
#import "Firestore/Source/Model/FSTDocumentDictionary.h"

#include "Firestore/core/src/firebase/firestore/model/document_key.h"
#include "Firestore/core/src/firebase/firestore/model/document_key_set.h"

@class FSTDocumentSet;
@class FSTDocumentViewChangeSet;
@class FSTMaybeDocument;
@class FSTQuery;
@class FSTTargetChange;
@class FSTViewSnapshot;

NS_ASSUME_NONNULL_BEGIN

#pragma mark - FSTViewDocumentChanges

/** The result of applying a set of doc changes to a view. */
@interface FSTViewDocumentChanges : NSObject

- (instancetype)init NS_UNAVAILABLE;

- (const firebase::firestore::model::DocumentKeySet &)mutatedKeys;

/** The new set of docs that should be in the view. */
@property(nonatomic, strong, readonly) FSTDocumentSet *documentSet;

/** The diff of this these docs with the previous set of docs. */
@property(nonatomic, strong, readonly) FSTDocumentViewChangeSet *changeSet;

/**
 * Whether the set of documents passed in was not sufficient to calculate the new state of the view
 * and there needs to be another pass based on the local cache.
 */
@property(nonatomic, assign, readonly) BOOL needsRefill;

@end

#pragma mark - FSTLimboDocumentChange

typedef NS_ENUM(NSInteger, FSTLimboDocumentChangeType) {
  FSTLimboDocumentChangeTypeAdded = 0,
  FSTLimboDocumentChangeTypeRemoved,
};

// A change to a particular document wrt to whether it is in "limbo".
@interface FSTLimboDocumentChange : NSObject

+ (instancetype)changeWithType:(FSTLimboDocumentChangeType)type
                           key:(firebase::firestore::model::DocumentKey)key;

- (id)init __attribute__((unavailable("Use a static constructor method.")));

- (const firebase::firestore::model::DocumentKey &)key;

@property(nonatomic, assign, readonly) FSTLimboDocumentChangeType type;
@end

#pragma mark - FSTViewChange

// A set of changes to a view.
@interface FSTViewChange : NSObject

- (id)init __attribute__((unavailable("Use a static constructor method.")));

@property(nonatomic, strong, readonly, nullable) FSTViewSnapshot *snapshot;
@property(nonatomic, strong, readonly) NSArray<FSTLimboDocumentChange *> *limboChanges;
@end

#pragma mark - FSTView

/**
 * View is responsible for computing the final merged truth of what docs are in a query. It gets
 * notified of local and remote changes to docs, and applies the query filters and limits to
 * determine the most correct possible results.
 */
@interface FSTView : NSObject

- (instancetype)init NS_UNAVAILABLE;

- (instancetype)initWithQuery:(FSTQuery *)query
              remoteDocuments:(firebase::firestore::model::DocumentKeySet)remoteDocuments
    NS_DESIGNATED_INITIALIZER;

/**
 * Iterates over a set of doc changes, applies the query limit, and computes what the new results
 * should be, what the changes were, and whether we may need to go back to the local cache for
 * more results. Does not make any changes to the view.
 *
 * @param docChanges The doc changes to apply to this view.
 * @return a new set of docs, changes, and refill flag.
 */
- (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges;

/**
 * Iterates over a set of doc changes, applies the query limit, and computes what the new results
 * should be, what the changes were, and whether we may need to go back to the local cache for
 * more results. Does not make any changes to the view.
 *
 * @param docChanges The doc changes to apply to this view.
 * @param previousChanges If this is being called with a refill, then start with this set of docs
 *     and changes instead of the current view.
 * @return a new set of docs, changes, and refill flag.
 */
- (FSTViewDocumentChanges *)computeChangesWithDocuments:(FSTMaybeDocumentDictionary *)docChanges
                                        previousChanges:
                                            (nullable FSTViewDocumentChanges *)previousChanges;

/**
 * Updates the view with the given ViewDocumentChanges.
 *
 * @param docChanges The set of changes to make to the view's docs.
 * @return A new FSTViewChange with the given docs, changes, and sync state.
 */
- (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges;

/**
 * Updates the view with the given FSTViewDocumentChanges and updates limbo docs and sync state from
 * the given (optional) target change.
 *
 * @param docChanges The set of changes to make to the view's docs.
 * @param targetChange A target change to apply for computing limbo docs and sync state.
 * @return A new FSTViewChange with the given docs, changes, and sync state.
 */
- (FSTViewChange *)applyChangesToDocuments:(FSTViewDocumentChanges *)docChanges
                              targetChange:(nullable FSTTargetChange *)targetChange;

/**
 * Applies an FSTOnlineState change to the view, potentially generating an FSTViewChange if the
 * view's syncState changes as a result.
 */
- (FSTViewChange *)applyChangedOnlineState:(FSTOnlineState)onlineState;

/**
 * The set of remote documents that the server has told us belongs to the target associated with
 * this view.
 */
- (const firebase::firestore::model::DocumentKeySet &)syncedDocuments;

@end

NS_ASSUME_NONNULL_END