Exploring the realms of Angular 2, TypeScript and RxJS is a thrilling journey for me as I delve into creating a basic application that makes use of the Salesforce Ajax Toolkit connections library.
In my quest, I am faced with the challenge of tackling token expiration whenever a method from the connections library is invoked. To address this, I have crafted a service that encapsulates the connections library by employing observables. A prime example can be seen in my customized wrapper function for the insert operation:
public insert(object: sforce.SObject): Observable<any> {
return new Observable(observer => {
// record insertion successful
let insertSuccess = (result) => {
observer.next(result);
observer.complete();
}
// error encountered during insertion
let insertError = (result) => {
// This part needs refinement
if (result.faultcode.indexOf('INVALID_SESSION_ID') != -1) {
this.refreshToken();
}
else {
observer.error(result);
}
}
let callback = { onSuccess: insertSuccess, onFailure: insertError };
sforce.connection.create([object], callback);
});
}
Another vital function in my arsenal is one that refreshes the access token:
public refreshToken(): void {
this.loginService.login().subscribe(
response => {
Globals.SESSION_TOKEN = response.access_token;
//initialize the salesforce connection
this.init(Globals.SESSION_TOKEN, this.loginService.AuthParams.SOAP_URL);
},
error => {
}
);
}
The crux of my endeavor lies in ensuring that the original insert
function waits for the completion of refreshToken
. Upon success, I aim to retry the initial insert operation, failing which I intend for the original insert observable to trigger observer.error
.
Though I've delved into retry
and retryWhen
, I find myself grappling with how to orchestrate them effectively to synchronize with the execution of the refreshToken()
function. Any insights or suggestions on navigating this challenge would be immensely valued. Many thanks in advance.