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.

To get the dizmo.Dizmo instance of another dizmo use the functions explained in the chapters Docking and Hierarchies.

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

dizmo.publicStorage.getProperty(path);
dizmo.publicStorage.setProperty(path);

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, and the second is a constant exchange.

Exchange data once

We illustrate how to exchange data when the docking occurs. First, we need to be notified about a dizmo docking to ours, then we grab the stdout value from the docked dizmo:

dizmo.onDock(function(dockingDizmo){
    var stdout=dockingDizmo.publicStorage.getProperty('stdout');
    // do something with the stdout variable
});

Constant data exchange

To constantly monitor data, use the subscribeTo function (or subscribeToRecursive for grouped data):

dizmo.onDock(function(dockingDizmo){
    subscriptionId = dockingDizmo.publicStorage.subscribeToProperty('stdout', function(path, val, oldVal) {
        var stdout = val;
        // do something with the stdout variable
    });
});

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

dizmo.onUndock(function(undockedDizmo) {
    dizmo.publicStorage.unsubscribeProperty(subscriptionId);
});

Practical example

Gauge will constantly display the current data of the other docked dizmo, thus we will use a subscription.

// When docked, read the 'stdout' of the other docked dizmo, update the value and 
// set the framecolor
dizmo.onDock(function(dockedDizmo) {
    self.subscriptionId = dockedDizmo.publicStorage.subscribeToProperty( 'stdout', function(path, val, oldVal) {
        var stdout = val;
        self.syncValueText(stdout);
        self.setBackgroundColor(stdout);
    });
});

Of course, once undocked, Gauge stops updating the value. Hence we need to cancel the subscription on the unDock event.

// When undocking, cancel the subcription
dizmo.onUndock(function(undockedDizmo) {
    if (self.subscriptionId !== undefined) {
        dizmo.publicStorage.unsubscribeProperty(self.subscriptionId);
    }
});

External resources

Main.js of the practical example Gauge on Github