@@ -7,7 +7,7 @@ import { NewSubscriptionRequest } from '../components/subscription-list/new-subs
7
7
providedIn : 'root'
8
8
} )
9
9
export class PubsubService {
10
- public currentHost = "http://localhost:8681"
10
+ public _currentHost$ = new BehaviorSubject < string > ( "http://localhost:8681" )
11
11
12
12
private _projectList = new BehaviorSubject < string [ ] > ( [ ] )
13
13
private _currentProject = new ReplaySubject < string > ( )
@@ -21,12 +21,27 @@ export class PubsubService {
21
21
public currentSubscription$ = this . _currentSubscription . asObservable ( )
22
22
23
23
constructor ( private http : HttpClient ) {
24
+ const prevHost = localStorage . getItem ( "host" )
25
+ if ( prevHost ) {
26
+ console . log ( 'loaded previous host' , prevHost )
27
+ this . _currentHost$ . next ( prevHost )
28
+ }
29
+
30
+ const prevProjects = localStorage . getItem ( "projects" ) ?? "[]"
31
+ const projects : string [ ] = JSON . parse ( prevProjects ) ?? [ ]
32
+ this . _projectList . next ( projects )
24
33
25
34
this . currentProject$ . subscribe ( project =>
26
35
this . topicList$ = this . listTopics ( project )
27
36
)
28
37
}
29
38
39
+ setHost ( hostUrl : string ) {
40
+ this . _currentHost$ . next ( hostUrl )
41
+
42
+ localStorage . setItem ( "host" , hostUrl )
43
+ }
44
+
30
45
selectProject ( projectId : string ) {
31
46
this . _currentProject . next ( projectId )
32
47
}
@@ -36,31 +51,34 @@ export class PubsubService {
36
51
newList . push ( newProject )
37
52
38
53
this . _projectList . next ( newList )
54
+
55
+ const jsonList = JSON . stringify ( newList )
56
+ localStorage . setItem ( "projects" , jsonList )
39
57
}
40
58
41
59
createTopic ( projectId : string , topicId : string ) {
42
- const url = `${ this . currentHost } /v1/projects/${ projectId } /topics/${ topicId } `
60
+ const url = `${ this . _currentHost$ . value } /v1/projects/${ projectId } /topics/${ topicId } `
43
61
44
62
return this . http . put < Topic > ( url , { } )
45
63
}
46
64
47
65
listTopics ( projectId : string ) {
48
- return this . http . get < { topics : Topic [ ] } > ( `${ this . currentHost } /v1/projects/${ projectId } /topics` ) . pipe ( map ( incoming => incoming ?. topics || [ ] ) )
66
+ return this . http . get < { topics : Topic [ ] } > ( `${ this . _currentHost$ . value } /v1/projects/${ projectId } /topics` ) . pipe ( map ( incoming => incoming ?. topics || [ ] ) )
49
67
}
50
68
51
69
createSubscription ( projectId : string , request : NewSubscriptionRequest ) {
52
- const url = `${ this . currentHost } /v1/projects/${ projectId } /subscriptions/${ request . name } `
70
+ const url = `${ this . _currentHost$ . value } /v1/projects/${ projectId } /subscriptions/${ request . name } `
53
71
54
72
return this . http . put < Subscription > ( url , { topic : request . topic , pushConfig : request . pushConfig } )
55
73
}
56
74
57
75
deleteSubscription ( subscriptionPath : string ) {
58
- const url = `${ this . currentHost } /v1/${ subscriptionPath } `
76
+ const url = `${ this . _currentHost$ . value } /v1/${ subscriptionPath } `
59
77
return this . http . delete ( url )
60
78
}
61
79
62
80
listSubscriptions ( projectId : string ) : Observable < Subscription [ ] > {
63
- return this . http . get < { subscriptions ?: string [ ] } > ( `${ this . currentHost } /v1/projects/${ projectId } /subscriptions` )
81
+ return this . http . get < { subscriptions ?: string [ ] } > ( `${ this . _currentHost$ . value } /v1/projects/${ projectId } /subscriptions` )
64
82
. pipe (
65
83
map ( incoming => incoming . subscriptions ) , // first we pull out the subscriptions object
66
84
map ( subNames => subNames ?? [ ] ) ,
@@ -70,7 +88,7 @@ export class PubsubService {
70
88
71
89
listSubscriptionsOnTopic ( topicPath : string ) : Observable < Subscription [ ] > {
72
90
console . log ( 'looking up subscriptions on' , topicPath )
73
- const url = `${ this . currentHost } /v1/${ topicPath } /subscriptions`
91
+ const url = `${ this . _currentHost$ . value } /v1/${ topicPath } /subscriptions`
74
92
console . log ( 'request url' , url )
75
93
return this . http . get < { subscriptions ?: string [ ] } > ( url )
76
94
. pipe (
@@ -81,25 +99,25 @@ export class PubsubService {
81
99
}
82
100
83
101
getSubscriptionDetails ( subscriptionPath : string ) {
84
- const url = `${ this . currentHost } /v1/${ subscriptionPath } `
102
+ const url = `${ this . _currentHost$ . value } /v1/${ subscriptionPath } `
85
103
return this . http . get < Subscription > ( url )
86
104
}
87
105
88
106
fetchMessages ( subPath : string , maxMessages : number ) {
89
107
return this . http
90
108
. post < { receivedMessages : ReceivedMessage [ ] } > (
91
- `${ this . currentHost } /v1/${ subPath } :pull` ,
109
+ `${ this . _currentHost$ . value } /v1/${ subPath } :pull` ,
92
110
{ returnImmediately : true , maxMessages }
93
111
) . pipe ( map ( incoming => incoming . receivedMessages ?? [ ] ) )
94
112
}
95
113
96
114
ackMessage ( subscriptionPath :string , ackIds : string [ ] ) {
97
- const url = `${ this . currentHost } /v1/${ subscriptionPath } :acknowledge`
115
+ const url = `${ this . _currentHost$ . value } /v1/${ subscriptionPath } :acknowledge`
98
116
return this . http . post ( url , { ackIds} )
99
117
}
100
118
101
119
publishMessages ( topicPath : string , messages : PubsubMessage [ ] ) {
102
- const url = `${ this . currentHost } /v1/${ topicPath } :publish`
120
+ const url = `${ this . _currentHost$ . value } /v1/${ topicPath } :publish`
103
121
return this . http . post < { messageIds : string [ ] } > ( url , { messages} )
104
122
}
105
123
}
0 commit comments