Skip to content

Commit

Permalink
feat: add requestMultiple method for supporting streaming pagination t…
Browse files Browse the repository at this point in the history
  • Loading branch information
jhillacre committed Jul 6, 2021
1 parent 67cdd14 commit f1a3dc7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
ISerializer,
IStreamingAPI,
ITransport,
RequestMultipleCancel,
RequestMultipleHandler,
SubscribeOptions,
SubscriptionHandler,
} from './interface';
Expand Down Expand Up @@ -298,6 +300,26 @@ class DCRFClient implements IStreamingAPI {
});
}

public requestMultiple(stream: string, payload: object, requestId: string = UUID.generate(), callback: RequestMultipleHandler): RequestMultipleCancel {
const selector = this.buildRequestResponseSelector(stream, requestId);

const listenerId = this.dispatcher.listen(selector, (data: typeof selector & { payload: { response_status: number, data: any } }) => {
const {payload: response} = data;
const responseStatus = response.response_status;

// 2xx is success
if (Math.floor(responseStatus / 100) === 2) {
callback(null, response.data);
} else {
callback(response, null);
}
});

return () => {
this.dispatcher.cancel(listenerId);
};
}

public send(object: object) {
return this.doSend(object, this.queue.send);
}
Expand Down
5 changes: 5 additions & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type DispatchListener<T> = (response: T) => any;
export
type SubscriptionHandler = (payload: {[prop: string]: any}, action: string) => any;

export
type RequestMultipleHandler = (error: {response_status: number, data: any} | null, payload: {[prop: string]: any} | null) => any;

export
type RequestMultipleCancel = () => void;

/**
* Calls all handlers whose selectors match an incoming payload.
Expand Down

0 comments on commit f1a3dc7

Please sign in to comment.