swimos / cookbook

Cookbook code snippets, gathered into runnable Gradle projects.
Apache License 2.0
16 stars 8 forks source link

Demand lane cookbook #11

Closed SirCipher closed 3 years ago

ajay-gov commented 3 years ago
ajay-gov commented 3 years ago

Code Sample for DemandValueLane

  /**
   * raw data
   */
  @SwimLane("raw")
  ValueLane<Value> raw = this.<Value>valueLane().didSet((n, o) -> {
    this.data.cue();
  });

  /**
   * data
   */
  @SwimLane("data")
  DemandLane<Value> data = this.<Value>demandLane().onCue(uplink -> {
    return transformRaw();
  });

  // Transform raw data to the desired format
  private Value transformRaw() {
    final Value v = this.raw.get();
    if (v.isDefined()) {
      return transform(v);
    } else {
      return v;
    }
  }
ajay-gov commented 3 years ago

Code sample for DemandMapLane

  @SwimLane("raw")
  protected MapLane<Value, Value> raw = this.<Value, Value>mapLane().didUpdate((key, newValue, oldValue) -> {
    this.data.cue(key);
  });

  @SwimLane("data")
  protected DemandMapLane<Value, Value> data = this.<Value, Value>demandMapLane().onCue((key, uplink) -> {
    final Value raw = this.raw.get(key);
    return transform(raw);
  }).onSync(uplink -> {
    return this.raw.keyIterator();
  });