1
1
// ------------------------------------------------------------------------------
2
2
// <auto-generated>
3
- // This code was generated with KY.Generator 6.3.0.0
3
+ // This code was generated with KY.Generator 6.4.0.0
4
+ //
4
5
// Manual changes to this file may cause unexpected behavior in your application.
5
6
// Manual changes to this file will be overwritten if the code is regenerated.
6
7
// </auto-generated>
@@ -12,10 +13,11 @@ import { WeatherForecast } from "../models/weather-forecast";
12
13
import { Injectable } from "@angular/core" ;
13
14
import { HubConnection } from "@microsoft/signalr" ;
14
15
import { HubConnectionBuilder } from "@microsoft/signalr" ;
15
- import { HubConnectionState } from "@microsoft/signalr" ;
16
16
import { Observable } from "rxjs" ;
17
- import { of } from "rxjs" ;
18
- import { flatMap } from "rxjs/operators" ;
17
+ import { filter } from "rxjs/operators" ;
18
+ import { map } from "rxjs/operators" ;
19
+ import { mergeMap } from "rxjs/operators" ;
20
+ import { take } from "rxjs/operators" ;
19
21
import { ReplaySubject } from "rxjs" ;
20
22
import { Subject } from "rxjs" ;
21
23
@@ -24,7 +26,7 @@ import { Subject } from "rxjs";
24
26
} )
25
27
export class WeatherHubService {
26
28
public serviceUrl : string = "" ;
27
- private connection : HubConnection ;
29
+ private connection : ReplaySubject < HubConnection > ;
28
30
private readonly timeouts : number [ ] = [ 0 , 0 , 1000 , 2000 , 5000 ] ;
29
31
private readonly statusSubject : ReplaySubject < ConnectionStatus > = new ReplaySubject < ConnectionStatus > ( 1 ) ;
30
32
public readonly status$ : Observable < ConnectionStatus > = this . statusSubject . asObservable ( ) ;
@@ -35,40 +37,44 @@ export class WeatherHubService {
35
37
if ( ! this . serviceUrl ) {
36
38
throw new Error ( "serviceUrl can not be empty. Set it via service.serviceUrl." )
37
39
}
38
- if ( ! this . connection ) {
39
- this . connection = new HubConnectionBuilder ( ) . withUrl ( this . serviceUrl ) . build ( ) ;
40
- this . connection . onclose ( ( ) => {
41
- this . statusSubject . next ( ConnectionStatus . connecting ) ;
42
- this . connect ( )
43
- } ) ;
44
- this . connection . on ( "Updated" , ( forecast : WeatherForecast [ ] ) => {
45
- this . updatedSubject . next ( forecast ) ;
46
- } ) ;
47
- }
48
- if ( this . connection . state === HubConnectionState . Connected ) {
49
- return of ( undefined ) ;
40
+ if ( this . connection ) {
41
+ return this . status$ . pipe ( filter ( ( status ) => status === ConnectionStatus . connected ) , take ( 1 ) , map ( ( ) => {
42
+ } ) ) ;
50
43
}
51
- this . statusSubject . next ( ConnectionStatus . connecting ) ;
52
- let subject = new Subject < void > ( ) ;
53
- this . connection . start ( ) . then ( ( ) => {
54
- subject . next ( ) ;
55
- subject . complete ( ) ;
56
- this . statusSubject . next ( ConnectionStatus . connected ) ;
57
- } ) . catch ( ( error ) => {
58
- this . statusSubject . next ( ConnectionStatus . disconnected ) ;
59
- let timeout : number = this . timeouts [ trial ] ;
60
- timeout = timeout || this . timeouts [ this . timeouts . length - 1 ] || 0 ;
61
- setTimeout ( ( ) => this . connect ( trial + 1 ) . subscribe ( ( ) => {
44
+ this . connection = new ReplaySubject < HubConnection > ( 1 ) ;
45
+ let hubConnection : HubConnection = new HubConnectionBuilder ( ) . withUrl ( this . serviceUrl ) . build ( ) ;
46
+ let startConnection : ( ) => Observable < void > = ( ) => {
47
+ this . statusSubject . next ( ConnectionStatus . connecting ) ;
48
+ let subject = new Subject < void > ( ) ;
49
+ hubConnection . start ( ) . then ( ( ) => {
62
50
subject . next ( ) ;
63
51
subject . complete ( ) ;
64
- } , ( innerError ) => subject . error ( innerError ) ) , timeout ) ;
52
+ this . statusSubject . next ( ConnectionStatus . connected ) ;
53
+ } ) . catch ( ( error ) => {
54
+ this . statusSubject . next ( ConnectionStatus . disconnected ) ;
55
+ let timeout : number = this . timeouts [ trial ] ;
56
+ trial ++ ;
57
+ timeout = timeout || this . timeouts [ this . timeouts . length - 1 ] || 0 ;
58
+ setTimeout ( ( ) => startConnection ( ) . subscribe ( ( ) => {
59
+ subject . next ( ) ;
60
+ subject . complete ( ) ;
61
+ } , ( innerError ) => subject . error ( innerError ) ) , timeout ) ;
62
+ } ) ;
63
+ return subject ;
64
+ } ;
65
+ hubConnection . on ( "Updated" , ( forecast : WeatherForecast [ ] ) => {
66
+ this . updatedSubject . next ( forecast ) ;
65
67
} ) ;
66
- return subject ;
68
+ hubConnection . onclose ( ( ) => {
69
+ startConnection ( ) ;
70
+ } ) ;
71
+ this . connection . next ( hubConnection ) ;
72
+ return startConnection ( ) ;
67
73
}
68
74
69
75
public fetch ( ) : Observable < void > {
70
76
let subject = new Subject < void > ( ) ;
71
- this . connect ( ) . pipe ( flatMap ( ( ) => this . connection . send ( "Fetch" ) ) ) . subscribe ( ( ) => {
77
+ this . connect ( ) . pipe ( mergeMap ( ( ) => this . connection ) , take ( 1 ) , mergeMap ( ( connection ) => connection . send ( "Fetch" ) ) ) . subscribe ( ( ) => {
72
78
subject . next ( ) ;
73
79
subject . complete ( ) ;
74
80
} , ( error ) => subject . error ( error ) ) ;
0 commit comments