store icon indicating copy to clipboard operation
store copied to clipboard

Sub Stores

Open amcdnl opened this issue 8 years ago • 26 comments

Ability to create stores within stores. https://gist.github.com/amcdnl/6998c0dbad7af5f424dc7719527f9fb8

One idea is to create dot notation with store names like:

@Store({
   name: 'cart'
})
export class CartStore{}

@Store({
   name: 'cart.items'
})
export class CartItemsStore {}

then you hook it up like:

@NgModule({
   imports: [
      NgxsModule.forRoot([CartStore, CartItemsStore])
   ]
})
export class MyModule {}

amcdnl avatar Mar 08 '18 16:03 amcdnl

NgModule misses the providers:[CartStore, CartItemsStore] even though that's not the point here

Dercetech avatar Mar 08 '18 16:03 Dercetech

You dont have to here, we handle the providers for you automatically.

amcdnl avatar Mar 08 '18 16:03 amcdnl

Good point

Dercetech avatar Mar 08 '18 16:03 Dercetech

A more general remark from an ngrx/store guy: shouldn't the @Store definition doesn't declare a State-like set of properties? Or would that be a purpose of the "defaults" property?

Dercetech avatar Mar 08 '18 16:03 Dercetech

You use defaults property to handle this:

@Store({
  name: 'foo',
  defaults: { cart: [] }
})

amcdnl avatar Mar 08 '18 16:03 amcdnl

Ok, then I like the idea :)

Dercetech avatar Mar 08 '18 17:03 Dercetech

I like the idea. I still worry about using strings though

deebloo avatar Mar 08 '18 17:03 deebloo

We can't use nested objects like that because we can't traverse them to hook them up as providers in the forRoot/forFeature methods because of AoT tracability.

amcdnl avatar Mar 08 '18 17:03 amcdnl

This would be cool but NOT doable.

@Store({
   name: 'cart',
   defaults: {
      items: ItemsStore
   }
})

Perhaps change the name of defaults if we were to do that ^^

or

@Store({
   name: 'cart',
   defaults: {},
   stores: {
      items: ItemsStore
   }
})

amcdnl avatar Mar 08 '18 17:03 amcdnl

Could web make Store a generic?

@Store<MyStateModel>({
   name: 'cart',
   defaults: {}, // this would adhere to the model passed in
})

deebloo avatar Mar 08 '18 17:03 deebloo

I don't follow how you are defining a sub store there

amcdnl avatar Mar 08 '18 18:03 amcdnl

the only thing I am talking about is when you use strings there is no type safety

deebloo avatar Mar 08 '18 19:03 deebloo

This is a tad more verbose but maybe it answers the riddle:

@Store<Cart>({
   state: (state) => state.cart,
   defaults: {}
})
export class Cart {}

@Store<CartList>({
   state: (state) => state.cart.list,
   defaults: {}
})
export class CartList {}

Not sure on implementation, but just tossing out ideas.

amcdnl avatar Mar 09 '18 00:03 amcdnl

hmmmmmmm. I is intrigued

deebloo avatar Mar 09 '18 01:03 deebloo

Essentially treating each store state input like a select. I thought it was clever

amcdnl avatar Mar 09 '18 13:03 amcdnl

state: (state) => state.cart,

It's not completely obvious from what piece of the state it is getting the cart. Is it the app state?

victormejia avatar Mar 09 '18 21:03 victormejia

Just to be a bit clear on this, what problem we are trying to solve with sub stores as opposed to having multiple stores are the root level? we should keep in mind that a feature may be lazy loaded / unloaded so it's store (or sub store) may need to be discarded.

tsawan avatar Mar 12 '18 10:03 tsawan

What about this:

@Store<Cart>({
   name: 'cart',
   defaults: {},
   children: {
      items: CartList
   }
})
export class Cart {}

@Store<CartList>({
   name: 'list'
   defaults: []
})
export class CartList {}

Similar to what vuex does - https://vuex.vuejs.org/en/modules.html

amcdnl avatar Mar 13 '18 03:03 amcdnl

Proposal:

@State<CartState>({
  selector: (state: AppState) => state.cart,
  defaults: {}
})
export class CartState {
  @Action(Checkout)
  checkout() {
    state.checkedout = true;
  }
}

@State<CartItemsState>({
   selector: (state: AppState) => state.cart.items,
   defaults: {
     moo: true
   }
})
export class CartItemsState {
  @Action(AddtoCart)
  addtocart(item) {
    state.push(item);
  }
}

amcdnl avatar Mar 14 '18 00:03 amcdnl

@amcdnl if someone doesn't care about substates can we make "selector" optional?

deebloo avatar Mar 14 '18 12:03 deebloo