Skip to content
This repository has been archived by the owner on Jun 30, 2022. It is now read-only.

Add idle mode for not "in component" used services. (performance optimization) #43

Open
betula opened this issue Apr 7, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@betula
Copy link
Member

betula commented Apr 7, 2020

Interested idea to add counter of subscribed components, and when count of subscribed react component stay 0, don't recalculate data in this store.
Think about It.

@betula betula added the enhancement New feature or request label Apr 7, 2020
@betula betula changed the title Add idle mode for services. (performance optimization) Add idle mode for not "in component" used services. (performance optimization) Apr 7, 2020
@betula
Copy link
Member Author

betula commented Apr 10, 2020

sleep - return action, when count of components use call useProvide or useSubscribe equal 0. Service is idle.
idle - return boolean, true when count of subscribed (through useProvide or useSubscribe) component is 0
awake - return action, when count of components who use call useProvide or useSubscribe more then 0

class Counters {
  todo = provide(Todo);
  store = {
    active: 0,
    completed: 0
  }
  constructor() {
    subscribe(this.todo, this.calculate, this);
    subscribe(TodoItemChanged, this.calculate, this);
    subscribe(awake(this), this.calculate, this);
    subscribe(sleep(this), this.sleep, this);
    this.calculate();
  }
  calculate() {
    if (idle(this)) return;
    const items = this.todo.store;
    const completed = items.filter(item => item.store.completed).length;
    const active = items.length - completed;
    this.store = { completed, active };
  }
  sleep() {
    console.log('sleep');
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant