-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug causing worker-initiated messages to be broadcast to every map instance #3133
Merged
+145
−17
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,30 +23,56 @@ function Actor(target, parent, mapId) { | |
this.target.addEventListener('message', this.receive, false); | ||
} | ||
|
||
/** | ||
* Sends a message from a main-thread map to a Worker or from a Worker back to | ||
* a main-thread map instance. | ||
* | ||
* @param {string} type The name of the target method to invoke or '[source-type].name' for a method on a WorkerSource. | ||
* @param {object} data | ||
* @param {Function} [callback] | ||
* @param {Array} [buffers] A list of buffers to "transfer" (see https://developer.mozilla.org/en-US/docs/Web/API/Transferable) | ||
* @param {string} [targetMapId] A particular mapId to which to send this message. | ||
* @private | ||
*/ | ||
Actor.prototype.send = function(type, data, callback, buffers, targetMapId) { | ||
var id = callback ? this.mapId + ':' + this.callbackID++ : null; | ||
if (callback) this.callbacks[id] = callback; | ||
this.postMessage({ | ||
targetMapId: targetMapId, | ||
sourceMapId: this.mapId, | ||
type: type, | ||
id: String(id), | ||
data: data | ||
}, buffers); | ||
}; | ||
|
||
Actor.prototype.receive = function(message) { | ||
var data = message.data, | ||
id = data.id, | ||
callback; | ||
|
||
if (data.targetMapId && this.mapId !== data.targetMapId) | ||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think there's a way you can make this code more self-documenting such that the comment is unnecessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. Will do |
||
|
||
if (data.type === '<response>') { | ||
callback = this.callbacks[data.id]; | ||
delete this.callbacks[data.id]; | ||
if (callback) callback(data.error || null, data.data); | ||
} else if (typeof data.id !== 'undefined' && this.parent[data.type]) { | ||
// data.type == 'load tile', 'remove tile', etc. | ||
this.parent[data.type](data.mapId, data.data, done.bind(this)); | ||
this.parent[data.type](data.sourceMapId, data.data, done.bind(this)); | ||
} else if (typeof data.id !== 'undefined' && this.parent.getWorkerSource) { | ||
// data.type == sourcetype.method | ||
var keys = data.type.split('.'); | ||
var workerSource = this.parent.getWorkerSource(data.mapId, keys[0]); | ||
var workerSource = this.parent.getWorkerSource(data.sourceMapId, keys[0]); | ||
workerSource[keys[1]](data.data, done.bind(this)); | ||
} else { | ||
this.parent[data.type](data.data); | ||
} | ||
|
||
function done(err, data, buffers) { | ||
this.postMessage({ | ||
mapId: this.mapId, | ||
sourceMapId: this.mapId, | ||
type: '<response>', | ||
id: String(id), | ||
error: err ? String(err) : null, | ||
|
@@ -55,10 +81,8 @@ Actor.prototype.receive = function(message) { | |
} | ||
}; | ||
|
||
Actor.prototype.send = function(type, data, callback, buffers) { | ||
var id = callback ? this.mapId + ':' + this.callbackID++ : null; | ||
if (callback) this.callbacks[id] = callback; | ||
this.postMessage({ mapId: this.mapId, type: type, id: String(id), data: data }, buffers); | ||
Actor.prototype.remove = function () { | ||
this.target.removeEventListener('message', this.receive, false); | ||
}; | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It feels like we're fighting our own architecture with this fake
Actor
. Can you think of a way to refactor & simplify?Worker
andActor
are so tightly coupled it might make sense to merge them.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think merging Worker and Actor is probably beyond the scope of this bug fix, but I certainly agree your broader point over in #3034.
I was trying to keep the scope of this change as limited as possible, but maybe one way to simplify this a bit would be to replace WorkerSource's (and WorkerTile#parse's)
actor: Actor
parameter with just asend: (type, data, callback)=>void
parameter.