3
* Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
4
* Copyright (C) 2008 Ali Sabil <ali.sabil@gmail.com>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public
8
* License as published by the Free Software Foundation; either
9
* version 2.1 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
namespace People.Utils {
28
* Class representing an asynchronous search, an instance of this class
29
* is returned by asynchronous query methods.
31
public class AsyncSearch<T> : Object {
32
private bool started = false;
33
private bool finished = false;
35
private Gee.List<T> buffered_hits;
38
* Signal emitted when a new hit is found.
40
* @param hit the new hit found
42
public signal void hit_found(T hit);
45
* Signal emitted when most of the hits for this AsyncSearch have been
48
public signal void done();
51
* Makes this AsyncSearch start emitting signals.
53
* Buffered hits found before this method is called are now propagated
54
* through several {@link AsyncSearch#hit_found} emissions.
56
* If the AsyncSearch was marked as done before this method is called,
57
* then {@link AsyncSearch#done} is emitted.
60
return_if_fail (started != true);
63
if (buffered_hits != null) {
64
foreach(T hit in buffered_hits)
66
buffered_hits.clear();
74
* Makes this AsyncSearch stop emitting signals.
76
* FIXME: for now, only suspends the signal emissions, as the hits
77
* are buffered again. If this is the wanted behavior, we
78
* should change the name of that method. If the wanted
79
* behavior is to actually stop the research for new hits,
80
* then we need to find a way to do so.
87
* Marks this AsyncSearch as finished and emits
88
* {@link AsyncSearch#done}.
90
* This method should only be used from the entity populating this
93
public void finish() {
100
* Adds a hit to this AsyncSearch.
102
* If this AsyncSearch is started, then the hit is propagated through
103
* a {@link AsyncSearch#hit_found} emission. Else, the hit is buffered.
105
* This method should only be used from the entity populating this
108
public void add_hit(T hit) {
112
if (buffered_hits == null)
113
buffered_hits = new Gee.ArrayList<T>();
114
buffered_hits.add(hit);