Debian Codegraph @

external/rx/Rx/NET/Source/System.Reactive.Experimental/Reactive/ListObservable.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Reactive.Disposables;
6 using System.Reactive.Linq;
7 using System.Reactive.Subjects;
8
9 namespace System.Reactive
10 {
11 /// <summary>
12 /// Represents an object that retains the elements of the observable sequence and signals the end of the sequence.
13 /// </summary>
14 /// <typeparam name="T">The type of elements received from the source sequence.</typeparam>
15 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "By design; Observable suffix takes precedence.")]
16 [Experimental]
17 public class ListObservable<T> : IList<T>, IObservable<object>
18 {
19 IDisposable subscription;
20 AsyncSubject<object> subject = new AsyncSubject<object>();
21 List<T> results = new List<T>();
22
23 /// <summary>
24 /// Constructs an object that retains the values of source and signals the end of the sequence.
25 /// </summary>
26 /// <param name="source">The observable sequence whose elements will be retained in the list.</param>
27 /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
28 public ListObservable(IObservable<T> source)
29 {
30 if (source == null)
31 throw new ArgumentNullException("source");
32
33 subscription = source.Subscribe(results.Add, subject.OnError, subject.OnCompleted);
34 }
35
36 void Wait()
37 {
38 subject.DefaultIfEmpty().Wait();
39 }
40
41 /// <summary>
42 /// Returns the last value of the observable sequence.
43 /// </summary>
44 public T Value
45 {
46 get
47 {
48 Wait();
49 if (results.Count == 0)
50 throw new InvalidOperationException(Strings_Linq.NO_ELEMENTS);
51 return results[results.Count - 1];
52 }
53 }
54 /// <summary>
55 /// Determines the index of a specific item in the ListObservable.
56 /// </summary>
57 /// <param name="item">The element to determine the index for.</param>
58 /// <returns>The index of the specified item in the list; -1 if not found.</returns>
59 public int IndexOf(T item)
60 {
61 Wait();
62 return results.IndexOf(item);
63 }
64
65 /// <summary>
66 /// Inserts an item to the ListObservable at the specified index.
67 /// </summary>
68 /// <param name="index">The index to insert the item at.</param>
69 /// <param name="item">The item to insert in the list.</param>
70 public void Insert(int index, T item)
71 {
72 Wait();
73 results.Insert(index, item);
74 }
75
76 /// <summary>
77 /// Removes the ListObservable item at the specified index.
78 /// </summary>
79 /// <param name="index">The index of the item to remove.</param>
80 public void RemoveAt(int index)
81 {
82 Wait();
83 results.RemoveAt(index);
84 }
85
86 /// <summary>
87 /// Gets or sets the element at the specified index.
88 /// </summary>
89 /// <param name="index">The index of the item to retrieve or set.</param>
90 public T this[int index]
91 {
92 get
93 {
94 Wait();
95 return results[index];
96 }
97 set
98 {
99 Wait();
100 results[index] = value;
101 }
102 }
103
104 /// <summary>
105 /// Adds an item to the ListObservable.
106 /// </summary>
107 /// <param name="item">The item to add to the list.</param>
108 public void Add(T item)
109 {
110 Wait();
111 results.Add(item);
112 }
113
114 /// <summary>
115 /// Removes all items from the ListObservable.
116 /// </summary>
117 public void Clear()
118 {
119 Wait();
120 results.Clear();
121 }
122
123 /// <summary>
124 /// Determines whether the ListObservable contains a specific value.
125 /// </summary>
126 /// <param name="item">The item to search for in the list.</param>
127 /// <returns>true if found; false otherwise.</returns>
128 public bool Contains(T item)
129 {
130 Wait();
131 return results.Contains(item);
132 }
133
134 /// <summary>
135 /// Copies the elements of the ListObservable to an System.Array, starting at a particular System.Array index.
136 /// </summary>
137 /// <param name="array">The array to copy elements to.</param>
138 /// <param name="arrayIndex">The start index in the array to start copying elements to.</param>
139 public void CopyTo(T[] array, int arrayIndex)
140 {
141 Wait();
142 results.CopyTo(array, arrayIndex);
143 }
144
145 /// <summary>
146 /// Gets the number of elements contained in the ListObservable.
147 /// </summary>
148 public int Count
149 {
150 get
151 {
152 Wait();
153 return results.Count;
154 }
155 }
156
157 /// <summary>
158 /// Gets a value that indicates whether the ListObservable is read-only.
159 /// </summary>
160 public bool IsReadOnly
161 {
162 get { return false; }
163 }
164
165 /// <summary>
166 /// Removes the first occurrence of a specific object from the ListObservable.
167 /// </summary>
168 /// <param name="item">The item to remove from the list.</param>
169 /// <returns>true if the item was found; false otherwise.</returns>
170 public bool Remove(T item)
171 {
172 Wait();
173 return results.Remove(item);
174 }
175
176 /// <summary>
177 /// Returns an enumerator that iterates through the collection.
178 /// </summary>
179 /// <returns>Enumerator over the list.</returns>
180 public IEnumerator<T> GetEnumerator()
181 {
182 Wait();
183 return results.GetEnumerator();
184 }
185
186 IEnumerator IEnumerable.GetEnumerator()
187 {
188 return this.GetEnumerator();
189 }
190
191 /// <summary>
192 /// Subscribes an observer to the ListObservable which will be notified upon completion.
193 /// </summary>
194 /// <param name="observer">The observer to send completion or error messages to.</param>
195 /// <returns>The disposable resource that can be used to unsubscribe.</returns>
196 /// <exception cref="ArgumentNullException"><paramref name="observer"/> is null.</exception>
197 public IDisposable Subscribe(IObserver<object> observer)
198 {
199 if (observer == null)
200 throw new ArgumentNullException("observer");
201
202 return new CompositeDisposable(subscription, subject.Subscribe(observer));
203 }
204 }
205 }