This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build(lib): Changed whole logic of Query library
- Loading branch information
1 parent
94f76b6
commit 2c25c5c
Showing
14 changed files
with
370 additions
and
157 deletions.
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
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
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,7 +1,10 @@ | ||
import * as QueryActions from './query.actions'; | ||
import * as QueryActions from './store/query.actions'; | ||
|
||
export * from './query.model'; | ||
export * from './query.reducers'; | ||
export * from './query.utils'; | ||
export * from './query.decorator'; | ||
export * from './query.helpers'; | ||
export * from './store/query.facade'; | ||
export * from './query.module'; | ||
|
||
export { QueryActions }; |
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { map, catchError } from 'rxjs/operators'; | ||
import { APP_BOOTSTRAP_LISTENER } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import { QueryConfig } from './query.model'; | ||
import * as QueryActions from './store/query.actions'; | ||
|
||
let _store; | ||
|
||
export const BOOTSTRAP_QUERY_PROVIDER = { | ||
provide: APP_BOOTSTRAP_LISTENER, | ||
multi: true, | ||
deps: [Store], | ||
useFactory: (s) => { | ||
_store = s; | ||
return (store) => store; | ||
} | ||
}; | ||
|
||
export const Query = (queryConfig: QueryConfig): MethodDecorator => ( | ||
target: any, | ||
propertyKey: string, | ||
descriptor: PropertyDescriptor | ||
) => { | ||
const original = descriptor.value; | ||
|
||
descriptor.value = function () { | ||
_store.dispatch(QueryActions.inProgress({ queryConfig })); | ||
|
||
return original.apply(this, arguments) | ||
.pipe( | ||
map((response) => { | ||
_store.dispatch(QueryActions.success({ queryConfig, response })); | ||
return response; | ||
}), | ||
catchError((error) => { | ||
_store.dispatch(QueryActions.failure({ queryConfig, error })); | ||
throw error; | ||
}) | ||
); | ||
}; | ||
|
||
return descriptor; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { QueryResponse, QueryConfig, QueryGroups, Queries } from './query.model'; | ||
import { QueryState } from './store/query.state'; | ||
import { isQueryGroupInProgress } from './query.utils'; | ||
|
||
export const getInitialQuery = (): QueryResponse<null> => ({ | ||
status: null, | ||
response: null, | ||
error: null, | ||
isDirty: false, | ||
isInProgress: false, | ||
isSuccess: false, | ||
isError: false, | ||
}); | ||
|
||
export const getQueriesGroup = ( | ||
queryState: QueryState, | ||
queryConfig: QueryConfig, | ||
query: QueryResponse<any>, | ||
groupName: string, | ||
): Queries[] => { | ||
const { groups } = queryState; | ||
const { name } = queryConfig; | ||
|
||
return { | ||
...(groups[groupName] ? groups[groupName].queries : []), | ||
[name]: query, | ||
}; | ||
}; | ||
|
||
export const getQueryGroups = ( | ||
queryState: QueryState, | ||
queryConfig: QueryConfig, | ||
query: QueryResponse<any> | ||
): QueryGroups => { | ||
const { groups: stateGroups } = queryState; | ||
const { groups } = queryConfig; | ||
const extendedGroups = { ...stateGroups }; | ||
|
||
if (!groups) { | ||
return extendedGroups; | ||
} | ||
|
||
groups.map(groupName => { | ||
const queries = getQueriesGroup(queryState, queryConfig, query, groupName); | ||
extendedGroups[groupName] = { | ||
isInProgress: isQueryGroupInProgress(queries), | ||
queries | ||
}; | ||
}); | ||
|
||
return extendedGroups; | ||
}; | ||
|
||
export const getQueries = ( | ||
queryState: QueryState, | ||
queryConfig: QueryConfig, | ||
query: QueryResponse<any> | ||
): Queries => { | ||
return { | ||
...queryState.queries, | ||
[queryConfig.name]: query | ||
}; | ||
}; | ||
|
||
export const parseQueryState = ( | ||
queryState: QueryState, | ||
queryConfig: QueryConfig, | ||
query: QueryResponse<any> | ||
): QueryState => { | ||
const groups = getQueryGroups(queryState, queryConfig, query); | ||
const queries = getQueries(queryState, queryConfig, query); | ||
|
||
return { | ||
...queryState, | ||
queries, | ||
groups | ||
}; | ||
}; |
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,13 +1,35 @@ | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
|
||
export interface Queries { | ||
[key: string]: QueryResponse<any>; | ||
} | ||
|
||
export interface QueryGroup { | ||
queries: Queries[]; | ||
isInProgress: boolean; | ||
} | ||
|
||
export interface QueryGroups { | ||
[key: string]: QueryGroup; | ||
} | ||
|
||
export interface QueryConfig { | ||
name: string; | ||
groups?: string[]; | ||
} | ||
|
||
export enum QueryStatus { | ||
Success = 'SUCCESS', | ||
InProgress = 'IN_PROGRESS', | ||
Failure = 'FAILURE', | ||
} | ||
|
||
export interface Query<T> { | ||
status?: QueryStatus; | ||
response?: T; | ||
error?: HttpErrorResponse; | ||
export interface QueryResponse<T> { | ||
response: T; | ||
error: HttpErrorResponse; | ||
status: QueryStatus; | ||
isSuccess: boolean; | ||
isError: boolean; | ||
isInProgress: boolean; | ||
isDirty: boolean; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { StoreModule } from '@ngrx/store'; | ||
import { QueryReducer } from './store/query.reducer'; | ||
|
||
import { QUERY_STORE_KEY } from './store/query.state'; | ||
import { QueryFacade } from './store/query.facade'; | ||
import { BOOTSTRAP_QUERY_PROVIDER } from './query.decorator'; | ||
|
||
@NgModule({ | ||
imports: [ | ||
StoreModule.forFeature(QUERY_STORE_KEY, QueryReducer), | ||
], | ||
providers: [ | ||
QueryFacade, | ||
BOOTSTRAP_QUERY_PROVIDER | ||
] | ||
}) | ||
export class QueryModule {} |
Oops, something went wrong.