Skip to content

Commit 8aef6d5

Browse files
author
Jordan Hamill
committed
Restore various observable queries
1 parent 125cdf9 commit 8aef6d5

File tree

1 file changed

+155
-12
lines changed

1 file changed

+155
-12
lines changed

Turf/Extensions/SecondaryIndex/ObservableCollection+IndexedCollection.swift

Lines changed: 155 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,199 @@ extension ObservableCollection where TCollection: IndexedCollection {
55

66
// MARK: Public methods
77

8+
/**
9+
Observe the values returned by `predicate` after every collection change.
10+
- note:
11+
- Thread safe.
12+
- parameter matching: Secondary indexed query to execute on collection change.
13+
*/
14+
public func values(matching clause: WhereClause) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
15+
return self.map { (collection, changeSet) in
16+
let newValues = collection.findValuesWhere(clause)
17+
return TransactionalValue(transaction: collection.readTransaction, value: newValues)
18+
}
19+
}
20+
821
/**
922
Observe the values returned by `predicate` after a collection change.
1023
If the query is expensive, the collection change set can be examined first by using `prefilterChangeSet`.
1124
- note:
1225
- Thread safe.
13-
- parameter clause: Secondary indexed query to execute on collection change.
14-
- parameter thread: Thread to execute the prefilter and potential query on.
15-
- parameter prefilterChangeSet: Executed before querying the collection to determine if the query is required.
26+
- parameter matching: Secondary indexed query to execute on collection change.
27+
- parameter prefilter: Executed before querying the collection to determine if the query is required.
1628
*/
1729
public func values(matching clause: WhereClause, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
1830
var previous: [TCollection.Value] = []
1931

2032
return self.filterChangeSet { (changeSet) -> Bool in
21-
return prefilter(changeSet: changeSet, previousValues: previous)
33+
return prefilter(changeSet: changeSet, previousValues: previous)
2234
}.map { (collection, changeSet) in
2335
let newValues = collection.findValuesWhere(clause)
2436
previous = newValues
2537
return TransactionalValue(transaction: collection.readTransaction, value: newValues)
38+
}
39+
}
40+
41+
/**
42+
Observe the values returned by `predicate` after every collection change.
43+
- note:
44+
- Thread safe.
45+
- parameter matching: Secondary indexed query to execute on collection change.
46+
*/
47+
public func indexableValues(matching clause: WhereClause) -> IndexableObservable<[TCollection.Value]> {
48+
return IndexableObservable(observable: self.map { return $0.0.findValuesWhere(clause) })
49+
}
50+
51+
/**
52+
Observe the values returned by `predicate` after a collection change.
53+
If the query is expensive, the collection change set can be examined first by using `prefilterChangeSet`.
54+
- note:
55+
- Thread safe.
56+
- parameter matching: Secondary indexed query to execute on collection change.
57+
- parameter prefilter: Executed before querying the collection to determine if the query is required.
58+
*/
59+
public func indexableValues(matching clause: WhereClause, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> IndexableObservable<[TCollection.Value]> {
60+
var previous: [TCollection.Value] = []
61+
62+
let observable = self.filterChangeSet { (changeSet) -> Bool in
63+
return prefilter(changeSet: changeSet, previousValues: previous)
64+
}.map { (collection, changeSet) -> [TCollection.Value] in
65+
let newValues = collection.findValuesWhere(clause)
66+
previous = newValues
67+
return newValues
2668
}
69+
return IndexableObservable(observable: observable)
2770
}
2871

29-
//TODO PreparedValuesWhereQuery - could it conform to a protocol to make these generic or would that make the signature unwieldy and slow
72+
// MARK: Prepared query
3073

31-
public func values(matching clause: WhereClause) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
74+
/**
75+
Observe the values returned by `predicate` after every collection change.
76+
- note:
77+
- Thread safe.
78+
- parameter matching: Secondary indexed query to execute on collection change.
79+
*/
80+
public func values(matching clause: PreparedValuesWhereQuery<Collections>) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
3281
return self.map { (collection, changeSet) in
3382
let newValues = collection.findValuesWhere(clause)
3483
return TransactionalValue(transaction: collection.readTransaction, value: newValues)
3584
}
3685
}
3786

38-
public func indexableValues(matching clause: WhereClause) -> IndexableObservable<[TCollection.Value]> {
87+
/**
88+
Observe the values returned by `predicate` after a collection change.
89+
If the query is expensive, the collection change set can be examined first by using `prefilterChangeSet`.
90+
- note:
91+
- Thread safe.
92+
- parameter matching: Secondary indexed query to execute on collection change.
93+
- parameter prefilter: Executed before querying the collection to determine if the query is required.
94+
*/
95+
public func values(matching clause: PreparedValuesWhereQuery<Collections>, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
96+
var previous: [TCollection.Value] = []
97+
98+
return self.filterChangeSet { (changeSet) -> Bool in
99+
return prefilter(changeSet: changeSet, previousValues: previous)
100+
}.map { (collection, changeSet) in
101+
let newValues = collection.findValuesWhere(clause)
102+
previous = newValues
103+
return TransactionalValue(transaction: collection.readTransaction, value: newValues)
104+
}
105+
}
106+
107+
/**
108+
Observe the values returned by `predicate` after every collection change.
109+
- note:
110+
- Thread safe.
111+
- parameter matching: Secondary indexed query to execute on collection change.
112+
*/
113+
public func indexableValues(matching clause: PreparedValuesWhereQuery<Collections>) -> IndexableObservable<[TCollection.Value]> {
39114
return IndexableObservable(observable: self.map { return $0.0.findValuesWhere(clause) })
40115
}
41116

42-
public func indexableValues(matching clause: WhereClause, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> IndexableObservable<[TCollection.Value]> {
117+
/**
118+
Observe the values returned by `predicate` after a collection change.
119+
If the query is expensive, the collection change set can be examined first by using `prefilterChangeSet`.
120+
- note:
121+
- Thread safe.
122+
- parameter matching: Secondary indexed query to execute on collection change.
123+
- parameter prefilter: Executed before querying the collection to determine if the query is required.
124+
*/
125+
public func indexableValues(matching clause: PreparedValuesWhereQuery<Collections>, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> IndexableObservable<[TCollection.Value]> {
43126
var previous: [TCollection.Value] = []
44127

45128
let observable = self.filterChangeSet { (changeSet) -> Bool in
46-
return prefilter(changeSet: changeSet, previousValues: previous)
129+
return prefilter(changeSet: changeSet, previousValues: previous)
47130
}.map { (collection, changeSet) -> [TCollection.Value] in
48131
let newValues = collection.findValuesWhere(clause)
49132
previous = newValues
50133
return newValues
51-
}
134+
}
52135
return IndexableObservable(observable: observable)
53136
}
54137

55-
// MARK: Prepared query
138+
// MARK: Raw SQL query
56139

140+
/**
141+
Observe the values returned by `predicate` after every collection change.
142+
- note:
143+
- Thread safe.
144+
- parameter matchingRawSql: Secondary indexed query to execute on collection change.
145+
*/
146+
public func values(matchingRawSql clause: String) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
147+
return self.map { (collection, changeSet) in
148+
let newValues = collection.findValuesWhere(clause)
149+
return TransactionalValue(transaction: collection.readTransaction, value: newValues)
150+
}
151+
}
57152

58-
// MARK: Raw SQL query
153+
/**
154+
Observe the values returned by `predicate` after a collection change.
155+
If the query is expensive, the collection change set can be examined first by using `prefilterChangeSet`.
156+
- note:
157+
- Thread safe.
158+
- parameter matchingRawSql: Secondary indexed query to execute on collection change.
159+
- parameter prefilter: Executed before querying the collection to determine if the query is required.
160+
*/
161+
public func values(matchingRawSql clause: String, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> Observable<TransactionalValue<[TCollection.Value], Collections>> {
162+
var previous: [TCollection.Value] = []
59163

164+
return self.filterChangeSet { (changeSet) -> Bool in
165+
return prefilter(changeSet: changeSet, previousValues: previous)
166+
}.map { (collection, changeSet) in
167+
let newValues = collection.findValuesWhere(clause)
168+
previous = newValues
169+
return TransactionalValue(transaction: collection.readTransaction, value: newValues)
170+
}
171+
}
172+
173+
/**
174+
Observe the values returned by `predicate` after every collection change.
175+
- note:
176+
- Thread safe.
177+
- parameter matchingRawSql: Secondary indexed query to execute on collection change.
178+
*/
179+
public func indexableValues(matchingRawSql clause: String) -> IndexableObservable<[TCollection.Value]> {
180+
return IndexableObservable(observable: self.map { return $0.0.findValuesWhere(clause) })
181+
}
182+
183+
/**
184+
Observe the values returned by `predicate` after a collection change.
185+
If the query is expensive, the collection change set can be examined first by using `prefilterChangeSet`.
186+
- note:
187+
- Thread safe.
188+
- parameter matchingRawSql: Secondary indexed query to execute on collection change.
189+
- parameter prefilter: Executed before querying the collection to determine if the query is required.
190+
*/
191+
public func indexableValues(matchingRawSql clause: String, prefilter: (changeSet: ChangeSet<String>, previousValues: [TCollection.Value]) -> Bool) -> IndexableObservable<[TCollection.Value]> {
192+
var previous: [TCollection.Value] = []
193+
194+
let observable = self.filterChangeSet { (changeSet) -> Bool in
195+
return prefilter(changeSet: changeSet, previousValues: previous)
196+
}.map { (collection, changeSet) -> [TCollection.Value] in
197+
let newValues = collection.findValuesWhere(clause)
198+
previous = newValues
199+
return newValues
200+
}
201+
return IndexableObservable(observable: observable)
202+
}
60203
}

0 commit comments

Comments
 (0)