2
2
// onto the JavaScript event loop. This implementation assumes there is a
3
3
// single thread and is *not* compatible with multiple WebAssembly workers sharing
4
4
// the same address space.
5
+ //
6
+ // TODO: Implement support for multiple threads. This will require a mechanism to
7
+ // wake up another thread, such as the `postMessage` API.
5
8
6
9
use futures:: future:: { Future , ExecuteError , Executor } ;
7
10
use futures:: executor:: { self , Notify , Spawn } ;
@@ -14,6 +17,7 @@ use webcore::try_from::TryInto;
14
17
use webcore:: value:: Reference ;
15
18
16
19
20
+ // TODO: Determine optimal values for these constants
17
21
// Initial capacity of the event queue
18
22
const INITIAL_QUEUE_CAPACITY : usize = 10 ;
19
23
// Iterations to wait before allowing the queue to shrink
@@ -170,6 +174,7 @@ impl EventLoopInner {
170
174
fn pop_task ( & self ) -> Option < Rc < SpawnedTask > > {
171
175
self . microtask_queue . borrow_mut ( ) . pop_front ( )
172
176
}
177
+ // Reclaim space from the queue if it's going to waste
173
178
fn shrink_if_necessary ( & self ) {
174
179
let mut queue = self . microtask_queue . borrow_mut ( ) ;
175
180
// We consider shrinking the queue if it is less than
@@ -179,7 +184,7 @@ impl EventLoopInner {
179
184
// `QUEUE_SHRINK_DELAY` iterations.
180
185
let shrink_counter = self . shrink_counter . get ( ) ;
181
186
if shrink_counter < QUEUE_SHRINK_DELAY {
182
- self . shrink_counter . set ( shrink_counter+ 1 ) ;
187
+ self . shrink_counter . set ( shrink_counter + 1 ) ;
183
188
} else {
184
189
queue. shrink_to_fit ( ) ;
185
190
self . shrink_counter . set ( 0 ) ;
@@ -190,10 +195,10 @@ impl EventLoopInner {
190
195
}
191
196
// Poll the queue until it is empty
192
197
fn drain ( & self ) {
198
+ self . shrink_if_necessary ( ) ;
193
199
while let Some ( task) = self . pop_task ( ) {
194
200
task. poll ( ) ;
195
201
}
196
- self . shrink_if_necessary ( ) ;
197
202
}
198
203
}
199
204
0 commit comments