Data Exchange / Communication between dizmos

Dizmos can communicate with each other and exchange data when they are moved alongside each other or when one lies on top of another. The former is called "docking" and the latter "arranging dizmos hierarchically".

As described in the chapter Persistence, nodes stored in "dizmo.publicStorage" can be accessed by other dizmos and thus be used to exchange data.

  dizmo.publicStorage.getProperty("stdout"); // get property on node stdout
  dizmo.publicStorage.setProperty("stdout","something to be written into stdout");

Hint: If you have any data which could be used by any other dizmo, write them to your publicStorage. There may be a use-case you have not thought of yet, but other developers will.

You have two possibilities to exchange and display data between docked dizmos. The first is to exchange data once "by contact", that is when the dizmos are docked or one dragged onto another. The second way is a constant exchange.

Exchange data once

Exchanging data via docking means that we first need to be notified about a dizmo docking to ours, then we grab the stdout value from the docked dizmo:

dizmo.canDock(function(dizmoToBeDocked){
    console.log(dizmoToBeDocked); // outputs the dizmo that needs to be docked
    return true;
});

dizmo.onDock(function(dockedDizmo){
    var stdout = dockedDizmo.publicStorage.getProperty('stdout');
    console.log(stdout);
});

Constant data exchange

To constantly monitor data, use the subscribeTo function.

dizmo.canDock(true);
var subscriptionId = {};
dizmo.onDock(function(dockedDizmo) {
    dizmo.privateStorage.setProperty('val', dockedDizmo.publicStorage.getProperty('stdout'));
    subscriptionId[dockedDizmo.identifier] = dockedDizmo.publicStorage.subscribeToProperty('stdout', function(path, val) {
      dizmo.privateStorage.setProperty('val', val);
    });
});

Remember to cancel the subscription once the dizmos are undocked. To cancel the subscription made above, you would use

dizmo.onUndock(function(undockedDizmo) {
    dizmo.privateStorage.setProperty('val', '');
    if (subscriptionId[undockedDizmo.identifier]) {
        dizmo.privateStorage.setProperty('val', '');
      dizmo.publicStorage.unsubscribeProperty(subscriptionId[undockedDizmo.identifier]);
        subscriptionId[undockedDizmo.identifier] = undefined;
    }
});