@@ -7,7 +7,7 @@ import { NewSubscriptionRequest } from '../components/subscription-list/new-subs
77 providedIn : 'root'
88} )
99export class PubsubService {
10- public currentHost = "http://localhost:8681"
10+ public _currentHost$ = new BehaviorSubject < string > ( "http://localhost:8681" )
1111
1212 private _projectList = new BehaviorSubject < string [ ] > ( [ ] )
1313 private _currentProject = new ReplaySubject < string > ( )
@@ -21,12 +21,27 @@ export class PubsubService {
2121 public currentSubscription$ = this . _currentSubscription . asObservable ( )
2222
2323 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 )
2433
2534 this . currentProject$ . subscribe ( project =>
2635 this . topicList$ = this . listTopics ( project )
2736 )
2837 }
2938
39+ setHost ( hostUrl : string ) {
40+ this . _currentHost$ . next ( hostUrl )
41+
42+ localStorage . setItem ( "host" , hostUrl )
43+ }
44+
3045 selectProject ( projectId : string ) {
3146 this . _currentProject . next ( projectId )
3247 }
@@ -36,31 +51,34 @@ export class PubsubService {
3651 newList . push ( newProject )
3752
3853 this . _projectList . next ( newList )
54+
55+ const jsonList = JSON . stringify ( newList )
56+ localStorage . setItem ( "projects" , jsonList )
3957 }
4058
4159 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 } `
4361
4462 return this . http . put < Topic > ( url , { } )
4563 }
4664
4765 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 || [ ] ) )
4967 }
5068
5169 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 } `
5371
5472 return this . http . put < Subscription > ( url , { topic : request . topic , pushConfig : request . pushConfig } )
5573 }
5674
5775 deleteSubscription ( subscriptionPath : string ) {
58- const url = `${ this . currentHost } /v1/${ subscriptionPath } `
76+ const url = `${ this . _currentHost$ . value } /v1/${ subscriptionPath } `
5977 return this . http . delete ( url )
6078 }
6179
6280 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` )
6482 . pipe (
6583 map ( incoming => incoming . subscriptions ) , // first we pull out the subscriptions object
6684 map ( subNames => subNames ?? [ ] ) ,
@@ -70,7 +88,7 @@ export class PubsubService {
7088
7189 listSubscriptionsOnTopic ( topicPath : string ) : Observable < Subscription [ ] > {
7290 console . log ( 'looking up subscriptions on' , topicPath )
73- const url = `${ this . currentHost } /v1/${ topicPath } /subscriptions`
91+ const url = `${ this . _currentHost$ . value } /v1/${ topicPath } /subscriptions`
7492 console . log ( 'request url' , url )
7593 return this . http . get < { subscriptions ?: string [ ] } > ( url )
7694 . pipe (
@@ -81,25 +99,25 @@ export class PubsubService {
8199 }
82100
83101 getSubscriptionDetails ( subscriptionPath : string ) {
84- const url = `${ this . currentHost } /v1/${ subscriptionPath } `
102+ const url = `${ this . _currentHost$ . value } /v1/${ subscriptionPath } `
85103 return this . http . get < Subscription > ( url )
86104 }
87105
88106 fetchMessages ( subPath : string , maxMessages : number ) {
89107 return this . http
90108 . post < { receivedMessages : ReceivedMessage [ ] } > (
91- `${ this . currentHost } /v1/${ subPath } :pull` ,
109+ `${ this . _currentHost$ . value } /v1/${ subPath } :pull` ,
92110 { returnImmediately : true , maxMessages }
93111 ) . pipe ( map ( incoming => incoming . receivedMessages ?? [ ] ) )
94112 }
95113
96114 ackMessage ( subscriptionPath :string , ackIds : string [ ] ) {
97- const url = `${ this . currentHost } /v1/${ subscriptionPath } :acknowledge`
115+ const url = `${ this . _currentHost$ . value } /v1/${ subscriptionPath } :acknowledge`
98116 return this . http . post ( url , { ackIds} )
99117 }
100118
101119 publishMessages ( topicPath : string , messages : PubsubMessage [ ] ) {
102- const url = `${ this . currentHost } /v1/${ topicPath } :publish`
120+ const url = `${ this . _currentHost$ . value } /v1/${ topicPath } :publish`
103121 return this . http . post < { messageIds : string [ ] } > ( url , { messages} )
104122 }
105123}
0 commit comments