mini_stream.dart
4.29 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
part of 'rx_stream.dart';
class Node<T> {
T? data;
Node<T>? next;
Node({this.data, this.next});
}
class MiniSubscription<T> {
const MiniSubscription(
this.data, this.onError, this.onDone, this.cancelOnError, this.listener);
final OnData<T> data;
final Function? onError;
final Callback? onDone;
final bool cancelOnError;
Future<void> cancel() async => listener.removeListener(this);
final FastList<T> listener;
}
class MiniStream<T> {
FastList<T> listenable = FastList<T>();
late T _value;
T get value => _value;
set value(T val) {
add(val);
}
void add(T event) {
_value = event;
listenable._notifyData(event);
}
void addError(Object error, [StackTrace? stackTrace]) {
listenable._notifyError(error, stackTrace);
}
int get length => listenable.length;
bool get hasListeners => listenable.isNotEmpty;
bool get isClosed => _isClosed;
MiniSubscription<T> listen(void Function(T event) onData,
{Function? onError,
void Function()? onDone,
bool cancelOnError = false}) {
final subs = MiniSubscription<T>(
onData,
onError,
onDone,
cancelOnError,
listenable,
);
listenable.addListener(subs);
return subs;
}
bool _isClosed = false;
void close() {
if (_isClosed) {
throw 'You can not close a closed Stream';
}
listenable._notifyDone();
listenable.clear();
_isClosed = true;
}
}
class FastList<T> {
Node<MiniSubscription<T>>? _head;
void _notifyData(T data) {
var currentNode = _head;
do {
currentNode?.data?.data(data);
currentNode = currentNode?.next;
} while (currentNode != null);
}
void _notifyDone() {
var currentNode = _head;
do {
currentNode?.data?.onDone?.call();
currentNode = currentNode?.next;
} while (currentNode != null);
}
void _notifyError(Object error, [StackTrace? stackTrace]) {
var currentNode = _head;
while (currentNode != null) {
currentNode.data!.onError?.call(error, stackTrace);
currentNode = currentNode.next;
}
}
/// Checks if this list is empty
bool get isEmpty => _head == null;
bool get isNotEmpty => !isEmpty;
/// Returns the length of this list
int get length {
var length = 0;
var currentNode = _head;
while (currentNode != null) {
currentNode = currentNode.next;
length++;
}
return length;
}
/// Shows the element at position [position]. `null` for invalid positions.
MiniSubscription<T>? _elementAt(int position) {
if (isEmpty || length < position || position < 0) return null;
var node = _head;
var current = 0;
while (current != position) {
node = node!.next;
current++;
}
return node!.data;
}
/// Inserts [data] at the end of the list.
void addListener(MiniSubscription<T> data) {
var newNode = Node(data: data);
if (isEmpty) {
_head = newNode;
} else {
var currentNode = _head!;
while (currentNode.next != null) {
currentNode = currentNode.next!;
}
currentNode.next = newNode;
}
}
bool contains(T element) {
var length = this.length;
for (var i = 0; i < length; i++) {
if (_elementAt(i) == element) return true;
if (length != this.length) {
throw ConcurrentModificationError(this);
}
}
return false;
}
void removeListener(MiniSubscription<T> element) {
var length = this.length;
for (var i = 0; i < length; i++) {
if (_elementAt(i) == element) {
_removeAt(i);
break;
}
}
}
void clear() {
var length = this.length;
for (var i = 0; i < length; i++) {
_removeAt(i);
}
}
MiniSubscription<T>? _removeAt(int position) {
var index = 0;
var currentNode = _head;
Node<MiniSubscription<T>>? previousNode;
if (isEmpty || length < position || position < 0) {
throw Exception('Invalid position');
} else if (position == 0) {
_head = _head!.next;
} else {
while (index != position) {
previousNode = currentNode;
currentNode = currentNode!.next;
index++;
}
if (previousNode == null) {
_head = null;
} else {
previousNode.next = currentNode!.next;
}
currentNode!.next = null;
}
return currentNode!.data;
}
}