gabrielguerrero / ngrx-traits

NGRX Traits is a library to help you compose and reuse state logic in your angular app. There is two versions, @ngrx-traits/signals supports ngrx-signals, and @ngrx-traits/{core, common} supports ngrx.
MIT License
44 stars 3 forks source link

Multiple withEntities and withEntitiesLocalFilter breaks signalStoreFeature #84

Open francobasilico opened 1 month ago

francobasilico commented 1 month ago

When creating a reusable signalStoreFeature and adding multiple withEntities, If I need to use a withEntitiesLocalFilter for one of the entities, signalStoreFeature is broken.

example that doesn't work:

const entity = type<Product>();
const collection1 = 'product';

const entity2 = type<{ otherField: boolean }>();
const collection2 = 'product2';

const withFeature = () =>
  signalStoreFeature(
    withEntities({ entity, collection: collection1 }),
    withEntities({ entity: entity2, collection: collection2 }),
    withEntitiesLocalFilter({
      entity,
      collection: collection1,
      defaultFilter: { test: 'test' },
      defaultDebounce: 0,
      filterFn: (entity, filter) => true,
    })
  );

if I move the withEntitiesLocalFilter next to the corresponding withEntities it works:

  const entity = type<Product>();
const collection1 = 'product';

const entity2 = type<{ otherField: boolean }>();
const collection2 = 'product2';

const withFeature = () =>
  signalStoreFeature(
    withEntities({ entity, collection: collection1 }),
    withEntitiesLocalFilter({
      entity,
      collection: collection1,
      defaultFilter: { test: 'test' },
      defaultDebounce: 0,
      filterFn: (entity, filter) => true,
    })
    withEntities({ entity: entity2, collection: collection2 }),

  );
gabrielguerrero commented 1 month ago

This might be related https://github.com/gabrielguerrero/ngrx-traits/issues/92, I think is a problem with how the signalfeature store are merged that makes them dependent on the order