rx-angular icon indicating copy to clipboard operation
rx-angular copied to clipboard

[Question] a new "reduce" API to RxState

Open TheSpiciestDev opened this issue 6 years ago • 3 comments

While I have found value in the connect API, I have also found myself wanting to "cancel" a connect function or not modify state. I am curious if the differences below could be found useful or worth considering.

this.reduce(interval(1000), (state, i) => {
  if (i % 3 === 0 && i % 5 === 0) {
    return {
      ...state,
      message: 'FizzBuzz'
    };
  }
  if (i % 3 === 0) {
    return {
      ...state,
      message: 'Fizz'
    };
  }
  if (i % 5 === 0) {
    return {
      ...state,
      message: 'Buzz'
    };
  }
  return state;
});
this.select().subscribe(console.log);

If the above method is connect instead of reduce then today we would see the following in the console:

{"message":"FizzBuzz"}
{"message":"FizzBuzz"}
{"message":"FizzBuzz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"Buzz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"Buzz"}
{"message":"Buzz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"FizzBuzz"}

Instead, a reduce method would return either a new state or the existing state reference to leverage the distinct-changed checks. Ultimately the above code could have a console that looks like below. Illustrating via our select that only new state references are being return by our reduce function.

{"message":"FizzBuzz"}
{"message":"Fizz"}
{"message":"Buzz"}
{"message":"Fizz"}
{"message":"Fizz"}
{"message":"Buzz"}
{"message":"Fizz"}
{"message":"FizzBuzz"}

Would there be any considerable value added by such a method? I don't know how often users would want to "cancel" their connect methods or if it's even a good practice to have an empty select observable.

TheSpiciestDev avatar Apr 28 '20 05:04 TheSpiciestDev

Very good input!

I see two layers in it:

  1. if statements and functional programming
  2. selecting the "ViewModel"

If statements To get rid of ifs and noop emissions switch to a filter approach like the following:

const interval1$ = interval(1000).pipe(share);
const updatesA$ = interval$.pipe(filter(i=> (i % 3 === 0 && i % 5 === 0)));
const updatesB$ = interval$.pipe(filter(i=> (i % 3 === 0)));
const updatesC$ = interval$.pipe(filter(i=> (i % 5 === 0)));

this.connect('message', updatesA$, () => 'FizzBuzz');
this.connect('message', updatesB$, () => 'Fizz');
this.connect('message', updatesC$, () => 'Buzz');

this.select().subscribe(console.log);

Selecting the ViewModel Often we need to select a subset of the managed state to render the template. This task can include one or both things:

  • Exposing a subset of properties from the state to the view
  • Running additional derivations on the selected state to e.g. calculate the sort order or visible items of a list or just the length.

A subset selection would look like that ATM:

this.select(({prop1, prop2, ...other}) => ({prop1, prop2}))
  .subscribe(console.log);

If the state is changes other properties too the selection will emit a changes too often. A simple approach would be to use pluck and combineLatest to acheive the desired output:

combineLatest(
  this.select(pluck( 'prop1'))
  this.select(pluck('prop2'))
)
  .subscribe(console.log);

In this case, we will ship an operator in the next minor release (1.1.x).

The second case is to enrich the state with additional derivation logic:

  const listLength$ = this.select(map(s => s.list.length));
  this.select(
    zip(listLength$),
    map((state, listLength) => ({
       ...state,
       listLength
    }))
  )
  .subscribe(console.log);

For this situation, we have an active discussion ongoing, and super open for input.


What do you think about the explanation? I did not address the new method but gave some solutions to the problem. Do you still see the need for such a method?

Just let me know. :)

BioPhoton avatar Apr 28 '20 15:04 BioPhoton

Thanks @BioPhoton - yes your explanations are great and your "if statements" example had clicked with me shortly after opening this issue, you however did a good job illustrating it! I don't see a need for such a method anymore.

If statements I am now wondering how scale-able this approach would be if the amount of conditions or branches within the reduce method increases. Probably though this would be considered code-smell - so maybe it is a non-issue.

Coming from NgRx and projects where we've kept the reducer methods pure and fully tested, I guess I am still with the perspective of a single method with conditions and returns vs. breaking down the reducer's branches into smaller methods to use within the .filters(...) you've illustrated and returning values with the connect methods.

Typing this aloud and with your explanations, I think what I have in mind is more convenience or syntax-sugar and could be built off of what RxState already offers. Not requiring any additional APIs or changes to RxState.

Selecting the ViewModel

I don't have much more functionally to offer or suggest here except what may be more syntax-sugar! The API is direct and to the point - there's a lot to work with! :)

Thanks again!

TheSpiciestDev avatar Apr 28 '20 17:04 TheSpiciestDev

I'm interested in your syntax sugar! :)

Testing: One bug reducer is hard to test, furthermore you can loose dead branches inside.

I suggest thinking in data flows than actions. Actions are fired and its unsure in which reducer they end. Also it's unsure where they are coming from in the reducer.

On component lever the concept of an action is useless. What to decouple from??

Also a reducer known from react/ngrx-store is not really needed. It's just a useless wrapper in the class.

What you use I call projectionFn or reducerFn. These are the functions passed to connect or set.

Data flow:

Source/Trigger => value/projection => new partial state.

Those glows can get tested together in one price and enable a more complete testing.

If you have any suggestions on input on what and how you want to test I can write some snippets together.

Just let me know.

BioPhoton avatar Apr 28 '20 19:04 BioPhoton