Skip to content

Commit

Permalink
feat(rpc/client): takes in batch responses
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Nov 1, 2019
1 parent 00a7bc7 commit 95ccfff
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions packages/rpc/src/client/ClientManager/ClientManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,7 @@ export class ClientManager {
}
}
private response(data: any): void {
try {
if (!data || typeof data !== 'object' || Array.isArray(data)) {
return this.report(
Error(`Unexpected response: ${JSON.stringify(data)}`)
);
}

const each = (data: any): void => {
// Notification
if (!Object.hasOwnProperty.call(data, 'id')) {
const notification: RPCNotification = data;
Expand Down Expand Up @@ -205,8 +199,32 @@ export class ClientManager {
}

this.report(Error(`Invalid response: ${JSON.stringify(response)}`));
};

try {
if (!data || typeof data !== 'object') {
return this.report(
Error(`Unexpected response: ${JSON.stringify(data)}`)
);
}
if (!Array.isArray(data)) {
return each(data);
}
} catch (err) {
this.report(err);
return this.report(err);
}

for (const item of data) {
try {
if (!item || typeof item !== 'object' || Array.isArray(item)) {
return this.report(
Error(`Unexpected response: ${JSON.stringify(item)}`)
);
}
each(item);
} catch (err) {
this.report(err);
}
}
}
private success(
Expand Down

0 comments on commit 95ccfff

Please sign in to comment.