swimos / swim

Full stack application platform for building stateful microservices, streaming APIs, and real-time UIs
https://www.swimos.org
Apache License 2.0
488 stars 41 forks source link

Join Value Lane creates duplicate didSet callbacks #68

Open DobromirM opened 2 years ago

DobromirM commented 2 years ago

Similar to #67 but with a slightly different output, potentially due to #65.

Example:

BasicPlane.java

public class BasicPlane extends AbstractPlane {

  @SwimRoute("/swim")
  AgentRoute<UnitAgent> unitAgentType;

  public static void main(String[] args) {
    final Kernel kernel = ServerLoader.loadServer();
    final ActorSpace space = (ActorSpace) kernel.getSpace("basic");

    kernel.start();
    System.out.println("Running Basic server...");
    kernel.run();

    space.command("/swim", "wakeup", Value.absent());
  }
}

UnitAgent.java

public class UnitAgent extends AbstractAgent {

  @SwimLane("people")
  ValueLane<Integer> people = this.valueLane();

  @SwimLane("addPeople")
  CommandLane<Integer> addPeople = this.<Integer>commandLane().onCommand(msg -> {
    this.people.set(this.people.get() + msg);
  });

  @SwimLane("join")
  JoinValueLane<Integer, Integer> join = this.<Integer, Integer>joinValueLane().didUpdate((Integer key, Integer newValue, Integer oldValue) -> {
    System.out.println("The people in room " + key + " are " + newValue + " from " + oldValue);
  });

  @Override
  public void didStart() {
    join.downlink(1).nodeUri("/swim").laneUri("people").open().didSet((newValue, oldValue) -> {
      System.out.println("people changed to " + newValue + " from " + oldValue);
    });
  }
}

Input

@command(node:"/swim", lane:"addPeople")5

Output

The people in room 1 are 0 from 0
people changed to 0 from 0
The people in room 1 are 0 from 0
The people in room 1 are 5 from 0
people changed to 5 from 5
The people in room 1 are 5 from 5

The first three messages are from the initial value and the next three are from the command.

DobromirM commented 2 years ago

Possible duplicate or related to #16