-
Notifications
You must be signed in to change notification settings - Fork 10.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
Adding "proper" message port for fake worker. #6948
Closed
Closed
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* globals pdfjsFilePath */ | ||
/* globals pdfjsFilePath, WeakMap */ | ||
|
||
'use strict'; | ||
|
||
|
@@ -1236,6 +1236,84 @@ var PDFWorker = (function PDFWorkerClosure() { | |
return PDFJS.fakeWorkerFilesLoadedCapability.promise; | ||
} | ||
|
||
function FakeWorkerPort(defer) { | ||
this._listeners = []; | ||
this._defer = defer; | ||
this._deferred = Promise.resolve(undefined); | ||
} | ||
FakeWorkerPort.prototype = { | ||
postMessage: function (obj, transfers) { | ||
function cloneValue(value) { | ||
// Trying to perform a structured clone close to the spec, including | ||
// transfers. | ||
if (typeof value !== 'object' || value === null) { | ||
return value; | ||
} | ||
if (clonned.has(value)) { // already clonned the object | ||
return clonned.get(value); | ||
} | ||
var result; | ||
var buffer; | ||
if ((buffer = value.buffer) && isArrayBuffer(buffer)) { | ||
// We found object with ArrayBuffer (typed array). | ||
var transferable = transfers && transfers.indexOf(buffer) >= 0; | ||
if (value === buffer) { | ||
// Special case when we are faking typed arrays in compatibility.js. | ||
result = value; | ||
} else if (transferable) { | ||
result = new value.constructor(buffer, value.byteOffset, | ||
value.byteLength); | ||
} else { | ||
result = new value.constructor(value); | ||
} | ||
clonned.set(value, result); | ||
return result; | ||
} | ||
result = Array.isArray(value) ? [] : {}; | ||
clonned.set(value, result); // adding to cache now for cyclic references | ||
// Cloning all value and object properties, however ignoring properties | ||
// defined via getter. | ||
for (var i in value) { | ||
var desc, p = value; | ||
while (!(desc = Object.getOwnPropertyDescriptor(p, i))) { | ||
p = Object.getPrototypeOf(p); | ||
} | ||
if (typeof desc.value === 'undefined' || | ||
typeof desc.value === 'function') { | ||
continue; | ||
} | ||
result[i] = cloneValue(desc.value); | ||
} | ||
return result; | ||
} | ||
|
||
if (!this._defer) { | ||
this._listeners.forEach(function (listener) { | ||
listener.call(this, {data: obj}); | ||
}, this); | ||
return; | ||
} | ||
|
||
var clonned = new WeakMap(); | ||
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. Spelling nit: shouldn't this be |
||
var e = {data: cloneValue(obj)}; | ||
this._deferred.then(function () { | ||
this._listeners.forEach(function (listener) { | ||
listener.call(this, e); | ||
}, this); | ||
}.bind(this)); | ||
}, | ||
addEventListener: function (name, listener) { | ||
this._listeners.push(listener); | ||
}, | ||
removeEventListener: function (name, listener) { | ||
var i = this._listeners.indexOf(listener); | ||
this._listeners.splice(i, 1); | ||
}, | ||
terminate: function () { | ||
this._listeners = []; | ||
} | ||
}; | ||
|
||
function createCDNWrapper(url) { | ||
// We will rely on blob URL's property to specify origin. | ||
// We want this function to fail in case if createObjectURL or Blob do not | ||
|
@@ -1387,24 +1465,11 @@ var PDFWorker = (function PDFWorkerClosure() { | |
return; | ||
} | ||
|
||
// If we don't use a worker, just post/sendMessage to the main thread. | ||
var port = { | ||
_listeners: [], | ||
postMessage: function (obj) { | ||
var e = {data: obj}; | ||
this._listeners.forEach(function (listener) { | ||
listener.call(this, e); | ||
}, this); | ||
}, | ||
addEventListener: function (name, listener) { | ||
this._listeners.push(listener); | ||
}, | ||
removeEventListener: function (name, listener) { | ||
var i = this._listeners.indexOf(listener); | ||
this._listeners.splice(i, 1); | ||
}, | ||
terminate: function () {} | ||
}; | ||
// We cannot turn on proper fake port simulation (this includes | ||
// structured cloning) when typed arrays are not supported. Relying | ||
// on a chance that messages will be sent in proper order. | ||
var isTypedArraysPresent = Uint8Array !== Float32Array; | ||
var port = new FakeWorkerPort(isTypedArraysPresent); | ||
this._port = port; | ||
|
||
// All fake workers use the same port, making id unique. | ||
|
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.
Why not just use
isArray
fromshared/util.js
?