kosich / rxjs-autorun

Re-evaluate an expression whenever Observable in it emits
MIT License
33 stars 2 forks source link

Untracking unused streams #8

Closed kosich closed 3 years ago

kosich commented 3 years ago

As @Jopie64 describes in #7, in an expression with a condition there might be a case when an Observable becomes unused:

const ms = timer(0, 1);
const s = timer(0, 1000);
run(() => $(s) % 2 : $(ms) : 'even');

Steps of calculation:

  1. s=0 that would result to "even"
  2. s=1 that would lead to subscription to ms and a thousand of ms values (or ~ thousand)
  3. s=2 means that expression emits "even" and ms stream is not needed for the next second
  4. s=3 means ms stream is again used and we'll emit a thousand of ms values

Current behavior: Due to ms being tracked, the expression would be still needlessly recalculated a thousand times. Even though at step # 3 we learned that ms is not used when s=2.

Expected behavior: I think, when ms is detected unused on step # 3, we should mark it as "untracked" until it is again used. So $ turns under the hood into _ until it's used again.