swimos / swim

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

Current value for map downlinks from a plane is always absent #65

Open DobromirM opened 3 years ago

DobromirM commented 3 years ago

If a map downlink is created from a PlaneContext the current value on didUpdate is always Value.absent().

Example:

BasicPlane.java

public class BasicPlane extends AbstractPlane {

  @SwimRoute("/unit/:id")
  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("/unit/foo", "wakeup", Value.absent());
  }

  @Override
  public void didStart() {
    super.didStart();

    //Plane downlink
    context.downlinkMap()
            .keyForm(Form.forString()).valueForm(Form.forInteger())
            .nodeUri("/unit/foo").laneUri("shoppingCart")
            .didUpdate((key, newValue, oldValue) -> {
              System.out.println("plane: " + key + " count changed to " + newValue + " from " + oldValue);
            })
            .open();
  }
}

UnitAgent.java

public class UnitAgent extends AbstractAgent {

  @SwimLane("shoppingCart")
  MapLane<String, Integer> shoppingCart = this.<String, Integer>mapLane()
      .didUpdate((key, newValue, oldValue) -> {
          System.out.println("agent: " + key + " count changed to " + newValue + " from " + oldValue);
      });

  @SwimLane("addItem")
  CommandLane<String> publish = this.<String>commandLane()
      .onCommand(msg -> {
        final int n = this.shoppingCart.getOrDefault(msg, 0) + 1;
        this.shoppingCart.put(msg, n);
      });

}

Input:

@command(node:"/unit/foo", lane:"addItem")"foo"
@command(node:"/unit/foo", lane:"addItem")"foo"
@command(node:"/unit/foo", lane:"addItem")"foo"

Output:

agent: foo count changed to 1 from 0
plane: foo count changed to 1 from 0
agent: foo count changed to 2 from 1
plane: foo count changed to 2 from 0
agent: foo count changed to 3 from 2
plane: foo count changed to 3 from 0

(The plane downlink messages should match the agent downlink messages)

DobromirM commented 3 years ago

The same problem does NOT exist for value downlinks.