Skip to content

Commit c7aa2c3

Browse files
committed
Review changes
1 parent 3cc0221 commit c7aa2c3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/webcore/executor.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// onto the JavaScript event loop. This implementation assumes there is a
33
// single thread and is *not* compatible with multiple WebAssembly workers sharing
44
// 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.
58

69
use futures::future::{Future, ExecuteError, Executor};
710
use futures::executor::{self, Notify, Spawn};
@@ -14,6 +17,7 @@ use webcore::try_from::TryInto;
1417
use webcore::value::Reference;
1518

1619

20+
// TODO: Determine optimal values for these constants
1721
// Initial capacity of the event queue
1822
const INITIAL_QUEUE_CAPACITY: usize = 10;
1923
// Iterations to wait before allowing the queue to shrink
@@ -117,6 +121,7 @@ impl EventLoopInner {
117121
var wrapper = function() {
118122
if (!callback.dropped) { callback() }
119123
};
124+
var nextTick;
120125

121126
// Modern browsers can use `MutationObserver` which allows
122127
// us to schedule a micro-task without allocating a promise.
@@ -127,7 +132,7 @@ impl EventLoopInner {
127132

128133
new MutationObserver( wrapper ).observe( node, { characterData: true } );
129134

130-
function nextTick() {
135+
nextTick = function() {
131136
state = !state;
132137
node.data = ( state ? "1" : "0" );
133138
}
@@ -136,7 +141,7 @@ impl EventLoopInner {
136141
} else {
137142
var promise = Promise.resolve( null );
138143

139-
function nextTick() {
144+
nextTick = function() {
140145
promise.then( wrapper );
141146
}
142147
}
@@ -170,6 +175,7 @@ impl EventLoopInner {
170175
fn pop_task(&self) -> Option< Rc< SpawnedTask > > {
171176
self.microtask_queue.borrow_mut().pop_front()
172177
}
178+
// Reclaim space from the queue if it's going to waste
173179
fn shrink_if_necessary(&self) {
174180
let mut queue = self.microtask_queue.borrow_mut();
175181
// We consider shrinking the queue if it is less than
@@ -179,7 +185,7 @@ impl EventLoopInner {
179185
// `QUEUE_SHRINK_DELAY` iterations.
180186
let shrink_counter = self.shrink_counter.get();
181187
if shrink_counter < QUEUE_SHRINK_DELAY {
182-
self.shrink_counter.set(shrink_counter+1);
188+
self.shrink_counter.set(shrink_counter + 1);
183189
} else {
184190
queue.shrink_to_fit();
185191
self.shrink_counter.set(0);
@@ -190,10 +196,10 @@ impl EventLoopInner {
190196
}
191197
// Poll the queue until it is empty
192198
fn drain(&self) {
199+
self.shrink_if_necessary();
193200
while let Some(task) = self.pop_task() {
194201
task.poll();
195202
}
196-
self.shrink_if_necessary();
197203
}
198204
}
199205

0 commit comments

Comments
 (0)