NgFrituur - Intro to Akita state management

Akita is a state management pattern, built on top of RxJS, which takes the idea of multiple data stores from Flux and the immutable updates from Redux, along with the concept of streaming data, to create the Observable Data Stores model.

Akita encourages simplicity. It saves you the hassle of creating boilerplate code and gives powerful tools with a moderate learning curve, suitable for both experienced and inexperienced developers alike.

Akita is based on object-oriented design principles instead of functional programming, so developers with OOP experience should feel right at home. Its opinionated structure provides your team with a fixed pattern that cannot be deviated from.

That last part about object-oriented design really got me interested as I mostly develop applications written in Java.

Akita, what?

  1. The Store is a single object that contains the store state and serves as the “single source of truth.”
  2. The only way to change the state is by calling set() or one of the update methods based on it.
  3. A component should NOT get data from the store directly but instead use a Query.
  4. Asynchronous logic and update calls should be encapsulated in services and data services.


Demo application

I decided to build a demo application about a subject we are very familiar with here in Belgium, a 'Frituur'. A frituur is a place where people can mainly buy and consume snacks such as fries, croquettes, frikandels and other delicious snacks.


The application shows a list of available snacks and gives the user the option to filter snacks. Here is a small video to quickly show you how it behaves:


The SnacksService is an Injectable that will get called from within a component and will trigger the initial data flow in the application. Both getSnacks()and getCategories() will fetch data from a local server and store it in the Akita entity store. updateSelectedCategory() updates UI related state:

  providedIn: 'root'
export class SnacksService {

  constructor(private http: HttpClient,
              private snacksStore: SnacksStore,
              private snacksQuery: SnacksQuery) {

  updateSelectedCategory(category: Category) {
      ui: {
        selectedCategory: category

  getData() {
    const snacksAndCategories$ = forkJoin([

    return this.snacksQuery.getHasCache() ? EMPTY : snacksAndCategories$;

  private getSnacks(): Observable<Snack[]> {
      tap(response => this.snacksStore.set(response))

  private getCategories(): Observable<Category[]> {
      tap(categories => this.snacksStore.update({categories}))

  private call<T>(url: string): Observable<T> {
    return this.http.get<T>(`${server}/${url}`);


The store is just a simple class extending EntityStore passing in the initial state and the entity type.

export interface SnacksState extends EntityState<Snack> {
  ui: {
    selectedCategory: Category;
  categories: Category[];

const initialState: Partial<SnacksState> = {
  ui: {
    selectedCategory: null

  providedIn: 'root'
  name: 'snacks'
export class SnacksStore extends EntityStore<SnacksState, Snack> {
  constructor() {


The query is used by components to get data out of the store. SnacksQuery has 3 visible 'operations'.

  • selectCategories$
    • Returns an observable of categories.
  • selectedCategory$
    • Returns an observable of the currently selected category.
  • selectVisibleSnacks()
    • Returns an observable of snacks based on the currently active category.
  providedIn: 'root'
export class SnacksQuery extends QueryEntity<SnacksState, Snack> {

  selectCategories$ = => state.categories);
  selectedCategory$ = => state.ui.selectedCategory);

  constructor(protected store: SnacksStore) {

  static filterBy(category) {
    return (snack) => {
      if (category) {
        return snack.category ===;
      return true;

  selectVisibleSnacks(): Observable<Snack[]> {
    return this.selectedCategory$.pipe(switchMap(category => {
      return this.selectAll({
        filterBy: SnacksQuery.filterBy(category)

Snacks overview


Automatically subscribing using the async pipe.

  [categories]="categories$ | async"

<ng-container *ngIf="!(loading$ | async); else loadingTpl">
    fxLayout="row wrap"
    fxLayoutGap="20px grid">
      *ngFor="let snack of snacks$ | async"

<ng-template #loadingTpl>
  <div class="progress">


One way data flow is used throughout the application, events as categoryChanged will be propagated to the container.

  selector: 'app-snacks',
  templateUrl: './snacks.component.html',
  styleUrls: ['./snacks.component.scss']
export class SnacksComponent implements OnInit {

  loading$: Observable<boolean>;
  snacks$: Observable<fromState.Snack[]>;
  categories$: Observable<fromState.Category[]>;

  constructor(private snacksQuery: fromState.SnacksQuery,
              private snacksService: fromState.SnacksService) {

  ngOnInit() {
    this.loading$ = this.snacksQuery.selectLoading();
    this.snacks$ = this.snacksQuery.selectVisibleSnacks();
    this.categories$ = this.snacksQuery.selectCategories$;

  private startDataFlow(): void {

  categoryChanged(category: fromState.Category): void {

You can find the full code over here. If you have any questions, do not hesitate to contact me or leave a comment below.