-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix displaying error message with Firefox #167
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,119 @@ | ||
import { Injectable, NgModule } from '@angular/core'; | ||
import { HttpClient, HttpParams } from '@angular/common/http'; | ||
import { Observable } from 'rxjs'; | ||
import { mergeMap } from 'rxjs/operators'; | ||
|
||
import { ConfigService } from './config.service'; | ||
import { Config, ConfigService } from './config.service'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
@NgModule() | ||
export class BackendService { | ||
backendUrl = 'http://localhost:3001'; | ||
constructor(private http: HttpClient, private configService: ConfigService) { | ||
if (configService.config && configService.config.backendUrl) { | ||
this.backendUrl = configService.config.backendUrl; | ||
} | ||
} | ||
constructor(private http: HttpClient, private configService: ConfigService) {} | ||
|
||
getBlocks(page: number, perPage: number): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/blocks`, { | ||
params: new HttpParams({ | ||
fromObject: { page: page.toString(), perPage: perPage.toString() } | ||
}) | ||
}); | ||
return this.request('/api/blocks'); | ||
} | ||
|
||
searchBlock(query: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/block/${query}`); | ||
return this.request(`/api/block/${query}`); | ||
} | ||
|
||
getBlock(blockHash: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/block/${blockHash}`); | ||
return this.request(`/api/block/${blockHash}`); | ||
} | ||
|
||
getBlockByHeight(height: number): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/block/height/${height}`); | ||
return this.request(`/api/block/height/${height}`); | ||
} | ||
|
||
getRawBlock(blockHash: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/block/${blockHash}/raw`); | ||
return this.request(`/api/block/${blockHash}/raw`); | ||
} | ||
|
||
getBlockTransactions( | ||
blockHash: string, | ||
page: number, | ||
perPage: number | ||
): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/block/${blockHash}/txns`, { | ||
params: new HttpParams({ | ||
return this.request( | ||
`/api/block/${blockHash}/txns`, | ||
new HttpParams({ | ||
fromObject: { page: page.toString(), perPage: perPage.toString() } | ||
}) | ||
}); | ||
); | ||
} | ||
|
||
getTransactions(page: number, perPage: number): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/transactions`, { | ||
params: new HttpParams({ | ||
return this.request( | ||
'/api/transactions', | ||
new HttpParams({ | ||
fromObject: { page: page.toString(), perPage: perPage.toString() } | ||
}) | ||
}); | ||
); | ||
} | ||
|
||
getTransaction(txId: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/tx/${txId}`); | ||
return this.request(`/api/tx/${txId}`); | ||
} | ||
|
||
getRawTransaction(txId: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/tx/${txId}/rawData`); | ||
return this.request(`/api/tx/${txId}/rawData`); | ||
} | ||
|
||
getAddressInfo(address: string, lastSeenTxid?: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/address/${address}`, { | ||
params: new HttpParams({ | ||
return this.request( | ||
`/api/address/${address}`, | ||
new HttpParams({ | ||
fromObject: { | ||
lastSeenTxid: (lastSeenTxid || '').toString() | ||
} | ||
}) | ||
}); | ||
); | ||
} | ||
|
||
searchTransaction(query: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/tx/${query}/get`); | ||
return this.request(`/api/tx/${query}/get`); | ||
} | ||
|
||
getColors(lastSeenColorId?: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/colors`, { | ||
params: new HttpParams({ | ||
return this.request( | ||
'/api/colors', | ||
new HttpParams({ | ||
fromObject: { | ||
lastSeenColorId: (lastSeenColorId || '').toString() | ||
} | ||
}) | ||
}); | ||
); | ||
} | ||
|
||
getColor(colorId: string, lastSeenTxid?: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/color/${colorId}`, { | ||
params: new HttpParams({ | ||
return this.request( | ||
`/api/color/${colorId}`, | ||
new HttpParams({ | ||
fromObject: { | ||
lastSeenTxid: (lastSeenTxid || '').toString() | ||
} | ||
}) | ||
}); | ||
); | ||
} | ||
|
||
validateOpenedValue(opened_value: string): Observable<any> { | ||
return this.http.get(`${this.backendUrl}/api/validate/${opened_value}`); | ||
return this.request(`/api/validate/${opened_value}`); | ||
} | ||
|
||
checkMaterialTrackingTransaction(txId: string): Observable<any> { | ||
return this.http.get( | ||
`${this.backendUrl}/api/check_material_tracking_balance/${txId}` | ||
return this.request(`/api/check_material_tracking_balance/${txId}`); | ||
} | ||
|
||
private request(url: string, params?: HttpParams): Observable<any> { | ||
return this.getConfig().pipe( | ||
mergeMap((config: Config) => { | ||
return this.http.get(`${config.backendUrl}/${url}`, { params }); | ||
}) | ||
); | ||
} | ||
|
||
private getConfig(): Observable<Config> { | ||
return this.configService.getConfig(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { HttpClient, HttpParams } from '@angular/common/http'; | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
|
||
import { Observable, throwError } from 'rxjs'; | ||
import { catchError, retry } from 'rxjs/operators'; | ||
import { catchError, mergeMap, retry } from 'rxjs/operators'; | ||
|
||
/*** | ||
* Config service to supply configuration items from assets/config.json file. | ||
|
@@ -21,6 +21,7 @@ export interface Config { | |
export class ConfigService { | ||
configUrl = 'assets/config.json'; | ||
config: Config; | ||
observable: Observable<Config>; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
|
@@ -34,6 +35,17 @@ export class ConfigService { | |
} | ||
|
||
getConfig(): Observable<Config> { | ||
if (this.config && this.observable) { | ||
return new Observable(observer => { | ||
observer.next(this.config); | ||
observer.complete(); | ||
}); | ||
} else { | ||
return (this.observable = this.loadConfig()); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 重複して config を取得するリクエストを投げそうだと思いました。 export class ConfigService {
configUrl = 'assets/config.json';
config: Config;
configObservable: Observable<Config>;
constructor(private http: HttpClient) {}
load() {
if (this.configObservable) return this.configObservable;
this.configObservable = this.loadConfig();
this.configObservable.subscribe(
(config: Config) => {
this.configObservable = null;
this.config = config;
},
error => console.error(error)
);
return this.configObservable;
}
getConfig(): Observable<Config> {
if (this.config) {
return new Observable(observer => {
observer.next(this.config);
observer.complete();
});
} else {
return this.load();
}
}
loadConfig() Observable<Config> {
return this.http.get<Config>(this.configUrl).pipe(
retry(3), // retry a failed request up to 3 times
catchError(this.handleError) // then handle the error
);
} |
||
|
||
private loadConfig(): Observable<Config> { | ||
return this.http.get<Config>(this.configUrl).pipe( | ||
retry(3), // retry a failed request up to 3 times | ||
catchError(this.handleError) // then handle the error | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この条件だと初回のconfig の GET リクエストの応答を待っている間に
getConfig()
が呼ばれると、複数回 GET リクエストを投げることになりませんか?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a2c7a99 修正しました。