GithubHelp home page GithubHelp logo

Select Combinations about store HOT 8 CLOSED

ngxs avatar ngxs commented on August 15, 2024
Select Combinations

from store.

Comments (8)

amcdnl avatar amcdnl commented on August 15, 2024

@deebloo - I have an idea for this, will be pretty easy to implement too.

@State({ ... })
export class ZooState {
   @Selector({
       combine: [CowState.cows, ...n*]
    })
    static animals(state: ZooStateModel, cows: Cows, ...n*) { ... }
}

from store.

leon avatar leon commented on August 15, 2024

I would prefer

@Selector([CowState.cows, FarmState.milktime])

from store.

amcdnl avatar amcdnl commented on August 15, 2024

Ya, I was trying to keep it where if we wanted to add other options down the road it would be easy but ya I agree

from store.

markwhitfeld avatar markwhitfeld commented on August 15, 2024

I have spent some time thinking about this feature.
I have tried to define clear rules on what would be expected from this.
Here are my thoughts on this:

  • (This is the existing usage) A @Selector decorator with zero parameters is only valid in a state class and provides the container class' state model as the only parameter :
@State<UserStateModel>( ... )
export class UserState {
  @Selector()  
  getUsers(userStateModel: UserStateModel) { ... }
}
  • Allow state classes to be passed in as parameters. This can be used from within any class, be it a State class or another service class. It only provides the models as requested (ie. it does not add the model for the containing class, no matter if it is defined in a state class or not) :
export class SomeStateClassOrRegularService {
  @Selector([UserState, TodoState])  
  getActiveUserTodos(userState: UserStateModel, todoState: TodoStateModel) { ... }
}
  • Allow selectors to be passed in. The rules would be the same as passing State Classes as parameters:
export class SomeStateClassOrRegularService {
  @Selector([UserState.getName, TodoState.getCount])  
  getUserTodoCount(userName: string, todoCount: number) { ... }
}
  • Or a mix of the two:
export class SomeStateClassOrRegularService {
  @Selector([UserState.getName, TodoState])  
  getUserTodos(userName: string, todoState: TodoStateModel) { ... }
}
  • A @Selector with no parameters would not be valid on a class that is not a state. There was an option that the global state could then be passed as the parameter, but this would break encapsulation because it would require the dev to have knowledge of the global state structure.
    To be clear, this would not be valid:
export class MyNonStateClass {
  @Selector()  // ERROR!
  static invalidSelector(thisIsNotValid: any) { ... }
}

Regarding the possibility later of adding options, I think that the options could be expressed as an object and the decorator could detect if an options object or an array was passed to it or both. As an example the only thing I could think of now that may be an option we could add in future is a different memoiser, so this is what this could look like:

export class SomeStateClassOrRegularService {
  @Selector([UserState.getName, TodoState], { memoizer: customMemoizer(3) })  
  getUserTodos(userName: string, todoState: TodoStateModel) { ... }
}

PS. selector option given as an example but I don't think we need to implement the options part until there is an actual need for it.

from store.

amcdnl avatar amcdnl commented on August 15, 2024

What would be the use case for a selector that doesn't belong to a state?

from store.

markwhitfeld avatar markwhitfeld commented on August 15, 2024

I think that the idea would be that if you need to create a selector that does a query across multiple states in your application and they do not share a common heirarchy (other than app state) then you could put this selector in a class not directly associated with a particular state class.
@leon mentioned this concept on slack in this comment: https://ngxs.slack.com/archives/CA0NS79MG/p1524210374000089
Maybe @deebloo can chip in here too.

from store.

juliusstoerrle avatar juliusstoerrle commented on August 15, 2024

Use Case for Seperate Selectors:
If I have a user module and multiple modules with relations to the user module and I want a selector pulling in a user with its related items. I would like to have this selector separate from any State. Because if I would put it in my UserState it break lazy loading of user related feature modules.

@markwhitfeld thats some great work, I like those ideas. I think most important is passing in other selectors.

from store.

amcdnl avatar amcdnl commented on August 15, 2024

Closing and marking for future.

from store.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.