-
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
Adds sendWithStream method in MessageHandler. #8430
Conversation
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've left a couple of comments, based on a very cursory look at the code.
Also, I'm wondering if there's any reason that the new unit-tests cannot be run on Travis?
Otherwise, let's add the new test file to https://github.com/mozilla/pdf.js/blob/master/test/unit/clitests.json.
src/shared/util.js
Outdated
@@ -1219,16 +1219,111 @@ function MessageHandler(sourceName, targetName, comObj) { | |||
this.targetName = targetName; | |||
this.comObj = comObj; | |||
this.callbackIndex = 1; | |||
this.streamId = 1; |
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.
Just above there's callbackIndex
, so can we please use the same same suffix for both callback
and stream
to reduce confusion here!?
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.
Yes, sure. I will change it to streamIndex
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.
Yes, sure. I will change it to
streamIndex
Thanks, but I now notice that this may become problematic with respect to the line length limit; it may be simpler to keep this.streamId
and just change this.callbackIndex
-> this.callbackId
instead :-)
Looking at the existing code, the callbackId
name is actually already used in most places, except here: https://github.com/mozilla/pdf.js/blob/master/src/shared/util.js#L1313.
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.
but I now notice that this may become problematic with respect to the line length limit
"line length limit"? I think i am not getting this point, can you please elaborate?
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 just noticed that there's a couple of fairly long lines in the patch already (for example this one), and it's possible that using streamIndex
instead of streamId
everywhere could perhaps unnecessarily go over the maximum limit of 80 characters per line (as enforced by ESLint).
However, it seems that we already use callbackId
in most places in the existing code, hence I'm suggesting to keep this.streamId
in this patch and just change the two existing occurrences of this.callbackIndex
to this.callbackId
instead.
Sorry about giving confusing/conflicting advice here!
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'm suggesting to keep
this.streamId
in this patch and just change the two existing occurrences ofthis.callbackIndex
tothis.callbackId
instead.
Okay, sure. Thanks for clarification here.
src/shared/util.js
Outdated
let streamId = data.streamId; | ||
switch (data.stream) { | ||
case 'start_complete': | ||
if (data.success === true || data.success === 'true') { |
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.
This looks slightly awkward to me; and why would/should success
ever be anything other than a boolean?
Looking below at the postMessage
calls, success
is always a boolean, so please simplify this to if (data.success) {
instead.
Please note: This comment applies in multiple places below as well.
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.
Just to be at safer side, i thought maybe somehow while sending data between main and worker, success
will change to string. But i am not sure.
Thanks for your suggestion, i will change this to if (data.success) {
. This looks more clear.
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.
Just to be at safer side, i thought maybe somehow while sending data between main and worker, success will change to string. But i am not sure.
That shouldn't be a problem, since the structured cloning algorithm maintains types (and throws on unsupported types).
If you want to check for yourself, look at the value/type of data.isReply
in the current code.
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.
That shouldn't be a problem, since the structured cloning algorithm maintains types (and throws on unsupported types).
Okay right, we are using structured cloning algorithm in postMessage
.
Thanks for clarification.
src/shared/util.js
Outdated
data.desiredSize > 0) { | ||
streamSinks[data.streamId].sinkCapability.resolve(); | ||
} | ||
// reset desiredSize property of sink on every pull. |
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.
Nit: For improved readability of comments, please remember to use proper capitalization at the start of sentences.
Please note: This probably applies elsewhere in the patch as well.
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.
Yeah, sure. Thanks.
src/shared/util.js
Outdated
break; | ||
case 'pull': | ||
// pull increases the desiredSize property of sink, so when it | ||
// changes from -ve to +ve set ready property as resolved promise. |
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.
Unless one already knows what ve
means, then this comment is unfortunately not very helpful. (This applies to another comment below as well.)
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.
Okay, i will make it negative
/positive
instead.
src/shared/util.js
Outdated
let streamSink = { | ||
enqueue(chunk, size) { | ||
var lastDesiredSize = this.desiredSize; | ||
this.desiredSize -= size !== undefined ? size : 1; |
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 change the signature to enqueue(chunk, size = 1) {
instead, since this line can then be simplified to this.desiredSize -= size
?
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.
Great, this is short and more clear. We will do this way.
src/shared/util.js
Outdated
* @param {Object} queueingStrategy strategy to signal backpressure based on | ||
* internal queue. | ||
* @param {Array} [transfers] Optional list of transfers/ArrayBuffers. | ||
* @return {ReadableStream} ReadableStream to read data in chunks. |
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.
Nit: Let's make this more consistent with how we generally write JSDoc comments (note that the other JSDoc comments in MessageHandler
aren't entirely correct):
* @param {string} actionName - Action to call.
* @param {JSON} data - JSON data to send.
* @param {Object} queueingStrategy - strategy to signal backpressure based on
* internal queue.
* @param {Array} [transfers] - Optional list of transfers/ArrayBuffers.
* @return {ReadableStream} ReadableStream to read data in chunks.
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.
note that the other JSDoc comments in MessageHandler aren't entirely correct
I referred those comments to write this :)
Okay, i will do as you suggested, and also change other comments in MessageHandler
to follow this.
src/shared/util.js
Outdated
* @return {ReadableStream} ReadableStream to read data in chunks. | ||
*/ | ||
sendWithStream(actionName, data, queueingStrategy, transfers) { | ||
var streamId = this.streamId++; |
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.
Please reduce the indentation to only two spaces within this method.
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.
Okay, sure.
test/unit/util_spec.js
Outdated
@@ -14,52 +14,52 @@ | |||
*/ | |||
|
|||
import { | |||
ReadableStream, removeNullCharacters, stringToPDFString | |||
ReadableStream, removeNullCharacters, stringToPDFString |
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.
Unnecessary change, please revert.
test/unit/util_stream_spec.js
Outdated
|
||
import { MessageHandler } from '../../src/shared/util'; | ||
|
||
describe('util stream', function () { |
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.
Nit: Please make this util_stream
instead, since that's consistent with existing tests.
test/unit/util_stream_spec.js
Outdated
} | ||
|
||
postMessage(obj) { | ||
let e = {data: obj}; |
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.
Please use a more descriptive variable name than e
.
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.
Okay, Thanks for your suggestions.
0d9797b
to
2dd23b0
Compare
Okay, but why it is failing the tests :-(
|
It seems that one of the tests fail on Travis, and I've noticed that while all tests pass locally, there still seem to be some errors regarding accessing removed properties. Note that you can easily run all (or part) of the unit-tests locally in a browser, by executing
This suggests that currently there's code trying to access certain properties after a |
31e7fe6
to
572a661
Compare
Yes, you are right. Actually, as Thanks for your help here :) |
Testing the latest version of the patch locally (on Windows), I get different behaviour in Firefox vs Chrome:
This unfortunately suggests that there're issues either in the |
Yes, you are right, i have Chrome(version 52) locally, and it is failing on this also. But in my opinion, |
I am not sure, but potentially this seems to be a timing problem. Running the test with different timeout value(e.g. [1] note: |
2147c2b
to
6dc7dc5
Compare
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.
Please also rebase to new master.
}).then(() => { | ||
sink.close(); | ||
}); | ||
}); |
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.
return sleep(5);
before exiting handler -- we need to be sure first sink.ready promise is resolved. Few place below needs that too.
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.
done
expect(result.value).toEqual([1, 2, 3, 4]); | ||
expect(result.done).toEqual(false); | ||
return sleep(10); | ||
}).then(() => { |
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.
add reason
param here and use it instead of log for expect below.
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.
Okay sure, done in new commit
34ae2f5
to
834e634
Compare
test/unit/util_stream_spec.js
Outdated
}); | ||
|
||
it('should ignore any pull after close is called', function (done) { | ||
function createPromiseCapability() { |
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 are you duplicating this here? Please import it from shared/util.js
instead!
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.
Okay, sure. Thanks.
src/shared/util.js
Outdated
let streamId = data.streamId; | ||
let sendStreamResponse = ({ stream, success }) => { | ||
comObj.postMessage({ sourceName, targetName, stream, | ||
success, streamId }); |
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.
Nit: The indentation is off here, please alight the parameters like this:
comObj.postMessage({ sourceName, targetName, stream,
success, streamId });
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.
done
src/shared/util.js
Outdated
let capability = createPromiseCapability(); | ||
let sendStreamRequest = ({ stream, chunk, reason, success }) => { | ||
comObj.postMessage({ sourceName, targetName, stream, streamId, | ||
chunk, reason, success }); |
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.
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.
done
I have a remark that is related to, but more general than this pull request. The unit tests are clear and help understand what the Streams API does (because I'm not familiar with it yet myself), but I think it would really help (since this is going to be one of the core components) to have some documentation about the terminology, such as "high watermark", "sink" and "queueing strategy". I think you have a really good reference with your write-up at https://docs.google.com/document/d/1yTPvPBhGkFn7XUntAt8wdVPckxHovYylNAIJOGnGRXc/edit and just extending that should already suffice. This is not something that needs to be done right away, but I just wanted to let you know that I think that it would help a lot with understanding the code. Other than that, this looks promising. Keep up the nice work! |
834e634
to
2d2ebd0
Compare
src/shared/util.js
Outdated
if (data.isReply) { | ||
var callbackId = data.callbackId; | ||
if (data.stream) { | ||
let sourceName = this.sourceName; |
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.
extract entire if (data.stream) into separate method e.g. '_processStreamMessage(data)'
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.
Okay, done in new commit.
src/shared/util.js
Outdated
@@ -1269,6 +1357,51 @@ function MessageHandler(sourceName, targetName, comObj) { | |||
error: reason | |||
}); | |||
}); | |||
} else if (data.streamId) { |
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.
extract entire if (data.streamId) into separate method e.g. '_createStreamSink(data)'
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.
Done, thanks for suggestions.
src/shared/util.js
Outdated
let sourceName = this.sourceName; | ||
let targetName = data.sourceName; | ||
let streamId = data.streamId; | ||
let sendStreamResponse = ({ stream, success }) => { |
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.
here and at sendStreamRequest add 'reason' parameter after success -- it shall be populated/used when success == false. reject on xxx_complete side shall accept this reason as argument.
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.
Done
src/shared/util.js
Outdated
success, streamId }); | ||
}; | ||
let deleteStreamController = () => { | ||
Promise.all([streamControllers[data.streamId].startCall, |
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.
nit: startCall on the new line
add .cancelCall promise here too
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.
Done
Yes you are right, I am also thinking about the same. Having documentation about the code and terminology will help to understand it better, even i can understand later what i have done :-D
Thank You :) |
I did not notice the specification yet, but it's surprisingly good to read and understand. It's a really helpful resource (that I see you already mentioned in your proposal) and I think extracting the most important terminology from it is a good idea. Thank you for considering that; I'll leave it for now (to not digress from the topic of this particular PR) and watch how to proposal progresses. |
2d2ebd0
to
c390064
Compare
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.
To improve readability and error handling
src/shared/util.js
Outdated
let callbacksCapabilities = this.callbacksCapabilities = Object.create(null); | ||
let ah = this.actionHandler = Object.create(null); | ||
|
||
let _processStreamMessage = (data) => { |
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.
looks good, but I wonder if we can move _processStreamMessage and _createStreamSink as a method of MessageHandler (we have this.comObj, this.streamSinks and this.streamControllers available to us)
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.
Done in new commit.
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 might be totally misunderstanding, but I'm interpreting #8430 (comment) as suggesting that they be moved into MessageHandler.prototype
instead (since the updated patch doesn't really change anything otherwise)!?
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 might be totally misunderstanding, but I'm interpreting #8430 (comment) as suggesting that they be moved into MessageHandler.prototype instead (since the updated patch doesn't really change anything otherwise)!?
I think i misunderstood #8439 (comment), sorry for that. I will move _processStreamMessage
and _createStreamSink
to MessageHandler.prototype
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 just thought earlier that maybe doing something like: let _createStreamSink = this._createStreamSink = () => {...}
will make _createStreamSink
a method of MessageHandler
.
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 will, but from performance point of view this methods without closure (in this case members of the prototype) will be more effective and easier to debug.
src/shared/util.js
Outdated
streamControllers[data.streamId].startCall.resolve(); | ||
} else { | ||
streamControllers[data.streamId].startCall.reject(); | ||
} |
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.
add utility function let resolveOrReject = (capability, success, reason) => { if (success) { capability.resolve(); ...
and use it at xxxx_complete branches
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.
Can we do this like: let resolveOrReject = ({ capability, success, reason }) => { if (success) { capability.resolve(); ...
, as i think we don't have reason for success == true
, and for consistency with sendStreamRequest
and sendStreamResponse
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's fine as 3 args, you can add undefined
for success == true or just don't supply it
src/shared/util.js
Outdated
streamSinks[data.streamId].desiredSize = data.desiredSize; | ||
let pullResult = streamSinks[data.streamId].onPull !== null ? | ||
streamSinks[data.streamId].onPull() : undefined; | ||
Promise.resolve(pullResult).then(() => { |
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.
add let resolveCall = (fn, args) => { if (!fn) return Promise.resolve(undefined); return new Promise( (resolve, reject) => { resolve(fn.apply(null, args)); }); }
and use it in pull, cancel and start cases.
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.
We require to call action[0]
with this = action[1]
in start case here. So maybe this function shall be let resolveCall = ({ fn, _this, args}) => { if (!fn) return Promise.resolve(undefined); return new Promise( (resolve, reject) => { resolve(fn.apply(_this, args)); }); }
instead.
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.
sure, you make make its signature (fn, args, thisArg)
and for pull/cancel call use thisArg == null.
src/shared/util.js
Outdated
break; | ||
case 'error': | ||
streamControllers[data.streamId].controller.error(data.reason); | ||
break; |
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.
call deleteStreamController when error
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.
Done
src/shared/util.js
Outdated
Promise.resolve(actionResult).then(() => { | ||
sendStreamRequest({ stream: 'start_complete', success: true }); | ||
}, () => { | ||
sendStreamRequest({ stream: 'start_complete', success: false }); |
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.
add reason here
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.
Done
src/shared/util.js
Outdated
let capability = createPromiseCapability(); | ||
let sendStreamRequest = ({ stream, chunk, success, reason }) => { | ||
comObj.postMessage({ sourceName, targetName, stream, streamId, | ||
chunk, reason, success }); |
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.
nit: switch 'success' and 'reason' places
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.
Done
src/shared/util.js
Outdated
// Delete streamController only when start, pull and | ||
// cancel callbacks are resolved, to avoid "TypeError". | ||
Promise.all([ | ||
streamControllers[data.streamId].startCall, |
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.
add let finalize = (promise) => { return Promise.resolve(promise).catch(() => {}); };
utility function and use it to wrap streamControllers[data.streamId].xxxCall here
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 i am not getting this utility function properly, can you please explain a bit.
We are going to use this function something like:
Promise.all([finalize(streamControllers[data.streamId].startCall), finalize(streamControllers[data.streamId].pullCall), finalize(streamControllers[data.streamId].cancelCall)]).then(() => {...})
?
src/shared/util.js
Outdated
this.ready = this.sinkCapability.promise; | ||
} | ||
sendStreamRequest({ stream: 'enqueue', chunk }); | ||
}, |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
src/shared/util.js
Outdated
close() { | ||
sendStreamRequest({ stream: 'close' }); | ||
delete that.streamSinks[streamId]; | ||
}, |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
src/shared/util.js
Outdated
}, | ||
error(reason) { | ||
sendStreamRequest({ stream: 'error', reason }); | ||
}, |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
_processStreamMessage(data) { | ||
let sourceName = this.sourceName; | ||
let targetName = data.sourceName; | ||
let streamId = data.streamId; |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
src/shared/util.js
Outdated
let sendStreamResponse = ({ stream, success, reason }) => { | ||
this.comObj.postMessage({ sourceName, targetName, stream, | ||
success, streamId, reason }); | ||
}; |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
src/shared/util.js
Outdated
} else { | ||
capability.reject(reason); | ||
} | ||
}; |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
src/shared/util.js
Outdated
}; | ||
let finalize = (promise) => { | ||
return Promise.resolve(promise).catch(() => {}); | ||
}; |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
src/shared/util.js
Outdated
this.comObj.postMessage({ sourceName, targetName, stream, | ||
success, streamId, reason }); | ||
}; | ||
let resolveOrReject = (capability, success, reason) => { |
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.
You can do function resolveOrReject(capability, success, reason) {
instead here.
Edit: Please note that if you're not accessing this
an arrow function isn't necessary!
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.
Done, thanks for the suggestion. I will take care of that.
src/shared/util.js
Outdated
capability.reject(reason); | ||
} | ||
}; | ||
let finalize = (promise) => { |
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.
You can do function finalize(promise) {
instead here.
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.
Done
onCancel: null, | ||
desiredSize, | ||
ready: null, | ||
}; |
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.
Nit: Adding a newline here would aid readability of the code.
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.
Done
dd64b94
to
8e811cd
Compare
src/shared/util.js
Outdated
}, queueingStrategy); | ||
}, | ||
|
||
resolveCall(fn, args, thisArg = null) { |
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.
resolveCall does not need to be a member of MessageHandler. Can you move it out of the class (e.g. just before MessageHandler)?
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.
resolveOrReject and finalize can go there too
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.
Done in new commit. Thanks.
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.
looks good (with resolveOrReject, finalize and resolveCall before MessageHandler constructor)
fc5b48a
to
ce08007
Compare
} | ||
} | ||
|
||
function finalize(promise) { |
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.
Unless I'm missing something, you're not actually passing Promise
s to this function (at line https://github.com/mozilla/pdf.js/pull/8430/files#diff-8d2b578d58d4d8c8a2975020fbbc84d9R1492 and below).
Rather, it seems that what you're passing is actually promiseCapabilities
, which means that this function currently doesn't really make sense to me; what am I missing here?
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.
Oh, yes. You are right. Thanks for pointing out.
src/shared/util.js
Outdated
// Delete streamController only when start, pull and | ||
// cancel callbacks are resolved, to avoid "TypeError". | ||
Promise.all([ | ||
finalize(this.streamControllers[data.streamId].startCall), |
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.
sorry. let's map all capabilities to promises Promise.all([this.streamControllers[data.streamId].startCall, this.streamControllers[data.streamId].pullCall,....].map((capability) => { return capability && finalize(capability.promise); }))
.
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.
Done in new commit. Thanks.
Adds functionality to accept Queueing Strategy in sendWithStream method. Using Queueing Strategy we can control the data that is enqueued into the sink, and hence regulated the flow of chunks from worker to main thread. Adds capability in pull and cancel methods. Adds ready and desiredSize property in streamSink. Adds unit test for ReadableStream and sendWithStream.
ce08007
to
bbd9968
Compare
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.
r=me, modulo passing tests.
/botio test |
From: Bot.io (Windows)ReceivedCommand cmd_test from @Snuffleupagus received. Current queue size: 0 Live output at: http://54.215.176.217:8877/b6e3d28eb33b99f/output.txt |
From: Bot.io (Linux m4)ReceivedCommand cmd_test from @Snuffleupagus received. Current queue size: 0 Live output at: http://54.67.70.0:8877/be28eabda275c01/output.txt |
From: Bot.io (Linux m4)SuccessFull output at http://54.67.70.0:8877/be28eabda275c01/output.txt Total script time: 17.14 mins
|
From: Bot.io (Windows)SuccessFull output at http://54.215.176.217:8877/b6e3d28eb33b99f/output.txt Total script time: 29.94 mins
|
Thank you for the patch. |
Adds sendWithStream method in MessageHandler.
This is second PR for the project Streams API in PDF.js.
This PR adds a method
sendWithStream
inMessageHandler
at shared/utils. We can use this method to pass data between main and worker thread using Streams API. This method returns a ReadableStream.The behaviour of stream is just like the standard ReadableStream Constructor.
This PR also contains tests for the functionalities of stream underlyingSource(pull, cancel) and streamSink(read data with blocking promise, error).