Skip to content

Commit d28ac56

Browse files
committed
split methods, unharcode
1 parent 04995ff commit d28ac56

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

src/util/actor.js

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Actor {
5353
* @param targetMapId A particular mapId to which to send this message.
5454
* @private
5555
*/
56-
send(type: string, data: mixed, callback: ?Function, targetMapId: ?string): ?Cancelable {
56+
send(type: string, data: mixed, callback: ?Function, targetMapId: ?string, mustQueue: ?string): ?Cancelable {
5757
// We're using a string ID instead of numbers because they are being used as object keys
5858
// anyway, and thus stringified implicitly. We use random IDs because an actor may receive
5959
// message from multiple other actors which could run in different execution context. A
@@ -68,6 +68,7 @@ class Actor {
6868
type,
6969
hasCallback: !!callback,
7070
targetMapId,
71+
mustQueue,
7172
sourceMapId: this.mapId,
7273
data: serialize(data, buffers)
7374
}, buffers);
@@ -110,7 +111,7 @@ class Actor {
110111
cancel();
111112
}
112113
} else {
113-
if (isWorker() || data.type === 'getResource') {
114+
if (isWorker() || data.mustQueue) {
114115
// In workers, store the tasks that we need to process before actually processing them. This
115116
// is necessary because we want to keep receiving messages, and in particular,
116117
// <cancel> messages. Some tasks may take a while in the worker thread, so before
@@ -123,31 +124,33 @@ class Actor {
123124
} else {
124125
// In the main thread, process messages immediately so that other work does not slip in
125126
// between getting partial data back from workers.
126-
this.process(id, data);
127+
this.processTask(id, data);
127128
}
128129
}
129130
}
130131

131-
process(id: number, task: any) {
132-
if (id === undefined && task === undefined) {
133-
if (!this.taskQueue.length) {
134-
return;
135-
}
136-
id = this.taskQueue.shift();
137-
task = this.tasks[id];
138-
delete this.tasks[id];
139-
// Schedule another process call if we know there's more to process _before_ invoking the
140-
// current task. This is necessary so that processing continues even if the current task
141-
// doesn't execute successfully.
142-
if (this.taskQueue.length) {
143-
this.invoker.trigger();
144-
}
145-
if (!task) {
146-
// If the task ID doesn't have associated task data anymore, it was canceled.
147-
return;
148-
}
132+
process() {
133+
if (!this.taskQueue.length) {
134+
return;
149135
}
136+
const id = this.taskQueue.shift();
137+
const task = this.tasks[id];
138+
delete this.tasks[id];
139+
// Schedule another process call if we know there's more to process _before_ invoking the
140+
// current task. This is necessary so that processing continues even if the current task
141+
// doesn't execute successfully.
142+
if (this.taskQueue.length) {
143+
this.invoker.trigger();
144+
}
145+
if (!task) {
146+
// If the task ID doesn't have associated task data anymore, it was canceled.
147+
return;
148+
}
149+
150+
this.processTask(id, task);
151+
}
150152

153+
processTask(id: number, task: any) {
151154
if (task.type === '<response>') {
152155
// The done() function in the counterpart has been called, and we are now
153156
// firing the callback in the originating actor, if there is one.

src/util/ajax.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ export const makeRequest = function(requestParameters: RequestParameters, callba
230230
return makeFetchRequest(requestParameters, callback);
231231
}
232232
if (isWorker() && self.worker && self.worker.actor) {
233-
return self.worker.actor.send('getResource', requestParameters, callback);
233+
const queueOnMainThread = true;
234+
return self.worker.actor.send('getResource', requestParameters, callback, undefined, queueOnMainThread);
234235
}
235236
}
236237
return makeXMLHttpRequest(requestParameters, callback);

0 commit comments

Comments
 (0)