Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class instance Stores based on Proxies #5

Open
BigAB opened this issue Aug 14, 2019 · 0 comments
Open

Class instance Stores based on Proxies #5

BigAB opened this issue Aug 14, 2019 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@BigAB
Copy link
Owner

BigAB commented Aug 14, 2019

So the idea would be

const ProductStore extends Store {
  products = [],
  category = null

  constructor({ productService = productService, limit = 3, offset = 0 }) {
    super({ services: { productService } });
    this.limit = limit;
    this.offset = offset;
  }
  
  showMoreProducts = ({ limit = this.limit, offset = this.offset }) => {
    this.limit = limit;
    await { meta, data: {  products = [] } } = this.productService.find({
        offset,
        limit,
        category: this.category
    });
    this.offset = meta.offset;
    this.products.push(...products);
  }

  filterByCategory( category ) {
    this.category = category;
    await { meta, data: {  products = [] } } = this.productService.find({
        offset: 0,
        limit: this.limit,
        category
    });
    this.offset = meta.offset;
    this.products.push(...products);
  }
}

const productStore =  new ProductStore();
const { filterByCategory, showMoreProducts } = productStore;
productStore.subscribe(state => console.log(state.products, state.category));
// log: [] null
productStore.dispatch({ type: filterByCategory, payload: 'toys' })
// log: [] 'toys'
// ...
// log: [product, product, product] 'toys'
showMoreProducts({ limit: 2 })
// log: [product, product, product] 'toys'
// ...
// log: [product, product, product, product, product] 'toys'

Anyway, that's the gist of the idea. Classes that work like normal JS classes, but the instances have the properties observed for changes, which ca be subscribed to, and you can either dispatch "standard actions" where the action.type is the actual method, payload and meta being passed as the first and second arguments respectively, from the store, or you can just call the store functions directly, both have the same result.

That's this idea, at least until I change it

@BigAB BigAB self-assigned this Aug 14, 2019
@BigAB BigAB added the enhancement New feature or request label Aug 14, 2019
@BigAB BigAB mentioned this issue Aug 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant