mirror of https://github.com/abpframework/abp.git
committed by
GitHub
10 changed files with 205 additions and 89 deletions
@ -0,0 +1,44 @@ |
|||
import { Injectable } from '@angular/core'; |
|||
import { HttpRequest } from '@angular/common/http'; |
|||
import { InternalStore } from '../utils/internal-store-utils'; |
|||
|
|||
export interface HttpWaitState { |
|||
requests: Set<HttpRequest<any>>; |
|||
} |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root', |
|||
}) |
|||
export class HttpWaitService { |
|||
protected store = new InternalStore<HttpWaitState>({ requests: new Set() }); |
|||
|
|||
getLoading() { |
|||
return !!this.store.state.requests.size; |
|||
} |
|||
|
|||
getLoading$() { |
|||
return this.store.sliceState(({ requests }) => !!requests.size); |
|||
} |
|||
|
|||
updateLoading$() { |
|||
return this.store.sliceUpdate(({ requests }) => !!requests.size); |
|||
} |
|||
|
|||
clearLoading() { |
|||
this.store.patch({ requests: new Set() }); |
|||
} |
|||
|
|||
addRequest(request: HttpRequest<any>) { |
|||
const requests = this.store.state.requests; |
|||
requests.add(request); |
|||
this.store.patch({ requests }); |
|||
} |
|||
|
|||
deleteRequest(request: HttpRequest<any>) { |
|||
const requests = this.store.state.requests; |
|||
requests.delete(request); |
|||
this.store.patch({ requests }); |
|||
} |
|||
|
|||
// TODO: Add filter function
|
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
import { Injectable } from '@angular/core'; |
|||
import { InternalStore } from '../utils/internal-store-utils'; |
|||
|
|||
export interface ResourceWaitState { |
|||
resources: Set<string>; |
|||
} |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root', |
|||
}) |
|||
export class ResourceWaitService { |
|||
private store = new InternalStore<ResourceWaitState>({ resources: new Set() }); |
|||
|
|||
getLoading() { |
|||
return !!this.store.state.resources.size; |
|||
} |
|||
|
|||
getLoading$() { |
|||
return this.store.sliceState(({ resources }) => !!resources.size); |
|||
} |
|||
|
|||
updateLoading$() { |
|||
return this.store.sliceUpdate(({ resources }) => !!resources.size); |
|||
} |
|||
|
|||
clearLoading() { |
|||
this.store.patch({ resources: new Set() }); |
|||
} |
|||
|
|||
addResource(resource: string) { |
|||
const resources = this.store.state.resources; |
|||
resources.add(resource); |
|||
this.store.patch({ resources }); |
|||
} |
|||
|
|||
deleteResource(resource: string) { |
|||
const resources = this.store.state.resources; |
|||
resources.delete(resource); |
|||
this.store.patch({ resources }); |
|||
} |
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
import { Injectable } from '@angular/core'; |
|||
import { |
|||
NavigationCancel, |
|||
NavigationEnd, |
|||
NavigationError, |
|||
NavigationStart, |
|||
Router, |
|||
} from '@angular/router'; |
|||
import { filter } from 'rxjs/operators'; |
|||
import { InternalStore } from '../utils/internal-store-utils'; |
|||
|
|||
export interface RouterWaitState { |
|||
loading: boolean; |
|||
} |
|||
|
|||
@Injectable({ |
|||
providedIn: 'root', |
|||
}) |
|||
export class RouterWaitService { |
|||
private store = new InternalStore<RouterWaitState>({ loading: false }); |
|||
constructor(private router: Router) { |
|||
this.router.events |
|||
.pipe( |
|||
filter( |
|||
event => |
|||
event instanceof NavigationStart || |
|||
event instanceof NavigationEnd || |
|||
event instanceof NavigationError || |
|||
event instanceof NavigationCancel, |
|||
), |
|||
) |
|||
.subscribe(event => { |
|||
if (event instanceof NavigationStart) this.setLoading(true); |
|||
else this.setLoading(false); |
|||
}); |
|||
} |
|||
|
|||
getLoading() { |
|||
return this.store.state.loading; |
|||
} |
|||
|
|||
getLoading$() { |
|||
return this.store.sliceState(({ loading }) => loading); |
|||
} |
|||
|
|||
updateLoading$() { |
|||
return this.store.sliceUpdate(({ loading }) => loading); |
|||
} |
|||
|
|||
setLoading(loading: boolean) { |
|||
this.store.patch({ loading }); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue