Skip to content
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

Streaming pagination support (streamingRequest) #31

Merged

Conversation

jhillacre
Copy link
Contributor

This PR adds a method to DCRFClient, allowing for a single request to listening for responses until the returned cancel function is called. The intention of creating this method is to support streaming pagination from djangochannelsrestframework, as described in #22.

@jhillacre jhillacre marked this pull request as ready for review July 7, 2021 15:39
Copy link
Owner

@theY4Kman theY4Kman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, thank you for stepping up once again; I appreciate the help :)

I see where you're going with this, and it seems like a solid foundation for implementing all sorts of streaming — not exclusively pagination. It's a great farkin idea, and I hadn't thought of implementing pagination like this. Hell yeah, brother!

As per usual, I care a lot about the naming, ramble a bit about past decisions I made, but I am a wee bit proud about pondering whether the canceler should return a Promise. I feel like that might be a good call :P

src/interface.ts Outdated
type RequestMultipleHandler = (error: {response_status: number, data: any} | null, payload: {[prop: string]: any} | null) => any;

export
type RequestMultipleCancel = () => void;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd pair better with Handler to have this be RequestMultipleCanceler. And I suppose the object being described is not technically a "cancel", but a thing that causes cancellation, so a "canceler".

Also, in learning from previous mistakes, it may be wise to have this return a Promise which, in the future, could have the ability to send a "stop" request to the server before resolving. It definitely makes sense to immediately (i.e. not on the next tick) remove the listener, though, to guarantee that once cancel() is called, callback will not be invoked again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a StreamingRequestPromise to replace StreamingRequestCanceler as the return type for DCRFClient#streamingRequest in 88d1b53.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In b90ec59, I'm being more certain that callback is not called after cancel;

src/interface.ts Outdated
Comment on lines 14 to 19
export
type RequestMultipleHandler = (error: {response_status: number, data: any} | null, payload: {[prop: string]: any} | null) => any;

export
type RequestMultipleCancel = () => void;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, actually, do you think streamingRequest makes more sense than requestMultiple? I'm concerned about the ambiguity of requestMultiple — like, does it make multiple requests? Is it requesting multiple objects? Or is it what it actually does: make a single request for multiple responses?

Perhaps DCRFClient.streamingRequest() with StreamingRequestHandler and StreamingRequestCanceler? (I was toying with DCRFClient.requestStreaming(), but I'm leaning toward streamingRequest.) What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this name change in 635369f.

test/test.ts Outdated
Comment on lines 254 to 256
const msg = transport.send.getCall(0).args[0];
const stream = msg.stream;
const requestId = msg.payload.request_id;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has nothing to do with this PR; I'm just now realizing how I could've done all of these three lines much more succinctly with a single destructuring assignment. This implementation I went with is so... Pythonic :P

If I wrote these tests again, I'd do it like this:

const [{stream, payload: {request_id: requestId}}] = transport.send.firstCall.args;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Made this refactor in 635369f.

src/interface.ts Outdated
@@ -406,6 +411,8 @@ interface IStreamingAPI {
* On failure, the promise will be rejected with the entire API response.
*/
request(stream: string, payload: object, requestId?: string): Promise<object>;

requestMultiple(stream: string, payload: object, callback:RequestMultipleHandler, requestId?: string): RequestMultipleCancel;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spacing

Suggested change
requestMultiple(stream: string, payload: object, callback:RequestMultipleHandler, requestId?: string): RequestMultipleCancel;
requestMultiple(stream: string, payload: object, callback: RequestMultipleHandler, requestId?: string): RequestMultipleCancel;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to see some JSDocs for the method before this gets released, but that can wait.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some JSDocs for this in 635369f.

@jhillacre jhillacre changed the title Streaming pagination support (requestMultiple) Streaming pagination support (streamingRequest) Jul 8, 2021
@jhillacre jhillacre requested a review from theY4Kman July 8, 2021 16:28
@jhillacre
Copy link
Contributor Author

Made those requested changes and responded to the individual comments, but not sure if that triggers a GitHub notification. (So here is a top-level comment to trigger a notification in case one wasn't generated.)

Copy link
Owner

@theY4Kman theY4Kman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

Hey, so with this being the second looooooong time-to-merge, and you actually using and developing new features, would you have interest in being a maintainer — as in, having the ability to push to master and release on npm?

@theY4Kman theY4Kman merged commit 8aee9f6 into theY4Kman:master Sep 24, 2021
@jhillacre
Copy link
Contributor Author

would you have interest in being a maintainer — as in, having the ability to push to master and release on npm?

My company ended up delivering the projects we were using dcrf on, so for now we are not wanting to take up a maintainer role.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants