Skip to content

Commit

Permalink
fix(local-traits): rollback allow writing effects inside TraitsLocal
Browse files Browse the repository at this point in the history
The previous feature to enable writing effects inside the LocalTraits brough a race condition that
makes the effects of the LocalTraits not be initialized when the component ngOnit was call this was
do to using the queueMicroTask to register services
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed Mar 30, 2022
1 parent da62ed4 commit 14a2dd7
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ import { ProductsLocalTraits } from './products.traits';
})
export class ProductSelectDialogComponent implements OnInit {
data$ = combineLatest([
this.store.select(this.localTraits.localSelectors.selectProductsList),
this.store.select(this.localTraits.localSelectors.isProductsLoading),
this.store.select(this.localTraits.localSelectors.selectProductSelected),
this.store.select(this.localTraits.selectors.selectProductsList),
this.store.select(this.localTraits.selectors.isProductsLoading),
this.store.select(this.localTraits.selectors.selectProductSelected),
]).pipe(
map(([products, isLoading, selectedProduct]) => ({
products,
Expand All @@ -61,19 +61,17 @@ export class ProductSelectDialogComponent implements OnInit {
constructor(private store: Store, private localTraits: ProductsLocalTraits) {}

ngOnInit() {
this.store.dispatch(this.localTraits.localActions.loadProducts());
this.store.dispatch(this.localTraits.actions.loadProducts());
}

select({ id }: Product) {
this.store.dispatch(this.localTraits.localActions.selectProduct({ id }));
this.store.dispatch(this.localTraits.actions.selectProduct({ id }));
}

filter(filters: ProductFilter) {
this.store.dispatch(
this.localTraits.localActions.filterProducts({ filters })
);
this.store.dispatch(this.localTraits.actions.filterProducts({ filters }));
}
sort(sort: Sort<Product>) {
this.store.dispatch(this.localTraits.localActions.sortProducts(sort));
this.store.dispatch(this.localTraits.actions.sortProducts(sort));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ import { Product, ProductFilter } from '../../../models';
import {
createEntityFeatureFactory,
LocalTraitsConfig,
TraitEffect,
TraitLocalEffectsFactory,
TraitsLocalStore,
} from 'ngrx-traits';
import { Injectable } from '@angular/core';
import { createEffect, ofType } from '@ngrx/effects';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { catchError, map, switchMap, tap } from 'rxjs/operators';
import { of } from 'rxjs';
import { ProductService } from '../../../services/product.service';
import { Store } from '@ngrx/store';

const productFeatureFactory = createEntityFeatureFactory(
{ entityName: 'product' },
Expand All @@ -30,31 +33,46 @@ const productFeatureFactory = createEntityFeatureFactory(
})
);

const productsEffect: TraitLocalEffectsFactory<typeof productFeatureFactory> = (
allActions
) => {
@Injectable()
class ProductsEffects extends TraitEffect {
loadProducts$ = createEffect(() =>
this.actions$.pipe(
ofType(allActions.loadProducts),
switchMap(() =>
//call your service to get the products data
this.productService.getProducts().pipe(
map((res) =>
allActions.loadProductsSuccess({ entities: res.resultList })
),
catchError(() => of(allActions.loadProductsFail()))
)
)
)
);

constructor(
actions$: Actions,
store: Store,
private productService: ProductService
) {
super(actions$, store);
}
}
return ProductsEffects;
};

@Injectable()
export class ProductsLocalTraits extends TraitsLocalStore<
typeof productFeatureFactory
> {
productService = this.injector.get(ProductService);

loadProducts$ = createEffect(() =>
this.actions$.pipe(
ofType(this.localActions.loadProducts),
switchMap(() =>
//call your service to get the products data
this.productService.getProducts().pipe(
map((res) =>
this.localActions.loadProductsSuccess({ entities: res.resultList })
),
catchError(() => of(this.localActions.loadProductsFail()))
)
)
)
);

setup(): LocalTraitsConfig<typeof productFeatureFactory> {
return {
componentName: 'ProductsPickerComponent',
traitsFactory: productFeatureFactory,
effectFactory: productsEffect,
};
}
}
94 changes: 19 additions & 75 deletions libs/ngrx-traits/src/lib/create-entity-feature.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,6 @@ const productMixedFactory = mixEntityFeatures({

@Injectable()
class ProductTraitLocal extends TraitsLocalStore<typeof productFeatureFactory> {
loadEntities$ = createEffect(() => {
return this.actions$.pipe(
ofType(this.localActions.loadProducts),
mapTo(
this.localActions.loadProductsSuccess({
entities: [
{ id: 1, name: 'name', description: 'description', price: 123 },
],
})
)
);
});

setup(): LocalTraitsConfig<typeof productFeatureFactory> {
return {
componentName: 'ProductTestComponent',
Expand All @@ -103,19 +90,6 @@ class ProductTraitLocal extends TraitsLocalStore<typeof productFeatureFactory> {
class ProductCombinedTraitLocal extends TraitsLocalStore<
typeof productCombinedFactory
> {
loadEntities$ = createEffect(() => {
return this.actions$.pipe(
ofType(this.localActions.products.loadProducts),
mapTo(
this.localActions.products.loadProductsSuccess({
entities: [
{ id: 1, name: 'name', description: 'description', price: 123 },
],
})
)
);
});

setup(): LocalTraitsConfig<typeof productCombinedFactory> {
return {
componentName: 'ProductCombinedTestComponent',
Expand All @@ -128,19 +102,6 @@ class ProductCombinedTraitLocal extends TraitsLocalStore<
class ProductMixedTraitLocal extends TraitsLocalStore<
typeof productMixedFactory
> {
loadEntities$ = createEffect(() => {
return this.actions$.pipe(
ofType(this.localActions.loadProducts),
mapTo(
this.localActions.loadProductsSuccess({
entities: [
{ id: 1, name: 'name', description: 'description', price: 123 },
],
})
)
);
});

setup(): LocalTraitsConfig<typeof productMixedFactory> {
return {
componentName: 'ProductMixedTestComponent',
Expand All @@ -153,19 +114,6 @@ class ProductMixedTraitLocal extends TraitsLocalStore<
class ProductAddEntityPropertiesTraitLocal extends TraitsLocalStore<
typeof productAddEntityPropertiesFactory
> {
loadEntities$ = createEffect(() => {
return this.actions$.pipe(
ofType(this.localActions.loadProducts),
mapTo(
this.localActions.loadProductsSuccess({
entities: [
{ id: 1, name: 'name', description: 'description', price: 123 },
],
})
)
);
});

setup(): LocalTraitsConfig<typeof productAddEntityPropertiesFactory> {
return {
componentName: 'ProductAddEntityPropertiesTestComponent',
Expand Down Expand Up @@ -447,37 +395,33 @@ describe('Ngrx-Traits Integration Test', () => {
describe('single feature trait local', () => {
it('test some action and selectors in products ', async () => {
const { localTrait, store } = initTraitLocal();
await basicProductTest(
localTrait.localActions,
localTrait.localSelectors,
store
);
await basicProductTest(localTrait.actions, localTrait.selectors, store);
});

describe('test combined trait local', () => {
it('test some action and selectors in products ', async () => {
const { localTrait, store } = initCombinedTraitLocal();
await basicProductTest(
localTrait.localActions.products,
localTrait.localSelectors.products,
localTrait.actions.products,
localTrait.selectors.products,
store
);
});

it('test some action and selectors in productOrders ', async () => {
const { localTrait, store } = initCombinedTraitLocal();
await basicProductOrdersTest(
localTrait.localActions.productOrders,
localTrait.localSelectors.productOrders,
localTrait.actions.productOrders,
localTrait.selectors.productOrders,
store
);
});

it('test some action and selectors in clients ', async () => {
const { localTrait, store } = initCombinedTraitLocal();
await basicClientsTest(
localTrait.localActions.clients,
localTrait.localSelectors.clients,
localTrait.actions.clients,
localTrait.selectors.clients,
store
);
});
Expand All @@ -487,26 +431,26 @@ describe('Ngrx-Traits Integration Test', () => {
it('test some action and selectors in products ', async () => {
const { localTrait, store } = initMixedTraitLocal();
await basicProductTest(
localTrait.localActions,
localTrait.localSelectors,
localTrait.actions,
localTrait.selectors,
store
);
});

it('test some action and selectors in productOrders ', async () => {
const { localTrait, store } = initMixedTraitLocal();
await basicProductOrdersTest(
localTrait.localActions,
localTrait.localSelectors,
localTrait.actions,
localTrait.selectors,
store
);
});

it('test some action and selectors in clients ', async () => {
const { localTrait, store } = initMixedTraitLocal();
await basicClientsTest(
localTrait.localActions,
localTrait.localSelectors,
localTrait.actions,
localTrait.selectors,
store
);
});
Expand All @@ -516,26 +460,26 @@ describe('Ngrx-Traits Integration Test', () => {
it('test some action and selectors in products ', async () => {
const { localTrait, store } = initAddEntityProperties();
await basicProductTest(
localTrait.localActions,
localTrait.localSelectors,
localTrait.actions,
localTrait.selectors,
store
);
});

it('test some action and selectors in productOrders ', async () => {
const { localTrait, store } = initAddEntityProperties();
await basicProductOrdersTest(
localTrait.localActions.productOrders,
localTrait.localSelectors.productOrders,
localTrait.actions.productOrders,
localTrait.selectors.productOrders,
store
);
});

it('test some action and selectors in clients ', async () => {
const { localTrait, store } = initAddEntityProperties();
await basicClientsTest(
localTrait.localActions.clients,
localTrait.localSelectors.clients,
localTrait.actions.clients,
localTrait.selectors.clients,
store
);
});
Expand Down
Loading

0 comments on commit 14a2dd7

Please sign in to comment.