Skip to content

Memoized selectors for Fluxor. They work similarly as selectors in ngrx/store and Redux.

License

Notifications You must be signed in to change notification settings

berhir/Fluxor.Selectors

Repository files navigation

Fluxor.Selectors

A PoC for memoized selectors for Fluxor. They work similar as selectors in ngrx/store and Redux.

I am quoting the ngrx/store documentation for the benefits:

Selectors are pure functions used for obtaining slices of store state. Fluxor.Selectors provide a few helper functions for optimizing this selection. Selectors provide many features when selecting slices of state:

  • Portability
  • Memoization
  • Composition
  • Testability
  • Type Safety

When using the SelectorFactory.CreateSelector and SelectorFactory.CreateFeatureSelector functions Fluxor.Selectors keep track of the latest arguments in which your selector function was invoked. Because selectors are pure functions, the last result can be returned when the arguments match without reinvoking your selector function. This can provide performance benefits, particularly with selectors that perform expensive computation. This practice is known as memoization.

Create Selectors

The easiest way to create selectors is to use the factory functions:

var selectCounterState = SelectorFactory.CreateFeatureSelector<CounterState>();
var selectCount = SelectorFactory.CreateSelector(selectCounterState, state => state.Count);

Selecting Values

There are two ways to use selectors. But before the selectors can be used, we need access to the IStore. Usually we get it via dependency injection.

  1. To return the selector result immediately, call the Select extension method.
// get via dependency injection
IStore store;

var count = store.Select(selectCount);
  1. Create a subscription that notifies when the selected value changes. A SelectorSubscription implements IState<T> (which implements IStateChangedNotifier) and INotifyPropertyChanged.
// get via dependency injection
IStore store;

var countSubscription = store.SubscribeSelector(selectCount);

Then the subscription can be used in a Razor component

<p role="status">Count: @countSubscription.Value</p>

or bind it in .NET MAUI or WPF

<Label Text="{Binding countSubscription.Value, StringFormat='Count: {0}'}" />

About

Memoized selectors for Fluxor. They work similarly as selectors in ngrx/store and Redux.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages