All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.spotify.mobius.EventProcessor Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
/*
 * -\-\-
 * Mobius
 * --
 * Copyright (c) 2017-2018 Spotify AB
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * -/-/-
 */
package com.spotify.mobius;

import static com.spotify.mobius.internal_util.Preconditions.checkNotNull;

import com.spotify.mobius.functions.Consumer;
import java.util.ArrayList;
import java.util.List;

/**
 * Processes events and emits effects and models as a result of that.
 *
 * @param  model type
 * @param  event type
 * @param  effect descriptor type
 */
class EventProcessor {

  private final MobiusStore store;
  private final Consumer effectConsumer;
  private final Consumer modelConsumer;

  // concurrency note: the two below fields are only read and written in synchronized sections,
  // hence no need for further coordination.
  private final List eventsReceivedBeforeInit = new ArrayList<>();
  private boolean initialised = false;

  EventProcessor(
      MobiusStore store, Consumer effectConsumer, Consumer modelConsumer) {
    this.store = checkNotNull(store);
    this.effectConsumer = checkNotNull(effectConsumer);
    this.modelConsumer = checkNotNull(modelConsumer);
  }

  synchronized void init() {
    if (initialised) {
      throw new IllegalStateException("already initialised");
    }

    First first = store.init();

    dispatchModel(first.model());
    dispatchEffects(first.effects());

    initialised = true;
    for (E event : eventsReceivedBeforeInit) {
      update(event);
    }
  }

  synchronized void update(E event) {
    if (!initialised) {
      eventsReceivedBeforeInit.add(event);
      return;
    }

    Next next = store.update(event);

    next.ifHasModel(
        new Consumer() {
          @Override
          public void accept(M model) {
            dispatchModel(model);
          }
        });
    dispatchEffects(next.effects());
  }

  private void dispatchModel(M model) {
    modelConsumer.accept(model);
  }

  private void dispatchEffects(Iterable effects) {
    for (F effect : effects) {
      effectConsumer.accept(effect);
    }
  }

  /**
   * Factory for event processors.
   *
   * @param  model type
   * @param  event type
   * @param  effect descriptor type
   */
  static class Factory {

    private final MobiusStore store;

    Factory(MobiusStore store) {
      this.store = checkNotNull(store);
    }

    public EventProcessor create(Consumer effectConsumer, Consumer modelConsumer) {
      return new EventProcessor<>(store, checkNotNull(effectConsumer), checkNotNull(modelConsumer));
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy