@@ -22,6 +22,7 @@ import {
22
22
PluginLogMessageDTO
23
23
} from "chatoverflow-api" ;
24
24
import { CryptoService } from "../../../crypto.service" ;
25
+ import { interval } from "rxjs" ;
25
26
26
27
@Component ( {
27
28
selector : 'better-repl' ,
@@ -36,6 +37,7 @@ export class BetterREPLComponent extends UpgradableComponent {
36
37
private lastRequestSuccessful = true ;
37
38
38
39
private authKey = "" ;
40
+ private lastPassword = "" ;
39
41
40
42
private connectorTypes : Array < string > ;
41
43
private requirementTypes : RequirementTypes ;
@@ -63,21 +65,66 @@ export class BetterREPLComponent extends UpgradableComponent {
63
65
private changeReqTypeValue = "" ;
64
66
private changeReqValueValue = "" ;
65
67
68
+ private pingpongCounter = interval ( 5000 ) ;
69
+ private pingpongStarted = false ;
70
+ private pingPongSubscriber = null ;
71
+
66
72
constructor ( private configService : ConfigService , private typeService : TypeService ,
67
73
private connectorService : ConnectorService , private instanceService : InstanceService ,
68
74
private cryptoService : CryptoService ) {
69
75
super ( ) ;
70
76
}
71
77
78
+ reloadEverything ( clearForms : boolean ) {
79
+ if ( clearForms ) {
80
+ this . authKey = "" ;
81
+ this . instanceLogOutput = [ ] ;
82
+ this . instanceRequirements = [ ] ;
83
+
84
+ this . mcSourceIdentifierValue = "" ;
85
+ this . mcConnectorTypeValue = "" ;
86
+ this . mcrSourceIdentifierValue = "" ;
87
+ this . mcrConnectorTypeValue = "" ;
88
+
89
+ this . instanceNameSSValue = "" ;
90
+ this . miPluginNameValue = "" ;
91
+ this . miPluginAuthorValue = "" ;
92
+ this . miInstanceNameValue = "" ;
93
+ this . requirementsInstanceNameValue = "" ;
94
+
95
+ this . changeReqInstanceNameValue = "" ;
96
+ this . changeReqIDValue = "" ;
97
+ this . changeReqTypeValue = "" ;
98
+ this . changeReqValueValue = "" ;
99
+
100
+ this . connectorTypes = [ ] ;
101
+ this . requirementTypes = null ;
102
+ this . pluginTypes = [ ] ;
103
+ this . pluginInstances = [ ] ;
104
+ this . connectorKeys = [ ] ;
105
+
106
+ } else {
107
+
108
+ this . requestTypes ( ) ;
109
+ this . getRegisteredConnectors ( ) ;
110
+ this . getInstances ( ) ;
111
+ }
112
+
113
+ }
114
+
72
115
requestTypes ( ) {
116
+ this . connectorTypes = [ ] ;
117
+ this . requirementTypes = null ;
118
+ this . pluginTypes = [ ] ;
119
+
73
120
this . typeService . getConnectorType ( this . authKey ) . subscribe ( ( response : Array < string > ) => {
74
121
this . logRequest ( "getConnectorType" , true , JSON . stringify ( response ) ) ;
75
122
this . connectorTypes = response ;
76
123
} , error => this . logGenericError ( "getConnectorType" ) ) ;
77
124
78
125
this . typeService . getRequirementType ( this . authKey ) . subscribe ( ( response : RequirementTypes ) => {
79
126
this . logRequest ( "getRequirementType" , true , JSON . stringify ( response ) ) ;
80
- this . requirementTypes = response
127
+ this . requirementTypes = response ;
81
128
} , error => this . logGenericError ( "getRequirementType" ) ) ;
82
129
83
130
this . typeService . getPlugin ( this . authKey ) . subscribe ( ( response : Array < PluginType > ) => {
@@ -105,19 +152,61 @@ export class BetterREPLComponent extends UpgradableComponent {
105
152
this . logResultMessage ( "postLogin" , response ) ;
106
153
if ( response . success ) {
107
154
this . authKey = response . message ;
108
- this . requestTypes ( ) ;
155
+ this . lastPassword = password ;
156
+ }
157
+ this . reloadEverything ( ! response . success ) ;
158
+ this . handlePingpong ( true ) ;
159
+ } , error => {
160
+ this . logGenericError ( "postLogin" ) ;
161
+ this . reloadEverything ( true ) ;
162
+ } ) ;
163
+ }
164
+
165
+ handlePingpong ( start : boolean ) {
166
+ if ( start ) {
167
+ if ( ! this . pingpongStarted ) {
168
+ this . pingPongSubscriber = this . pingpongCounter . subscribe ( n => this . pingpong ( ) ) ;
169
+ this . pingpongStarted = true ;
170
+ }
171
+ } else {
172
+ if ( this . pingPongSubscriber != null ) {
173
+ this . pingPongSubscriber . unsubscribe ( ) ;
174
+ }
175
+ this . pingpongStarted = false ;
176
+ }
177
+ }
178
+
179
+ pingpong ( ) {
180
+ this . configService . postPing ( this . authKey ) . subscribe ( ( response : ResultMessage ) => {
181
+ if ( response . success ) {
182
+ // Pong
183
+ } else {
184
+ this . reloadEverything ( true ) ;
185
+ }
186
+ } , error => {
187
+ if ( error . status == 401 || error . status == 400 ) {
188
+ // Try to login again
189
+ this . login ( this . lastPassword ) ;
190
+
191
+ } else if ( error . status == 0 ) {
192
+ this . reloadEverything ( true ) ;
109
193
}
110
- } , error => this . logGenericError ( "postLogin" ) ) ;
194
+ } )
111
195
}
112
196
113
197
register ( password : string ) {
114
198
this . configService . postRegister ( { password : password } ) . subscribe ( ( response : ResultMessage ) => {
115
199
this . logResultMessage ( "postRegister" , response ) ;
116
200
if ( response . success ) {
117
201
this . authKey = response . message ;
118
- this . requestTypes ( ) ;
202
+ this . reloadEverything ( false ) ;
119
203
}
120
- } , error => this . logGenericError ( "postRegister" ) ) ;
204
+ this . reloadEverything ( ! response . success ) ;
205
+ this . handlePingpong ( true ) ;
206
+ } , error => {
207
+ this . logGenericError ( "postRegister" ) ;
208
+ this . reloadEverything ( true ) ;
209
+ } ) ;
121
210
}
122
211
123
212
getRequirementImpl ( apiType : string ) {
@@ -133,6 +222,7 @@ export class BetterREPLComponent extends UpgradableComponent {
133
222
}
134
223
135
224
getRegisteredConnectors ( ) {
225
+ this . connectorKeys = [ ] ;
136
226
this . connectorService . getConnectors ( this . authKey ) . subscribe ( ( response : Array < ConnectorKey > ) => {
137
227
this . logRequest ( "getConnectors" , true , JSON . stringify ( response ) ) ;
138
228
this . connectorKeys = response ;
@@ -213,6 +303,7 @@ export class BetterREPLComponent extends UpgradableComponent {
213
303
}
214
304
215
305
getInstances ( ) {
306
+ this . pluginInstances = [ ] ;
216
307
this . instanceService . getInstances ( this . authKey ) . subscribe ( ( response : Array < PluginInstance > ) => {
217
308
this . pluginInstances = response ;
218
309
this . logRequest ( "getInstances" , true , JSON . stringify ( response ) ) ;
0 commit comments