Skip to content

Commit bf65607

Browse files
assafkmMohaban, Assaf
andauthored
Dev rxrpc js http transport merge (#18)
* Adding Http transport * fixing unnecessary rebuilds * fix polling empty-string bug, and change polling period to 1sec * getting the data as JSON instead of string * handling subscribtion in post function Co-authored-by: Mohaban, Assaf <[email protected]>
1 parent a8e990a commit bf65607

File tree

4 files changed

+257
-450
lines changed

4 files changed

+257
-450
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@ task deleteBuildFiles(type: Delete) {
1212
delete "$projectDir/dist", "$project.buildDir"
1313
}
1414

15+
yarn {
16+
it.args.addAll provider {["--network-timeout", "600000"]}
17+
}
18+
1519
clean.dependsOn(deleteBuildFiles)

src/lib/rxrpc-http-transport.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('RxRpc Http Transport test suite', function () {
1515
let resp: {}
1616

1717
beforeEach(() => {
18-
transport = new RxRpcHttpTransport("http://funnyName/");
18+
transport = new RxRpcHttpTransport("https://funnyName/");
1919
clientId = "12345678";
2020
incomingMessages = [];
2121
resp = {
@@ -31,10 +31,10 @@ describe('RxRpc Http Transport test suite', function () {
3131
expect(incomingMessages[0]).toEqual(clientId)
3232
})
3333

34-
it('Poll with data as a string', async () => {
34+
it('Poll multiple messages with data as JSON', async () => {
3535
const data1 = "{\"invocationId\":1,\"result\":{\"type\":\"Data\",\"data\":\"Hello, Angular #0\",\"error\":null}}"
3636
const data2 = "{\"invocationId\":1,\"result\":{\"type\":\"Data\",\"data\":\"Hello, Angular #1\",\"error\":null}}"
37-
resp['data'] = `${data1}\n${data2}`
37+
resp['data'] = JSON.parse(`[${data1},\n${data2}]`)
3838
mockedAxios.post.mockImplementation(() => Promise.resolve(resp));
3939

4040
transport.connect().subscribe(connection => connection.poll().subscribe(msg => {
@@ -46,9 +46,9 @@ describe('RxRpc Http Transport test suite', function () {
4646
expect(incomingMessages[1]).toEqual(JSON.parse(data2));
4747
})
4848

49-
it('Poll with data as an object', async () => {
49+
it('Poll with data as JSON', async () => {
5050
const data = "{\"invocationId\":1,\"result\":{\"type\":\"Data\",\"data\":\"Hello, Angular #0\",\"error\":null}}"
51-
resp['data'] = JSON.parse(data)
51+
resp['data'] = JSON.parse(`[${data}]`)
5252
mockedAxios.post.mockImplementation(() => Promise.resolve(resp));
5353

5454
transport.connect().subscribe(connection => connection.poll().subscribe(msg => {

src/lib/rxrpc-http-transport.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {RxRpcConnection, RxRpcTransport} from './rxrpc-transport';
22
import {interval, Observable, of, Subject, Subscription} from 'rxjs';
3-
import {filter, map, mergeMap, retry, tap} from "rxjs/operators";
3+
import {mergeMap, map, retry, filter, tap} from "rxjs/operators";
44
import {HttpAttributes} from "./rxrpc-http-attributes";
55
import axios, {AxiosResponse} from 'axios';
66
import {fromPromise} from "rxjs/internal-compatibility";
@@ -26,7 +26,7 @@ export class RxRpcHttpConnection implements RxRpcConnection {
2626
retry(options.pollingRetryCount))
2727
.subscribe(
2828
() => {},
29-
err => this.incoming.error(err),
29+
() => {},
3030
() => this.incoming.complete());
3131
}
3232

@@ -43,23 +43,23 @@ export class RxRpcHttpConnection implements RxRpcConnection {
4343
}
4444

4545
poll(): Observable<any> {
46-
return this.post('polling')
47-
.pipe(
48-
map(resp => resp.data),
49-
filter(data => data !== ""),
50-
mergeMap(data => {
51-
if(typeof data === 'string') {
52-
return fromArray(data.split("\n").map(s => JSON.parse(s)));
53-
}
54-
return of(data);
55-
}),
56-
tap(obj => this.incoming.next(obj)));
46+
return this.post('polling');
5747
}
5848

59-
post(path: string, msg?: any): Observable<AxiosResponse<string>> {
49+
post(path: string, msg?: any): Observable<any> {
6050
const headers = {};
6151
headers[HttpAttributes.ClientIdAttribute] = this.clientId;
6252
return fromPromise(axios.post<string>(`${this.uri}/${path}`, msg, {headers: headers}))
53+
.pipe(
54+
map(resp => resp.data),
55+
mergeMap(data => {
56+
return fromArray(data);
57+
}),
58+
tap(
59+
obj => this.incoming.next(obj),
60+
err => this.incoming.error(err)
61+
)
62+
);
6363
}
6464
}
6565

0 commit comments

Comments
 (0)